Skip to content

Commit

Permalink
Fix module entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagobustamante committed May 10, 2017
1 parent 30feaa8 commit 25f0c92
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 54 deletions.
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-rest-swagger",
"version": "0.0.2",
"version": "0.0.3",
"description": "Generate Swagger files from a typescript-rest project",
"keywords": [
"typescript",
Expand All @@ -12,8 +12,8 @@
"codegen",
"generation"
],
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"main": "./dist/decorators.js",
"typings": "./dist/decorators.d.ts",
"scripts": {
"start": "tsc -w",
"build": "npm run clean && tsc",
Expand Down Expand Up @@ -84,6 +84,10 @@
"bin": {
"swaggerGen": "dist/cli.js"
},
"directories": {
"lib": "dist",
"doc": "doc"
},
"engines": {
"node": ">=6.0.0"
},
Expand Down
31 changes: 9 additions & 22 deletions src/metadata/controllerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ export class ControllerGenerator {
const sourceFile = this.node.parent.getSourceFile();

return {
consumes: this.getMethodAccept(),
consumes: this.getDecoratorValues('Accept'),
location: sourceFile.fileName,
methods: this.buildMethods(),
name: this.node.name.text,
path: this.pathValue || '',
produces: this.getMethodProduces()
produces: this.getDecoratorValues('Produces'),
tags: this.getDecoratorValues('Tags')
};
}

Expand All @@ -39,31 +40,17 @@ export class ControllerGenerator {
.map(generator => generator.generate());
}

private getMethodAccept() {
private getDecoratorValues(decoratorName: string) {
if (!this.node.parent) { throw new Error('Controller node doesn\'t have a valid parent source file.'); }
if (!this.node.name) { throw new Error('Controller node doesn\'t have a valid name.'); }

const consumesDecorators = getDecorators(this.node, decorator => decorator.text === 'Accept');
if (!consumesDecorators || !consumesDecorators.length) { return []; }
if (consumesDecorators.length > 1) {
throw new Error(`Only one Accept decorator allowed in '${this.node.name.text}' controller.`);
const decorators = getDecorators(this.node, decorator => decorator.text === decoratorName);
if (!decorators || !decorators.length) { return []; }
if (decorators.length > 1) {
throw new Error(`Only one ${decoratorName} decorator allowed in '${this.node.name.text}' controller.`);
}

const decorator = consumesDecorators[0];
return decorator.arguments;
}

private getMethodProduces() {
if (!this.node.parent) { throw new Error('Controller node doesn\'t have a valid parent source file.'); }
if (!this.node.name) { throw new Error('Controller node doesn\'t have a valid name.'); }

const producesDecorators = getDecorators(this.node, decorator => decorator.text === 'Produces');
if (!producesDecorators || !producesDecorators.length) { return []; }
if (producesDecorators.length > 1) {
throw new Error(`Only one Produces decorator allowed in '${this.node.name.text}' controller.`);
}

const decorator = producesDecorators[0];
const decorator = decorators[0];
return decorator.arguments;
}
}
1 change: 1 addition & 0 deletions src/metadata/metadataGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface Controller {
path: string;
consumes: string[];
produces: string[];
tags: string[];
}

export interface Method {
Expand Down
34 changes: 6 additions & 28 deletions src/metadata/methodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ export class MethodGenerator {
responses.push(this.getMethodSuccessResponse(type));

return {
consumes: this.getMethodAccept(),
consumes: this.getDecoratorValues('Accept'),
deprecated: isExistJSDocTag(this.node, 'deprecated'),
description: getJSDocDescription(this.node),
method: this.method,
name: identifier.text,
parameters: this.buildParameters(),
path: this.path,
produces: this.getMethodProduces(),
produces: this.getDecoratorValues('Produces'),
responses,
security: this.getMethodSecurity(),
summary: getJSDocTag(this.node, 'summary'),
tags: this.getMethodTags(),
tags: this.getDecoratorValues('Tags'),
type
};
}
Expand Down Expand Up @@ -175,39 +175,17 @@ export class MethodGenerator {
return example;
}

private getMethodTags() {
const tagsDecorators = getDecorators(this.node, decorator => decorator.text === 'Tags');
private getDecoratorValues(decoratorName: string) {
const tagsDecorators = getDecorators(this.node, decorator => decorator.text === decoratorName);
if (!tagsDecorators || !tagsDecorators.length) { return []; }
if (tagsDecorators.length > 1) {
throw new Error(`Only one Tags decorator allowed in '${this.getCurrentLocation}' method.`);
throw new Error(`Only one ${decoratorName} decorator allowed in '${this.getCurrentLocation}' method.`);
}

const decorator = tagsDecorators[0];
return decorator.arguments;
}

private getMethodAccept() {
const consumesDecorators = getDecorators(this.node, decorator => decorator.text === 'Accept');
if (!consumesDecorators || !consumesDecorators.length) { return []; }
if (consumesDecorators.length > 1) {
throw new Error(`Only one Accept decorator allowed in '${this.getCurrentLocation()}' method.`);
}

const decorator = consumesDecorators[0];
return decorator.arguments;
}

private getMethodProduces() {
const producesDecorators = getDecorators(this.node, decorator => decorator.text === 'Produces');
if (!producesDecorators || !producesDecorators.length) { return []; }
if (producesDecorators.length > 1) {
throw new Error(`Only one Produces decorator allowed in '${this.getCurrentLocation()}' method.`);
}

const decorator = producesDecorators[0];
return decorator.arguments;
}

private getMethodSecurity() {
const securityDecorators = getDecorators(this.node, decorator => decorator.text === 'Security');
if (!securityDecorators || !securityDecorators.length) { return undefined; }
Expand Down
1 change: 1 addition & 0 deletions src/swagger/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class SpecGenerator {
paths[path] = paths[path] || {};
method.consumes = _.union(controller.consumes, method.consumes);
method.produces = _.union(controller.produces, method.produces);
method.tags = _.union(controller.tags, method.tags);

this.buildPathMethod(controller.name, method, paths[path]);
});
Expand Down
1 change: 0 additions & 1 deletion test/unit/definitions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ describe('Definition generation', () => {
expect(spec.paths).to.have.property('/mypath');
});
});

});

0 comments on commit 25f0c92

Please sign in to comment.