@kozojs/core 0.3.10 → 0.3.12
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 +51 -1
- package/lib/index.js +42 -13
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -301,10 +301,15 @@ declare class Kozo<TServices extends Services = Services> {
|
|
|
301
301
|
loadRoutes(routesDir?: string): Promise<this>;
|
|
302
302
|
generateClient(baseUrl?: string): string;
|
|
303
303
|
generateClient(options?: ClientGeneratorOptions): string;
|
|
304
|
+
get(path: string, handler: KozoHandler<{}, TServices>): this;
|
|
304
305
|
get<S extends RouteSchema>(path: string, schema: S, handler: KozoHandler<S, TServices>): this;
|
|
306
|
+
post(path: string, handler: KozoHandler<{}, TServices>): this;
|
|
305
307
|
post<S extends RouteSchema>(path: string, schema: S, handler: KozoHandler<S, TServices>): this;
|
|
308
|
+
put(path: string, handler: KozoHandler<{}, TServices>): this;
|
|
306
309
|
put<S extends RouteSchema>(path: string, schema: S, handler: KozoHandler<S, TServices>): this;
|
|
310
|
+
patch(path: string, handler: KozoHandler<{}, TServices>): this;
|
|
307
311
|
patch<S extends RouteSchema>(path: string, schema: S, handler: KozoHandler<S, TServices>): this;
|
|
312
|
+
delete(path: string, handler: KozoHandler<{}, TServices>): this;
|
|
308
313
|
delete<S extends RouteSchema>(path: string, schema: S, handler: KozoHandler<S, TServices>): this;
|
|
309
314
|
private register;
|
|
310
315
|
/**
|
|
@@ -336,6 +341,8 @@ declare function createKozo<TServices extends Services = Services>(config?: Kozo
|
|
|
336
341
|
type ZValidatorErrors = {
|
|
337
342
|
instancePath: string;
|
|
338
343
|
message: string;
|
|
344
|
+
keyword?: string;
|
|
345
|
+
path?: (string | number)[];
|
|
339
346
|
}[];
|
|
340
347
|
type ZValidator = ((data: unknown) => boolean) & {
|
|
341
348
|
errors: ZValidatorErrors | null;
|
|
@@ -416,6 +423,7 @@ declare function fastWriteError(err: unknown, res: ServerResponse): void;
|
|
|
416
423
|
*/
|
|
417
424
|
interface ValidationError {
|
|
418
425
|
field: string;
|
|
426
|
+
path?: (string | number)[];
|
|
419
427
|
message: string;
|
|
420
428
|
code: string;
|
|
421
429
|
value?: unknown;
|
|
@@ -654,4 +662,46 @@ declare class OpenAPIGenerator {
|
|
|
654
662
|
declare function generateSwaggerHtml(specUrl: string, title?: string): string;
|
|
655
663
|
declare function createOpenAPIGenerator(config: OpenAPIConfig): OpenAPIGenerator;
|
|
656
664
|
|
|
657
|
-
|
|
665
|
+
/**
|
|
666
|
+
* Common pagination query schema.
|
|
667
|
+
* Use it directly as the `query` field to avoid repeating this pattern everywhere.
|
|
668
|
+
*
|
|
669
|
+
* @example
|
|
670
|
+
* app.get('/users', { query: paginationSchema }, (ctx) => {
|
|
671
|
+
* const { page, limit } = ctx.query; // fully typed
|
|
672
|
+
* });
|
|
673
|
+
*/
|
|
674
|
+
declare const paginationSchema: z.ZodObject<{
|
|
675
|
+
page: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
676
|
+
limit: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
677
|
+
}, z.core.$strip>;
|
|
678
|
+
/**
|
|
679
|
+
* Route param schema for `:id` routes that expect a UUID.
|
|
680
|
+
*
|
|
681
|
+
* @example
|
|
682
|
+
* app.get('/users/:id', { params: uuidParams }, (ctx) => ctx.params.id);
|
|
683
|
+
*/
|
|
684
|
+
declare const uuidParams: z.ZodObject<{
|
|
685
|
+
id: z.ZodString;
|
|
686
|
+
}, z.core.$strip>;
|
|
687
|
+
/**
|
|
688
|
+
* Route param schema for `:id` routes that expect a positive integer.
|
|
689
|
+
*
|
|
690
|
+
* @example
|
|
691
|
+
* app.get('/posts/:id', { params: idParams }, (ctx) => ctx.params.id);
|
|
692
|
+
*/
|
|
693
|
+
declare const idParams: z.ZodObject<{
|
|
694
|
+
id: z.ZodCoercedNumber<unknown>;
|
|
695
|
+
}, z.core.$strip>;
|
|
696
|
+
/**
|
|
697
|
+
* Standard success/deleted response schema.
|
|
698
|
+
*
|
|
699
|
+
* @example
|
|
700
|
+
* app.delete('/users/:id', { params: uuidParams, response: deletedSchema }, ...);
|
|
701
|
+
*/
|
|
702
|
+
declare const deletedSchema: z.ZodObject<{
|
|
703
|
+
success: z.ZodBoolean;
|
|
704
|
+
deletedId: z.ZodString;
|
|
705
|
+
}, z.core.$strip>;
|
|
706
|
+
|
|
707
|
+
export { type ClientGeneratorOptions, type CompiledRoute, ERROR_RESPONSES, ForbiddenError, type InferResponse, type InferSchema, type InflightTracker, Kozo, type KozoConfig, type KozoContext, type KozoEnv, KozoError, type KozoHandler, type NativeKozoContext, type NativeKozoHandler, NotFoundError, type OpenAPIConfig, OpenAPIGenerator, type OpenAPIInfo, type OpenAPISpec, type Plugin, type ProblemDetails, type RouteInfo, type RouteSchema, SchemaCompiler, type Services, ShutdownManager, type ShutdownOptions, type ShutdownState, UnauthorizedError, type ValidationError, ValidationFailedError, buildNativeContext, compileRouteHandler, createInflightTracker, createKozo, createOpenAPIGenerator, createShutdownManager, deletedSchema, fastCL, fastWrite400, fastWrite404, fastWrite500, fastWriteError, fastWriteHtml, fastWriteJson, fastWriteJsonStatus, fastWriteText, forbiddenResponse, formatAjvErrors, formatZodErrors, generateSwaggerHtml, generateTypedClient, idParams, internalErrorResponse, notFoundResponse, paginationSchema, trackRequest, unauthorizedResponse, uuidParams, validationErrorResponse };
|
package/lib/index.js
CHANGED
|
@@ -257,6 +257,7 @@ function formatAjvErrors(errors) {
|
|
|
257
257
|
if (!errors || errors.length === 0) return [];
|
|
258
258
|
return errors.map((err) => ({
|
|
259
259
|
field: err.instancePath?.replace(/^\//, "").replace(/\//g, ".") || err.params?.missingProperty || "unknown",
|
|
260
|
+
path: err.path,
|
|
260
261
|
message: err.message || "Invalid value",
|
|
261
262
|
code: err.keyword || "invalid",
|
|
262
263
|
value: err.data
|
|
@@ -741,7 +742,9 @@ function makeZValidator(schema) {
|
|
|
741
742
|
}
|
|
742
743
|
fn.errors = r.error.issues.map((i) => ({
|
|
743
744
|
instancePath: i.path.length ? "/" + i.path.join("/") : "",
|
|
744
|
-
message: i.message
|
|
745
|
+
message: i.message,
|
|
746
|
+
keyword: i.code,
|
|
747
|
+
path: i.path
|
|
745
748
|
}));
|
|
746
749
|
return false;
|
|
747
750
|
};
|
|
@@ -2207,20 +2210,25 @@ var Kozo = class {
|
|
|
2207
2210
|
}));
|
|
2208
2211
|
return generateTypedClient(routeInfos, options);
|
|
2209
2212
|
}
|
|
2210
|
-
get(path,
|
|
2211
|
-
return this.register("get", path,
|
|
2213
|
+
get(path, schemaOrHandler, handler) {
|
|
2214
|
+
if (typeof schemaOrHandler === "function") return this.register("get", path, {}, schemaOrHandler);
|
|
2215
|
+
return this.register("get", path, schemaOrHandler, handler);
|
|
2212
2216
|
}
|
|
2213
|
-
post(path,
|
|
2214
|
-
return this.register("post", path,
|
|
2217
|
+
post(path, schemaOrHandler, handler) {
|
|
2218
|
+
if (typeof schemaOrHandler === "function") return this.register("post", path, {}, schemaOrHandler);
|
|
2219
|
+
return this.register("post", path, schemaOrHandler, handler);
|
|
2215
2220
|
}
|
|
2216
|
-
put(path,
|
|
2217
|
-
return this.register("put", path,
|
|
2221
|
+
put(path, schemaOrHandler, handler) {
|
|
2222
|
+
if (typeof schemaOrHandler === "function") return this.register("put", path, {}, schemaOrHandler);
|
|
2223
|
+
return this.register("put", path, schemaOrHandler, handler);
|
|
2218
2224
|
}
|
|
2219
|
-
patch(path,
|
|
2220
|
-
return this.register("patch", path,
|
|
2225
|
+
patch(path, schemaOrHandler, handler) {
|
|
2226
|
+
if (typeof schemaOrHandler === "function") return this.register("patch", path, {}, schemaOrHandler);
|
|
2227
|
+
return this.register("patch", path, schemaOrHandler, handler);
|
|
2221
2228
|
}
|
|
2222
|
-
delete(path,
|
|
2223
|
-
return this.register("delete", path,
|
|
2229
|
+
delete(path, schemaOrHandler, handler) {
|
|
2230
|
+
if (typeof schemaOrHandler === "function") return this.register("delete", path, {}, schemaOrHandler);
|
|
2231
|
+
return this.register("delete", path, schemaOrHandler, handler);
|
|
2224
2232
|
}
|
|
2225
2233
|
register(method, path, schema, handler) {
|
|
2226
2234
|
this.routes.push({ method, path, schema });
|
|
@@ -2378,7 +2386,7 @@ function buildNativeContext(req, res, params, body, services, serialize) {
|
|
|
2378
2386
|
}
|
|
2379
2387
|
|
|
2380
2388
|
// src/index.ts
|
|
2381
|
-
import { z } from "zod";
|
|
2389
|
+
import { z as z2 } from "zod";
|
|
2382
2390
|
|
|
2383
2391
|
// src/openapi.ts
|
|
2384
2392
|
import { zodToJsonSchema as _zodToJsonSchema } from "zod-to-json-schema";
|
|
@@ -2794,6 +2802,23 @@ async function applyFileSystemRouting(app, options = {}) {
|
|
|
2794
2802
|
function createFileSystemRouting(options = {}) {
|
|
2795
2803
|
return (app) => applyFileSystemRouting(app, options);
|
|
2796
2804
|
}
|
|
2805
|
+
|
|
2806
|
+
// src/helpers.ts
|
|
2807
|
+
import { z } from "zod";
|
|
2808
|
+
var paginationSchema = z.object({
|
|
2809
|
+
page: z.coerce.number().int().min(1).default(1),
|
|
2810
|
+
limit: z.coerce.number().int().min(1).max(100).default(10)
|
|
2811
|
+
});
|
|
2812
|
+
var uuidParams = z.object({
|
|
2813
|
+
id: z.string().uuid()
|
|
2814
|
+
});
|
|
2815
|
+
var idParams = z.object({
|
|
2816
|
+
id: z.coerce.number().int().positive()
|
|
2817
|
+
});
|
|
2818
|
+
var deletedSchema = z.object({
|
|
2819
|
+
success: z.boolean(),
|
|
2820
|
+
deletedId: z.string()
|
|
2821
|
+
});
|
|
2797
2822
|
export {
|
|
2798
2823
|
ERROR_RESPONSES,
|
|
2799
2824
|
ForbiddenError,
|
|
@@ -2822,6 +2847,7 @@ export {
|
|
|
2822
2847
|
createKozo,
|
|
2823
2848
|
createOpenAPIGenerator,
|
|
2824
2849
|
createShutdownManager,
|
|
2850
|
+
deletedSchema,
|
|
2825
2851
|
errorHandler,
|
|
2826
2852
|
fastCL,
|
|
2827
2853
|
fastWrite400,
|
|
@@ -2837,13 +2863,16 @@ export {
|
|
|
2837
2863
|
formatZodErrors,
|
|
2838
2864
|
generateSwaggerHtml,
|
|
2839
2865
|
generateTypedClient,
|
|
2866
|
+
idParams,
|
|
2840
2867
|
internalErrorResponse,
|
|
2841
2868
|
logger,
|
|
2842
2869
|
notFoundResponse,
|
|
2870
|
+
paginationSchema,
|
|
2843
2871
|
rateLimit,
|
|
2844
2872
|
trackRequest,
|
|
2845
2873
|
unauthorizedResponse,
|
|
2874
|
+
uuidParams,
|
|
2846
2875
|
validationErrorResponse,
|
|
2847
|
-
z
|
|
2876
|
+
z2 as z
|
|
2848
2877
|
};
|
|
2849
2878
|
//# sourceMappingURL=index.js.map
|