mirror of
https://github.com/twentyhq/twenty.git
synced 2024-09-11 23:37:21 +03:00
fix serverurl in openapi docs for self hosted instance (#4390)
* fix serverurl in openapi docs for self hosted instance * fixed server url slash, moved calculation to enviroment function, fixed openapi path hardcoded api.twenty.com
This commit is contained in:
parent
d8b370720c
commit
f847b64fd1
@ -48,10 +48,13 @@ const TokenForm = ({
|
||||
await submitToken(event.target.value);
|
||||
};
|
||||
|
||||
const updateBaseUrl = (baseUrl) => {
|
||||
setBaseUrl(baseUrl);
|
||||
submitBaseUrl?.(baseUrl);
|
||||
localStorage.setItem('baseUrl', JSON.stringify({ baseUrl: baseUrl }));
|
||||
const updateBaseUrl = (baseUrl: string) => {
|
||||
const url = baseUrl?.endsWith('/')
|
||||
? baseUrl.substring(0, baseUrl.length - 1)
|
||||
: baseUrl;
|
||||
setBaseUrl(url);
|
||||
submitBaseUrl?.(url);
|
||||
localStorage.setItem('baseUrl', JSON.stringify({ baseUrl: url }));
|
||||
};
|
||||
|
||||
const validateToken = (openApiJson) => {
|
||||
|
@ -22,9 +22,7 @@ export class ApiRestService {
|
||||
request: Request,
|
||||
data: ApiRestQuery,
|
||||
): Promise<ApiRestResponse> {
|
||||
const baseUrl =
|
||||
this.environmentService.getServerUrl() ||
|
||||
`${request.protocol}://${request.get('host')}`;
|
||||
const baseUrl = this.environmentService.getBaseUrl(request);
|
||||
|
||||
try {
|
||||
return await this.httpService.axiosRef.post(`${baseUrl}/graphql`, data, {
|
||||
|
@ -18,9 +18,7 @@ export class ApiRestMetadataService {
|
||||
) {}
|
||||
|
||||
async callMetadata(request, data: ApiRestQuery) {
|
||||
const baseUrl =
|
||||
this.environmentService.getServerUrl() ||
|
||||
`${request.protocol}://${request.get('host')}`;
|
||||
const baseUrl = this.environmentService.getBaseUrl(request);
|
||||
|
||||
try {
|
||||
return await this.httpService.axiosRef.post(`${baseUrl}/metadata`, data, {
|
||||
|
@ -23,7 +23,7 @@ export class OpenApiController {
|
||||
@Req() request: Request,
|
||||
@Res() res: Response,
|
||||
) {
|
||||
const data = await this.openApiService.generateMetaDataSchema();
|
||||
const data = await this.openApiService.generateMetaDataSchema(request);
|
||||
|
||||
res.send(data);
|
||||
}
|
||||
|
@ -25,16 +25,20 @@ import {
|
||||
getSingleResultSuccessResponse,
|
||||
} from 'src/core/open-api/utils/responses.utils';
|
||||
import { getRequestBody } from 'src/core/open-api/utils/request-body.utils';
|
||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||
|
||||
@Injectable()
|
||||
export class OpenApiService {
|
||||
constructor(
|
||||
private readonly tokenService: TokenService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
private readonly objectMetadataService: ObjectMetadataService,
|
||||
) {}
|
||||
|
||||
async generateCoreSchema(request: Request): Promise<OpenAPIV3_1.Document> {
|
||||
const schema = baseSchema('core');
|
||||
const baseUrl = this.environmentService.getBaseUrl(request);
|
||||
|
||||
const schema = baseSchema('core', baseUrl);
|
||||
|
||||
let objectMetadataItems;
|
||||
|
||||
@ -86,9 +90,12 @@ export class OpenApiService {
|
||||
return schema;
|
||||
}
|
||||
|
||||
async generateMetaDataSchema(): Promise<OpenAPIV3_1.Document> {
|
||||
//TODO Add once Rest MetaData api is ready
|
||||
const schema = baseSchema('metadata');
|
||||
async generateMetaDataSchema(
|
||||
request: Request,
|
||||
): Promise<OpenAPIV3_1.Document> {
|
||||
const baseUrl = this.environmentService.getBaseUrl(request);
|
||||
|
||||
const schema = baseSchema('metadata', baseUrl);
|
||||
|
||||
schema.tags = [{ name: 'placeholder' }];
|
||||
|
||||
|
@ -4,6 +4,7 @@ import { computeOpenApiPath } from 'src/core/open-api/utils/path.utils';
|
||||
|
||||
export const baseSchema = (
|
||||
schemaName: 'core' | 'metadata',
|
||||
serverUrl: string,
|
||||
): OpenAPIV3_1.Document => {
|
||||
return {
|
||||
openapi: '3.0.3',
|
||||
@ -23,9 +24,7 @@ export const baseSchema = (
|
||||
// Testing purposes
|
||||
servers: [
|
||||
{
|
||||
url: `https://api.twenty.com/rest/${
|
||||
schemaName !== 'core' ? schemaName : ''
|
||||
}`,
|
||||
url: `${serverUrl}/rest/${schemaName !== 'core' ? schemaName : ''}`,
|
||||
description: 'Production Development',
|
||||
},
|
||||
],
|
||||
@ -49,6 +48,6 @@ export const baseSchema = (
|
||||
description: 'Find out more about **Twenty**',
|
||||
url: 'https://twenty.com',
|
||||
},
|
||||
paths: { [`/open-api/${schemaName}`]: computeOpenApiPath() },
|
||||
paths: { [`/open-api/${schemaName}`]: computeOpenApiPath(serverUrl) },
|
||||
};
|
||||
};
|
||||
|
@ -95,7 +95,9 @@ export const computeSingleResultPath = (
|
||||
} as OpenAPIV3_1.PathItemObject;
|
||||
};
|
||||
|
||||
export const computeOpenApiPath = (): OpenAPIV3_1.PathItemObject => {
|
||||
export const computeOpenApiPath = (
|
||||
serverUrl: string,
|
||||
): OpenAPIV3_1.PathItemObject => {
|
||||
return {
|
||||
get: {
|
||||
tags: ['General'],
|
||||
@ -103,7 +105,7 @@ export const computeOpenApiPath = (): OpenAPIV3_1.PathItemObject => {
|
||||
operationId: 'GetOpenApiSchema',
|
||||
servers: [
|
||||
{
|
||||
url: 'https://api.twenty.com/',
|
||||
url: serverUrl,
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
|
@ -2,6 +2,8 @@
|
||||
import { Injectable, LogLevel } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
import { Request } from 'express';
|
||||
|
||||
import { EmailDriver } from 'src/integrations/email/interfaces/email.interface';
|
||||
|
||||
import { LoggerDriverType } from 'src/integrations/logger/interfaces';
|
||||
@ -86,7 +88,19 @@ export class EnvironmentService {
|
||||
}
|
||||
|
||||
getServerUrl(): string {
|
||||
return this.configService.get<string>('SERVER_URL')!;
|
||||
const url = this.configService.get<string>('SERVER_URL')!;
|
||||
|
||||
if (url?.endsWith('/')) {
|
||||
return url.substring(0, url.length - 1);
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
getBaseUrl(request: Request): string {
|
||||
return (
|
||||
this.getServerUrl() || `${request.protocol}://${request.get('host')}`
|
||||
);
|
||||
}
|
||||
|
||||
getAccessTokenSecret(): string {
|
||||
|
Loading…
Reference in New Issue
Block a user