Skip to content

Commit

Permalink
fix serverurl in openapi docs for self hosted instance
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanlaschke committed Mar 10, 2024
1 parent 86c0f31 commit 179314d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
19 changes: 15 additions & 4 deletions packages/twenty-server/src/core/open-api/open-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@ 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.getServerUrl() ||
`${request.protocol}://${request.get('host')}`;

const schema = baseSchema('core', baseUrl);

let objectMetadataItems;

Expand Down Expand Up @@ -86,9 +92,14 @@ 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.getServerUrl() ||
`${request.protocol}://${request.get('host')}`;

const schema = baseSchema('metadata', baseUrl);

schema.tags = [{ name: 'placeholder' }];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
},
],
Expand Down

0 comments on commit 179314d

Please sign in to comment.