@omer-x/next-openapi-json-generator 2.0.0 → 2.0.2

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 CHANGED
@@ -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" && typeof exports !== "undefined") {
230
+ return require(
231
+ /* webpackIgnore: true */
232
+ "typescript"
233
+ ).transpileModule;
234
+ }
235
+ const { transpileModule } = await import(
236
+ /* webpackIgnore: true */
237
+ "typescript"
238
+ );
239
+ return 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,9 +304,9 @@ function deepEqual(a, b) {
272
304
  }
273
305
 
274
306
  // src/core/zod-to-openapi.ts
275
- var import_zod = require("zod");
307
+ var import_zod2 = require("zod");
276
308
  function convertToOpenAPI(schema, isArray) {
277
- return import_zod.z.toJSONSchema(isArray ? schema.array() : schema);
309
+ return import_zod2.z.toJSONSchema(isArray ? schema.array() : schema);
278
310
  }
279
311
 
280
312
  // src/core/mask.ts
package/dist/index.js CHANGED
@@ -1,3 +1,10 @@
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
9
  import path4 from "path";
3
10
  import getPackageMetadata from "@omer-x/package-metadata";
@@ -85,6 +92,8 @@ async function isDocumentedRoute(routePath2) {
85
92
  // src/core/next.ts
86
93
  import fs3 from "fs/promises";
87
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" && typeof exports !== "undefined") {
201
+ return __require(
202
+ /* webpackIgnore: true */
203
+ "typescript"
204
+ ).transpileModule;
205
+ }
206
+ const { transpileModule } = await import(
207
+ /* webpackIgnore: true */
208
+ "typescript"
209
+ );
210
+ return 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
 
@@ -236,9 +275,9 @@ function deepEqual(a, b) {
236
275
  }
237
276
 
238
277
  // src/core/zod-to-openapi.ts
239
- import { z } from "zod";
278
+ import { z as z2 } from "zod";
240
279
  function convertToOpenAPI(schema, isArray) {
241
- return z.toJSONSchema(isArray ? schema.array() : schema);
280
+ return z2.toJSONSchema(isArray ? schema.array() : schema);
242
281
  }
243
282
 
244
283
  // src/core/mask.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omer-x/next-openapi-json-generator",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "a Next.js plugin to generate OpenAPI documentation from route handlers",
5
5
  "keywords": [
6
6
  "next.js",