1
0
Fork 0
mirror of https://github.com/hedgedoc/hedgedoc.git synced 2025-04-02 18:23:04 +00:00

feat: extend api request builder with custom base url

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-04-04 16:16:52 +02:00
parent ca586413d2
commit 8977100830
5 changed files with 26 additions and 4 deletions

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -23,10 +23,11 @@ export abstract class ApiRequestBuilder<ResponseType> {
/**
* Initializes a new API call with the default request options.
*
* @param endpoint The target endpoint without a leading slash.
* @param endpoint The target endpoint without a leading slash
* @param baseUrl An optional base URL that is used for the endpoint
*/
constructor(endpoint: string) {
this.targetUrl = `api/private/${endpoint}`
constructor(endpoint: string, baseUrl?: string) {
this.targetUrl = `${baseUrl ?? ''}api/private/${endpoint}`
}
protected async sendRequestAndVerifyResponse(httpMethod: RequestInit['method']): Promise<ApiResponse<ResponseType>> {

View file

@ -18,6 +18,12 @@ describe('DeleteApiRequestBuilder', () => {
afterAll(() => {
global.fetch = originalFetch
})
it('uses the custom base url as prefix', async () => {
expectFetch('https://example.org/api/private/test', 200, { method: 'DELETE' })
await new DeleteApiRequestBuilder<string>('test', 'https://example.org/').sendRequest()
})
describe('sendRequest without body', () => {
it('without headers', async () => {
expectFetch('api/private/test', 204, { method: 'DELETE' })

View file

@ -19,6 +19,11 @@ describe('GetApiRequestBuilder', () => {
global.fetch = originalFetch
})
it('uses the custom base url as prefix', async () => {
expectFetch('https://example.org/api/private/test', 200, { method: 'GET' })
await new GetApiRequestBuilder<string>('test', 'https://example.org/').sendRequest()
})
describe('sendRequest', () => {
it('without headers', async () => {
expectFetch('api/private/test', 200, { method: 'GET' })

View file

@ -19,6 +19,11 @@ describe('PostApiRequestBuilder', () => {
global.fetch = originalFetch
})
it('uses the custom base url as prefix', async () => {
expectFetch('https://example.org/api/private/test', 200, { method: 'POST' })
await new PostApiRequestBuilder<string, undefined>('test', 'https://example.org/').sendRequest()
})
describe('sendRequest without body', () => {
it('without headers', async () => {
expectFetch('api/private/test', 201, { method: 'POST' })

View file

@ -19,6 +19,11 @@ describe('PutApiRequestBuilder', () => {
global.fetch = originalFetch
})
it('uses the custom base url as prefix', async () => {
expectFetch('https://example.org/api/private/test', 200, { method: 'PUT' })
await new PutApiRequestBuilder<string, undefined>('test', 'https://example.org/').sendRequest()
})
describe('sendRequest without body', () => {
it('without headers', async () => {
expectFetch('api/private/test', 200, { method: 'PUT' })