Skip to content

Commit

Permalink
openapi checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
iyobo committed Feb 24, 2022
1 parent f93c066 commit 973fced
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
37 changes: 33 additions & 4 deletions src/openapi/OpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface AmalaMetadata {
export let openApiSpec: OpenAPIV3_1.Document = {
openapi: '3.0.1',
info: {
'title': '',
'title': 'API',
description: 'powered by AmalaJS (https://github.com/iyobo/amala)',
'version': '1.0.0'
},
Expand All @@ -38,6 +38,28 @@ export let openApiSpec: OpenAPIV3_1.Document = {
externalDocs: undefined
};

function convertRegexpToSwagger(path) {
const swaggerPath = [];

let paramMode = false;
for (const c of path) {

if (c === ':') {
paramMode = true;
swaggerPath.push('{');
} else if (paramMode && c === '/') {
paramMode = false;
swaggerPath.push('}/');
} else {
swaggerPath.push(c);
}
}

if (paramMode) swaggerPath.push('}');

return swaggerPath.join('');
}

export function generateOpenApi(metaData: AmalaMetadata, options: AmalaOptions) {

// incorporate custom spec values
Expand Down Expand Up @@ -124,7 +146,7 @@ export function generateOpenApi(metaData: AmalaMetadata, options: AmalaOptions)
for (const actionName in controllerMeta.actions) {
// e.g getUsers
const actionMeta = controllerMeta.actions[actionName];
const fullPath = basePath + (actionMeta.path === '/' ? '' : actionMeta.path);
const fullPath = convertRegexpToSwagger(basePath + (actionMeta.path === '/' ? '' : actionMeta.path));
const verb = actionMeta.verb;

paths[fullPath] = paths[fullPath] || {};
Expand Down Expand Up @@ -167,7 +189,8 @@ export function generateOpenApi(metaData: AmalaMetadata, options: AmalaOptions)
}

// eslint-disable-next-line new-cap
const f = new argType();
// const refl = argType.name;
// console.log(refl);
parameters.push({
in: argExistsIn,
name,
Expand All @@ -178,6 +201,12 @@ export function generateOpenApi(metaData: AmalaMetadata, options: AmalaOptions)
});
}

// console.log(
// 'meta-'+actionMeta.target.name,
// Reflect.getMetadata('design:type', actionMeta.target()),
// Reflect.getMetadata('design:paramtypes', actionMeta.target),
// Reflect.getMetadata('design:returntype', actionMeta.target)
// );

// finalize iteration changes of path
paths[fullPath][verb] = {
Expand All @@ -195,7 +224,7 @@ export function generateOpenApi(metaData: AmalaMetadata, options: AmalaOptions)
// @ts-ignore //????
'application/json': {
schema: {
$ref: `#/components/schemas/Error`
$ref: `#/components/schemas/${actionMeta.target.name}`
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tests/util/controllers/ArgController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const CustomDeco = ()=>Ctx('query');
@Controller('/arg')
export class ArgController {
@Post('/:model/:id')
async twoParams(@Params() params: any, @Params('id') id: any) {
async twoParams(@Params() params, @Params('id') id: string) {
return {params, id};
}

Expand Down
1 change: 1 addition & 0 deletions src/util/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ export const startTestServer = async ()=> {

app.listen(port);
}
console.log(Reflect.get)

startTestServer()

0 comments on commit 973fced

Please sign in to comment.