Skip to content

Commit c8bce4e

Browse files
author
Abilio Henrique
committed
updates:
- change build target to es5 - fix bug where uuid library wasn't correctly imported - Add unit tests for openapi generation - add test fixtures
1 parent eee24ac commit c8bce4e

13 files changed

+233
-19
lines changed

.jestrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"moduleFileExtensions": [ "ts", "tsx", "js" ],
66
"testPathIgnorePatterns": [
77
"/node_modules/",
8-
"./dist"
8+
"d.ts"
99
]
1010
}

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@
99
"dist"
1010
],
1111
"scripts": {
12-
"test": "jest --config ./.jestrc",
12+
"test": "yarn test:unit",
13+
"test:unit": "jest -i --config .jestrc",
1314
"lint": "eslint src",
14-
"test:watch": "yarn test -- -i --watch",
15+
"test:watch": "jest --config .jestrc -i -t '/[^/.]+\\.test' --watch",
1516
"release:npm": "cd dist && npm publish",
1617
"docs": "typedoc --out ./dist/docs --target es6 --readme README.md",
1718
"build": "node scripts/package"
1819
},
1920
"devDependencies": {
21+
"@types/jest": "^19.2.2",
2022
"@types/node": "^7.0.12",
23+
"@types/typescript": "^2.0.0",
2124
"eslint": "^3.19.0",
2225
"eslint-config-airbnb-base": "^11.1.3",
2326
"eslint-plugin-import": "^2.2.0",
2427
"jest": "^19.0.2",
25-
"serverless": "^1.9.0",
28+
"serverless": "^1.11.0",
2629
"ts-jest": "^19.0.8",
2730
"ts-node": "^3.0.2",
2831
"tslint": "^5.1.0",
@@ -33,8 +36,10 @@
3336
"dependencies": {
3437
"@jdw/jst": "^1.0.0-beta.10",
3538
"@types/bluebird": "^3.5.2",
36-
"@types/fs-promise": "^1.0.1",
39+
"@types/chalk": "^0.4.31",
40+
"@types/fs-promise": "^1.0.3",
3741
"@types/js-yaml": "^3.5.29",
42+
"@types/uuid": "^2.0.29",
3843
"bluebird": "^3.5.0",
3944
"chalk": "^1.1.3",
4045
"fs-promise": "^2.0.2",

src/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { dereference } from '@jdw/jst';
22
import * as c from 'chalk';
33
import * as openApiValidator from 'swagger2openapi/validate.js';
44

5-
import uuid from 'uuid';
5+
import * as uuid from 'uuid';
66
import { IParameterConfig, IServerlessFunctionConfig, IServiceDescription } from './types';
77
import { clone, merge } from './utils';
88

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,4 @@ export default class ServerlessOpenAPIDocumentation {
134134
}
135135

136136
module.exports = ServerlessOpenAPIDocumentation;
137-
;
137+
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "JSON API Schema",
4+
"description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
5+
"type": "object",
6+
"required": [
7+
"errors"
8+
],
9+
"properties": {
10+
"errors": {
11+
"type": "array",
12+
"items": {
13+
"$ref": "#/definitions/error"
14+
},
15+
"uniqueItems": true
16+
},
17+
"meta": {
18+
"$ref": "#/definitions/meta"
19+
},
20+
"links": {
21+
"$ref": "#/definitions/links"
22+
}
23+
},
24+
"additionalProperties": false,
25+
"definitions": {
26+
"meta": {
27+
"description": "Non-standard meta-information that can not be represented as an attribute or relationship.",
28+
"type": "object",
29+
"additionalProperties": true
30+
},
31+
"links": {
32+
"description": "A resource object **MAY** contain references to other resource objects (\"relationships\"). Relationships may be to-one or to-many. Relationships can be specified by including a member in a resource's links object.",
33+
"type": "object",
34+
"properties": {
35+
"self": {
36+
"description": "A `self` member, whose value is a URL for the relationship itself (a \"relationship URL\"). This URL allows the client to directly manipulate the relationship. For example, it would allow a client to remove an `author` from an `article` without deleting the people resource itself.",
37+
"type": "string",
38+
"format": "uri"
39+
},
40+
"related": {
41+
"$ref": "#/definitions/link"
42+
}
43+
},
44+
"additionalProperties": true
45+
},
46+
"link": {
47+
"description": "A link **MUST** be represented as either: a string containing the link's URL or a link object.",
48+
"oneOf": [
49+
{
50+
"description": "A string containing the link's URL.",
51+
"type": "string",
52+
"format": "uri"
53+
},
54+
{
55+
"type": "object",
56+
"required": [
57+
"href"
58+
],
59+
"properties": {
60+
"href": {
61+
"description": "A string containing the link's URL.",
62+
"type": "string",
63+
"format": "uri"
64+
},
65+
"meta": {
66+
"$ref": "#/definitions/meta"
67+
}
68+
}
69+
}
70+
]
71+
},
72+
"error": {
73+
"type": "object",
74+
"properties": {
75+
"id": {
76+
"description": "A unique identifier for this particular occurrence of the problem.",
77+
"type": "string"
78+
},
79+
"links": {
80+
"$ref": "#/definitions/links"
81+
},
82+
"status": {
83+
"description": "The HTTP status code applicable to this problem, expressed as a string value.",
84+
"type": "string"
85+
},
86+
"code": {
87+
"description": "An application-specific error code, expressed as a string value.",
88+
"type": "string"
89+
},
90+
"title": {
91+
"description": "A short, human-readable summary of the problem. It **SHOULD NOT** change from occurrence to occurrence of the problem, except for purposes of localization.",
92+
"type": "string"
93+
},
94+
"detail": {
95+
"description": "A human-readable explanation specific to this occurrence of the problem.",
96+
"type": "string"
97+
},
98+
"source": {
99+
"type": "object",
100+
"properties": {
101+
"pointer": {
102+
"description": "A JSON Pointer [RFC6901] to the associated entity in the request document [e.g. \"/data\" for a primary data object, or \"/data/attributes/title\" for a specific attribute].",
103+
"type": "string"
104+
},
105+
"parameter": {
106+
"description": "A string indicating which query parameter caused the error.",
107+
"type": "string"
108+
}
109+
}
110+
},
111+
"meta": {
112+
"$ref": "#/definitions/meta"
113+
}
114+
},
115+
"additionalProperties": false
116+
}
117+
}
118+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title" : "Empty Schema",
4+
"type" : "object"
5+
}

test/fixtures/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "openapi-test-package"
3+
}

