feat: add internal api url

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-10-08 20:28:06 +02:00 committed by David Mehren
parent db2d8614a6
commit f43c9fd2b1
6 changed files with 77 additions and 36 deletions

View file

@ -14,7 +14,7 @@ import type { Alias, NewAliasDto, PrimaryAliasDto } from './types'
* @param noteIdOrAlias The note id or an existing alias for a note.
* @param newAlias The new alias.
* @return Information about the newly created alias.
* @throws {Error} when the api request wasn't successfull
* @throws {Error} when the api request wasn't successful
*/
export const addAlias = async (noteIdOrAlias: string, newAlias: string): Promise<Alias> => {
const response = await new PostApiRequestBuilder<Alias, NewAliasDto>('alias')

View file

@ -8,6 +8,7 @@ import type { ApiErrorResponse } from '../api-error-response'
import { ApiResponse } from '../api-response'
import { defaultConfig, defaultHeaders } from '../default-config'
import deepmerge from 'deepmerge'
import { baseUrlFromEnvExtractor } from '../../../utils/base-url-from-env-extractor'
/**
* Builder to construct and execute a call to the HTTP API.
@ -27,7 +28,18 @@ export abstract class ApiRequestBuilder<ResponseType> {
* @param baseUrl An optional base URL that is used for the endpoint
*/
constructor(endpoint: string, baseUrl?: string) {
this.targetUrl = `${baseUrl ?? '/'}api/private/${endpoint}`
const actualBaseUrl = this.determineBaseUrl(baseUrl)
this.targetUrl = `${actualBaseUrl}api/private/${endpoint}`
}
/**
* Determines the API base URL by checking if the request is made on the server or client
*
* @return the base url
*/
private determineBaseUrl(baseUrl?: string) {
return typeof window !== 'undefined' ? baseUrl ?? '/' : baseUrlFromEnvExtractor.extractBaseUrls().internalApiUrl
}
protected async sendRequestAndVerifyResponse(httpMethod: RequestInit['method']): Promise<ApiResponse<ResponseType>> {