mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-24 18:56:32 -05:00
fix: usage of internal api
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
3076bb4cc3
commit
a2b291294e
4 changed files with 37 additions and 41 deletions
|
@ -39,7 +39,20 @@ export abstract class ApiRequestBuilder<ResponseType> {
|
||||||
* @return the base url
|
* @return the base url
|
||||||
*/
|
*/
|
||||||
private determineBaseUrl(baseUrl?: string) {
|
private determineBaseUrl(baseUrl?: string) {
|
||||||
return typeof window !== 'undefined' ? baseUrl ?? '/' : baseUrlFromEnvExtractor.extractBaseUrls().internalApiUrl
|
if (this.isSSR()) {
|
||||||
|
const internalApiUrl = baseUrlFromEnvExtractor.extractBaseUrls().internalApiUrl
|
||||||
|
const actualBaseUrl = internalApiUrl ?? baseUrl
|
||||||
|
if (actualBaseUrl === undefined) {
|
||||||
|
throw new Error("Can't make request without forced base url and without internal api url")
|
||||||
|
}
|
||||||
|
return actualBaseUrl
|
||||||
|
} else {
|
||||||
|
return baseUrl ?? '/'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private isSSR() {
|
||||||
|
return typeof window === 'undefined'
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async sendRequestAndVerifyResponse(httpMethod: RequestInit['method']): Promise<ApiResponse<ResponseType>> {
|
protected async sendRequestAndVerifyResponse(httpMethod: RequestInit['method']): Promise<ApiResponse<ResponseType>> {
|
||||||
|
|
|
@ -10,7 +10,7 @@ import React, { createContext } from 'react'
|
||||||
export interface BaseUrls {
|
export interface BaseUrls {
|
||||||
renderer: string
|
renderer: string
|
||||||
editor: string
|
editor: string
|
||||||
internalApiUrl: string
|
internalApiUrl: string | undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BaseUrlContextProviderProps {
|
interface BaseUrlContextProviderProps {
|
||||||
|
|
|
@ -83,7 +83,7 @@ describe('BaseUrlFromEnvExtractor', () => {
|
||||||
expect(() => sut.extractBaseUrls()).toThrow()
|
expect(() => sut.extractBaseUrls()).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should copy editor base url to renderer base/internal api url if url is omitted', () => {
|
it('should copy editor base url to renderer base url if url is omitted', () => {
|
||||||
process.env.HD_BASE_URL = 'https://editor1.example.org/'
|
process.env.HD_BASE_URL = 'https://editor1.example.org/'
|
||||||
delete process.env.HD_RENDERER_BASE_URL
|
delete process.env.HD_RENDERER_BASE_URL
|
||||||
delete process.env.HD_INTERNAL_API_URL
|
delete process.env.HD_INTERNAL_API_URL
|
||||||
|
@ -91,7 +91,7 @@ describe('BaseUrlFromEnvExtractor', () => {
|
||||||
|
|
||||||
expect(sut.extractBaseUrls()).toStrictEqual({
|
expect(sut.extractBaseUrls()).toStrictEqual({
|
||||||
renderer: 'https://editor1.example.org/',
|
renderer: 'https://editor1.example.org/',
|
||||||
internalApiUrl: 'https://editor1.example.org/',
|
internalApiUrl: undefined,
|
||||||
editor: 'https://editor1.example.org/'
|
editor: 'https://editor1.example.org/'
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
import type { BaseUrls } from '../components/common/base-url/base-url-context-provider'
|
import type { BaseUrls } from '../components/common/base-url/base-url-context-provider'
|
||||||
import { Logger } from './logger'
|
import { Logger } from './logger'
|
||||||
import { isTestMode, isBuildTime } from './test-modes'
|
import { isTestMode, isBuildTime } from './test-modes'
|
||||||
import { NoSubdirectoryAllowedError, parseUrl } from '@hedgedoc/commons'
|
import { parseUrl } from '@hedgedoc/commons'
|
||||||
import { Optional } from '@mrdrogdrog/optional'
|
import { Optional } from '@mrdrogdrog/optional'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,54 +17,35 @@ export class BaseUrlFromEnvExtractor {
|
||||||
private readonly logger = new Logger('Base URL Configuration')
|
private readonly logger = new Logger('Base URL Configuration')
|
||||||
|
|
||||||
private extractUrlFromEnvVar(envVarValue: string | undefined): Optional<URL> {
|
private extractUrlFromEnvVar(envVarValue: string | undefined): Optional<URL> {
|
||||||
try {
|
return parseUrl(envVarValue).orThrow(() => new Error(`${envVarValue} isn't a valid URL`))
|
||||||
return parseUrl(envVarValue)
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof NoSubdirectoryAllowedError) {
|
|
||||||
this.logger.error(error.message)
|
|
||||||
return Optional.empty()
|
|
||||||
} else {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private extractEditorBaseUrlFromEnv(): Optional<URL> {
|
private extractUrlFromEnv(envVarName: string): Optional<URL> {
|
||||||
const envValue = this.extractUrlFromEnvVar(process.env.HD_BASE_URL)
|
|
||||||
if (envValue.isEmpty()) {
|
|
||||||
this.logger.error("HD_BASE_URL isn't a valid URL!")
|
|
||||||
}
|
|
||||||
return envValue
|
|
||||||
}
|
|
||||||
|
|
||||||
private extractUrlFromEnv(editorBaseUrl: URL, envVarName: string): Optional<URL> {
|
|
||||||
if (isTestMode) {
|
if (isTestMode) {
|
||||||
this.logger.info(`Test mode activated. Using editor base url for ${envVarName}.`)
|
this.logger.info(`Test mode activated. Using editor base url for ${envVarName}.`)
|
||||||
return Optional.of(editorBaseUrl)
|
return Optional.empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!process.env[envVarName]) {
|
if (!process.env[envVarName]) {
|
||||||
this.logger.info(`${envVarName} is unset. Using editor base url.`)
|
this.logger.info(`${envVarName} is unset.`)
|
||||||
return Optional.of(editorBaseUrl)
|
return Optional.empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.extractUrlFromEnvVar(process.env[envVarName])
|
return this.extractUrlFromEnvVar(process.env[envVarName])
|
||||||
}
|
}
|
||||||
|
|
||||||
private renewBaseUrls(): BaseUrls {
|
private renewBaseUrls(): BaseUrls {
|
||||||
return this.extractEditorBaseUrlFromEnv()
|
return this.extractUrlFromEnvVar(process.env.HD_BASE_URL)
|
||||||
.flatMap((editorBaseUrl) =>
|
.map((editorBaseUrl) => {
|
||||||
this.extractUrlFromEnv(editorBaseUrl, 'HD_RENDERER_BASE_URL').flatMap((rendererBaseUrl) =>
|
const rendererBaseUrl = this.extractUrlFromEnv('HD_RENDERER_BASE_URL').orElse(editorBaseUrl)
|
||||||
this.extractUrlFromEnv(editorBaseUrl, 'HD_INTERNAL_API_URL').map((internalApiUrl) => {
|
const internalApiUrl = this.extractUrlFromEnv('HD_INTERNAL_API_URL').orElse(undefined)
|
||||||
return {
|
return {
|
||||||
editor: editorBaseUrl.toString(),
|
editor: editorBaseUrl.toString(),
|
||||||
renderer: rendererBaseUrl.toString(),
|
renderer: rendererBaseUrl.toString(),
|
||||||
internalApiUrl: internalApiUrl.toString()
|
internalApiUrl: internalApiUrl?.toString()
|
||||||
}
|
} as BaseUrls
|
||||||
})
|
})
|
||||||
)
|
.orElseThrow(() => new Error("couldn't parse env"))
|
||||||
)
|
|
||||||
.orElseThrow(() => new Error('couldnt parse env vars'))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -94,7 +75,9 @@ export class BaseUrlFromEnvExtractor {
|
||||||
}
|
}
|
||||||
this.logger.info('Editor base URL', this.baseUrls.editor.toString())
|
this.logger.info('Editor base URL', this.baseUrls.editor.toString())
|
||||||
this.logger.info('Renderer base URL', this.baseUrls.renderer.toString())
|
this.logger.info('Renderer base URL', this.baseUrls.renderer.toString())
|
||||||
this.logger.info('Internal API URL', this.baseUrls.internalApiUrl.toString())
|
if (this.baseUrls.internalApiUrl !== undefined) {
|
||||||
|
this.logger.info('Internal API URL', this.baseUrls.internalApiUrl?.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue