@kozojs/core 0.3.11 → 0.3.13

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
@@ -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
  /**
@@ -657,4 +662,46 @@ declare class OpenAPIGenerator {
657
662
  declare function generateSwaggerHtml(specUrl: string, title?: string): string;
658
663
  declare function createOpenAPIGenerator(config: OpenAPIConfig): OpenAPIGenerator;
659
664
 
660
- 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, fastCL, fastWrite400, fastWrite404, fastWrite500, fastWriteError, fastWriteHtml, fastWriteJson, fastWriteJsonStatus, fastWriteText, forbiddenResponse, formatAjvErrors, formatZodErrors, generateSwaggerHtml, generateTypedClient, internalErrorResponse, notFoundResponse, trackRequest, unauthorizedResponse, validationErrorResponse };
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
@@ -2144,6 +2144,37 @@ function routeScore(path) {
2144
2144
  }
2145
2145
 
2146
2146
  // src/app.ts
2147
+ var KozoGroup = class {
2148
+ constructor(prefix, parent) {
2149
+ this.prefix = prefix;
2150
+ this.parent = parent;
2151
+ }
2152
+ get(path, schemaOrHandler, handler) {
2153
+ if (typeof schemaOrHandler === "function") this.parent.get(this.prefix + path, schemaOrHandler);
2154
+ else this.parent.get(this.prefix + path, schemaOrHandler, handler);
2155
+ return this;
2156
+ }
2157
+ post(path, schemaOrHandler, handler) {
2158
+ if (typeof schemaOrHandler === "function") this.parent.post(this.prefix + path, schemaOrHandler);
2159
+ else this.parent.post(this.prefix + path, schemaOrHandler, handler);
2160
+ return this;
2161
+ }
2162
+ put(path, schemaOrHandler, handler) {
2163
+ if (typeof schemaOrHandler === "function") this.parent.put(this.prefix + path, schemaOrHandler);
2164
+ else this.parent.put(this.prefix + path, schemaOrHandler, handler);
2165
+ return this;
2166
+ }
2167
+ patch(path, schemaOrHandler, handler) {
2168
+ if (typeof schemaOrHandler === "function") this.parent.patch(this.prefix + path, schemaOrHandler);
2169
+ else this.parent.patch(this.prefix + path, schemaOrHandler, handler);
2170
+ return this;
2171
+ }
2172
+ delete(path, schemaOrHandler, handler) {
2173
+ if (typeof schemaOrHandler === "function") this.parent.delete(this.prefix + path, schemaOrHandler);
2174
+ else this.parent.delete(this.prefix + path, schemaOrHandler, handler);
2175
+ return this;
2176
+ }
2177
+ };
2147
2178
  var Kozo = class {
2148
2179
  app;
2149
2180
  services;
@@ -2210,20 +2241,39 @@ var Kozo = class {
2210
2241
  }));
2211
2242
  return generateTypedClient(routeInfos, options);
2212
2243
  }
2213
- get(path, schema, handler) {
2214
- return this.register("get", path, schema, handler);
2244
+ get(path, schemaOrHandler, handler) {
2245
+ if (typeof schemaOrHandler === "function") return this.register("get", path, {}, schemaOrHandler);
2246
+ return this.register("get", path, schemaOrHandler, handler);
2215
2247
  }
2216
- post(path, schema, handler) {
2217
- return this.register("post", path, schema, handler);
2248
+ post(path, schemaOrHandler, handler) {
2249
+ if (typeof schemaOrHandler === "function") return this.register("post", path, {}, schemaOrHandler);
2250
+ return this.register("post", path, schemaOrHandler, handler);
2218
2251
  }
2219
- put(path, schema, handler) {
2220
- return this.register("put", path, schema, handler);
2252
+ put(path, schemaOrHandler, handler) {
2253
+ if (typeof schemaOrHandler === "function") return this.register("put", path, {}, schemaOrHandler);
2254
+ return this.register("put", path, schemaOrHandler, handler);
2221
2255
  }
2222
- patch(path, schema, handler) {
2223
- return this.register("patch", path, schema, handler);
2256
+ patch(path, schemaOrHandler, handler) {
2257
+ if (typeof schemaOrHandler === "function") return this.register("patch", path, {}, schemaOrHandler);
2258
+ return this.register("patch", path, schemaOrHandler, handler);
2224
2259
  }
2225
- delete(path, schema, handler) {
2226
- return this.register("delete", path, schema, handler);
2260
+ delete(path, schemaOrHandler, handler) {
2261
+ if (typeof schemaOrHandler === "function") return this.register("delete", path, {}, schemaOrHandler);
2262
+ return this.register("delete", path, schemaOrHandler, handler);
2263
+ }
2264
+ /**
2265
+ * Group routes under a common path prefix.
2266
+ *
2267
+ * @example
2268
+ * app.group('/users', (r) => {
2269
+ * r.get('/', { query: paginationSchema }, (ctx) => listUsers(ctx.query));
2270
+ * r.get('/:id', { params: uuidParams }, (ctx) => getUser(ctx.params.id));
2271
+ * r.post('/', { body: CreateUserSchema }, (ctx) => createUser(ctx.body));
2272
+ * });
2273
+ */
2274
+ group(prefix, fn) {
2275
+ fn(new KozoGroup(prefix, this));
2276
+ return this;
2227
2277
  }
