@kozojs/core 0.3.12 → 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.js +70 -0
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
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;
|
|
@@ -2230,6 +2261,20 @@ var Kozo = class {
|
|
|
2230
2261
|
if (typeof schemaOrHandler === "function") return this.register("delete", path, {}, schemaOrHandler);
|
|
2231
2262
|
return this.register("delete", path, schemaOrHandler, handler);
|
|
2232
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;
|
|
2277
|
+
}
|
|
2233
2278
|
register(method, path, schema, handler) {
|
|
2234
2279
|
this.routes.push({ method, path, schema });
|
|
2235
2280
|
const compiled = SchemaCompiler.compile(schema);
|
|
@@ -2805,6 +2850,28 @@ function createFileSystemRouting(options = {}) {
|
|
|
2805
2850
|
|
|
2806
2851
|
// src/helpers.ts
|
|
2807
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
|
+
}
|
|
2808
2875
|
var paginationSchema = z.object({
|
|
2809
2876
|
page: z.coerce.number().int().min(1).default(1),
|
|
2810
2877
|
limit: z.coerce.number().int().min(1).max(100).default(10)
|
|
@@ -2831,6 +2898,7 @@ export {
|
|
|
2831
2898
|
UnauthorizedError2 as HttpUnauthorizedError,
|
|
2832
2899
|
Kozo,
|
|
2833
2900
|
KozoError,
|
|
2901
|
+
KozoGroup,
|
|
2834
2902
|
NotFoundError,
|
|
2835
2903
|
OpenAPIGenerator,
|
|
2836
2904
|
SchemaCompiler,
|
|
@@ -2847,6 +2915,7 @@ export {
|
|
|
2847
2915
|
createKozo,
|
|
2848
2916
|
createOpenAPIGenerator,
|
|
2849
2917
|
createShutdownManager,
|
|
2918
|
+
defineEnv,
|
|
2850
2919
|
deletedSchema,
|
|
2851
2920
|
errorHandler,
|
|
2852
2921
|
fastCL,
|
|
@@ -2867,6 +2936,7 @@ export {
|
|
|
2867
2936
|
internalErrorResponse,
|
|
2868
2937
|
logger,
|
|
2869
2938
|
notFoundResponse,
|
|
2939
|
+
paginate,
|
|
2870
2940
|
paginationSchema,
|
|
2871
2941
|
rateLimit,
|
|
2872
2942
|
trackRequest,
|