Skip to content

Commit

Permalink
💚 Added a correct swagger for sdk publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Jul 25, 2024
1 parent 317d3d2 commit 7dcf9b8
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class WebhookController {
}

@ApiOperation({
operationId: 'createWebhookMetadata',
operationId: 'createWebhook',
summary: 'Add webhook metadata',
})
@ApiBody({ type: WebhookDto })
Expand All @@ -138,7 +138,7 @@ export class WebhookController {
}

@ApiOperation({
operationId: 'createWebhookMetadata',
operationId: 'createWebhook',
summary: 'Add webhook metadata',
})
@ApiBody({ type: WebhookDto })
Expand Down
20 changes: 15 additions & 5 deletions packages/api/src/@core/field-mapping/field-mapping.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ import {
DefineTargetFieldDto,
MapFieldToProviderDto,
} from './dto/create-custom-field.dto';
import { ApiResponse, ApiTags, ApiBody, ApiOperation } from '@nestjs/swagger';
import {
ApiResponse,
ApiTags,
ApiBody,
ApiOperation,
ApiExcludeEndpoint,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '@@core/auth/guards/jwt-auth.guard';

@ApiTags('field-mappings')
@ApiTags('fieldMappings')
@Controller('field-mappings')
export class FieldMappingController {
constructor(
Expand All @@ -33,6 +39,7 @@ export class FieldMappingController {
})
@ApiResponse({ status: 200 })
@UseGuards(JwtAuthGuard)
@ApiExcludeEndpoint()
@Get('entities')
getEntities() {
return this.fieldMappingService.getEntities();
Expand All @@ -43,6 +50,7 @@ export class FieldMappingController {
summary: 'Retrieve field mappings',
})
@ApiResponse({ status: 200 })
@ApiExcludeEndpoint()
@Get('attribute')
@UseGuards(JwtAuthGuard)
getAttributes(@Request() req: any) {
Expand All @@ -55,14 +63,15 @@ export class FieldMappingController {
summary: 'Retrieve field mappings values',
})
@ApiResponse({ status: 200 })
@ApiExcludeEndpoint()
@Get('value')
@UseGuards(JwtAuthGuard)
getValues() {
return this.fieldMappingService.getValues();
}

@ApiOperation({
operationId: 'defineTargetField',
operationId: 'define',
summary: 'Define target Field',
})
@ApiBody({ type: DefineTargetFieldDto })
Expand All @@ -82,7 +91,7 @@ export class FieldMappingController {
}

@ApiOperation({
operationId: 'createCustomField',
operationId: 'create',
summary: 'Create Custom Field',
})
@ApiBody({ type: CustomFieldCreateDto })
Expand All @@ -94,7 +103,7 @@ export class FieldMappingController {
return this.fieldMappingService.createCustomField(data, id_project);
}

@ApiOperation({ operationId: 'mapField', summary: 'Map Custom Field' })
@ApiOperation({ operationId: 'map', summary: 'Map Custom Field' })
@ApiBody({ type: MapFieldToProviderDto })
@ApiResponse({ status: 201 })
@UseGuards(JwtAuthGuard)
Expand All @@ -108,6 +117,7 @@ export class FieldMappingController {
summary: 'Retrieve Custom Properties',
})
@ApiResponse({ status: 200 })
@ApiExcludeEndpoint()
@UseGuards(JwtAuthGuard)
@Get('properties')
getCustomProperties(
Expand Down
12 changes: 6 additions & 6 deletions packages/api/src/@core/linked-users/linked-users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '@nestjs/swagger';
import { JwtAuthGuard } from '@@core/auth/guards/jwt-auth.guard';

@ApiTags('linked-users')
@ApiTags('linkedUsers')
@Controller('linked-users')
export class LinkedUsersController {
constructor(
Expand All @@ -32,7 +32,7 @@ export class LinkedUsersController {
this.logger.setContext(LinkedUsersController.name);
}

@ApiOperation({ operationId: 'addLinkedUser', summary: 'Add Linked User' })
@ApiOperation({ operationId: 'createLinkedUser', summary: 'Add Linked User' })
@ApiBody({ type: CreateLinkedUserDto })
@ApiResponse({ status: 201 })
@UseGuards(JwtAuthGuard)
Expand All @@ -42,7 +42,7 @@ export class LinkedUsersController {
}

@ApiOperation({
operationId: 'addBatchLinkedUsers',
operationId: 'importBatch',
summary: 'Add Batch Linked Users',
})
@ApiBody({ type: CreateBatchLinkedUserDto })
Expand All @@ -54,7 +54,7 @@ export class LinkedUsersController {
}

@ApiOperation({
operationId: 'fetchLinkedUsers',
operationId: 'listLinkedUsers',
summary: 'Retrieve Linked Users',
})
@ApiResponse({ status: 200 })
Expand All @@ -66,7 +66,7 @@ export class LinkedUsersController {
}

@ApiOperation({
operationId: 'getLinkedUser',
operationId: 'retrieveLinkedUser',
summary: 'Retrieve a Linked User',
})
@ApiQuery({ name: 'id', required: true, type: String })
Expand All @@ -79,7 +79,7 @@ export class LinkedUsersController {
}

@ApiOperation({
operationId: 'linkedUserFromRemoteId',
operationId: 'remoteId',
summary: 'Retrieve a Linked User From A Remote Id',
})
@ApiQuery({ name: 'remoteId', required: true, type: String })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class PassthroughController {
}

@ApiOperation({
operationId: 'passthroughRequest',
operationId: 'request',
summary: 'Make a passthrough request',
})
@ApiQuery({ name: 'integrationId', required: true, type: String })
Expand Down
1 change: 0 additions & 1 deletion packages/api/src/ats/activity/activity.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Body,
Query,
Get,
Patch,
Param,
Headers,
UseGuards,
Expand Down
37 changes: 36 additions & 1 deletion packages/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ import { useContainer } from 'class-validator';
import * as cors from 'cors';
import * as yaml from 'js-yaml';

function addSpeakeasyGroup(document: any) {
for (const path in document.paths) {
const pathParts = path.split('/').filter((part) => part);
let groupName;

if (pathParts[0] === 'webhook') {
groupName = 'webhook';
} else if (pathParts[0] === 'sync') {
groupName = 'sync';
} else if (pathParts[0] === 'linked-users') {
groupName = 'linkedUsers';
} else if (pathParts[0] === 'field-mappings') {
groupName = 'fieldMappings';
} else if (pathParts.length >= 2) {
groupName = `${pathParts[0].toLowerCase()}.${pathParts[1].toLowerCase()}`;
}

if (groupName) {
for (const method in document.paths[path]) {
document.paths[path][method]['x-speakeasy-group'] = groupName;
}
}
}
}

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const config = new DocumentBuilder()
Expand All @@ -28,7 +53,17 @@ async function bootstrap() {
})
.build();
const document = SwaggerModule.createDocument(app, config);

// Dynamically add extended specs
const extendedSpecs = {
'x-speakeasy-name-override': [
{ operationId: '^retrieve.*', methodNameOverride: 'retrieve' },
{ operationId: '^list.*', methodNameOverride: 'list' },
{ operationId: '^create.*', methodNameOverride: 'create' },
],
};
document['x-speakeasy-name-override'] =
extendedSpecs['x-speakeasy-name-override']; // Add extended specs
addSpeakeasyGroup(document);
useContainer(app.select(AppModule), { fallbackOnErrors: true });

SwaggerModule.setup('docs', app, document);
Expand Down
19 changes: 19 additions & 0 deletions packages/api/swagger/overlay.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
overlay: 1.0.0
info:
title: Add x-speakeasy-name-override to endpoints
version: 0.0.1
actions:
- target: "$.paths[*][get,post,put,delete]"
description: Add x-speakeasy-name-override to all endpoint methods
update:
x-speakeasy-name-override:
$eval: |
if (current.operationId.startsWith('list')) {
return 'list';
} else if (current.operationId.startsWith('retrieve')) {
return 'retrieve';
} else if (current.operationId.startsWith('create')) {
return 'create';
} else {
return current.operationId;
}
Loading

0 comments on commit 7dcf9b8

Please sign in to comment.