Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add api operation head decorator #80

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ You can quickly test swagger-express-ts with the project example [example-swagge
- [@ApiOperationPut](./wiki/api-operation-put.decorator.md)
- [@ApiOperationPatch](./wiki/api-operation-patch.decorator.md)
- [@ApiOperationDelete](./wiki/api-operation-delete.decorator.md)
- [@ApiOperationHead](./wiki/api-operation-head.decorator.md)

## For any questions, suggestions, or feature requests

Expand Down
5 changes: 5 additions & 0 deletions lib/swagger-express-ts-lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions lib/swagger-express-ts-lib/src/api-operation-head.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { SwaggerService } from './swagger.service';
import { IApiOperationArgsBase } from './i-api-operation-args.base';
export interface IApiOperationHeadArgs extends IApiOperationArgsBase {}

export function ApiOperationHead(args: IApiOperationHeadArgs): MethodDecorator {
return (
target: any,
propertyKey: string | symbol,
descriptor: PropertyDescriptor
) => {
SwaggerService.getInstance().addOperationHead(args, target, propertyKey);
};
}
1 change: 1 addition & 0 deletions lib/swagger-express-ts-lib/src/i-swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface ISwaggerPath {
put?: ISwaggerOperation;
patch?: ISwaggerOperation;
delete?: ISwaggerOperation;
head?: ISwaggerOperation;
}

export interface ISwaggerDefinitionPropertyItems {
Expand Down
4 changes: 4 additions & 0 deletions lib/swagger-express-ts-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export {
IApiOperationDeleteArgs,
ApiOperationDelete,
} from './api-operation-delete.decorator';
export {
IApiOperationHeadArgs,
ApiOperationHead,
} from './api-operation-head.decorator';

export {
IApiModelPropertyArgs,
Expand Down
64 changes: 64 additions & 0 deletions lib/swagger-express-ts-lib/src/swagger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { IApiOperationPatchArgs } from './api-operation-patch.decorator';
import { IApiOperationDeleteArgs } from './api-operation-delete.decorator';
import { SwaggerDefinitionConstant } from './swagger-definition.constant';
import { ISwaggerBuildDefinitionModel } from './swagger.builder';
import { IApiOperationHeadArgs } from './api-operation-head.decorator';
const expect = chai.expect;

describe('SwaggerService', () => {
Expand Down Expand Up @@ -244,6 +245,69 @@ describe('SwaggerService', () => {
});
});

describe('addOperationHead', () => {
const pathArgs: IApiPathArgs = {
path: '/versions',
name: 'Version',
};
const pathTarget: any = {
name: 'VersionsController',
};
const operationHeadTarget: any = {
constructor: {
name: 'VersionsController',
},
};
let propertyKey: string | symbol;
let expectedPaths: { [key: string]: ISwaggerPath };

beforeEach(() => {
SwaggerService.getInstance().addPath(pathArgs, pathTarget);
});

describe('expect no content', () => {
beforeEach(() => {
propertyKey = 'getVersions';
expectedPaths = {
'/versions': {
head: {
consumes: [SwaggerDefinitionConstant.Consume.JSON],
operationId: 'getVersions',
produces: [SwaggerDefinitionConstant.Produce.JSON],
responses: {
204: {
description: 'Success',
},
},
tags: ['Version'],
},
},
};
});

it('expect default', () => {
const operationHeadArgs: IApiOperationHeadArgs = {
responses: {
204: {
description: 'Success',
},
},
};

SwaggerService.getInstance().addOperationHead(
operationHeadArgs,
operationHeadTarget,
propertyKey
);

SwaggerService.getInstance().buildSwagger();
expect(
SwaggerService.getInstance().getData().paths
).to.deep.equal(expectedPaths);
});
});
});

describe('addOperationGet', () => {
const pathArgs: IApiPathArgs = {
path: '/versions',
Expand Down
32 changes: 29 additions & 3 deletions lib/swagger-express-ts-lib/src/swagger.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'assert';
import * as _ from 'lodash';
import { IApiModelArgs } from '.';
import { IApiModelArgs, IApiOperationHeadArgs } from '.';
import { IApiModelPropertyArgs } from './api-model-property.decorator';
import { IApiOperationGetArgs } from './api-operation-get.decorator';
import { IApiOperationPostArgs } from './api-operation-post.decorator';
Expand Down Expand Up @@ -41,6 +41,7 @@ interface IPath {
put?: ISwaggerOperation;
patch?: ISwaggerOperation;
delete?: ISwaggerOperation;
head?: ISwaggerOperation;
}

interface IController {
Expand Down Expand Up @@ -190,6 +191,19 @@ export class SwaggerService {
);
}

public addOperationHead(
args: IApiOperationHeadArgs,
target: any,
propertyKey: string | symbol
): void {
assert.ok(args, 'Args are required.');
assert.ok(args.responses, 'Responses are required.');
if (args.parameters) {
assert.ok(!args.parameters.body, 'Parameter body is not required.');
}
this.addOperation('head', args, target, propertyKey);
}

public addOperationGet(
args: IApiOperationGetArgs,
target: any,
Expand Down Expand Up @@ -292,6 +306,12 @@ export class SwaggerService {
controller
);
}
if (path.head) {
swaggerPath.head = this.buildSwaggerOperation(
path.head,
controller
);
}
if (path.path && path.path.length > 0) {
data.paths[controller.path.concat(path.path)] = {...data.paths[controller.path.concat(path.path)],...swaggerPath};
} else {
Expand Down Expand Up @@ -404,12 +424,14 @@ export class SwaggerService {
target: any,
propertyKey: string | symbol
): void {
const targetName = target.constructor.name !== 'Function' ? target.constructor.name : target.name;

let currentController: IController = {
paths: {},
};
for (const index in this.controllerMap) {
const controller = this.controllerMap[index];
if (index === target.constructor.name) {
if (index === targetName) {
currentController = controller;
}
}
Expand Down Expand Up @@ -448,7 +470,11 @@ export class SwaggerService {
currentPath.delete = this.buildOperation(args, target, propertyKey);
}

this.controllerMap[target.constructor.name] = currentController;
if ('head' === operation) {
currentPath.head = this.buildOperation(args, target, propertyKey);
}

this.controllerMap[targetName] = currentController;
}

private buildOperation(
Expand Down
Loading