@kozojs/core 0.3.2 → 0.3.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/lib/index.d.ts +10 -5
- package/lib/index.js +28 -17
- package/lib/index.js.map +1 -1
- package/package.json +2 -4
package/lib/index.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ import { IncomingMessage, ServerResponse, Server } from 'node:http';
|
|
|
5
5
|
import { z } from 'zod';
|
|
6
6
|
export { z } from 'zod';
|
|
7
7
|
import { TSchema, Static } from '@sinclair/typebox';
|
|
8
|
-
import { ValidateFunction } from 'ajv';
|
|
9
8
|
export { CorsOptions, FileSystemRoutingOptions, HttpBadRequestError, HttpConflictError, HttpError, HttpForbiddenError, HttpInternalServerError, HttpNotFoundError, HttpUnauthorizedError, LoggerOptions, ManifestHttpMethod, ManifestRoute, RateLimitOptions, RoutesManifest, applyFileSystemRouting, clearRateLimitStore, cors, createFileSystemRouting, errorHandler, logger, rateLimit } from './middleware/index.js';
|
|
10
9
|
|
|
11
10
|
type SchemaType = z.ZodType<any> | TSchema;
|
|
@@ -334,14 +333,20 @@ declare class Kozo<TServices extends Services = Services> {
|
|
|
334
333
|
}
|
|
335
334
|
declare function createKozo<TServices extends Services = Services>(config?: KozoConfig<TServices>): Kozo<TServices>;
|
|
336
335
|
|
|
336
|
+
type ZValidatorErrors = {
|
|
337
|
+
instancePath: string;
|
|
338
|
+
message: string;
|
|
339
|
+
}[];
|
|
340
|
+
type ZValidator = ((data: unknown) => boolean) & {
|
|
341
|
+
errors: ZValidatorErrors | null;
|
|
342
|
+
};
|
|
337
343
|
type CompiledHandler = (c: Context) => Promise<Response> | Response;
|
|
338
344
|
type UserHandler = (c: any) => any;
|
|
339
345
|
type CompiledRoute = {
|
|
340
|
-
validateBody?:
|
|
341
|
-
validateQuery?:
|
|
342
|
-
validateParams?:
|
|
346
|
+
validateBody?: ZValidator;
|
|
347
|
+
validateQuery?: ZValidator;
|
|
348
|
+
validateParams?: ZValidator;
|
|
343
349
|
serialize?: (data: any) => string;
|
|
344
|
-
errors?: any;
|
|
345
350
|
};
|
|
346
351
|
declare class SchemaCompiler {
|
|
347
352
|
static compile(schema: RouteSchema): CompiledRoute;
|
package/lib/index.js
CHANGED
|
@@ -603,8 +603,6 @@ async function createUwsServer(opts) {
|
|
|
603
603
|
|
|
604
604
|
// src/compiler.ts
|
|
605
605
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
606
|
-
import Ajv from "ajv";
|
|
607
|
-
import addFormats from "ajv-formats";
|
|
608
606
|
import fastJson from "fast-json-stringify";
|
|
609
607
|
|
|
610
608
|
// src/fast-response.ts
|
|
@@ -727,12 +725,28 @@ function fastWriteError(err, res) {
|
|
|
727
725
|
}
|
|
728
726
|
|
|
729
727
|
// src/compiler.ts
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
728
|
+
function makeZValidator(schema) {
|
|
729
|
+
const fn = function(data) {
|
|
730
|
+
const r = schema.safeParse(data);
|
|
731
|
+
fn.errors = null;
|
|
732
|
+
if (r.success) {
|
|
733
|
+
if (data !== null && typeof data === "object" && !Array.isArray(data)) {
|
|
734
|
+
const d = data;
|
|
735
|
+
const rd = r.data;
|
|
736
|
+
for (const k of Object.keys(d)) if (!(k in rd)) delete d[k];
|
|
737
|
+
Object.assign(d, rd);
|
|
738
|
+
}
|
|
739
|
+
return true;
|
|
740
|
+
}
|
|
741
|
+
fn.errors = r.error.issues.map((i) => ({
|
|
742
|
+
instancePath: i.path.length ? "/" + i.path.join("/") : "",
|
|
743
|
+
message: i.message
|
|
744
|
+
}));
|
|
745
|
+
return false;
|
|
746
|
+
};
|
|
747
|
+
fn.errors = null;
|
|
748
|
+
return fn;
|
|
749
|
+
}
|
|
736
750
|
function isZodSchema(schema) {
|
|
737
751
|
return typeof schema === "object" && schema !== null && "safeParse" in schema;
|
|
738
752
|
}
|
|
@@ -751,17 +765,14 @@ function toJsonBody(result) {
|
|
|
751
765
|
var SchemaCompiler = class {
|
|
752
766
|
static compile(schema) {
|
|
753
767
|
const compiled = {};
|
|
754
|
-
if (schema.body) {
|
|
755
|
-
|
|
756
|
-
compiled.validateBody = ajv.compile(jsonSchema);
|
|
768
|
+
if (schema.body && isZodSchema(schema.body)) {
|
|
769
|
+
compiled.validateBody = makeZValidator(schema.body);
|
|
757
770
|
}
|
|
758
|
-
if (schema.query) {
|
|
759
|
-
|
|
760
|
-
compiled.validateQuery = ajv.compile(jsonSchema);
|
|
771
|
+
if (schema.query && isZodSchema(schema.query)) {
|
|
772
|
+
compiled.validateQuery = makeZValidator(schema.query);
|
|
761
773
|
}
|
|
762
|
-
if (schema.params) {
|
|
763
|
-
|
|
764
|
-
compiled.validateParams = ajv.compile(jsonSchema);
|
|
774
|
+
if (schema.params && isZodSchema(schema.params)) {
|
|
775
|
+
compiled.validateParams = makeZValidator(schema.params);
|
|
765
776
|
}
|
|
766
777
|
if (schema.response) {
|
|
767
778
|
if (typeof schema.response === "object" && !isZodSchema(schema.response)) {
|