test/serverless.yml renamed to test/fixtures/serverless.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,3 @@ custom:
7171
properties:
7272
SomeAttribute:
7373
type: string
74-
75-
plugins:
76-
- serverless-openapi-documentation

test/fixtures/tsconfig.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"outDir":"./dist",
7+
"inlineSourceMap": true,
8+
"inlineSources": true,
9+
"allowJs": false,
10+
"allowSyntheticDefaultImports": true,
11+
"declaration": true,
12+
"lib": [
13+
"es2015",
14+
"es2016",
15+
"es2017.object"
16+
]
17+
},
18+
"exclude": [
19+
"node_modules",
20+
"dist"
21+
],
22+
"include": [
23+
"src/**/*",
24+
"**/*"
25+
]
26+
}

test/openapi-generate.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as fs from 'fs-promise';
2+
import * as path from 'path';
3+
import * as Serverless from 'serverless';
4+
import DocumentGenerator from '../src/generate';
5+
6+
class ServerlessInterface extends Serverless {
7+
public service: any = {};
8+
}
9+
10+
describe('OpenAPI Documentation Generator', () => {
11+
it('Generates OpenAPI document', async () => {
12+
const serverlessConfig = await fs.readFile('test/fixtures/serverless.yml');
13+
// console.log(serverlessConfig.toString());
14+
// const fuck = yaml.safeLoad(serverlessConfig.toString());
15+
// console.log(JSON.stringify(fuck, null, 2));
16+
const sls: ServerlessInterface = new Serverless();
17+
sls.config.update({
18+
servicePath: path.join(process.cwd(), 'test/fixtures'),
19+
});
20+
const config = await sls.yamlParser.parse(path.join(process.cwd(), 'test/fixtures/serverless.yml'));
21+
sls.pluginManager.cliOptions = { stage: 'dev' };
22+
await sls.service.load(config);
23+
sls.variables.populateService({});
24+
25+
if ('documentation' in sls.service.custom) {
26+
const docGen = new DocumentGenerator(sls.service.custom.documentation);
27+
} else {
28+
throw new Error('Cannot find "documentation" in custom section of "serverless.yml"');
29+
}
30+
});
31+
});

0 commit comments

Comments
 (0)