@omer-x/next-openapi-json-generator 1.3.4 → 2.0.1

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.
Files changed (3) hide show
  1. package/dist/index.cjs +57 -52
  2. package/dist/index.js +67 -55
  3. package/package.json +17 -17
package/dist/index.cjs CHANGED
@@ -28,11 +28,11 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
 
30
30
  // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- default: () => src_default
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ default: () => index_default
34
34
  });
35
- module.exports = __toCommonJS(src_exports);
35
+ module.exports = __toCommonJS(index_exports);
36
36
 
37
37
  // src/core/generateOpenApiSpec.ts
38
38
  var import_node_path4 = __toESM(require("path"), 1);
@@ -121,6 +121,8 @@ async function isDocumentedRoute(routePath2) {
121
121
  // src/core/next.ts
122
122
  var import_promises3 = __toESM(require("fs/promises"), 1);
123
123
  var import_node_path2 = __toESM(require("path"), 1);
124
+ var import_next_openapi_route_handler = require("@omer-x/next-openapi-route-handler");
125
+ var import_zod = require("zod");
124
126
 
125
127
  // src/utils/generateRandomString.ts
126
128
  function generateRandomString(length) {
@@ -159,20 +161,17 @@ function detectMiddlewareName(code2) {
159
161
  return match ? match[1] : null;
160
162
  }
161
163
 
162
- // src/core/transpile.ts
163
- var import_typescript = require("typescript");
164
-
165
164
  // src/utils/removeImports.ts
166
165
  function removeImports(code2) {
167
166
  return code2.replace(/(^import\s+[^;]+;?$|^import\s+[^;]*\sfrom\s.+;?$)/gm, "").replace(/(^import\s+{[\s\S]+?}\s+from\s+["'][^"']+["'];?)/gm, "").trim();
168
167
  }
169
168
 
170
169
  // src/core/transpile.ts
171
- function fixExports(code2) {
170
+ function fixExportsInCommonJS(code2) {
172
171
  const validMethods = ["GET", "POST", "PUT", "PATCH", "DELETE"];
173
172
  const exportFixer1 = validMethods.map((method) => `exports.${method} = void 0;
174
- `);
175
- const exportFixer2 = `module.exports = { ${validMethods.map((m) => `${m}: exports.${m}`).join(", ")} }`;
173
+ `).join("\n");
174
+ const exportFixer2 = `module.exports = { ${validMethods.map((m) => `${m}: exports.${m}`).join(", ")} };`;
176
175
  return `${exportFixer1}
177
176
  ${code2}
178
177
  ${exportFixer2}`;
@@ -180,15 +179,24 @@ ${exportFixer2}`;
180
179
  function injectMiddlewareFixer(middlewareName) {
181
180
  return `const ${middlewareName} = (handler) => handler;`;
182
181
  }
183
- function transpile(rawCode, routeDefinerName, middlewareName) {
184
- const code2 = fixExports(removeImports(rawCode));
182
+ function transpile(isCommonJS, rawCode, middlewareName, transpileModule) {
185
183
  const parts = [
186
- `import ${routeDefinerName} from '@omer-x/next-openapi-route-handler';`,
187
- "import z from 'zod';",
188
184
  middlewareName ? injectMiddlewareFixer(middlewareName) : "",
189
- code2
185
+ removeImports(rawCode)
190
186
  ];
191
- return (0, import_typescript.transpile)(parts.join("\n"));
187
+ const output = transpileModule(parts.join("\n"), {
188
+ compilerOptions: {
189
+ module: isCommonJS ? 3 : 99,
190
+ target: 99,
191
+ sourceMap: false,
192
+ inlineSourceMap: false,
193
+ inlineSources: false
194
+ }
195
+ });
196
+ if (isCommonJS) {
197
+ return fixExportsInCommonJS(output.outputText);
198
+ }
199
+ return output.outputText;
192
200
  }
193
201
 
194
202
  // src/core/next.ts
@@ -203,22 +211,46 @@ async function findAppFolderPath() {
203
211
  }
204
212
  return null;
205
213
  }
206
- function safeEval(code, routePath) {
214
+ async function safeEval(code, routePath) {
207
215
  try {
208
- return eval(code);
216
+ if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
217
+ return eval(code);
218
+ }
219
+ return await import(
220
+ /* webpackIgnore: true */
221
+ `data:text/javascript,${encodeURIComponent(code)}`
222
+ );
209
223
  } catch (error) {
210
224
  console.log(`An error occured while evaluating the route exports from "${routePath}"`);
211
225
  throw error;
212
226
  }
213
227
  }
228
+ async function getModuleTranspiler() {
229
+ if (typeof require === "undefined") {
230
+ const { transpileModule } = await import(
231
+ /* webpackIgnore: true */
232
+ "typescript"
233
+ );
234
+ return transpileModule;
235
+ }
236
+ return require(
237
+ /* webpackIgnore: true */
238
+ "typescript"
239
+ ).transpileModule;
240
+ }
214
241
  async function getRouteExports(routePath2, routeDefinerName, schemas) {
215
242
  const rawCode = await import_promises3.default.readFile(routePath2, "utf-8");
216
243
  const middlewareName = detectMiddlewareName(rawCode);
217
- const code2 = transpile(rawCode, routeDefinerName, middlewareName);
244
+ const isCommonJS = typeof module !== "undefined" && typeof module.exports !== "undefined";
245
+ const code2 = transpile(isCommonJS, rawCode, middlewareName, await getModuleTranspiler());
218
246
  const fixedCode = Object.keys(schemas).reduce(injectSchemas, code2);
247
+ global[routeDefinerName] = import_next_openapi_route_handler.defineRoute;
248
+ global.z = import_zod.z;
219
249
  global.schemas = schemas;
220
- const result = safeEval(fixedCode, routePath2);
250
+ const result = await safeEval(fixedCode, routePath2);
221
251
  delete global.schemas;
252
+ delete global[routeDefinerName];
253
+ delete global.z;
222
254
  return result;
223
255
  }
224
256
 
@@ -272,36 +304,9 @@ function deepEqual(a, b) {
272
304
  }
273
305
 
274
306
  // src/core/zod-to-openapi.ts
275
- var import_zod_to_json_schema = require("zod-to-json-schema");
276
-
277
- // src/utils/zod-schema.ts
278
- function isFile(schema) {
279
- const file = new File([], "nothing.txt");
280
- const plainObject = { name: "test", size: 0 };
281
- const fileResult = schema.safeParse(file);
282
- const objectResult = schema.safeParse(plainObject);
283
- return fileResult.success && !objectResult.success;
284
- }
285
-
286
- // src/core/zod-to-openapi.ts
307
+ var import_zod2 = require("zod");
287
308
  function convertToOpenAPI(schema, isArray) {
288
- const result = (0, import_zod_to_json_schema.zodToJsonSchema)(isArray ? schema.array() : schema, {
289
- target: "openApi3",
290
- $refStrategy: "none"
291
- });
292
- if (result.type === "object" && result.properties) {
293
- for (const [propName, prop] of Object.entries(schema.shape)) {
294
- if (isFile(prop)) {
295
- result.properties[propName] = {
296
- type: "string",
297
- format: "binary",
298
- description: prop.description
299
- // contentEncoding: "base64", // swagger-ui-react doesn't support this
300
- };
301
- }
302
- }
303
- }
304
- return result;
309
+ return import_zod2.z.toJSONSchema(isArray ? schema.array() : schema);
305
310
  }
306
311
 
307
312
  // src/core/mask.ts
@@ -469,7 +474,7 @@ async function generateOpenApiSpec(schemas, {
469
474
  securitySchemes
470
475
  }
471
476
  };
472
- return {
477
+ return JSON.parse(JSON.stringify({
473
478
  openapi: "3.1.0",
474
479
  info: {
475
480
  title: metadata.serviceName,
@@ -480,8 +485,8 @@ async function generateOpenApiSpec(schemas, {
480
485
  ...clearUnusedSchemasOption ? clearUnusedSchemas(pathsAndComponents) : pathsAndComponents,
481
486
  security,
482
487
  tags: []
483
- };
488
+ }));
484
489
  }
485
490
 
486
491
  // src/index.ts
487
- var src_default = generateOpenApiSpec;
492
+ var index_default = generateOpenApiSpec;
package/dist/index.js CHANGED
@@ -1,5 +1,12 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // src/core/generateOpenApiSpec.ts
2
- import path4 from "node:path";
9
+ import path4 from "path";
3
10
  import getPackageMetadata from "@omer-x/package-metadata";
4
11
 
5
12
  // src/utils/object.ts
@@ -35,7 +42,7 @@ function clearUnusedSchemas({
35
42
  // src/core/dir.ts
36
43
  import { constants } from "fs";
37
44
  import fs from "fs/promises";
38
- import path from "node:path";
45
+ import path from "path";
39
46
  import { Minimatch } from "minimatch";
40
47
  async function directoryExists(dirPath) {
41
48
  try {
@@ -72,7 +79,7 @@ function filterDirectoryItems(rootPath, items, include, exclude) {
72
79
  }
73
80
 
74
81
  // src/core/isDocumentedRoute.ts
75
- import fs2 from "node:fs/promises";
82
+ import fs2 from "fs/promises";
76
83
  async function isDocumentedRoute(routePath2) {
77
84
  try {
78
85
  const rawCode = await fs2.readFile(routePath2, "utf-8");
@@ -83,8 +90,10 @@ async function isDocumentedRoute(routePath2) {
83
90
  }
84
91
 
85
92
  // src/core/next.ts
86
- import fs3 from "node:fs/promises";
87
- import path2 from "node:path";
93
+ import fs3 from "fs/promises";
94
+ import path2 from "path";
95
+ import { defineRoute } from "@omer-x/next-openapi-route-handler";
96
+ import { z } from "zod";
88
97
 
89
98
  // src/utils/generateRandomString.ts
90
99
  function generateRandomString(length) {
@@ -123,20 +132,17 @@ function detectMiddlewareName(code2) {
123
132
  return match ? match[1] : null;
124
133
  }
125
134
 
126
- // src/core/transpile.ts
127
- import { transpile as tsTranspile } from "typescript";
128
-
129
135
  // src/utils/removeImports.ts
130
136
  function removeImports(code2) {
131
137
  return code2.replace(/(^import\s+[^;]+;?$|^import\s+[^;]*\sfrom\s.+;?$)/gm, "").replace(/(^import\s+{[\s\S]+?}\s+from\s+["'][^"']+["'];?)/gm, "").trim();
132
138
  }
133
139
 
134
140
  // src/core/transpile.ts
135
- function fixExports(code2) {
141
+ function fixExportsInCommonJS(code2) {
136
142
  const validMethods = ["GET", "POST", "PUT", "PATCH", "DELETE"];
137
143
  const exportFixer1 = validMethods.map((method) => `exports.${method} = void 0;
138
- `);
139
- const exportFixer2 = `module.exports = { ${validMethods.map((m) => `${m}: exports.${m}`).join(", ")} }`;
144
+ `).join("\n");
145
+ const exportFixer2 = `module.exports = { ${validMethods.map((m) => `${m}: exports.${m}`).join(", ")} };`;
140
146
  return `${exportFixer1}
141
147
  ${code2}
142
148
  ${exportFixer2}`;
@@ -144,15 +150,24 @@ ${exportFixer2}`;
144
150
  function injectMiddlewareFixer(middlewareName) {
145
151
  return `const ${middlewareName} = (handler) => handler;`;
146
152
  }
147
- function transpile(rawCode, routeDefinerName, middlewareName) {
148
- const code2 = fixExports(removeImports(rawCode));
153
+ function transpile(isCommonJS, rawCode, middlewareName, transpileModule) {
149
154
  const parts = [
150
- `import ${routeDefinerName} from '@omer-x/next-openapi-route-handler';`,
151
- "import z from 'zod';",
152
155
  middlewareName ? injectMiddlewareFixer(middlewareName) : "",
153
- code2
156
+ removeImports(rawCode)
154
157
  ];
155
- return tsTranspile(parts.join("\n"));
158
+ const output = transpileModule(parts.join("\n"), {
159
+ compilerOptions: {
160
+ module: isCommonJS ? 3 : 99,
161
+ target: 99,
162
+ sourceMap: false,
163
+ inlineSourceMap: false,
164
+ inlineSources: false
165
+ }
166
+ });
167
+ if (isCommonJS) {
168
+ return fixExportsInCommonJS(output.outputText);
169
+ }
170
+ return output.outputText;
156
171
  }
157
172
 
158
173
  // src/core/next.ts
@@ -167,22 +182,46 @@ async function findAppFolderPath() {
167
182
  }
168
183
  return null;
169
184
  }
170
- function safeEval(code, routePath) {
185
+ async function safeEval(code, routePath) {
171
186
  try {
172
- return eval(code);
187
+ if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
188
+ return eval(code);
189
+ }
190
+ return await import(
191
+ /* webpackIgnore: true */
192
+ `data:text/javascript,${encodeURIComponent(code)}`
193
+ );
173
194
  } catch (error) {
174
195
  console.log(`An error occured while evaluating the route exports from "${routePath}"`);
175
196
  throw error;
176
197
  }
177
198
  }
199
+ async function getModuleTranspiler() {
200
+ if (typeof __require === "undefined") {
201
+ const { transpileModule } = await import(
202
+ /* webpackIgnore: true */
203
+ "typescript"
204
+ );
205
+ return transpileModule;
206
+ }
207
+ return __require(
208
+ /* webpackIgnore: true */
209
+ "typescript"
210
+ ).transpileModule;
211
+ }
178
212
  async function getRouteExports(routePath2, routeDefinerName, schemas) {
179
213
  const rawCode = await fs3.readFile(routePath2, "utf-8");
180
214
  const middlewareName = detectMiddlewareName(rawCode);
181
- const code2 = transpile(rawCode, routeDefinerName, middlewareName);
215
+ const isCommonJS = typeof module !== "undefined" && typeof module.exports !== "undefined";
216
+ const code2 = transpile(isCommonJS, rawCode, middlewareName, await getModuleTranspiler());
182
217
  const fixedCode = Object.keys(schemas).reduce(injectSchemas, code2);
218
+ global[routeDefinerName] = defineRoute;
219
+ global.z = z;
183
220
  global.schemas = schemas;
184
- const result = safeEval(fixedCode, routePath2);
221
+ const result = await safeEval(fixedCode, routePath2);
185
222
  delete global.schemas;
223
+ delete global[routeDefinerName];
224
+ delete global.z;
186
225
  return result;
187
226
  }
188
227
 
@@ -207,7 +246,7 @@ function verifyOptions(include, exclude) {
207
246
  }
208
247
 
209
248
  // src/core/getRoutePathName.ts
210
- import path3 from "node:path";
249
+ import path3 from "path";
211
250
  function getRoutePathName(filePath, rootPath) {
212
251
  const dirName = path3.dirname(filePath);
213
252
  return "/" + path3.relative(rootPath, dirName).replaceAll("[", "{").replaceAll("]", "}").replaceAll("\\", "/");
@@ -236,36 +275,9 @@ function deepEqual(a, b) {
236
275
  }
237
276
 
238
277
  // src/core/zod-to-openapi.ts
239
- import { zodToJsonSchema } from "zod-to-json-schema";
240
-
241
- // src/utils/zod-schema.ts
242
- function isFile(schema) {
243
- const file = new File([], "nothing.txt");
244
- const plainObject = { name: "test", size: 0 };
245
- const fileResult = schema.safeParse(file);
246
- const objectResult = schema.safeParse(plainObject);
247
- return fileResult.success && !objectResult.success;
248
- }
249
-
250
- // src/core/zod-to-openapi.ts
278
+ import { z as z2 } from "zod";
251
279
  function convertToOpenAPI(schema, isArray) {
252
- const result = zodToJsonSchema(isArray ? schema.array() : schema, {
253
- target: "openApi3",
254
- $refStrategy: "none"
255
- });
256
- if (result.type === "object" && result.properties) {
257
- for (const [propName, prop] of Object.entries(schema.shape)) {
258
- if (isFile(prop)) {
259
- result.properties[propName] = {
260
- type: "string",
261
- format: "binary",
262
- description: prop.description
263
- // contentEncoding: "base64", // swagger-ui-react doesn't support this
264
- };
265
- }
266
- }
267
- }
268
- return result;
280
+ return z2.toJSONSchema(isArray ? schema.array() : schema);
269
281
  }
270
282
 
271
283
  // src/core/mask.ts
@@ -433,7 +445,7 @@ async function generateOpenApiSpec(schemas, {
433
445
  securitySchemes
434
446
  }
435
447
  };
436
- return {
448
+ return JSON.parse(JSON.stringify({
437
449
  openapi: "3.1.0",
438
450
  info: {
439
451
  title: metadata.serviceName,
@@ -444,11 +456,11 @@ async function generateOpenApiSpec(schemas, {
444
456
  ...clearUnusedSchemasOption ? clearUnusedSchemas(pathsAndComponents) : pathsAndComponents,
445
457
  security,
446
458
  tags: []
447
- };
459
+ }));
448
460
  }
449
461
 
450
462
  // src/index.ts
451
- var src_default = generateOpenApiSpec;
463
+ var index_default = generateOpenApiSpec;
452
464
  export {
453
- src_default as default
465
+ index_default as default
454
466
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omer-x/next-openapi-json-generator",
3
- "version": "1.3.4",
3
+ "version": "2.0.1",
4
4
  "description": "a Next.js plugin to generate OpenAPI documentation from route handlers",
5
5
  "keywords": [
6
6
  "next.js",
@@ -40,28 +40,28 @@
40
40
  }
41
41
  },
42
42
  "scripts": {
43
- "test": "jest",
43
+ "test": "vitest run --coverage",
44
+ "test:watch": "vitest --coverage",
44
45
  "dev": "tsup --watch",
45
46
  "build": "tsup"
46
47
  },
48
+ "peerDependencies": {
49
+ "@omer-x/next-openapi-route-handler": "^2",
50
+ "@omer-x/openapi-types": "^1",
51
+ "zod": "^4",
52
+ "typescript": "^5"
53
+ },
47
54
  "dependencies": {
48
55
  "@omer-x/package-metadata": "^1.0.2",
49
- "minimatch": "^10.0.1",
50
- "typescript": "^5.7.2",
51
- "zod-to-json-schema": "^3.24.1"
56
+ "minimatch": "^10.0.3"
52
57
  },
53
58
  "devDependencies": {
54
- "@omer-x/eslint-config": "^2.1.2",
55
- "@types/node": "^22.10.2",
56
- "eslint": "^9.16.0",
57
- "semantic-release": "^24.2.0",
58
- "ts-jest": "^29.2.5",
59
- "ts-node": "^10.9.2",
60
- "tsup": "^8.3.5",
61
- "zod": "^3.24.1"
62
- },
63
- "peerDependencies": {
64
- "@omer-x/next-openapi-route-handler": "^1",
65
- "@omer-x/openapi-types": "^1"
59
+ "@omer-x/eslint-config": "^2.1.3",
60
+ "@types/node": "^24.0.14",
61
+ "@vitest/coverage-v8": "^3.2.4",
62
+ "eslint": "^9.31.0",
63
+ "semantic-release": "^24.2.7",
64
+ "tsup": "^8.5.0",
65
+ "vitest": "^3.2.4"
66
66
  }
67
67
  }