2228
2278
  register(method, path, schema, handler) {
2229
2279
  this.routes.push({ method, path, schema });
@@ -2381,7 +2431,7 @@ function buildNativeContext(req, res, params, body, services, serialize) {
2381
2431
  }
2382
2432
 
2383
2433
  // src/index.ts
2384
- import { z } from "zod";
2434
+ import { z as z2 } from "zod";
2385
2435
 
2386
2436
  // src/openapi.ts
2387
2437
  import { zodToJsonSchema as _zodToJsonSchema } from "zod-to-json-schema";
@@ -2797,6 +2847,45 @@ async function applyFileSystemRouting(app, options = {}) {
2797
2847
  function createFileSystemRouting(options = {}) {
2798
2848
  return (app) => applyFileSystemRouting(app, options);
2799
2849
  }
2850
+
2851
+ // src/helpers.ts
2852
+ import { z } from "zod";
2853
+ function defineEnv(shape) {
2854
+ const schema = z.object(shape);
2855
+ const result = schema.safeParse(process.env);
2856
+ if (!result.success) {
2857
+ const errors = result.error.issues.map((i) => ` ${i.path.join(".")}: ${i.message}`).join("\n");
2858
+ throw new Error(`[Kozo] Invalid environment variables:
2859
+ ${errors}`);
2860
+ }
2861
+ return result.data;
2862
+ }
2863
+ function paginate(items, page, limit) {
2864
+ const start = (page - 1) * limit;
2865
+ return {
2866
+ data: items.slice(start, start + limit),
2867
+ total: items.length,
2868
+ page,
2869
+ limit,
2870
+ totalPages: Math.ceil(items.length / limit),
2871
+ hasNext: start + limit < items.length,
2872
+ hasPrev: page > 1
2873
+ };
2874
+ }
2875
+ var paginationSchema = z.object({
2876
+ page: z.coerce.number().int().min(1).default(1),
2877
+ limit: z.coerce.number().int().min(1).max(100).default(10)
2878
+ });
2879
+ var uuidParams = z.object({
2880
+ id: z.string().uuid()
2881
+ });
2882
+ var idParams = z.object({
2883
+ id: z.coerce.number().int().positive()
2884
+ });
2885
+ var deletedSchema = z.object({
2886
+ success: z.boolean(),
2887
+ deletedId: z.string()
2888
+ });
2800
2889
  export {
2801
2890
  ERROR_RESPONSES,
2802
2891
  ForbiddenError,
@@ -2809,6 +2898,7 @@ export {
2809
2898
  UnauthorizedError2 as HttpUnauthorizedError,
2810
2899
  Kozo,
2811
2900
  KozoError,
2901
+ KozoGroup,
2812
2902
  NotFoundError,
2813
2903
  OpenAPIGenerator,
2814
2904
  SchemaCompiler,
@@ -2825,6 +2915,8 @@ export {
2825
2915
  createKozo,
2826
2916
  createOpenAPIGenerator,
2827
2917
  createShutdownManager,
2918
+ defineEnv,
2919
+ deletedSchema,
2828
2920
  errorHandler,
2829
2921
  fastCL,
2830
2922
  fastWrite400,
@@ -2840,13 +2932,17 @@ export {
2840
2932
  formatZodErrors,
2841
2933
  generateSwaggerHtml,
2842
2934
  generateTypedClient,
2935
+ idParams,
2843
2936
  internalErrorResponse,
2844
2937
  logger,
2845
2938
  notFoundResponse,
2939
+ paginate,
2940
+ paginationSchema,
2846
2941
  rateLimit,
2847
2942
  trackRequest,
2848
2943
  unauthorizedResponse,
2944
+ uuidParams,
2849
2945
  validationErrorResponse,
2850
- z
2946
+ z2 as z
2851
2947
  };
2852
2948
  //# sourceMappingURL=index.js.map