@visulima/api-platform 1.0.1 → 1.0.3
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/CHANGELOG.md +29 -0
- package/README.md +2 -2
- package/bin/index.js +46 -0
- package/dist/{chunk-XXZ56SKG.mjs → chunk-2M45MXC2.mjs} +1 -2
- package/dist/chunk-2M45MXC2.mjs.map +1 -0
- package/dist/chunk-3Y3YBDRS.mjs +284 -0
- package/dist/chunk-3Y3YBDRS.mjs.map +1 -0
- package/dist/chunk-NY75KGAH.js +284 -0
- package/dist/chunk-NY75KGAH.js.map +1 -0
- package/dist/{chunk-AJKZCWFG.js → chunk-S46GRBOS.js} +1 -2
- package/dist/chunk-S46GRBOS.js.map +1 -0
- package/dist/index-server.d.ts +2 -0
- package/dist/index-server.js +9 -10
- package/dist/index-server.js.map +1 -1
- package/dist/index-server.mjs +7 -8
- package/dist/index-server.mjs.map +1 -1
- package/dist/next/cli/index.js +8 -8
- package/dist/next/cli/index.js.map +1 -1
- package/dist/next/cli/index.mjs +8 -8
- package/dist/next/cli/index.mjs.map +1 -1
- package/dist/next/index-browser.js +2 -2
- package/dist/next/index-browser.mjs +1 -1
- package/dist/next/index-server.d.ts +9 -5
- package/dist/next/index-server.js +4 -4
- package/dist/next/index-server.mjs +2 -2
- package/next/cli/package.json +12 -3
- package/next/package.json +9 -0
- package/package.json +20 -20
- package/recipes/api/swagger.ts +12 -0
- package/recipes/pages/redoc-ui.tsx +5 -0
- package/recipes/pages/swagger-ui.tsx +5 -0
- package/dist/chunk-2LATTLUM.mjs +0 -166
- package/dist/chunk-2LATTLUM.mjs.map +0 -1
- package/dist/chunk-AJKZCWFG.js.map +0 -1
- package/dist/chunk-S7GUPAL4.js +0 -166
- package/dist/chunk-S7GUPAL4.js.map +0 -1
- package/dist/chunk-XXZ56SKG.mjs.map +0 -1
- package/src/connect/create-node-router.ts +0 -44
- package/src/connect/handler.ts +0 -46
- package/src/connect/middleware/cors-middleware.ts +0 -10
- package/src/connect/middleware/http-header-normalizer.ts +0 -93
- package/src/connect/middleware/rate-limiter-middleware.ts +0 -43
- package/src/connect/middleware/serializers-middleware.ts +0 -121
- package/src/connect/serializers/types.d.ts +0 -1
- package/src/connect/serializers/xml.ts +0 -13
- package/src/connect/serializers/yaml.ts +0 -7
- package/src/error-handler/jsonapi-error-handler.ts +0 -46
- package/src/error-handler/problem-error-handler.ts +0 -44
- package/src/error-handler/types.d.ts +0 -14
- package/src/error-handler/utils.ts +0 -39
- package/src/index-browser.tsx +0 -1
- package/src/index-server.ts +0 -75
- package/src/next/cli/index.ts +0 -2
- package/src/next/cli/list/api-route-file-parser.ts +0 -74
- package/src/next/cli/list/collect-api-route-files.ts +0 -42
- package/src/next/cli/list/list-command.ts +0 -105
- package/src/next/cli/list/routes-render.ts +0 -62
- package/src/next/cli/list/types.d.ts +0 -1
- package/src/next/index-browser.tsx +0 -3
- package/src/next/index-server.ts +0 -6
- package/src/next/routes/api/swagger.ts +0 -23
- package/src/next/routes/pages/swagger/get-static-properties-swagger.ts +0 -32
- package/src/next/routes/pages/swagger/redoc.tsx +0 -35
- package/src/next/routes/pages/swagger/swagger.tsx +0 -44
- package/src/next/webpack/with-open-api.ts +0 -63
- package/src/swagger/extend-swagger-spec.ts +0 -167
- package/src/swagger/swagger-handler.ts +0 -83
- package/src/utils.ts +0 -37
- package/src/zod/date-in-schema.ts +0 -57
- package/src/zod/date-out-schema.ts +0 -41
- package/src/zod/index.ts +0 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@visulima/api-platform",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "visulima api platform",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"anolilab",
|
|
@@ -71,26 +71,27 @@
|
|
|
71
71
|
"browser": "dist/index-browser.mjs",
|
|
72
72
|
"types": "dist/index-server.d.ts",
|
|
73
73
|
"files": [
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
74
|
+
"bin/**",
|
|
75
|
+
"next/**",
|
|
76
|
+
"dist/**",
|
|
77
|
+
"recipes/**",
|
|
77
78
|
"README.md",
|
|
78
79
|
"CHANGELOG.md",
|
|
79
80
|
"LICENSE.md"
|
|
80
81
|
],
|
|
81
82
|
"scripts": {
|
|
82
|
-
"build": "cross-env NODE_ENV=development tsup",
|
|
83
|
-
"build:prod": "cross-env NODE_ENV=production tsup",
|
|
83
|
+
"build": "rimraf dist && cross-env NODE_ENV=development tsup",
|
|
84
|
+
"build:prod": "rimraf dist && cross-env NODE_ENV=production tsup",
|
|
84
85
|
"clean": "rimraf node_modules dist",
|
|
85
86
|
"coverage": "vitest run --coverage",
|
|
86
87
|
"dev": "pnpm predev && pnpm run build --watch",
|
|
87
|
-
"lint:eslint": "cross-env NO_LOGS=true eslint --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.cjs",
|
|
88
|
+
"lint:eslint": "cross-env NO_LOGS=true eslint . --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.cjs --cache --cache-strategy content .",
|
|
88
89
|
"lint:eslint:fix": "pnpm run lint:eslint --fix",
|
|
89
90
|
"test": "vitest"
|
|
90
91
|
},
|
|
91
92
|
"dependencies": {
|
|
92
|
-
"@visulima/connect": "1.
|
|
93
|
-
"@visulima/jsdoc-open-api": "1.2.
|
|
93
|
+
"@visulima/connect": "1.3.1",
|
|
94
|
+
"@visulima/jsdoc-open-api": "1.2.1",
|
|
94
95
|
"accepts": "^1.3.8",
|
|
95
96
|
"case": "^1.6.3",
|
|
96
97
|
"debug": "^4.3.4",
|
|
@@ -120,14 +121,13 @@
|
|
|
120
121
|
"@types/qs": "^6.9.7",
|
|
121
122
|
"@types/react": "^18.0.21",
|
|
122
123
|
"@types/react-dom": "^18.0.6",
|
|
123
|
-
"@types/swagger-jsdoc": "^6.0.1",
|
|
124
124
|
"@types/swagger-ui-react": "^4.11.0",
|
|
125
125
|
"@types/webpack": "^5.28.0",
|
|
126
126
|
"@typescript-eslint/eslint-plugin": "^5.40.0",
|
|
127
127
|
"@typescript-eslint/parser": "^5.40.0",
|
|
128
|
-
"@visulima/crud": "1.0.
|
|
129
|
-
"@visulima/readdir": "1.3.
|
|
130
|
-
"chalk": "
|
|
128
|
+
"@visulima/crud": "1.0.2",
|
|
129
|
+
"@visulima/readdir": "1.3.1",
|
|
130
|
+
"chalk": "5.1.2",
|
|
131
131
|
"commander": "^9.4.1",
|
|
132
132
|
"core-js": "^3.25.5",
|
|
133
133
|
"cors": "^2.8.5",
|
|
@@ -145,16 +145,16 @@
|
|
|
145
145
|
"eslint-plugin-optimize-regex": "^1.2.1",
|
|
146
146
|
"eslint-plugin-promise": "^6.0.1",
|
|
147
147
|
"eslint-plugin-radar": "^0.2.1",
|
|
148
|
-
"eslint-plugin-react": "7.31.
|
|
148
|
+
"eslint-plugin-react": "7.31.11",
|
|
149
149
|
"eslint-plugin-react-hooks": "4.6.0",
|
|
150
150
|
"eslint-plugin-simple-import-sort": "^8.0.0",
|
|
151
151
|
"eslint-plugin-sort-keys-fix": "^1.1.2",
|
|
152
152
|
"eslint-plugin-testing-library": "^5.7.2",
|
|
153
|
-
"eslint-plugin-unicorn": "^
|
|
153
|
+
"eslint-plugin-unicorn": "^45.0.0",
|
|
154
154
|
"eslint-plugin-you-dont-need-lodash-underscore": "^6.12.0",
|
|
155
155
|
"eslint-plugin-you-dont-need-momentjs": "^1.6.0",
|
|
156
156
|
"mobx": "^6.6.2",
|
|
157
|
-
"next": "^12.
|
|
157
|
+
"next": "^12.0.0 || ^13.0.0",
|
|
158
158
|
"next-test-api-route-handler": "^4.0.0-canary.1",
|
|
159
159
|
"node-mocks-http": "^1.11.0",
|
|
160
160
|
"openapi-types": "^12.0.2",
|
|
@@ -170,22 +170,22 @@
|
|
|
170
170
|
"swagger-ui-react": "^4.14.3",
|
|
171
171
|
"tsup": "^6.2.3",
|
|
172
172
|
"typescript": "^4.8.4",
|
|
173
|
-
"vitest": "^0.
|
|
173
|
+
"vitest": "^0.25.0",
|
|
174
174
|
"webpack": "^5.74.0",
|
|
175
175
|
"zod": "^3.19.1"
|
|
176
176
|
},
|
|
177
177
|
"peerDependencies": {
|
|
178
|
-
"next": "^12.3.1",
|
|
179
178
|
"react": "^18.2.0",
|
|
180
179
|
"react-dom": "^18.2.0",
|
|
181
180
|
"zod": "^3.19.1"
|
|
182
181
|
},
|
|
183
182
|
"optionalDependencies": {
|
|
184
|
-
"@visulima/crud": "1.0.
|
|
185
|
-
"@visulima/readdir": "1.3.
|
|
183
|
+
"@visulima/crud": "1.0.2",
|
|
184
|
+
"@visulima/readdir": "1.3.1",
|
|
186
185
|
"chalk": "4.1.2",
|
|
187
186
|
"commander": "^9.4.1",
|
|
188
187
|
"cors": "^2.8.5",
|
|
188
|
+
"next": "^12.0.0 || ^13.0.0",
|
|
189
189
|
"rate-limiter-flexible": "^2.3.11",
|
|
190
190
|
"redoc": "^2.0.0",
|
|
191
191
|
"swagger-ui-react": "^4.14.3",
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { getSwaggerStaticProps as getSwaggerStaticProperties, RedocPage } from "@visulima/api-platform/next";
|
|
2
|
+
|
|
3
|
+
export const getStaticProps = getSwaggerStaticProperties(`${process.env.NEXT_PUBLIC_APP_ORIGIN}/api/swagger`);
|
|
4
|
+
|
|
5
|
+
export default RedocPage(`${process.env.NEXT_PUBLIC_APP_NAME} swagger`);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { getSwaggerStaticProps as getSwaggerStaticProperties, SwaggerPage } from "@visulima/api-platform/next";
|
|
2
|
+
|
|
3
|
+
export const getStaticProps = getSwaggerStaticProperties(`${process.env.NEXT_PUBLIC_APP_ORIGIN}/api/swagger`);
|
|
4
|
+
|
|
5
|
+
export default SwaggerPage(`${process.env.NEXT_PUBLIC_APP_NAME} swagger`);
|
package/dist/chunk-2LATTLUM.mjs
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
// src/swagger/swagger-handler.ts
|
|
2
|
-
import { modelsToOpenApi } from "@visulima/crud";
|
|
3
|
-
import debug from "debug";
|
|
4
|
-
import merge from "lodash.merge";
|
|
5
|
-
import { existsSync, readFileSync } from "fs";
|
|
6
|
-
import path from "path";
|
|
7
|
-
|
|
8
|
-
// src/connect/serializers/yaml.ts
|
|
9
|
-
import { stringify } from "yaml";
|
|
10
|
-
var yamlTransformer = (data) => stringify(data, { indent: 2 });
|
|
11
|
-
var yaml_default = yamlTransformer;
|
|
12
|
-
|
|
13
|
-
// src/swagger/extend-swagger-spec.ts
|
|
14
|
-
import { header as headerCase } from "case";
|
|
15
|
-
var extendComponentSchemas = (spec, schemaName, schema) => {
|
|
16
|
-
if (typeof spec.components !== "object") {
|
|
17
|
-
spec.components = {};
|
|
18
|
-
}
|
|
19
|
-
if (typeof spec.components.schemas !== "object") {
|
|
20
|
-
spec.components.schemas = {};
|
|
21
|
-
}
|
|
22
|
-
if (typeof spec.components.schemas[schemaName] === "undefined") {
|
|
23
|
-
spec.components.schemas[schemaName] = schema;
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
var extendComponentExamples = (spec, exampleName, example) => {
|
|
27
|
-
if (typeof spec.components !== "object") {
|
|
28
|
-
spec.components = {};
|
|
29
|
-
}
|
|
30
|
-
if (typeof spec.components.examples !== "object") {
|
|
31
|
-
spec.components.examples = {};
|
|
32
|
-
}
|
|
33
|
-
if (typeof spec.components.examples[exampleName] === "undefined") {
|
|
34
|
-
spec.components.examples[exampleName] = example;
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
function extendSwaggerWithMediaTypeSchema(responseSpec, allowedMediaTypes, pathKey, spec, methodSpec, status) {
|
|
38
|
-
let examples;
|
|
39
|
-
Object.entries(responseSpec.content).forEach(([mediaName, contentSpec]) => {
|
|
40
|
-
if (typeof contentSpec.schema === "object") {
|
|
41
|
-
const { schema } = contentSpec;
|
|
42
|
-
if (mediaName === "application/json" && typeof contentSpec.examples !== "undefined") {
|
|
43
|
-
examples = contentSpec.examples;
|
|
44
|
-
}
|
|
45
|
-
if (typeof schema.$ref !== "undefined") {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const schemaIsArray = schema.type === "array";
|
|
49
|
-
Object.entries(allowedMediaTypes || {}).forEach(([mediaType, allowed]) => {
|
|
50
|
-
var _a, _b, _c;
|
|
51
|
-
if (!allowed) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const schemaName = `${headerCase(pathKey.trim().replace("/", ""))}${mediaType === "application/ld+json" ? ".jsonld" : ""}`;
|
|
55
|
-
extendComponentSchemas(spec, schemaName, schema);
|
|
56
|
-
if (typeof ((_c = (_b = (_a = methodSpec == null ? void 0 : methodSpec.responses) == null ? void 0 : _a[status]) == null ? void 0 : _b.content[mediaType]) == null ? void 0 : _c.schema) === "undefined") {
|
|
57
|
-
methodSpec.responses[status].content[mediaType] = { schema: {} };
|
|
58
|
-
}
|
|
59
|
-
methodSpec.responses[status].content[mediaType].schema = schemaIsArray ? {
|
|
60
|
-
type: "array",
|
|
61
|
-
items: {
|
|
62
|
-
$ref: `#/components/schemas/${schemaName}`
|
|
63
|
-
}
|
|
64
|
-
} : {
|
|
65
|
-
$ref: `#/components/schemas/${schemaName}`
|
|
66
|
-
};
|
|
67
|
-
if (typeof methodSpec.produces === "undefined") {
|
|
68
|
-
methodSpec.produces = [];
|
|
69
|
-
}
|
|
70
|
-
methodSpec.produces.push(mediaType);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
return examples;
|
|
75
|
-
}
|
|
76
|
-
function extendSwaggerWithMediaTypeExamples(responseSpec, allowedMediaTypes, pathKey, spec, examples, methodSpec, status) {
|
|
77
|
-
Object.keys(responseSpec.content).forEach((mediaName) => {
|
|
78
|
-
if (mediaName === "application/json") {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
Object.entries(allowedMediaTypes || {}).forEach(([mediaType, allowed]) => {
|
|
82
|
-
var _a, _b, _c;
|
|
83
|
-
if (!allowed) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const examplesName = `${headerCase(pathKey.trim().replace("/", ""))}${mediaType === "application/ld+json" ? ".jsonld" : ""}`;
|
|
87
|
-
extendComponentExamples(spec, examplesName, examples);
|
|
88
|
-
if (typeof ((_c = (_b = (_a = methodSpec == null ? void 0 : methodSpec.responses) == null ? void 0 : _a[status]) == null ? void 0 : _b.content[mediaType]) == null ? void 0 : _c.examples) === "undefined") {
|
|
89
|
-
methodSpec.responses[status].content[mediaType] = { examples: {} };
|
|
90
|
-
}
|
|
91
|
-
methodSpec.responses[status].content[mediaType].examples = examples;
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
function extendSwaggerSpec(spec, allowedMediaTypes) {
|
|
96
|
-
if (typeof spec === "object" && typeof spec.paths === "object") {
|
|
97
|
-
Object.entries(spec.paths).forEach(([pathKey, pathSpec]) => {
|
|
98
|
-
Object.values(pathSpec).forEach((methodSpec) => {
|
|
99
|
-
if (typeof methodSpec.responses === "object") {
|
|
100
|
-
Object.entries(methodSpec.responses).forEach(([status, responseSpec]) => {
|
|
101
|
-
if (typeof responseSpec.content === "object") {
|
|
102
|
-
let examples = extendSwaggerWithMediaTypeSchema(responseSpec, allowedMediaTypes, pathKey, spec, methodSpec, status);
|
|
103
|
-
if (typeof examples !== "undefined") {
|
|
104
|
-
extendSwaggerWithMediaTypeExamples(responseSpec, allowedMediaTypes, pathKey, spec, examples, methodSpec, status);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
return spec;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// src/swagger/swagger-handler.ts
|
|
116
|
-
var swaggerCrudDebug = debug("visulima:api-platform:swagger:crud:get-static-properties-swagger");
|
|
117
|
-
var swaggerHandler = (options = {}) => {
|
|
118
|
-
const {
|
|
119
|
-
allowedMediaTypes = {
|
|
120
|
-
"application/json": true
|
|
121
|
-
},
|
|
122
|
-
swaggerFilePath,
|
|
123
|
-
crud
|
|
124
|
-
} = options;
|
|
125
|
-
return async (request, response) => {
|
|
126
|
-
const swaggerPath = path.join(process.cwd(), swaggerFilePath || "swagger/swagger.json");
|
|
127
|
-
if (!existsSync(swaggerPath)) {
|
|
128
|
-
throw new Error(`Swagger file not found at ${swaggerPath}. Did you change the output path in "withOpenApi" inside the next.config.js file?`);
|
|
129
|
-
}
|
|
130
|
-
const fileContents = readFileSync(swaggerPath, "utf8");
|
|
131
|
-
let spec = extendSwaggerSpec(JSON.parse(fileContents), allowedMediaTypes);
|
|
132
|
-
let crudSwagger = {};
|
|
133
|
-
if (typeof crud !== "undefined") {
|
|
134
|
-
try {
|
|
135
|
-
const modelsOpenApi = await modelsToOpenApi(crud);
|
|
136
|
-
crudSwagger = {
|
|
137
|
-
components: { schemas: modelsOpenApi.schemas, examples: modelsOpenApi.examples },
|
|
138
|
-
tags: modelsOpenApi.tags,
|
|
139
|
-
paths: modelsOpenApi.paths
|
|
140
|
-
};
|
|
141
|
-
crudSwagger = extendSwaggerSpec(crudSwagger, allowedMediaTypes);
|
|
142
|
-
swaggerCrudDebug(JSON.stringify(crudSwagger, null, 2));
|
|
143
|
-
spec = merge(spec, crudSwagger);
|
|
144
|
-
} catch (error) {
|
|
145
|
-
console.log(error);
|
|
146
|
-
throw new Error("Please install @visulima/crud to use the crud swagger generator.");
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (typeof request.headers.accept === "string" && /yaml|yml/.test(request.headers.accept)) {
|
|
150
|
-
response.statusCode = 200;
|
|
151
|
-
response.setHeader("Content-Type", request.headers.accept);
|
|
152
|
-
response.end(yaml_default(spec));
|
|
153
|
-
} else {
|
|
154
|
-
response.statusCode = 200;
|
|
155
|
-
response.setHeader("Content-Type", "application/json");
|
|
156
|
-
response.end(JSON.stringify(spec, null, 2));
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
};
|
|
160
|
-
var swagger_handler_default = swaggerHandler;
|
|
161
|
-
|
|
162
|
-
export {
|
|
163
|
-
yaml_default,
|
|
164
|
-
swagger_handler_default
|
|
165
|
-
};
|
|
166
|
-
//# sourceMappingURL=chunk-2LATTLUM.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/swagger/swagger-handler.ts","../src/connect/serializers/yaml.ts","../src/swagger/extend-swagger-spec.ts"],"sourcesContent":["// eslint-disable-next-line unicorn/prevent-abbreviations,import/no-extraneous-dependencies\nimport type { ModelsToOpenApiParameters, SwaggerModelsConfig } from \"@visulima/crud\";\n// eslint-disable-next-line unicorn/prevent-abbreviations,import/no-extraneous-dependencies\nimport { modelsToOpenApi } from \"@visulima/crud\";\nimport debug from \"debug\";\nimport merge from \"lodash.merge\";\nimport { existsSync, readFileSync } from \"node:fs\";\nimport type { IncomingMessage, ServerResponse } from \"node:http\";\nimport path from \"node:path\";\nimport type { OAS3Definition, Tag } from \"swagger-jsdoc\";\n\nimport yamlTransformer from \"../connect/serializers/yaml\";\nimport extendSwaggerSpec from \"./extend-swagger-spec\";\n\n// eslint-disable-next-line testing-library/no-debugging-utils\nconst swaggerCrudDebug = debug(\"visulima:api-platform:swagger:crud:get-static-properties-swagger\");\n\nconst swaggerHandler = (\n options: Partial<{\n allowedMediaTypes: { [key: string]: boolean };\n swaggerFilePath: string;\n crud: Exclude<ModelsToOpenApiParameters, \"swagger\"> & {\n swagger?: {\n models?: SwaggerModelsConfig<string>;\n };\n };\n }> = {},\n) => {\n const {\n allowedMediaTypes = {\n \"application/json\": true,\n },\n swaggerFilePath,\n crud,\n } = options;\n\n return async <Request extends IncomingMessage, Response extends ServerResponse>(request: Request, response: Response) => {\n const swaggerPath = path.join(process.cwd(), swaggerFilePath || \"swagger/swagger.json\");\n\n if (!existsSync(swaggerPath)) {\n throw new Error(`Swagger file not found at ${swaggerPath}. Did you change the output path in \"withOpenApi\" inside the next.config.js file?`);\n }\n\n const fileContents = readFileSync(swaggerPath, \"utf8\");\n\n let spec = extendSwaggerSpec(JSON.parse(fileContents) as OAS3Definition, allowedMediaTypes) as OAS3Definition;\n let crudSwagger: Partial<OAS3Definition> = {};\n\n if (typeof crud !== \"undefined\") {\n try {\n const modelsOpenApi = await modelsToOpenApi(crud);\n\n crudSwagger = {\n components: { schemas: modelsOpenApi.schemas, examples: modelsOpenApi.examples },\n tags: modelsOpenApi.tags as Tag[],\n paths: modelsOpenApi.paths,\n };\n\n crudSwagger = extendSwaggerSpec(crudSwagger, allowedMediaTypes);\n\n swaggerCrudDebug(JSON.stringify(crudSwagger, null, 2));\n\n spec = merge(spec, crudSwagger);\n } catch (error) {\n // eslint-disable-next-line no-console\n console.log(error);\n throw new Error(\"Please install @visulima/crud to use the crud swagger generator.\");\n }\n }\n\n if (typeof request.headers.accept === \"string\" && /yaml|yml/.test(request.headers.accept)) {\n response.statusCode = 200;\n response.setHeader(\"Content-Type\", request.headers.accept);\n response.end(yamlTransformer(spec));\n } else {\n response.statusCode = 200;\n response.setHeader(\"Content-Type\", \"application/json\");\n response.end(JSON.stringify(spec, null, 2));\n }\n };\n};\n\nexport default swaggerHandler;\n","import { stringify } from \"yaml\";\n\nimport type { Serializer } from \"./types\";\n\nconst yamlTransformer: Serializer = (data) => stringify(data, { indent: 2 });\n\nexport default yamlTransformer;\n","import { header as headerCase } from \"case\";\nimport type { OpenAPIV3 } from \"openapi-types\";\nimport type { OAS3Definition, Operation, Responses } from \"swagger-jsdoc\";\n\nconst extendComponentSchemas = (spec: Partial<OAS3Definition>, schemaName: string, schema: OpenAPIV3.SchemaObject) => {\n if (typeof spec.components !== \"object\") {\n // eslint-disable-next-line no-param-reassign\n spec.components = {};\n }\n\n if (typeof spec.components.schemas !== \"object\") {\n // eslint-disable-next-line no-param-reassign\n spec.components.schemas = {};\n }\n\n if (typeof spec.components.schemas[schemaName] === \"undefined\") {\n // eslint-disable-next-line no-param-reassign\n spec.components.schemas[schemaName] = schema;\n }\n};\n\nconst extendComponentExamples = (spec: Partial<OAS3Definition>, exampleName: string, example: OpenAPIV3.SchemaObject) => {\n if (typeof spec.components !== \"object\") {\n // eslint-disable-next-line no-param-reassign\n spec.components = {};\n }\n\n if (typeof spec.components.examples !== \"object\") {\n // eslint-disable-next-line no-param-reassign\n spec.components.examples = {};\n }\n\n if (typeof spec.components.examples[exampleName] === \"undefined\") {\n // eslint-disable-next-line no-param-reassign\n spec.components.examples[exampleName] = example;\n }\n};\n\nfunction extendSwaggerWithMediaTypeSchema(\n responseSpec: OpenAPIV3.ResponseObject,\n allowedMediaTypes: { [p: string]: boolean } | undefined,\n pathKey: string,\n spec: Partial<OAS3Definition>,\n methodSpec: Operation,\n status: string,\n) {\n let examples:\n | {\n [media: string]: OpenAPIV3.ReferenceObject | OpenAPIV3.ExampleObject;\n }\n | undefined;\n\n Object.entries(responseSpec.content as object).forEach(([mediaName, contentSpec]) => {\n if (typeof contentSpec.schema === \"object\") {\n const { schema } = contentSpec;\n\n if (mediaName === \"application/json\" && typeof contentSpec.examples !== \"undefined\") {\n examples = contentSpec.examples;\n }\n\n if (typeof (schema as OpenAPIV3.ReferenceObject).$ref !== \"undefined\") {\n return;\n }\n\n const schemaIsArray = (schema as OpenAPIV3.SchemaObject).type === \"array\";\n\n Object.entries(allowedMediaTypes || {}).forEach(([mediaType, allowed]) => {\n if (!allowed) {\n return;\n }\n\n // eslint-disable-next-line max-len\n const schemaName = `${headerCase(pathKey.trim().replace(\"/\", \"\"))}${mediaType === \"application/ld+json\" ? \".jsonld\" : \"\"}`;\n\n extendComponentSchemas(spec as OAS3Definition, schemaName, schema as OpenAPIV3.SchemaObject);\n\n if (typeof methodSpec?.responses?.[status]?.content[mediaType]?.schema === \"undefined\") {\n // eslint-disable-next-line no-param-reassign\n (methodSpec.responses as Responses)[status].content[mediaType] = { schema: {} };\n }\n\n // eslint-disable-next-line no-param-reassign\n (methodSpec.responses as Responses)[status].content[mediaType].schema = schemaIsArray\n ? {\n type: \"array\",\n items: {\n $ref: `#/components/schemas/${schemaName}`,\n },\n }\n : {\n $ref: `#/components/schemas/${schemaName}`,\n };\n\n if (typeof methodSpec.produces === \"undefined\") {\n // eslint-disable-next-line no-param-reassign\n methodSpec.produces = [];\n }\n\n methodSpec.produces.push(mediaType);\n });\n }\n });\n\n return examples;\n}\n\nfunction extendSwaggerWithMediaTypeExamples(\n responseSpec: OpenAPIV3.ResponseObject,\n allowedMediaTypes: { [p: string]: boolean } | undefined,\n pathKey: string,\n spec: Partial<OAS3Definition>,\n examples: { [p: string]: OpenAPIV3.ReferenceObject | OpenAPIV3.ExampleObject } | undefined,\n methodSpec: Operation,\n status: string,\n) {\n Object.keys(responseSpec.content as object).forEach((mediaName) => {\n if (mediaName === \"application/json\") {\n return;\n }\n\n Object.entries(allowedMediaTypes || {}).forEach(([mediaType, allowed]) => {\n if (!allowed) {\n return;\n }\n\n // eslint-disable-next-line max-len\n const examplesName = `${headerCase(pathKey.trim().replace(\"/\", \"\"))}${mediaType === \"application/ld+json\" ? \".jsonld\" : \"\"}`;\n\n extendComponentExamples(spec as OAS3Definition, examplesName, examples as OpenAPIV3.SchemaObject);\n\n if (typeof methodSpec?.responses?.[status]?.content[mediaType]?.examples === \"undefined\") {\n // eslint-disable-next-line no-param-reassign\n (methodSpec.responses as Responses)[status].content[mediaType] = { examples: {} };\n }\n\n // eslint-disable-next-line no-param-reassign\n (methodSpec.responses as Responses)[status].content[mediaType].examples = examples;\n });\n });\n}\n\n// eslint-disable-next-line radar/cognitive-complexity\nexport default function extendSwaggerSpec(spec: Partial<OAS3Definition>, allowedMediaTypes?: { [key: string]: boolean }): Partial<OAS3Definition> {\n if (typeof spec === \"object\" && typeof spec.paths === \"object\") {\n Object.entries(spec.paths).forEach(([pathKey, pathSpec]) => {\n Object.values(pathSpec).forEach((methodSpec) => {\n if (typeof methodSpec.responses === \"object\") {\n Object.entries<OpenAPIV3.ResponseObject>(methodSpec.responses).forEach(([status, responseSpec]) => {\n if (typeof responseSpec.content === \"object\") {\n let examples:\n | {\n [media: string]: OpenAPIV3.ReferenceObject | OpenAPIV3.ExampleObject;\n }\n | undefined = extendSwaggerWithMediaTypeSchema(responseSpec, allowedMediaTypes, pathKey, spec, methodSpec, status);\n\n if (typeof examples !== \"undefined\") {\n extendSwaggerWithMediaTypeExamples(responseSpec, allowedMediaTypes, pathKey, spec, examples, methodSpec, status);\n }\n }\n });\n }\n });\n });\n }\n\n return spec;\n}\n"],"mappings":";AAGA,SAAS,uBAAuB;AAChC,OAAO,WAAW;AAClB,OAAO,WAAW;AAClB,SAAS,YAAY,oBAAoB;AAEzC,OAAO,UAAU;;;ACRjB,SAAS,iBAAiB;AAI1B,IAAM,kBAA8B,CAAC,SAAS,UAAU,MAAM,EAAE,QAAQ,EAAE,CAAC;AAE3E,IAAO,eAAQ;;;ACNf,SAAS,UAAU,kBAAkB;AAIrC,IAAM,yBAAyB,CAAC,MAA+B,YAAoB,WAAmC;AAClH,MAAI,OAAO,KAAK,eAAe,UAAU;AAErC,SAAK,aAAa,CAAC;AAAA,EACvB;AAEA,MAAI,OAAO,KAAK,WAAW,YAAY,UAAU;AAE7C,SAAK,WAAW,UAAU,CAAC;AAAA,EAC/B;AAEA,MAAI,OAAO,KAAK,WAAW,QAAQ,gBAAgB,aAAa;AAE5D,SAAK,WAAW,QAAQ,cAAc;AAAA,EAC1C;AACJ;AAEA,IAAM,0BAA0B,CAAC,MAA+B,aAAqB,YAAoC;AACrH,MAAI,OAAO,KAAK,eAAe,UAAU;AAErC,SAAK,aAAa,CAAC;AAAA,EACvB;AAEA,MAAI,OAAO,KAAK,WAAW,aAAa,UAAU;AAE9C,SAAK,WAAW,WAAW,CAAC;AAAA,EAChC;AAEA,MAAI,OAAO,KAAK,WAAW,SAAS,iBAAiB,aAAa;AAE9D,SAAK,WAAW,SAAS,eAAe;AAAA,EAC5C;AACJ;AAEA,SAA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gB,kBAAkB;AACrD,eAAS,IAAI,KAAK,UAAU,MAAM,MAAM,CAAC,CAAC;AAAA,IAC9C;AAAA,EACJ;AACJ;AAEA,IAAO,0BAAQ;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/next/routes/pages/swagger/redoc.tsx","../src/next/routes/pages/swagger/swagger.tsx","../src/next/routes/pages/swagger/get-static-properties-swagger.ts"],"names":["Head","React"],"mappings":";AAGA,OAAO,UAAU;AACjB,OAAO,WAAW;AAIlB,SAAS,uBAAuB;AAKhC,IAAM,mBAI0D,CAAC,MAAM,UAAU,CAAC,MAAM,CAAC,EAAE,YAAY,MAC3F,0DACI,oCAAC,YACG,oCAAC,eAAO,IAAK,GACb,oCAAC,eACI;AAAA;AAAA;AAAA;AAAA,CAKL,CACJ,GAEA,oCAAC;AAAA,EAAiB,GAAG;AAAA,EAAS,MAAM;AAAA,CAAa,CACrD;AAGZ,IAAO,gBAAQ;;;ACjCf,OAAO,aAAa;AAEpB,OAAOA,WAAU;AAEjB,OAAOC,YAAW;AAKlB,IAAM,YAAY,QAIf,OAAO,qBAAqB,EAAE,KAAK,MAAM,CAAC;AAC7C,QAIG,OAAO,oCAAoC,EAAE,KAAK,MAAM,CAAC;AAG5D,IAAM,qBAI0D,CAAC,MAAM,UAAU,CAAC,MAAM,CAAC,EAAE,YAAY,MAC3F,gBAAAA,OAAA,cAAAA,OAAA,gBACI,gBAAAA,OAAA,cAACD,OAAA,MACG,gBAAAC,OAAA,cAAC,eAAO,IAAK,GACb,gBAAAA,OAAA,cAAC,eACI;AAAA;AAAA;AAAA;AAAA,CAKL,CACJ,GAEA,gBAAAA,OAAA,cAAC;AAAA,EAAW,GAAG;AAAA,EAAS,MAAM;AAAA,CAAa,CAC/C;AAGZ,IAAO,kBAAQ;;;AC1Cf,OAAO,WAAW;AAKlB,IAAM,eAAe,MAAM,6DAA6D;AAGxF,IAAM,iBAEgB,CAAC,eAAe,YAKhC;AAEF,QAAM,WAAW,MAAM,MAAM,UAAU;AACvC,QAAM,cAAc,MAAM,SAAS,KAAK;AAExC,eAAa,WAAW;AAExB,SAAO;AAAA,IACH,OAAO;AAAA,MACH;AAAA,MACA,aAAa,KAAK,MAAM,KAAK,UAAU,WAAW,CAAC;AAAA,IACvD;AAAA,EACJ;AACJ;AAEA,IAAO,wCAAQ","sourcesContent":["// eslint-disable-next-line import/no-extraneous-dependencies\nimport type { InferGetStaticPropsType, NextPage } from \"next\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport Head from \"next/head\";\nimport React from \"react\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport type { RedocStandaloneProps } from \"redoc\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport { RedocStandalone } from \"redoc\";\n\nimport getStaticProps from \"./get-static-properties-swagger\";\n\n// eslint-disable-next-line max-len\nconst RedocApiDocument: (\n name: string,\n swagger?: Exclude<RedocStandaloneProps, \"spec\">,\n // eslint-disable-next-line max-len,unicorn/no-useless-undefined\n) => NextPage<InferGetStaticPropsType<typeof getStaticProps>> = (name, swagger = {}) => ({ swaggerData }: InferGetStaticPropsType<typeof getStaticProps>) => (\n <>\n <Head>\n <title>{name}</title>\n <style>\n {`\nbody {\n background: #fafafa !important;\n}\n`}\n </style>\n </Head>\n {/* eslint-disable-next-line react/jsx-props-no-spreading */}\n <RedocStandalone {...swagger} spec={swaggerData} />\n </>\n);\n\nexport default RedocApiDocument;\n","import type { InferGetStaticPropsType, NextPage } from \"next\";\nimport dynamic from \"next/dynamic\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport Head from \"next/head\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport React from \"react\";\nimport type { SwaggerUIProps } from \"swagger-ui-react\";\n\nimport getStaticProps from \"./get-static-properties-swagger\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nconst SwaggerUI = dynamic<{\n spec: any;\n // @ts-ignore\n // eslint-disable-next-line import/no-extraneous-dependencies\n}>(import(\"swagger-ui-react\"), { ssr: false });\ndynamic<{\n spec: any;\n // @ts-ignore\n // eslint-disable-next-line import/no-extraneous-dependencies\n}>(import(\"swagger-ui-react/swagger-ui.css\"), { ssr: false });\n\n// eslint-disable-next-line max-len\nconst SwaggerApiDocument: (\n name: string,\n swagger?: Exclude<SwaggerUIProps, \"spec\">,\n // eslint-disable-next-line max-len,unicorn/no-useless-undefined\n) => NextPage<InferGetStaticPropsType<typeof getStaticProps>> = (name, swagger = {}) => ({ swaggerData }: InferGetStaticPropsType<typeof getStaticProps>) => (\n <>\n <Head>\n <title>{name}</title>\n <style>\n {`\nbody {\n background: #fafafa !important;\n}\n`}\n </style>\n </Head>\n {/* eslint-disable-next-line react/jsx-props-no-spreading */}\n <SwaggerUI {...swagger} spec={swaggerData} />\n </>\n);\n\nexport default SwaggerApiDocument;\n","// eslint-disable-next-line unicorn/prevent-abbreviations\nimport debug from \"debug\";\nimport type { GetStaticProps } from \"next\";\nimport type { OAS3Definition } from \"swagger-jsdoc\";\n\n// eslint-disable-next-line testing-library/no-debugging-utils\nconst swaggerDebug = debug(\"visulima:api-platform:swagger:get-static-properties-swagger\");\n\n// eslint-disable-next-line unicorn/consistent-function-scoping\nconst getStaticProps: (\n swaggerUrl: string,\n) => GetStaticProps = (swaggerUrl) => async (): Promise<{\n props: {\n swaggerUrl: string;\n swaggerData: OAS3Definition;\n };\n}> => {\n // eslint-disable-next-line compat/compat\n const response = await fetch(swaggerUrl);\n const swaggerData = await response.json();\n\n swaggerDebug(swaggerData);\n\n return {\n props: {\n swaggerUrl,\n swaggerData: JSON.parse(JSON.stringify(swaggerData)),\n },\n };\n};\n\nexport default getStaticProps;\n"]}
|
package/dist/chunk-S7GUPAL4.js
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/swagger/swagger-handler.ts
|
|
2
|
-
var _crud = require('@visulima/crud');
|
|
3
|
-
var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
|
|
4
|
-
var _lodashmerge = require('lodash.merge'); var _lodashmerge2 = _interopRequireDefault(_lodashmerge);
|
|
5
|
-
var _fs = require('fs');
|
|
6
|
-
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
7
|
-
|
|
8
|
-
// src/connect/serializers/yaml.ts
|
|
9
|
-
var _yaml = require('yaml');
|
|
10
|
-
var yamlTransformer = (data) => _yaml.stringify.call(void 0, data, { indent: 2 });
|
|
11
|
-
var yaml_default = yamlTransformer;
|
|
12
|
-
|
|
13
|
-
// src/swagger/extend-swagger-spec.ts
|
|
14
|
-
var _case = require('case');
|
|
15
|
-
var extendComponentSchemas = (spec, schemaName, schema) => {
|
|
16
|
-
if (typeof spec.components !== "object") {
|
|
17
|
-
spec.components = {};
|
|
18
|
-
}
|
|
19
|
-
if (typeof spec.components.schemas !== "object") {
|
|
20
|
-
spec.components.schemas = {};
|
|
21
|
-
}
|
|
22
|
-
if (typeof spec.components.schemas[schemaName] === "undefined") {
|
|
23
|
-
spec.components.schemas[schemaName] = schema;
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
var extendComponentExamples = (spec, exampleName, example) => {
|
|
27
|
-
if (typeof spec.components !== "object") {
|
|
28
|
-
spec.components = {};
|
|
29
|
-
}
|
|
30
|
-
if (typeof spec.components.examples !== "object") {
|
|
31
|
-
spec.components.examples = {};
|
|
32
|
-
}
|
|
33
|
-
if (typeof spec.components.examples[exampleName] === "undefined") {
|
|
34
|
-
spec.components.examples[exampleName] = example;
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
function extendSwaggerWithMediaTypeSchema(responseSpec, allowedMediaTypes, pathKey, spec, methodSpec, status) {
|
|
38
|
-
let examples;
|
|
39
|
-
Object.entries(responseSpec.content).forEach(([mediaName, contentSpec]) => {
|
|
40
|
-
if (typeof contentSpec.schema === "object") {
|
|
41
|
-
const { schema } = contentSpec;
|
|
42
|
-
if (mediaName === "application/json" && typeof contentSpec.examples !== "undefined") {
|
|
43
|
-
examples = contentSpec.examples;
|
|
44
|
-
}
|
|
45
|
-
if (typeof schema.$ref !== "undefined") {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const schemaIsArray = schema.type === "array";
|
|
49
|
-
Object.entries(allowedMediaTypes || {}).forEach(([mediaType, allowed]) => {
|
|
50
|
-
var _a, _b, _c;
|
|
51
|
-
if (!allowed) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const schemaName = `${_case.header.call(void 0, pathKey.trim().replace("/", ""))}${mediaType === "application/ld+json" ? ".jsonld" : ""}`;
|
|
55
|
-
extendComponentSchemas(spec, schemaName, schema);
|
|
56
|
-
if (typeof ((_c = (_b = (_a = methodSpec == null ? void 0 : methodSpec.responses) == null ? void 0 : _a[status]) == null ? void 0 : _b.content[mediaType]) == null ? void 0 : _c.schema) === "undefined") {
|
|
57
|
-
methodSpec.responses[status].content[mediaType] = { schema: {} };
|
|
58
|
-
}
|
|
59
|
-
methodSpec.responses[status].content[mediaType].schema = schemaIsArray ? {
|
|
60
|
-
type: "array",
|
|
61
|
-
items: {
|
|
62
|
-
$ref: `#/components/schemas/${schemaName}`
|
|
63
|
-
}
|
|
64
|
-
} : {
|
|
65
|
-
$ref: `#/components/schemas/${schemaName}`
|
|
66
|
-
};
|
|
67
|
-
if (typeof methodSpec.produces === "undefined") {
|
|
68
|
-
methodSpec.produces = [];
|
|
69
|
-
}
|
|
70
|
-
methodSpec.produces.push(mediaType);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
return examples;
|
|
75
|
-
}
|
|
76
|
-
function extendSwaggerWithMediaTypeExamples(responseSpec, allowedMediaTypes, pathKey, spec, examples, methodSpec, status) {
|
|
77
|
-
Object.keys(responseSpec.content).forEach((mediaName) => {
|
|
78
|
-
if (mediaName === "application/json") {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
Object.entries(allowedMediaTypes || {}).forEach(([mediaType, allowed]) => {
|
|
82
|
-
var _a, _b, _c;
|
|
83
|
-
if (!allowed) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const examplesName = `${_case.header.call(void 0, pathKey.trim().replace("/", ""))}${mediaType === "application/ld+json" ? ".jsonld" : ""}`;
|
|
87
|
-
extendComponentExamples(spec, examplesName, examples);
|
|
88
|
-
if (typeof ((_c = (_b = (_a = methodSpec == null ? void 0 : methodSpec.responses) == null ? void 0 : _a[status]) == null ? void 0 : _b.content[mediaType]) == null ? void 0 : _c.examples) === "undefined") {
|
|
89
|
-
methodSpec.responses[status].content[mediaType] = { examples: {} };
|
|
90
|
-
}
|
|
91
|
-
methodSpec.responses[status].content[mediaType].examples = examples;
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
function extendSwaggerSpec(spec, allowedMediaTypes) {
|
|
96
|
-
if (typeof spec === "object" && typeof spec.paths === "object") {
|
|
97
|
-
Object.entries(spec.paths).forEach(([pathKey, pathSpec]) => {
|
|
98
|
-
Object.values(pathSpec).forEach((methodSpec) => {
|
|
99
|
-
if (typeof methodSpec.responses === "object") {
|
|
100
|
-
Object.entries(methodSpec.responses).forEach(([status, responseSpec]) => {
|
|
101
|
-
if (typeof responseSpec.content === "object") {
|
|
102
|
-
let examples = extendSwaggerWithMediaTypeSchema(responseSpec, allowedMediaTypes, pathKey, spec, methodSpec, status);
|
|
103
|
-
if (typeof examples !== "undefined") {
|
|
104
|
-
extendSwaggerWithMediaTypeExamples(responseSpec, allowedMediaTypes, pathKey, spec, examples, methodSpec, status);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
return spec;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// src/swagger/swagger-handler.ts
|
|
116
|
-
var swaggerCrudDebug = _debug2.default.call(void 0, "visulima:api-platform:swagger:crud:get-static-properties-swagger");
|
|
117
|
-
var swaggerHandler = (options = {}) => {
|
|
118
|
-
const {
|
|
119
|
-
allowedMediaTypes = {
|
|
120
|
-
"application/json": true
|
|
121
|
-
},
|
|
122
|
-
swaggerFilePath,
|
|
123
|
-
crud
|
|
124
|
-
} = options;
|
|
125
|
-
return async (request, response) => {
|
|
126
|
-
const swaggerPath = _path2.default.join(process.cwd(), swaggerFilePath || "swagger/swagger.json");
|
|
127
|
-
if (!_fs.existsSync.call(void 0, swaggerPath)) {
|
|
128
|
-
throw new Error(`Swagger file not found at ${swaggerPath}. Did you change the output path in "withOpenApi" inside the next.config.js file?`);
|
|
129
|
-
}
|
|
130
|
-
const fileContents = _fs.readFileSync.call(void 0, swaggerPath, "utf8");
|
|
131
|
-
let spec = extendSwaggerSpec(JSON.parse(fileContents), allowedMediaTypes);
|
|
132
|
-
let crudSwagger = {};
|
|
133
|
-
if (typeof crud !== "undefined") {
|
|
134
|
-
try {
|
|
135
|
-
const modelsOpenApi = await _crud.modelsToOpenApi.call(void 0, crud);
|
|
136
|
-
crudSwagger = {
|
|
137
|
-
components: { schemas: modelsOpenApi.schemas, examples: modelsOpenApi.examples },
|
|
138
|
-
tags: modelsOpenApi.tags,
|
|
139
|
-
paths: modelsOpenApi.paths
|
|
140
|
-
};
|
|
141
|
-
crudSwagger = extendSwaggerSpec(crudSwagger, allowedMediaTypes);
|
|
142
|
-
swaggerCrudDebug(JSON.stringify(crudSwagger, null, 2));
|
|
143
|
-
spec = _lodashmerge2.default.call(void 0, spec, crudSwagger);
|
|
144
|
-
} catch (error) {
|
|
145
|
-
console.log(error);
|
|
146
|
-
throw new Error("Please install @visulima/crud to use the crud swagger generator.");
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (typeof request.headers.accept === "string" && /yaml|yml/.test(request.headers.accept)) {
|
|
150
|
-
response.statusCode = 200;
|
|
151
|
-
response.setHeader("Content-Type", request.headers.accept);
|
|
152
|
-
response.end(yaml_default(spec));
|
|
153
|
-
} else {
|
|
154
|
-
response.statusCode = 200;
|
|
155
|
-
response.setHeader("Content-Type", "application/json");
|
|
156
|
-
response.end(JSON.stringify(spec, null, 2));
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
};
|
|
160
|
-
var swagger_handler_default = swaggerHandler;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
exports.yaml_default = yaml_default; exports.swagger_handler_default = swagger_handler_default;
|
|
166
|
-
//# sourceMappingURL=chunk-S7GUPAL4.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/swagger/swagger-handler.ts","../src/connect/serializers/yaml.ts","../src/swagger/extend-swagger-spec.ts"],"names":[],"mappings":";AAGA,SAAS,uBAAuB;AAChC,OAAO,WAAW;AAClB,OAAO,WAAW;AAClB,SAAS,YAAY,oBAAoB;AAEzC,OAAO,UAAU;;;ACRjB,SAAS,iBAAiB;AAI1B,IAAM,kBAA8B,CAAC,SAAS,UAAU,MAAM,EAAE,QAAQ,EAAE,CAAC;AAE3E,IAAO,eAAQ;;;ACNf,SAAS,UAAU,kBAAkB;AAIrC,IAAM,yBAAyB,CAAC,MAA+B,YAAoB,WAAmC;AAClH,MAAI,OAAO,KAAK,eAAe,UAAU;AAErC,SAAK,aAAa,CAAC;AAAA,EACvB;AAEA,MAAI,OAAO,KAAK,WAAW,YAAY,UAAU;AAE7C,SAAK,WAAW,UAAU,CAAC;AAAA,EAC/B;AAEA,MAAI,OAAO,KAAK,WAAW,QAAQ,gBAAgB,aAAa;AAE5D,SAAK,WAAW,QAAQ,cAAc;AAAA,EAC1C;AACJ;AAEA,IAAM,0BAA0B,CAAC,MAA+B,aAAqB,YAAoC;AACrH,MAAI,OAAO,KAAK,eAAe,UAAU;AAErC,SAAK,aAAa,CAAC;AAAA,EACvB;AAEA,MAAI,OAAO,KAAK,WAAW,aAAa,UAAU;AAE9C,SAAK,WAAW,WAAW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UAAU,gBAAgB,QAAQ,QAAQ,MAAM;AACzD,eAAS,IAAI,aAAgB,IAAI,CAAC;AAAA,IACtC,OAAO;AACH,eAAS,aAAa;AACtB,eAAS,UAAU,gBAAgB,kBAAkB;AACrD,eAAS,IAAI,KAAK,UAAU,MAAM,MAAM,CAAC,CAAC;AAAA,IAC9C;AAAA,EACJ;AACJ;AAEA,IAAO,0BAAQ","sourcesContent":["// eslint-disable-next-line unicorn/prevent-abbreviations,import/no-extraneous-dependencies\nimport type { ModelsToOpenApiParameters, SwaggerModelsConfig } from \"@visulima/crud\";\n// eslint-disable-next-line unicorn/prevent-abbreviations,import/no-extraneous-dependencies\nimport { modelsToOpenApi } from \"@visulima/crud\";\nimport debug from \"debug\";\nimport merge from \"lodash.merge\";\nimport { existsSync, readFileSync } from \"node:fs\";\nimport type { IncomingMessage, ServerResponse } from \"node:http\";\nimport path from \"node:path\";\nimport type { OAS3Definition, Tag } from \"swagger-jsdoc\";\n\nimport yamlTransformer from \"../connect/serializers/yaml\";\nimport extendSwaggerSpec from \"./extend-swagger-spec\";\n\n// eslint-disable-next-line testing-library/no-debugging-utils\nconst swaggerCrudDebug = debug(\"visulima:api-platform:swagger:crud:get-static-properties-swagger\");\n\nconst swaggerHandler = (\n options: Partial<{\n allowedMediaTypes: { [key: string]: boolean };\n swaggerFilePath: string;\n crud: Exclude<ModelsToOpenApiParameters, \"swagger\"> & {\n swagger?: {\n models?: SwaggerModelsConfig<string>;\n };\n };\n }> = {},\n) => {\n const {\n allowedMediaTypes = {\n \"application/json\": true,\n },\n swaggerFilePath,\n crud,\n } = options;\n\n return async <Request extends IncomingMessage, Response extends ServerResponse>(request: Request, response: Response) => {\n const swaggerPath = path.join(process.cwd(), swaggerFilePath || \"swagger/swagger.json\");\n\n if (!existsSync(swaggerPath)) {\n throw new Error(`Swagger file not found at ${swaggerPath}. Did you change the output path in \"withOpenApi\" inside the next.config.js file?`);\n }\n\n const fileContents = readFileSync(swaggerPath, \"utf8\");\n\n let spec = extendSwaggerSpec(JSON.parse(fileContents) as OAS3Definition, allowedMediaTypes) as OAS3Definition;\n let crudSwagger: Partial<OAS3Definition> = {};\n\n if (typeof crud !== \"undefined\") {\n try {\n const modelsOpenApi = await modelsToOpenApi(crud);\n\n crudSwagger = {\n components: { schemas: modelsOpenApi.schemas, examples: modelsOpenApi.examples },\n tags: modelsOpenApi.tags as Tag[],\n paths: modelsOpenApi.paths,\n };\n\n crudSwagger = extendSwaggerSpec(crudSwagger, allowedMediaTypes);\n\n swaggerCrudDebug(JSON.stringify(crudSwagger, null, 2));\n\n spec = merge(spec, crudSwagger);\n } catch (error) {\n // eslint-disable-next-line no-console\n console.log(error);\n throw new Error(\"Please install @visulima/crud to use the crud swagger generator.\");\n }\n }\n\n if (typeof request.headers.accept === \"string\" && /yaml|yml/.test(request.headers.accept)) {\n response.statusCode = 200;\n response.setHeader(\"Content-Type\", request.headers.accept);\n response.end(yamlTransformer(spec));\n } else {\n response.statusCode = 200;\n response.setHeader(\"Content-Type\", \"application/json\");\n response.end(JSON.stringify(spec, null, 2));\n }\n };\n};\n\nexport default swaggerHandler;\n","import { stringify } from \"yaml\";\n\nimport type { Serializer } from \"./types\";\n\nconst yamlTransformer: Serializer = (data) => stringify(data, { indent: 2 });\n\nexport default yamlTransformer;\n","import { header as headerCase } from \"case\";\nimport type { OpenAPIV3 } from \"openapi-types\";\nimport type { OAS3Definition, Operation, Responses } from \"swagger-jsdoc\";\n\nconst extendComponentSchemas = (spec: Partial<OAS3Definition>, schemaName: string, schema: OpenAPIV3.SchemaObject) => {\n if (typeof spec.components !== \"object\") {\n // eslint-disable-next-line no-param-reassign\n spec.components = {};\n }\n\n if (typeof spec.components.schemas !== \"object\") {\n // eslint-disable-next-line no-param-reassign\n spec.components.schemas = {};\n }\n\n if (typeof spec.components.schemas[schemaName] === \"undefined\") {\n // eslint-disable-next-line no-param-reassign\n spec.components.schemas[schemaName] = schema;\n }\n};\n\nconst extendComponentExamples = (spec: Partial<OAS3Definition>, exampleName: string, example: OpenAPIV3.SchemaObject) => {\n if (typeof spec.components !== \"object\") {\n // eslint-disable-next-line no-param-reassign\n spec.components = {};\n }\n\n if (typeof spec.components.examples !== \"object\") {\n // eslint-disable-next-line no-param-reassign\n spec.components.examples = {};\n }\n\n if (typeof spec.components.examples[exampleName] === \"undefined\") {\n // eslint-disable-next-line no-param-reassign\n spec.components.examples[exampleName] = example;\n }\n};\n\nfunction extendSwaggerWithMediaTypeSchema(\n responseSpec: OpenAPIV3.ResponseObject,\n allowedMediaTypes: { [p: string]: boolean } | undefined,\n pathKey: string,\n spec: Partial<OAS3Definition>,\n methodSpec: Operation,\n status: string,\n) {\n let examples:\n | {\n [media: string]: OpenAPIV3.ReferenceObject | OpenAPIV3.ExampleObject;\n }\n | undefined;\n\n Object.entries(responseSpec.content as object).forEach(([mediaName, contentSpec]) => {\n if (typeof contentSpec.schema === \"object\") {\n const { schema } = contentSpec;\n\n if (mediaName === \"application/json\" && typeof contentSpec.examples !== \"undefined\") {\n examples = contentSpec.examples;\n }\n\n if (typeof (schema as OpenAPIV3.ReferenceObject).$ref !== \"undefined\") {\n return;\n }\n\n const schemaIsArray = (schema as OpenAPIV3.SchemaObject).type === \"array\";\n\n Object.entries(allowedMediaTypes || {}).forEach(([mediaType, allowed]) => {\n if (!allowed) {\n return;\n }\n\n // eslint-disable-next-line max-len\n const schemaName = `${headerCase(pathKey.trim().replace(\"/\", \"\"))}${mediaType === \"application/ld+json\" ? \".jsonld\" : \"\"}`;\n\n extendComponentSchemas(spec as OAS3Definition, schemaName, schema as OpenAPIV3.SchemaObject);\n\n if (typeof methodSpec?.responses?.[status]?.content[mediaType]?.schema === \"undefined\") {\n // eslint-disable-next-line no-param-reassign\n (methodSpec.responses as Responses)[status].content[mediaType] = { schema: {} };\n }\n\n // eslint-disable-next-line no-param-reassign\n (methodSpec.responses as Responses)[status].content[mediaType].schema = schemaIsArray\n ? {\n type: \"array\",\n items: {\n $ref: `#/components/schemas/${schemaName}`,\n },\n }\n : {\n $ref: `#/components/schemas/${schemaName}`,\n };\n\n if (typeof methodSpec.produces === \"undefined\") {\n // eslint-disable-next-line no-param-reassign\n methodSpec.produces = [];\n }\n\n methodSpec.produces.push(mediaType);\n });\n }\n });\n\n return examples;\n}\n\nfunction extendSwaggerWithMediaTypeExamples(\n responseSpec: OpenAPIV3.ResponseObject,\n allowedMediaTypes: { [p: string]: boolean } | undefined,\n pathKey: string,\n spec: Partial<OAS3Definition>,\n examples: { [p: string]: OpenAPIV3.ReferenceObject | OpenAPIV3.ExampleObject } | undefined,\n methodSpec: Operation,\n status: string,\n) {\n Object.keys(responseSpec.content as object).forEach((mediaName) => {\n if (mediaName === \"application/json\") {\n return;\n }\n\n Object.entries(allowedMediaTypes || {}).forEach(([mediaType, allowed]) => {\n if (!allowed) {\n return;\n }\n\n // eslint-disable-next-line max-len\n const examplesName = `${headerCase(pathKey.trim().replace(\"/\", \"\"))}${mediaType === \"application/ld+json\" ? \".jsonld\" : \"\"}`;\n\n extendComponentExamples(spec as OAS3Definition, examplesName, examples as OpenAPIV3.SchemaObject);\n\n if (typeof methodSpec?.responses?.[status]?.content[mediaType]?.examples === \"undefined\") {\n // eslint-disable-next-line no-param-reassign\n (methodSpec.responses as Responses)[status].content[mediaType] = { examples: {} };\n }\n\n // eslint-disable-next-line no-param-reassign\n (methodSpec.responses as Responses)[status].content[mediaType].examples = examples;\n });\n });\n}\n\n// eslint-disable-next-line radar/cognitive-complexity\nexport default function extendSwaggerSpec(spec: Partial<OAS3Definition>, allowedMediaTypes?: { [key: string]: boolean }): Partial<OAS3Definition> {\n if (typeof spec === \"object\" && typeof spec.paths === \"object\") {\n Object.entries(spec.paths).forEach(([pathKey, pathSpec]) => {\n Object.values(pathSpec).forEach((methodSpec) => {\n if (typeof methodSpec.responses === \"object\") {\n Object.entries<OpenAPIV3.ResponseObject>(methodSpec.responses).forEach(([status, responseSpec]) => {\n if (typeof responseSpec.content === \"object\") {\n let examples:\n | {\n [media: string]: OpenAPIV3.ReferenceObject | OpenAPIV3.ExampleObject;\n }\n | undefined = extendSwaggerWithMediaTypeSchema(responseSpec, allowedMediaTypes, pathKey, spec, methodSpec, status);\n\n if (typeof examples !== \"undefined\") {\n extendSwaggerWithMediaTypeExamples(responseSpec, allowedMediaTypes, pathKey, spec, examples, methodSpec, status);\n }\n }\n });\n }\n });\n });\n }\n\n return spec;\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/next/routes/pages/swagger/redoc.tsx","../src/next/routes/pages/swagger/swagger.tsx","../src/next/routes/pages/swagger/get-static-properties-swagger.ts"],"sourcesContent":["// eslint-disable-next-line import/no-extraneous-dependencies\nimport type { InferGetStaticPropsType, NextPage } from \"next\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport Head from \"next/head\";\nimport React from \"react\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport type { RedocStandaloneProps } from \"redoc\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport { RedocStandalone } from \"redoc\";\n\nimport getStaticProps from \"./get-static-properties-swagger\";\n\n// eslint-disable-next-line max-len\nconst RedocApiDocument: (\n name: string,\n swagger?: Exclude<RedocStandaloneProps, \"spec\">,\n // eslint-disable-next-line max-len,unicorn/no-useless-undefined\n) => NextPage<InferGetStaticPropsType<typeof getStaticProps>> = (name, swagger = {}) => ({ swaggerData }: InferGetStaticPropsType<typeof getStaticProps>) => (\n <>\n <Head>\n <title>{name}</title>\n <style>\n {`\nbody {\n background: #fafafa !important;\n}\n`}\n </style>\n </Head>\n {/* eslint-disable-next-line react/jsx-props-no-spreading */}\n <RedocStandalone {...swagger} spec={swaggerData} />\n </>\n);\n\nexport default RedocApiDocument;\n","import type { InferGetStaticPropsType, NextPage } from \"next\";\nimport dynamic from \"next/dynamic\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport Head from \"next/head\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport React from \"react\";\nimport type { SwaggerUIProps } from \"swagger-ui-react\";\n\nimport getStaticProps from \"./get-static-properties-swagger\";\n// eslint-disable-next-line import/no-extraneous-dependencies\nconst SwaggerUI = dynamic<{\n spec: any;\n // @ts-ignore\n // eslint-disable-next-line import/no-extraneous-dependencies\n}>(import(\"swagger-ui-react\"), { ssr: false });\ndynamic<{\n spec: any;\n // @ts-ignore\n // eslint-disable-next-line import/no-extraneous-dependencies\n}>(import(\"swagger-ui-react/swagger-ui.css\"), { ssr: false });\n\n// eslint-disable-next-line max-len\nconst SwaggerApiDocument: (\n name: string,\n swagger?: Exclude<SwaggerUIProps, \"spec\">,\n // eslint-disable-next-line max-len,unicorn/no-useless-undefined\n) => NextPage<InferGetStaticPropsType<typeof getStaticProps>> = (name, swagger = {}) => ({ swaggerData }: InferGetStaticPropsType<typeof getStaticProps>) => (\n <>\n <Head>\n <title>{name}</title>\n <style>\n {`\nbody {\n background: #fafafa !important;\n}\n`}\n </style>\n </Head>\n {/* eslint-disable-next-line react/jsx-props-no-spreading */}\n <SwaggerUI {...swagger} spec={swaggerData} />\n </>\n);\n\nexport default SwaggerApiDocument;\n","// eslint-disable-next-line unicorn/prevent-abbreviations\nimport debug from \"debug\";\nimport type { GetStaticProps } from \"next\";\nimport type { OAS3Definition } from \"swagger-jsdoc\";\n\n// eslint-disable-next-line testing-library/no-debugging-utils\nconst swaggerDebug = debug(\"visulima:api-platform:swagger:get-static-properties-swagger\");\n\n// eslint-disable-next-line unicorn/consistent-function-scoping\nconst getStaticProps: (\n swaggerUrl: string,\n) => GetStaticProps = (swaggerUrl) => async (): Promise<{\n props: {\n swaggerUrl: string;\n swaggerData: OAS3Definition;\n };\n}> => {\n // eslint-disable-next-line compat/compat\n const response = await fetch(swaggerUrl);\n const swaggerData = await response.json();\n\n swaggerDebug(swaggerData);\n\n return {\n props: {\n swaggerUrl,\n swaggerData: JSON.parse(JSON.stringify(swaggerData)),\n },\n };\n};\n\nexport default getStaticProps;\n"],"mappings":";AAGA,OAAO,UAAU;AACjB,OAAO,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ncomingMessage, ServerResponse } from "node:http";
|
|
3
|
-
import type { AnyZodObject } from "zod";
|
|
4
|
-
import { ZodObject } from "zod";
|
|
5
|
-
|
|
6
|
-
import type { ErrorHandlers } from "../error-handler/types";
|
|
7
|
-
import { onError, onNoMatch } from "./handler";
|
|
8
|
-
import httpHeaderNormalizerMiddleware from "./middleware/http-header-normalizer";
|
|
9
|
-
import type { Serializers } from "./middleware/serializers-middleware";
|
|
10
|
-
import serializersMiddleware from "./middleware/serializers-middleware";
|
|
11
|
-
|
|
12
|
-
const createNodeRouter = <
|
|
13
|
-
Request extends IncomingMessage,
|
|
14
|
-
Response extends ServerResponse,
|
|
15
|
-
Schema extends AnyZodObject = ZodObject<{ body?: AnyZodObject; headers?: AnyZodObject; query?: AnyZodObject }>,
|
|
16
|
-
>(
|
|
17
|
-
options: {
|
|
18
|
-
middlewares?: {
|
|
19
|
-
"http-header-normalizer"?: { canonical?: boolean; normalizeHeaderKey?: (key: string, canonical: boolean) => string };
|
|
20
|
-
serializers?: {
|
|
21
|
-
serializers?: Serializers;
|
|
22
|
-
defaultContentType?: string;
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
errorHandlers?: ErrorHandlers;
|
|
26
|
-
showTrace?: boolean;
|
|
27
|
-
} = {},
|
|
28
|
-
) => {
|
|
29
|
-
const router = new NodeRouter<Request, Response, Schema>({
|
|
30
|
-
onNoMatch,
|
|
31
|
-
onError: onError(options.errorHandlers || [], options.showTrace || false),
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
return router
|
|
35
|
-
.use(httpHeaderNormalizerMiddleware(options?.middlewares?.["http-header-normalizer"] || {}))
|
|
36
|
-
.use(
|
|
37
|
-
serializersMiddleware(
|
|
38
|
-
options?.middlewares?.serializers?.serializers || [],
|
|
39
|
-
options?.middlewares?.serializers?.defaultContentType || "application/json; charset=utf-8",
|
|
40
|
-
),
|
|
41
|
-
);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export default createNodeRouter;
|