@pjts/swagger 0.0.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +7 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/openapi.d.ts +9 -0
- package/dist/openapi.js +139 -0
- package/dist/openapi.js.map +1 -0
- package/package.json +40 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { OpenAPIObject } from 'openapi3-ts';
|
|
2
|
+
import { Description } from './openapi';
|
|
3
|
+
export * from './openapi';
|
|
4
|
+
export declare const buildSwaggerServer: (schema: OpenAPIObject, description?: Description) => {
|
|
5
|
+
start: (port: number) => Promise<void>;
|
|
6
|
+
stop: () => Promise<void>;
|
|
7
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildSwaggerServer = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const express_1 = tslib_1.__importDefault(require("express"));
|
|
6
|
+
const swagger_ui_express_1 = tslib_1.__importDefault(require("swagger-ui-express"));
|
|
7
|
+
const core_1 = require("@pjts/core");
|
|
8
|
+
tslib_1.__exportStar(require("./openapi"), exports);
|
|
9
|
+
const buildSwaggerServer = (schema, description) => {
|
|
10
|
+
const app = (0, express_1.default)();
|
|
11
|
+
app.use('/docs', swagger_ui_express_1.default.serve, swagger_ui_express_1.default.setup({
|
|
12
|
+
...schema,
|
|
13
|
+
info: description || schema.info,
|
|
14
|
+
}));
|
|
15
|
+
app.use('/raw', (_req, res) => {
|
|
16
|
+
res.status(200).send(schema);
|
|
17
|
+
});
|
|
18
|
+
let server;
|
|
19
|
+
return {
|
|
20
|
+
start: async (port) => {
|
|
21
|
+
server = app.listen(port, () => {
|
|
22
|
+
core_1.Logger.info('Swagger documentation running on port ' + port);
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
stop: async () => {
|
|
26
|
+
await server?.close();
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
exports.buildSwaggerServer = buildSwaggerServer;
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,8DAA6B;AAC7B,oFAA0C;AAE1C,qCAAmC;AAInC,oDAAyB;AAElB,MAAM,kBAAkB,GAAG,CAAC,MAAqB,EAAE,WAAyB,EAAE,EAAE;IACrF,MAAM,GAAG,GAAG,IAAA,iBAAO,GAAE,CAAA;IACrB,GAAG,CAAC,GAAG,CACL,OAAO,EACP,4BAAS,CAAC,KAAK,EACf,4BAAS,CAAC,KAAK,CAAC;QACd,GAAG,MAAM;QACT,IAAI,EAAE,WAAW,IAAI,MAAM,CAAC,IAAI;KACjC,CAAC,CACH,CAAA;IAED,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE;QAC5B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IAC9B,CAAC,CAAC,CAAA;IAEF,IAAI,MAAmB,CAAA;IACvB,OAAO;QACL,KAAK,EAAE,KAAK,EAAE,IAAY,EAAE,EAAE;YAC5B,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;gBAC7B,aAAM,CAAC,IAAI,CAAC,wCAAwC,GAAG,IAAI,CAAC,CAAA;YAC9D,CAAC,CAAC,CAAA;QACJ,CAAC;QACD,IAAI,EAAE,KAAK,IAAI,EAAE;YACf,MAAM,MAAM,EAAE,KAAK,EAAE,CAAA;QACvB,CAAC;KACF,CAAA;AACH,CAAC,CAAA;AA1BY,QAAA,kBAAkB,sBA0B9B"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { OpenAPIObject } from 'openapi3-ts';
|
|
2
|
+
import { AppModule } from '@pjts/core';
|
|
3
|
+
export interface Description {
|
|
4
|
+
version: string;
|
|
5
|
+
description: string;
|
|
6
|
+
name: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const collapsePrimitiveTypes: (schema: Record<string, any>) => {};
|
|
9
|
+
export declare const buildOpenAPISchema: (appModules: AppModule<any>[], port: number, description: Description, prefix: string) => OpenAPIObject;
|
package/dist/openapi.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildOpenAPISchema = exports.collapsePrimitiveTypes = void 0;
|
|
4
|
+
const class_validator_jsonschema_1 = require("class-validator-jsonschema");
|
|
5
|
+
const core_1 = require("@pjts/core");
|
|
6
|
+
const normalizeUrl = (route) => {
|
|
7
|
+
return route
|
|
8
|
+
.split('/')
|
|
9
|
+
.map(chunk => {
|
|
10
|
+
if (!chunk.startsWith(':')) {
|
|
11
|
+
return chunk;
|
|
12
|
+
}
|
|
13
|
+
return '{' + chunk.slice(1) + '}';
|
|
14
|
+
})
|
|
15
|
+
.join('/');
|
|
16
|
+
};
|
|
17
|
+
const collapsePrimitiveTypes = (schema) => {
|
|
18
|
+
return Object.entries(schema).reduce((acc, [name, value]) => {
|
|
19
|
+
if (value.properties?.value && Object.keys(value.properties).length === 1) {
|
|
20
|
+
acc[name] = value.properties.value;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
acc[name] = value;
|
|
24
|
+
}
|
|
25
|
+
return acc;
|
|
26
|
+
}, {});
|
|
27
|
+
};
|
|
28
|
+
exports.collapsePrimitiveTypes = collapsePrimitiveTypes;
|
|
29
|
+
const buildOpenAPISchema = (appModules, port, description, prefix) => {
|
|
30
|
+
const schemas = (0, class_validator_jsonschema_1.validationMetadatasToSchemas)();
|
|
31
|
+
const primitiveSchemas = (0, core_1.getOpenAPISchemas)();
|
|
32
|
+
const components = (0, exports.collapsePrimitiveTypes)({
|
|
33
|
+
...primitiveSchemas,
|
|
34
|
+
...schemas,
|
|
35
|
+
});
|
|
36
|
+
return {
|
|
37
|
+
openapi: '3.0.3',
|
|
38
|
+
info: {
|
|
39
|
+
title: description?.name || 'PJX',
|
|
40
|
+
description: description?.description || '-',
|
|
41
|
+
version: description?.version || '-',
|
|
42
|
+
},
|
|
43
|
+
servers: [
|
|
44
|
+
{
|
|
45
|
+
url: `http://localhost:${port}`,
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
components: {
|
|
49
|
+
securitySchemes: {
|
|
50
|
+
bearerAuth: {
|
|
51
|
+
name: 'authorization',
|
|
52
|
+
type: 'apiKey',
|
|
53
|
+
in: 'header',
|
|
54
|
+
scheme: 'bearer',
|
|
55
|
+
description: 'Bearer JWT',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
schemas: components,
|
|
59
|
+
},
|
|
60
|
+
security: [
|
|
61
|
+
{
|
|
62
|
+
bearerAuth: [],
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
paths: appModules.reduce((paths, appModule) => {
|
|
66
|
+
const moduleDescriptor = (0, core_1.getModuleDescriptor)(appModule);
|
|
67
|
+
moduleDescriptor.routes.forEach(route => {
|
|
68
|
+
route.endpoints.forEach(endpoint => {
|
|
69
|
+
const bodyParams = endpoint.params.filter(param => param.source === 'body');
|
|
70
|
+
const url = normalizeUrl((0, core_1.buildServerUrl)(moduleDescriptor, route, endpoint, prefix));
|
|
71
|
+
paths[url] = {
|
|
72
|
+
...paths[url],
|
|
73
|
+
[endpoint.httpMethod.toLowerCase()]: {
|
|
74
|
+
description: route.name + '.' + endpoint.method,
|
|
75
|
+
parameters: endpoint.params
|
|
76
|
+
.filter(param => param.source !== 'headers' && param.source !== 'middleware')
|
|
77
|
+
.map(param => {
|
|
78
|
+
const subPath = Array.isArray(param.path) ? param.path : [param.path];
|
|
79
|
+
return subPath.map(path => ({
|
|
80
|
+
name: path,
|
|
81
|
+
in: param.source === 'params' ? 'path' : param.source,
|
|
82
|
+
schema: param.source === 'body'
|
|
83
|
+
? {
|
|
84
|
+
$ref: `#/components/schemas/${param.dto}`,
|
|
85
|
+
}
|
|
86
|
+
: undefined,
|
|
87
|
+
}));
|
|
88
|
+
})
|
|
89
|
+
.flat(),
|
|
90
|
+
responses: {
|
|
91
|
+
[endpoint.status]: {
|
|
92
|
+
description: 'OK',
|
|
93
|
+
},
|
|
94
|
+
400: {
|
|
95
|
+
description: 'Bad Request',
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
requestBody: bodyParams.length > 0
|
|
99
|
+
? {
|
|
100
|
+
content: {
|
|
101
|
+
'application/json': {
|
|
102
|
+
schema: {
|
|
103
|
+
type: 'object',
|
|
104
|
+
properties: bodyParams.reduce((acc, param) => {
|
|
105
|
+
if (Array.isArray(param.path)) {
|
|
106
|
+
param.path.forEach(subPath => {
|
|
107
|
+
acc[subPath] = {
|
|
108
|
+
$ref: `#/components/schemas/${param.dto}`,
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
else if (primitiveSchemas[param.dto]?.type !== 'object') {
|
|
113
|
+
acc[param.path] = {
|
|
114
|
+
type: primitiveSchemas[param.dto]?.type,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
acc[param.path] = {
|
|
119
|
+
$ref: `#/components/schemas/${param.dto}`,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return acc;
|
|
123
|
+
}, {}),
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
}
|
|
128
|
+
: undefined,
|
|
129
|
+
tags: [route.name],
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
return paths;
|
|
135
|
+
}, {}),
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
exports.buildOpenAPISchema = buildOpenAPISchema;
|
|
139
|
+
//# sourceMappingURL=openapi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openapi.js","sourceRoot":"","sources":["../src/openapi.ts"],"names":[],"mappings":";;;AACA,2EAAyE;AACzE,qCAA8F;AAQ9F,MAAM,YAAY,GAAG,CAAC,KAAa,EAAE,EAAE;IACrC,OAAO,KAAK;SACT,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,KAAK,CAAC,EAAE;QACX,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YAC1B,OAAO,KAAK,CAAA;SACb;QACD,OAAO,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,CAAA;IACnC,CAAC,CAAC;SACD,IAAI,CAAC,GAAG,CAAC,CAAA;AACd,CAAC,CAAA;AAEM,MAAM,sBAAsB,GAAG,CAAC,MAA2B,EAAE,EAAE;IACpE,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE;QAC1D,IAAI,KAAK,CAAC,UAAU,EAAE,KAAK,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE;YACzE,GAAG,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,UAAU,CAAC,KAAK,CAAA;SACnC;aAAM;YACL,GAAG,CAAC,IAAI,CAAC,GAAG,KAAK,CAAA;SAClB;QACD,OAAO,GAAG,CAAA;IACZ,CAAC,EAAE,EAAE,CAAC,CAAA;AACR,CAAC,CAAA;AATY,QAAA,sBAAsB,0BASlC;AAEM,MAAM,kBAAkB,GAAG,CAChC,UAA4B,EAC5B,IAAY,EACZ,WAAwB,EACxB,MAAc,EACC,EAAE;IACjB,MAAM,OAAO,GAAG,IAAA,yDAA4B,GAAE,C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
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"author": "Piotr Józefów",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": "^18.14.0"
|
|
11
|
+
},
|
|
12
|
+
"engineStrict": true,
|
|
13
|
+
"scripts": {
|
|
14
|
+
"lint": "eslint src/ --ext .js,.jsx,.ts,.tsx",
|
|
15
|
+
"test": "NODE_ENV=test jest --coverage=false --verbose",
|
|
16
|
+
"test:watch": "NODE_ENV=test jest --coverage=false --watchAll --testNamePattern=",
|
|
17
|
+
"test:coverage": "yarn test --collectCoverage",
|
|
18
|
+
"build": "rm -rf ./dist && tsc",
|
|
19
|
+
"package": "npm run build && npm publish --access=public",
|
|
20
|
+
"prepare": "npm install @pjts/core@latest & npm version patch"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/jest": "^29.4.0",
|
|
24
|
+
"jest": "^29.4.2",
|
|
25
|
+
"prettier": "^2.8.4",
|
|
26
|
+
"ts-jest": "^29.0.5",
|
|
27
|
+
"ts-node": "^10.9.1",
|
|
28
|
+
"typescript": "^4.9.5"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@pjts/core": "^0.0.59",
|
|
32
|
+
"class-validator-jsonschema": "^5.0.0",
|
|
33
|
+
"express": "^4.18.2",
|
|
34
|
+
"openapi3-ts": "^3.1.2",
|
|
35
|
+
"swagger-ui-express": "^4.6.0"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"registry": "https://registry.npmjs.org"
|
|
39
|
+
}
|
|
40
|
+
}
|