@spikers/next-openapi-json-generator 2.0.1 → 2.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/dist/index.cjs +32 -30
- package/dist/index.js +32 -30
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -109,10 +109,14 @@ function filterDirectoryItems(rootPath, items, include, exclude) {
|
|
|
109
109
|
|
|
110
110
|
// src/core/isDocumentedRoute.ts
|
|
111
111
|
var import_promises2 = __toESM(require("fs/promises"), 1);
|
|
112
|
-
|
|
112
|
+
var SUPPORTED_PACKAGE_NAMES = [
|
|
113
|
+
"@spikers/next-openapi-route-handler",
|
|
114
|
+
"@omer-x/next-openapi-route-handler"
|
|
115
|
+
];
|
|
116
|
+
async function isDocumentedRoute(routePath) {
|
|
113
117
|
try {
|
|
114
|
-
const rawCode = await import_promises2.default.readFile(
|
|
115
|
-
return rawCode.includes(
|
|
118
|
+
const rawCode = await import_promises2.default.readFile(routePath, "utf-8");
|
|
119
|
+
return SUPPORTED_PACKAGE_NAMES.some((name) => rawCode.includes(name));
|
|
116
120
|
} catch {
|
|
117
121
|
return false;
|
|
118
122
|
}
|
|
@@ -121,7 +125,7 @@ async function isDocumentedRoute(routePath2) {
|
|
|
121
125
|
// src/core/next.ts
|
|
122
126
|
var import_promises3 = __toESM(require("fs/promises"), 1);
|
|
123
127
|
var import_node_path2 = __toESM(require("path"), 1);
|
|
124
|
-
var import_next_openapi_route_handler = require("@
|
|
128
|
+
var import_next_openapi_route_handler = require("@spikers/next-openapi-route-handler");
|
|
125
129
|
var import_zod = require("zod");
|
|
126
130
|
|
|
127
131
|
// src/utils/generateRandomString.ts
|
|
@@ -130,9 +134,9 @@ function generateRandomString(length) {
|
|
|
130
134
|
}
|
|
131
135
|
|
|
132
136
|
// src/utils/string-preservation.ts
|
|
133
|
-
function preserveStrings(
|
|
137
|
+
function preserveStrings(code) {
|
|
134
138
|
let replacements = {};
|
|
135
|
-
const output =
|
|
139
|
+
const output = code.replace(/(['"`])((?:\\.|(?!\1).)*)\1/g, (match, quote, content) => {
|
|
136
140
|
const replacementId = generateRandomString(32);
|
|
137
141
|
replacements = {
|
|
138
142
|
...replacements,
|
|
@@ -142,38 +146,38 @@ function preserveStrings(code2) {
|
|
|
142
146
|
});
|
|
143
147
|
return { output, replacements };
|
|
144
148
|
}
|
|
145
|
-
function restoreStrings(
|
|
146
|
-
return
|
|
149
|
+
function restoreStrings(code, replacements) {
|
|
150
|
+
return code.replace(/<@~(.*?)~@>/g, (_, replacementId) => {
|
|
147
151
|
return replacements[replacementId];
|
|
148
152
|
});
|
|
149
153
|
}
|
|
150
154
|
|
|
151
155
|
// src/core/injectSchemas.ts
|
|
152
|
-
function injectSchemas(
|
|
153
|
-
const { output: preservedCode, replacements } = preserveStrings(
|
|
156
|
+
function injectSchemas(code, refName) {
|
|
157
|
+
const { output: preservedCode, replacements } = preserveStrings(code);
|
|
154
158
|
const preservedCodeWithSchemasInjected = preservedCode.replace(new RegExp(`\\b${refName}\\.`, "g"), `global.schemas[${refName}].`).replace(new RegExp(`\\b${refName}\\b`, "g"), `"${refName}"`).replace(new RegExp(`queryParams:\\s*['"\`]${refName}['"\`]`, "g"), `queryParams: global.schemas["${refName}"]`).replace(new RegExp(`pathParams:\\s*['"\`]${refName}['"\`]`, "g"), `pathParams: global.schemas["${refName}"]`);
|
|
155
159
|
return restoreStrings(preservedCodeWithSchemasInjected, replacements);
|
|
156
160
|
}
|
|
157
161
|
|
|
158
162
|
// src/core/middleware.ts
|
|
159
|
-
function detectMiddlewareName(
|
|
160
|
-
const match =
|
|
163
|
+
function detectMiddlewareName(code) {
|
|
164
|
+
const match = code.match(/middleware:\s*(\w+)/);
|
|
161
165
|
return match ? match[1] : null;
|
|
162
166
|
}
|
|
163
167
|
|
|
164
168
|
// src/utils/removeImports.ts
|
|
165
|
-
function removeImports(
|
|
166
|
-
return
|
|
169
|
+
function removeImports(code) {
|
|
170
|
+
return code.replace(/(^import\s+[^;]+;?$|^import\s+[^;]*\sfrom\s.+;?$)/gm, "").replace(/(^import\s+{[\s\S]+?}\s+from\s+["'][^"']+["'];?)/gm, "").trim();
|
|
167
171
|
}
|
|
168
172
|
|
|
169
173
|
// src/core/transpile.ts
|
|
170
|
-
function fixExportsInCommonJS(
|
|
174
|
+
function fixExportsInCommonJS(code) {
|
|
171
175
|
const validMethods = ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
|
172
176
|
const exportFixer1 = validMethods.map((method) => `exports.${method} = void 0;
|
|
173
177
|
`).join("\n");
|
|
174
178
|
const exportFixer2 = `module.exports = { ${validMethods.map((m) => `${m}: exports.${m}`).join(", ")} };`;
|
|
175
179
|
return `${exportFixer1}
|
|
176
|
-
${
|
|
180
|
+
${code}
|
|
177
181
|
${exportFixer2}`;
|
|
178
182
|
}
|
|
179
183
|
function injectMiddlewareFixer(middlewareName) {
|
|
@@ -213,13 +217,12 @@ async function findAppFolderPath() {
|
|
|
213
217
|
}
|
|
214
218
|
async function safeEval(code, routePath) {
|
|
215
219
|
try {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
);
|
|
220
|
+
const exports2 = {};
|
|
221
|
+
const module2 = { exports: exports2 };
|
|
222
|
+
const require2 = () => ({});
|
|
223
|
+
const fn = new Function("exports", "module", "require", code);
|
|
224
|
+
fn(exports2, module2, require2);
|
|
225
|
+
return module2.exports;
|
|
223
226
|
} catch (error) {
|
|
224
227
|
console.log(`An error occured while evaluating the route exports from "${routePath}"`);
|
|
225
228
|
throw error;
|
|
@@ -238,16 +241,15 @@ async function getModuleTranspiler() {
|
|
|
238
241
|
);
|
|
239
242
|
return transpileModule;
|
|
240
243
|
}
|
|
241
|
-
async function getRouteExports(
|
|
242
|
-
const rawCode = await import_promises3.default.readFile(
|
|
244
|
+
async function getRouteExports(routePath, routeDefinerName, schemas) {
|
|
245
|
+
const rawCode = await import_promises3.default.readFile(routePath, "utf-8");
|
|
243
246
|
const middlewareName = detectMiddlewareName(rawCode);
|
|
244
|
-
const
|
|
245
|
-
const
|
|
246
|
-
const fixedCode = Object.keys(schemas).reduce(injectSchemas, code2);
|
|
247
|
+
const code = transpile(true, rawCode, middlewareName, await getModuleTranspiler());
|
|
248
|
+
const fixedCode = Object.keys(schemas).reduce(injectSchemas, code);
|
|
247
249
|
global[routeDefinerName] = import_next_openapi_route_handler.defineRoute;
|
|
248
250
|
global.z = import_zod.z;
|
|
249
251
|
global.schemas = schemas;
|
|
250
|
-
const result = await safeEval(fixedCode,
|
|
252
|
+
const result = await safeEval(fixedCode, routePath);
|
|
251
253
|
delete global.schemas;
|
|
252
254
|
delete global[routeDefinerName];
|
|
253
255
|
delete global.z;
|
|
@@ -278,7 +280,7 @@ function verifyOptions(include, exclude) {
|
|
|
278
280
|
var import_node_path3 = __toESM(require("path"), 1);
|
|
279
281
|
function getRoutePathName(filePath, rootPath) {
|
|
280
282
|
const dirName = import_node_path3.default.dirname(filePath);
|
|
281
|
-
return "/" + import_node_path3.default.relative(rootPath, dirName).replaceAll("[", "{").replaceAll("]", "}").replaceAll("\\", "/");
|
|
283
|
+
return "/" + import_node_path3.default.relative(rootPath, dirName).replaceAll("[", "{").replaceAll("]", "}").replaceAll("\\", "/").replaceAll(/\([^)]*\)/g, "");
|
|
282
284
|
}
|
|
283
285
|
|
|
284
286
|
// src/utils/deepEqual.ts
|
package/dist/index.js
CHANGED
|
@@ -80,10 +80,14 @@ function filterDirectoryItems(rootPath, items, include, exclude) {
|
|
|
80
80
|
|
|
81
81
|
// src/core/isDocumentedRoute.ts
|
|
82
82
|
import fs2 from "fs/promises";
|
|
83
|
-
|
|
83
|
+
var SUPPORTED_PACKAGE_NAMES = [
|
|
84
|
+
"@spikers/next-openapi-route-handler",
|
|
85
|
+
"@omer-x/next-openapi-route-handler"
|
|
86
|
+
];
|
|
87
|
+
async function isDocumentedRoute(routePath) {
|
|
84
88
|
try {
|
|
85
|
-
const rawCode = await fs2.readFile(
|
|
86
|
-
return rawCode.includes(
|
|
89
|
+
const rawCode = await fs2.readFile(routePath, "utf-8");
|
|
90
|
+
return SUPPORTED_PACKAGE_NAMES.some((name) => rawCode.includes(name));
|
|
87
91
|
} catch {
|
|
88
92
|
return false;
|
|
89
93
|
}
|
|
@@ -92,7 +96,7 @@ async function isDocumentedRoute(routePath2) {
|
|
|
92
96
|
// src/core/next.ts
|
|
93
97
|
import fs3 from "fs/promises";
|
|
94
98
|
import path2 from "path";
|
|
95
|
-
import { defineRoute } from "@
|
|
99
|
+
import { defineRoute } from "@spikers/next-openapi-route-handler";
|
|
96
100
|
import { z } from "zod";
|
|
97
101
|
|
|
98
102
|
// src/utils/generateRandomString.ts
|
|
@@ -101,9 +105,9 @@ function generateRandomString(length) {
|
|
|
101
105
|
}
|
|
102
106
|
|
|
103
107
|
// src/utils/string-preservation.ts
|
|
104
|
-
function preserveStrings(
|
|
108
|
+
function preserveStrings(code) {
|
|
105
109
|
let replacements = {};
|
|
106
|
-
const output =
|
|
110
|
+
const output = code.replace(/(['"`])((?:\\.|(?!\1).)*)\1/g, (match, quote, content) => {
|
|
107
111
|
const replacementId = generateRandomString(32);
|
|
108
112
|
replacements = {
|
|
109
113
|
...replacements,
|
|
@@ -113,38 +117,38 @@ function preserveStrings(code2) {
|
|
|
113
117
|
});
|
|
114
118
|
return { output, replacements };
|
|
115
119
|
}
|
|
116
|
-
function restoreStrings(
|
|
117
|
-
return
|
|
120
|
+
function restoreStrings(code, replacements) {
|
|
121
|
+
return code.replace(/<@~(.*?)~@>/g, (_, replacementId) => {
|
|
118
122
|
return replacements[replacementId];
|
|
119
123
|
});
|
|
120
124
|
}
|
|
121
125
|
|
|
122
126
|
// src/core/injectSchemas.ts
|
|
123
|
-
function injectSchemas(
|
|
124
|
-
const { output: preservedCode, replacements } = preserveStrings(
|
|
127
|
+
function injectSchemas(code, refName) {
|
|
128
|
+
const { output: preservedCode, replacements } = preserveStrings(code);
|
|
125
129
|
const preservedCodeWithSchemasInjected = preservedCode.replace(new RegExp(`\\b${refName}\\.`, "g"), `global.schemas[${refName}].`).replace(new RegExp(`\\b${refName}\\b`, "g"), `"${refName}"`).replace(new RegExp(`queryParams:\\s*['"\`]${refName}['"\`]`, "g"), `queryParams: global.schemas["${refName}"]`).replace(new RegExp(`pathParams:\\s*['"\`]${refName}['"\`]`, "g"), `pathParams: global.schemas["${refName}"]`);
|
|
126
130
|
return restoreStrings(preservedCodeWithSchemasInjected, replacements);
|
|
127
131
|
}
|
|
128
132
|
|
|
129
133
|
// src/core/middleware.ts
|
|
130
|
-
function detectMiddlewareName(
|
|
131
|
-
const match =
|
|
134
|
+
function detectMiddlewareName(code) {
|
|
135
|
+
const match = code.match(/middleware:\s*(\w+)/);
|
|
132
136
|
return match ? match[1] : null;
|
|
133
137
|
}
|
|
134
138
|
|
|
135
139
|
// src/utils/removeImports.ts
|
|
136
|
-
function removeImports(
|
|
137
|
-
return
|
|
140
|
+
function removeImports(code) {
|
|
141
|
+
return code.replace(/(^import\s+[^;]+;?$|^import\s+[^;]*\sfrom\s.+;?$)/gm, "").replace(/(^import\s+{[\s\S]+?}\s+from\s+["'][^"']+["'];?)/gm, "").trim();
|
|
138
142
|
}
|
|
139
143
|
|
|
140
144
|
// src/core/transpile.ts
|
|
141
|
-
function fixExportsInCommonJS(
|
|
145
|
+
function fixExportsInCommonJS(code) {
|
|
142
146
|
const validMethods = ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
|
143
147
|
const exportFixer1 = validMethods.map((method) => `exports.${method} = void 0;
|
|
144
148
|
`).join("\n");
|
|
145
149
|
const exportFixer2 = `module.exports = { ${validMethods.map((m) => `${m}: exports.${m}`).join(", ")} };`;
|
|
146
150
|
return `${exportFixer1}
|
|
147
|
-
${
|
|
151
|
+
${code}
|
|
148
152
|
${exportFixer2}`;
|
|
149
153
|
}
|
|
150
154
|
function injectMiddlewareFixer(middlewareName) {
|
|
@@ -184,13 +188,12 @@ async function findAppFolderPath() {
|
|
|
184
188
|
}
|
|
185
189
|
async function safeEval(code, routePath) {
|
|
186
190
|
try {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
);
|
|
191
|
+
const exports2 = {};
|
|
192
|
+
const module = { exports: exports2 };
|
|
193
|
+
const require2 = () => ({});
|
|
194
|
+
const fn = new Function("exports", "module", "require", code);
|
|
195
|
+
fn(exports2, module, require2);
|
|
196
|
+
return module.exports;
|
|
194
197
|
} catch (error) {
|
|
195
198
|
console.log(`An error occured while evaluating the route exports from "${routePath}"`);
|
|
196
199
|
throw error;
|
|
@@ -209,16 +212,15 @@ async function getModuleTranspiler() {
|
|
|
209
212
|
);
|
|
210
213
|
return transpileModule;
|
|
211
214
|
}
|
|
212
|
-
async function getRouteExports(
|
|
213
|
-
const rawCode = await fs3.readFile(
|
|
215
|
+
async function getRouteExports(routePath, routeDefinerName, schemas) {
|
|
216
|
+
const rawCode = await fs3.readFile(routePath, "utf-8");
|
|
214
217
|
const middlewareName = detectMiddlewareName(rawCode);
|
|
215
|
-
const
|
|
216
|
-
const
|
|
217
|
-
const fixedCode = Object.keys(schemas).reduce(injectSchemas, code2);
|
|
218
|
+
const code = transpile(true, rawCode, middlewareName, await getModuleTranspiler());
|
|
219
|
+
const fixedCode = Object.keys(schemas).reduce(injectSchemas, code);
|
|
218
220
|
global[routeDefinerName] = defineRoute;
|
|
219
221
|
global.z = z;
|
|
220
222
|
global.schemas = schemas;
|
|
221
|
-
const result = await safeEval(fixedCode,
|
|
223
|
+
const result = await safeEval(fixedCode, routePath);
|
|
222
224
|
delete global.schemas;
|
|
223
225
|
delete global[routeDefinerName];
|
|
224
226
|
delete global.z;
|
|
@@ -249,7 +251,7 @@ function verifyOptions(include, exclude) {
|
|
|
249
251
|
import path3 from "path";
|
|
250
252
|
function getRoutePathName(filePath, rootPath) {
|
|
251
253
|
const dirName = path3.dirname(filePath);
|
|
252
|
-
return "/" + path3.relative(rootPath, dirName).replaceAll("[", "{").replaceAll("]", "}").replaceAll("\\", "/");
|
|
254
|
+
return "/" + path3.relative(rootPath, dirName).replaceAll("[", "{").replaceAll("]", "}").replaceAll("\\", "/").replaceAll(/\([^)]*\)/g, "");
|
|
253
255
|
}
|
|
254
256
|
|
|
255
257
|
// src/utils/deepEqual.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spikers/next-openapi-json-generator",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "a Next.js plugin to generate OpenAPI documentation from route handlers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"next.js",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"@omer-x/json-schema-types": "^1",
|
|
47
|
-
"@
|
|
47
|
+
"@spikers/next-openapi-route-handler": "^2",
|
|
48
48
|
"@omer-x/openapi-types": "^1",
|
|
49
49
|
"typescript": "^5",
|
|
50
50
|
"zod": "^4"
|