@kozojs/core 0.3.4 → 0.3.6
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/lib/index.js +23 -137
- package/lib/index.js.map +1 -1
- package/package.json +3 -5
package/lib/index.js
CHANGED
|
@@ -601,10 +601,6 @@ async function createUwsServer(opts) {
|
|
|
601
601
|
});
|
|
602
602
|
}
|
|
603
603
|
|
|
604
|
-
// src/compiler.ts
|
|
605
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
606
|
-
import fastJson from "fast-json-stringify";
|
|
607
|
-
|
|
608
604
|
// src/fast-response.ts
|
|
609
605
|
var CL_CACHE = /* @__PURE__ */ (() => {
|
|
610
606
|
const arr = new Array(1e4);
|
|
@@ -750,7 +746,6 @@ function makeZValidator(schema) {
|
|
|
750
746
|
function isZodSchema(schema) {
|
|
751
747
|
return typeof schema === "object" && schema !== null && "safeParse" in schema;
|
|
752
748
|
}
|
|
753
|
-
var ZOD_OPTS = { $refStrategy: "none", target: "jsonSchema7" };
|
|
754
749
|
var HDR_JSON = { "Content-Type": "application/json" };
|
|
755
750
|
var RESPONSE_INIT_200 = Object.freeze({ status: 200, headers: HDR_JSON });
|
|
756
751
|
function jsonResponse200(body) {
|
|
@@ -775,23 +770,7 @@ var SchemaCompiler = class {
|
|
|
775
770
|
compiled.validateParams = makeZValidator(schema.params);
|
|
776
771
|
}
|
|
777
772
|
if (schema.response) {
|
|
778
|
-
|
|
779
|
-
const keys = Object.keys(schema.response);
|
|
780
|
-
const isStatusMap = keys.length > 0 && keys.every((k) => !isNaN(Number(k)));
|
|
781
|
-
if (isStatusMap) {
|
|
782
|
-
const responseSchemas = schema.response;
|
|
783
|
-
if (responseSchemas[200]) {
|
|
784
|
-
const jsonSchema = isZodSchema(responseSchemas[200]) ? zodToJsonSchema(responseSchemas[200], ZOD_OPTS) : responseSchemas[200];
|
|
785
|
-
compiled.serialize = fastJson(jsonSchema);
|
|
786
|
-
}
|
|
787
|
-
} else {
|
|
788
|
-
compiled.serialize = fastJson(schema.response);
|
|
789
|
-
}
|
|
790
|
-
} else {
|
|
791
|
-
const responseSchema = schema.response;
|
|
792
|
-
const jsonSchema = zodToJsonSchema(responseSchema, ZOD_OPTS);
|
|
793
|
-
compiled.serialize = fastJson(jsonSchema);
|
|
794
|
-
}
|
|
773
|
+
compiled.serialize = JSON.stringify;
|
|
795
774
|
}
|
|
796
775
|
return compiled;
|
|
797
776
|
}
|
|
@@ -2030,7 +2009,7 @@ function createShutdownManager() {
|
|
|
2030
2009
|
}
|
|
2031
2010
|
|
|
2032
2011
|
// src/router.ts
|
|
2033
|
-
import {
|
|
2012
|
+
import { readdir } from "fs/promises";
|
|
2034
2013
|
import { join } from "path";
|
|
2035
2014
|
import { pathToFileURL } from "url";
|
|
2036
2015
|
|
|
@@ -2080,6 +2059,19 @@ function isRouteFile(filename) {
|
|
|
2080
2059
|
}
|
|
2081
2060
|
|
|
2082
2061
|
// src/router.ts
|
|
2062
|
+
async function scanFiles(dir, base = "") {
|
|
2063
|
+
const results = [];
|
|
2064
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
2065
|
+
for (const e of entries) {
|
|
2066
|
+
const rel = base ? `${base}/${e.name}` : e.name;
|
|
2067
|
+
if (e.isDirectory()) {
|
|
2068
|
+
results.push(...await scanFiles(join(dir, e.name), rel));
|
|
2069
|
+
} else if (/\.(ts|js)$/.test(e.name) && !e.name.startsWith("_") && !e.name.endsWith(".test.ts") && !e.name.endsWith(".test.js") && !e.name.endsWith(".spec.ts") && !e.name.endsWith(".spec.js")) {
|
|
2070
|
+
results.push(rel);
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
return results;
|
|
2074
|
+
}
|
|
2083
2075
|
async function scanRoutes(options) {
|
|
2084
2076
|
const { routesDir, verbose = true } = options;
|
|
2085
2077
|
const routes = [];
|
|
@@ -2088,12 +2080,7 @@ async function scanRoutes(options) {
|
|
|
2088
2080
|
\u{1F50D} Scanning routes in: ${routesDir}
|
|
2089
2081
|
`);
|
|
2090
2082
|
}
|
|
2091
|
-
const
|
|
2092
|
-
const files = await glob(pattern, {
|
|
2093
|
-
cwd: routesDir,
|
|
2094
|
-
nodir: true,
|
|
2095
|
-
ignore: ["**/_*.ts", "**/_*.js", "**/*.test.ts", "**/*.spec.ts"]
|
|
2096
|
-
});
|
|
2083
|
+
const files = await scanFiles(routesDir);
|
|
2097
2084
|
for (const file of files) {
|
|
2098
2085
|
if (!isRouteFile(file)) continue;
|
|
2099
2086
|
const parsed = fileToPath(file);
|
|
@@ -2389,110 +2376,9 @@ function buildNativeContext(req, res, params, body, services, serialize) {
|
|
|
2389
2376
|
import { z } from "zod";
|
|
2390
2377
|
|
|
2391
2378
|
// src/openapi.ts
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
if (!def) {
|
|
2396
|
-
return { type: "object" };
|
|
2397
|
-
}
|
|
2398
|
-
const typeName = def.typeName;
|
|
2399
|
-
switch (typeName) {
|
|
2400
|
-
case "ZodString": {
|
|
2401
|
-
const result = { type: "string" };
|
|
2402
|
-
for (const check of def.checks || []) {
|
|
2403
|
-
if (check.kind === "min") result.minLength = check.value;
|
|
2404
|
-
if (check.kind === "max") result.maxLength = check.value;
|
|
2405
|
-
if (check.kind === "email") result.format = "email";
|
|
2406
|
-
if (check.kind === "url") result.format = "uri";
|
|
2407
|
-
if (check.kind === "uuid") result.format = "uuid";
|
|
2408
|
-
if (check.kind === "regex") result.pattern = check.regex.source;
|
|
2409
|
-
}
|
|
2410
|
-
return result;
|
|
2411
|
-
}
|
|
2412
|
-
case "ZodNumber": {
|
|
2413
|
-
const result = { type: "number" };
|
|
2414
|
-
for (const check of def.checks || []) {
|
|
2415
|
-
if (check.kind === "min") result.minimum = check.value;
|
|
2416
|
-
if (check.kind === "max") result.maximum = check.value;
|
|
2417
|
-
if (check.kind === "int") result.type = "integer";
|
|
2418
|
-
}
|
|
2419
|
-
return result;
|
|
2420
|
-
}
|
|
2421
|
-
case "ZodBoolean":
|
|
2422
|
-
return { type: "boolean" };
|
|
2423
|
-
case "ZodNull":
|
|
2424
|
-
return { type: "null" };
|
|
2425
|
-
case "ZodArray":
|
|
2426
|
-
return {
|
|
2427
|
-
type: "array",
|
|
2428
|
-
items: zodToJsonSchema2(def.type)
|
|
2429
|
-
};
|
|
2430
|
-
case "ZodObject": {
|
|
2431
|
-
const properties = {};
|
|
2432
|
-
const required = [];
|
|
2433
|
-
const shape = def.shape?.() || def.shape;
|
|
2434
|
-
if (shape) {
|
|
2435
|
-
for (const [key, value] of Object.entries(shape)) {
|
|
2436
|
-
properties[key] = zodToJsonSchema2(value);
|
|
2437
|
-
const fieldDef = value._def;
|
|
2438
|
-
if (fieldDef?.typeName !== "ZodOptional" && fieldDef?.typeName !== "ZodDefault") {
|
|
2439
|
-
required.push(key);
|
|
2440
|
-
}
|
|
2441
|
-
}
|
|
2442
|
-
}
|
|
2443
|
-
return {
|
|
2444
|
-
type: "object",
|
|
2445
|
-
properties,
|
|
2446
|
-
...required.length > 0 ? { required } : {}
|
|
2447
|
-
};
|
|
2448
|
-
}
|
|
2449
|
-
case "ZodEnum":
|
|
2450
|
-
return {
|
|
2451
|
-
type: "string",
|
|
2452
|
-
enum: def.values
|
|
2453
|
-
};
|
|
2454
|
-
case "ZodNativeEnum":
|
|
2455
|
-
return {
|
|
2456
|
-
type: "string",
|
|
2457
|
-
enum: Object.values(def.values)
|
|
2458
|
-
};
|
|
2459
|
-
case "ZodLiteral":
|
|
2460
|
-
return {
|
|
2461
|
-
type: typeof def.value,
|
|
2462
|
-
enum: [def.value]
|
|
2463
|
-
};
|
|
2464
|
-
case "ZodUnion":
|
|
2465
|
-
return {
|
|
2466
|
-
oneOf: def.options.map((opt) => zodToJsonSchema2(opt))
|
|
2467
|
-
};
|
|
2468
|
-
case "ZodOptional":
|
|
2469
|
-
return {
|
|
2470
|
-
...zodToJsonSchema2(def.innerType),
|
|
2471
|
-
nullable: true
|
|
2472
|
-
};
|
|
2473
|
-
case "ZodNullable":
|
|
2474
|
-
return {
|
|
2475
|
-
...zodToJsonSchema2(def.innerType),
|
|
2476
|
-
nullable: true
|
|
2477
|
-
};
|
|
2478
|
-
case "ZodDefault":
|
|
2479
|
-
return {
|
|
2480
|
-
...zodToJsonSchema2(def.innerType),
|
|
2481
|
-
default: def.defaultValue()
|
|
2482
|
-
};
|
|
2483
|
-
case "ZodRecord":
|
|
2484
|
-
return {
|
|
2485
|
-
type: "object",
|
|
2486
|
-
additionalProperties: zodToJsonSchema2(def.valueType)
|
|
2487
|
-
};
|
|
2488
|
-
case "ZodDate":
|
|
2489
|
-
return { type: "string", format: "date-time" };
|
|
2490
|
-
case "ZodAny":
|
|
2491
|
-
case "ZodUnknown":
|
|
2492
|
-
return {};
|
|
2493
|
-
default:
|
|
2494
|
-
return { type: "object" };
|
|
2495
|
-
}
|
|
2379
|
+
import { zodToJsonSchema as _zodToJsonSchema } from "zod-to-json-schema";
|
|
2380
|
+
function zodToJsonSchema(zodSchema) {
|
|
2381
|
+
return _zodToJsonSchema(zodSchema, { $refStrategy: "none" });
|
|
2496
2382
|
}
|
|
2497
2383
|
var OpenAPIGenerator = class {
|
|
2498
2384
|
config;
|
|
@@ -2581,7 +2467,7 @@ var OpenAPIGenerator = class {
|
|
|
2581
2467
|
}
|
|
2582
2468
|
}
|
|
2583
2469
|
if (schema?.query) {
|
|
2584
|
-
const querySchema =
|
|
2470
|
+
const querySchema = zodToJsonSchema(schema.query);
|
|
2585
2471
|
if (querySchema.properties) {
|
|
2586
2472
|
for (const [name, propSchema] of Object.entries(querySchema.properties)) {
|
|
2587
2473
|
operation.parameters.push({
|
|
@@ -2594,7 +2480,7 @@ var OpenAPIGenerator = class {
|
|
|
2594
2480
|
}
|
|
2595
2481
|
}
|
|
2596
2482
|
if (schema?.params) {
|
|
2597
|
-
const paramsSchema =
|
|
2483
|
+
const paramsSchema = zodToJsonSchema(schema.params);
|
|
2598
2484
|
if (paramsSchema.properties) {
|
|
2599
2485
|
for (const [name, propSchema] of Object.entries(paramsSchema.properties)) {
|
|
2600
2486
|
const existingIdx = operation.parameters.findIndex(
|
|
@@ -2611,7 +2497,7 @@ var OpenAPIGenerator = class {
|
|
|
2611
2497
|
required: true,
|
|
2612
2498
|
content: {
|
|
2613
2499
|
"application/json": {
|
|
2614
|
-
schema:
|
|
2500
|
+
schema: zodToJsonSchema(schema.body)
|
|
2615
2501
|
}
|
|
2616
2502
|
}
|
|
2617
2503
|
};
|
|
@@ -2622,7 +2508,7 @@ var OpenAPIGenerator = class {
|
|
|
2622
2508
|
description: this.getStatusDescription(parseInt(status)),
|
|
2623
2509
|
content: {
|
|
2624
2510
|
"application/json": {
|
|
2625
|
-
schema:
|
|
2511
|
+
schema: zodToJsonSchema(responseSchema)
|
|
2626
2512
|
}
|
|
2627
2513
|
}
|
|
2628
2514
|
};
|