@kozojs/core 0.3.2 → 0.3.4

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 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?: ValidateFunction;
341
- validateQuery?: ValidateFunction;
342
- validateParams?: ValidateFunction;
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;
@@ -425,42 +430,42 @@ interface ProblemDetails {
425
430
  }
426
431
  declare const ERROR_RESPONSES: {
427
432
  readonly VALIDATION_FAILED: {
428
- readonly type: "https://kozo.dev/errors/validation-failed";
433
+ readonly type: "https://kozo-docs.vercel.app/docs/core/errors#validation-failed";
429
434
  readonly title: "Validation Failed";
430
435
  readonly status: 400;
431
436
  };
432
437
  readonly INVALID_BODY: {
433
- readonly type: "https://kozo.dev/errors/invalid-body";
438
+ readonly type: "https://kozo-docs.vercel.app/docs/core/errors#invalid-body";
434
439
  readonly title: "Invalid Request Body";
435
440
  readonly status: 400;
436
441
  };
437
442
  readonly INVALID_QUERY: {
438
- readonly type: "https://kozo.dev/errors/invalid-query";
443
+ readonly type: "https://kozo-docs.vercel.app/docs/core/errors#invalid-query";
439
444
  readonly title: "Invalid Query Parameters";
440
445
  readonly status: 400;
441
446
  };
442
447
  readonly INVALID_PARAMS: {
443
- readonly type: "https://kozo.dev/errors/invalid-params";
448
+ readonly type: "https://kozo-docs.vercel.app/docs/core/errors#invalid-params";
444
449
  readonly title: "Invalid Path Parameters";
445
450
  readonly status: 400;
446
451
  };
447
452
  readonly INTERNAL_ERROR: {
448
- readonly type: "https://kozo.dev/errors/internal-error";
453
+ readonly type: "https://kozo-docs.vercel.app/docs/core/errors#internal-error";
449
454
  readonly title: "Internal Server Error";
450
455
  readonly status: 500;
451
456
  };
452
457
  readonly NOT_FOUND: {
453
- readonly type: "https://kozo.dev/errors/not-found";
458
+ readonly type: "https://kozo-docs.vercel.app/docs/core/errors#not-found";
454
459
  readonly title: "Resource Not Found";
455
460
  readonly status: 404;
456
461
  };
457
462
  readonly UNAUTHORIZED: {
458
- readonly type: "https://kozo.dev/errors/unauthorized";
463
+ readonly type: "https://kozo-docs.vercel.app/docs/core/errors#unauthorized";
459
464
  readonly title: "Unauthorized";
460
465
  readonly status: 401;
461
466
  };
462
467
  readonly FORBIDDEN: {
463
- readonly type: "https://kozo.dev/errors/forbidden";
468
+ readonly type: "https://kozo-docs.vercel.app/docs/core/errors#forbidden";
464
469
  readonly title: "Forbidden";
465
470
  readonly status: 403;
466
471
  };
package/lib/index.js CHANGED
@@ -207,42 +207,42 @@ import { createServer as netCreateServer } from "net";
207
207
  var CONTENT_TYPE_PROBLEM = "application/problem+json";
208
208
  var ERROR_RESPONSES = {
209
209
  VALIDATION_FAILED: {
210
- type: "https://kozo.dev/errors/validation-failed",
210
+ type: "https://kozo-docs.vercel.app/docs/core/errors#validation-failed",
211
211
  title: "Validation Failed",
212
212
  status: 400
213
213
  },
214
214
  INVALID_BODY: {
215
- type: "https://kozo.dev/errors/invalid-body",
215
+ type: "https://kozo-docs.vercel.app/docs/core/errors#invalid-body",
216
216
  title: "Invalid Request Body",
217
217
  status: 400
218
218
  },
219
219
  INVALID_QUERY: {
220
- type: "https://kozo.dev/errors/invalid-query",
220
+ type: "https://kozo-docs.vercel.app/docs/core/errors#invalid-query",
221
221
  title: "Invalid Query Parameters",
222
222
  status: 400
223
223
  },
224
224
  INVALID_PARAMS: {
225
- type: "https://kozo.dev/errors/invalid-params",
225
+ type: "https://kozo-docs.vercel.app/docs/core/errors#invalid-params",
226
226
  title: "Invalid Path Parameters",
227
227
  status: 400
228
228
  },
229
229
  INTERNAL_ERROR: {
230
- type: "https://kozo.dev/errors/internal-error",
230
+ type: "https://kozo-docs.vercel.app/docs/core/errors#internal-error",
231
231
  title: "Internal Server Error",
232
232
  status: 500
233
233
  },
234
234
  NOT_FOUND: {
235
- type: "https://kozo.dev/errors/not-found",
235
+ type: "https://kozo-docs.vercel.app/docs/core/errors#not-found",
236
236
  title: "Resource Not Found",
237
237
  status: 404
238
238
  },
239
239
  UNAUTHORIZED: {
240
- type: "https://kozo.dev/errors/unauthorized",
240
+ type: "https://kozo-docs.vercel.app/docs/core/errors#unauthorized",
241
241
  title: "Unauthorized",
242
242
  status: 401
243
243
  },
244
244
  FORBIDDEN: {
245
- type: "https://kozo.dev/errors/forbidden",
245
+ type: "https://kozo-docs.vercel.app/docs/core/errors#forbidden",
246
246
  title: "Forbidden",
247
247
  status: 403
248
248
  }
@@ -352,7 +352,7 @@ var KozoError = class extends Error {
352
352
  }
353
353
  toResponse(instance) {
354
354
  const body = {
355
- type: `https://kozo.dev/errors/${this.code}`,
355
+ type: `https://kozo-docs.vercel.app/docs/core/errors#${this.code}`,
356
356
  title: this.message,
357
357
  status: this.statusCode
358
358
  };
@@ -378,7 +378,7 @@ var ValidationFailedError = class extends KozoError {
378
378
  }
379
379
  toResponse(instance) {
380
380
  const body = {
381
- type: "https://kozo.dev/errors/validation-failed",
381
+ type: "https://kozo-docs.vercel.app/docs/core/errors#validation-failed",
382
382
  title: this.message,
383
383
  status: 400,
384
384
  errors: this.errors
@@ -424,7 +424,7 @@ var STATUS_TEXT = {
424
424
  503: "503 Service Unavailable"
425
425
  };
426
426
  var BODY_404 = JSON.stringify({
427
- type: "https://kozo.dev/errors/not-found",
427
+ type: "https://kozo-docs.vercel.app/docs/core/errors#not-found",
428
428
  title: "Resource Not Found",
429
429
  status: 404
430
430
  });
@@ -432,7 +432,7 @@ var NO_BODY_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "DELETE", "OPTIONS
432
432
  var CT_JSON = "application/json";
433
433
  var CT_PROBLEM = "application/problem+json";
434
434
  var BODY_500 = JSON.stringify({
435
- type: "https://kozo.dev/errors/internal-error",
435
+ type: "https://kozo-docs.vercel.app/docs/core/errors#internal-error",
436
436
  title: "Internal Server Error",
437
437
  status: 500
438
438
  });
@@ -445,7 +445,7 @@ function uwsFastWriteJson(uwsRes, body) {
445
445
  }
446
446
  function uwsFastWrite400(field, errors, uwsRes) {
447
447
  const body = JSON.stringify({
448
- type: "https://kozo.dev/errors/validation-failed",
448
+ type: "https://kozo-docs.vercel.app/docs/core/errors#validation-failed",
449
449
  title: "Validation Failed",
450
450
  status: 400,
451
451
  errors: (errors ?? []).map((e) => ({
@@ -470,7 +470,7 @@ function uwsFastWrite500(uwsRes) {
470
470
  function uwsFastWriteError(err, uwsRes) {
471
471
  if (err instanceof KozoError) {
472
472
  const body = JSON.stringify({
473
- type: `https://kozo.dev/errors/${err.code}`,
473
+ type: `https://kozo-docs.vercel.app/docs/core/errors#${err.code}`,
474
474
  title: err.message,
475
475
  status: err.statusCode
476
476
  });
@@ -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
@@ -621,13 +619,13 @@ var CT_PROBLEM2 = "application/problem+json";
621
619
  var CT_TEXT = "text/plain";
622
620
  var CT_HTML = "text/html; charset=utf-8";
623
621
  var BODY_4042 = JSON.stringify({
624
- type: "https://kozo.dev/errors/not-found",
622
+ type: "https://kozo-docs.vercel.app/docs/core/errors#not-found",
625
623
  title: "Resource Not Found",
626
624
  status: 404
627
625
  });
628
626
  var LEN_404 = fastCL(BODY_4042.length);
629
627
  var BODY_5002 = JSON.stringify({
630
- type: "https://kozo.dev/errors/internal-error",
628
+ type: "https://kozo-docs.vercel.app/docs/core/errors#internal-error",
631
629
  title: "Internal Server Error",
632
630
  status: 500
633
631
  });
@@ -690,7 +688,7 @@ function fastWrite500(res) {
690
688
  }
691
689
  function fastWrite400(field, errors, res) {
692
690
  const body = JSON.stringify({
693
- type: "https://kozo.dev/errors/validation-failed",
691
+ type: "https://kozo-docs.vercel.app/docs/core/errors#validation-failed",
694
692
  title: "Validation Failed",
695
693
  status: 400,
696
694
  errors: (errors ?? []).map((e) => ({
@@ -710,7 +708,7 @@ function fastWrite400(field, errors, res) {
710
708
  function fastWriteError(err, res) {
711
709
  if (err instanceof KozoError) {
712
710
  const body = JSON.stringify({
713
- type: `https://kozo.dev/errors/${err.code}`,
711
+ type: `https://kozo-docs.vercel.app/docs/core/errors#${err.code}`,
714
712
  title: err.message,
715
713
  status: err.statusCode
716
714
  });
@@ -727,12 +725,28 @@ function fastWriteError(err, res) {
727
725
  }
728
726
 
729
727
  // src/compiler.ts
730
- var ajv = new Ajv({
731
- removeAdditional: "all",
732
- useDefaults: true,
733
- coerceTypes: true
734
- });
735
- addFormats(ajv);
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
- const jsonSchema = isZodSchema(schema.body) ? zodToJsonSchema(schema.body, ZOD_OPTS) : schema.body;
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
- const jsonSchema = isZodSchema(schema.query) ? zodToJsonSchema(schema.query, ZOD_OPTS) : schema.query;
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
- const jsonSchema = isZodSchema(schema.params) ? zodToJsonSchema(schema.params, ZOD_OPTS) : schema.params;
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)) {