clear-router 2.3.4 → 2.4.0
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/dist/core/index.d.cts +1 -1
- package/dist/core/index.d.mts +1 -1
- package/dist/express/index.cjs +30 -2
- package/dist/express/index.d.cts +2 -1
- package/dist/express/index.d.mts +2 -1
- package/dist/express/index.mjs +30 -2
- package/dist/fastify/index.cjs +25 -2
- package/dist/fastify/index.d.cts +2 -1
- package/dist/fastify/index.d.mts +2 -1
- package/dist/fastify/index.mjs +25 -2
- package/dist/h3/index.cjs +20 -2
- package/dist/h3/index.d.cts +2 -1
- package/dist/h3/index.d.mts +2 -1
- package/dist/h3/index.mjs +20 -2
- package/dist/hono/index.cjs +14 -9
- package/dist/hono/index.d.cts +1 -1
- package/dist/hono/index.d.mts +1 -1
- package/dist/hono/index.mjs +14 -9
- package/dist/responses-HOePPAl8.mjs +73 -0
- package/dist/responses-r-O3jS-S.cjs +91 -0
- package/dist/{router-BqoRyQJh.d.cts → router-A-9JuZPi.d.cts} +1 -1
- package/dist/{router-DCNjgCV6.d.mts → router-CVeqd7Ro.d.mts} +1 -1
- package/dist/types/h3.d.mts +1 -1
- package/package.json +1 -1
package/dist/core/index.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as CoreRouter } from "../router-
|
|
1
|
+
import { t as CoreRouter } from "../router-A-9JuZPi.cjs";
|
|
2
2
|
export { CoreRouter };
|
package/dist/core/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as CoreRouter } from "../router-
|
|
1
|
+
import { t as CoreRouter } from "../router-CVeqd7Ro.mjs";
|
|
2
2
|
export { CoreRouter };
|
package/dist/express/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_router = require('../router-C-c43ybe.cjs');
|
|
3
|
+
const require_responses = require('../responses-r-O3jS-S.cjs');
|
|
3
4
|
|
|
4
5
|
//#region src/express/router.ts
|
|
5
6
|
/**
|
|
@@ -14,6 +15,31 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
14
15
|
static ensureRequestBodyAccessor(req) {
|
|
15
16
|
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
16
17
|
}
|
|
18
|
+
static async sendReturnValue(req, res, value, method, path) {
|
|
19
|
+
if (require_responses.responseWasSent(res) || value === res || require_responses.responseWasSent(value)) return;
|
|
20
|
+
const meta = require_responses.resolveResponseMeta(value, {
|
|
21
|
+
headers: req.headers,
|
|
22
|
+
method,
|
|
23
|
+
path
|
|
24
|
+
});
|
|
25
|
+
if (!meta) return;
|
|
26
|
+
res.status(meta.status);
|
|
27
|
+
if (require_responses.isFetchResponse(meta.body)) {
|
|
28
|
+
meta.body.headers.forEach((headerValue, key) => {
|
|
29
|
+
res.setHeader(key, headerValue);
|
|
30
|
+
});
|
|
31
|
+
res.status(meta.body.status);
|
|
32
|
+
const body = Buffer.from(await meta.body.arrayBuffer());
|
|
33
|
+
res.send(body);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (meta.contentType && !res.getHeader("Content-Type")) res.setHeader("Content-Type", meta.contentType);
|
|
37
|
+
if (meta.isEmpty) {
|
|
38
|
+
res.sendStatus(meta.status);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
res.send(meta.body);
|
|
42
|
+
}
|
|
17
43
|
/**
|
|
18
44
|
* Adds a new route to the router with the specified HTTP methods, path, handler, and optional middlewares.
|
|
19
45
|
*
|
|
@@ -178,7 +204,8 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
178
204
|
params: ctx.req.params
|
|
179
205
|
});
|
|
180
206
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
181
|
-
await Promise.resolve(result);
|
|
207
|
+
const resolved = await Promise.resolve(result);
|
|
208
|
+
await Router.sendReturnValue(req, res, resolved, method, route.path);
|
|
182
209
|
} catch (error) {
|
|
183
210
|
next(error);
|
|
184
211
|
}
|
|
@@ -207,7 +234,8 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
207
234
|
params: ctx.req.params
|
|
208
235
|
});
|
|
209
236
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
210
|
-
await Promise.resolve(result);
|
|
237
|
+
const resolved = await Promise.resolve(result);
|
|
238
|
+
await Router.sendReturnValue(req, res, resolved, method, route.path);
|
|
211
239
|
} catch (error) {
|
|
212
240
|
next(error);
|
|
213
241
|
}
|
package/dist/express/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as Handler, d as ApiResourceMiddleware, f as ControllerAction, l as HttpContext, m as HttpMethod, r as Route, t as CoreRouter, u as Middleware } from "../router-
|
|
1
|
+
import { c as Handler, d as ApiResourceMiddleware, f as ControllerAction, l as HttpContext, m as HttpMethod, r as Route, t as CoreRouter, u as Middleware } from "../router-A-9JuZPi.cjs";
|
|
2
2
|
import { Router as Router$1 } from "express";
|
|
3
3
|
|
|
4
4
|
//#region src/express/router.d.ts
|
|
@@ -12,6 +12,7 @@ import { Router as Router$1 } from "express";
|
|
|
12
12
|
declare class Router extends CoreRouter {
|
|
13
13
|
protected static routerStateNamespace: string;
|
|
14
14
|
private static ensureRequestBodyAccessor;
|
|
15
|
+
private static sendReturnValue;
|
|
15
16
|
/**
|
|
16
17
|
* Adds a new route to the router with the specified HTTP methods, path, handler, and optional middlewares.
|
|
17
18
|
*
|
package/dist/express/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as Handler, d as ApiResourceMiddleware, f as ControllerAction, l as HttpContext, m as HttpMethod, r as Route, t as CoreRouter, u as Middleware } from "../router-
|
|
1
|
+
import { c as Handler, d as ApiResourceMiddleware, f as ControllerAction, l as HttpContext, m as HttpMethod, r as Route, t as CoreRouter, u as Middleware } from "../router-CVeqd7Ro.mjs";
|
|
2
2
|
import { Router as Router$1 } from "express";
|
|
3
3
|
|
|
4
4
|
//#region src/express/router.d.ts
|
|
@@ -12,6 +12,7 @@ import { Router as Router$1 } from "express";
|
|
|
12
12
|
declare class Router extends CoreRouter {
|
|
13
13
|
protected static routerStateNamespace: string;
|
|
14
14
|
private static ensureRequestBodyAccessor;
|
|
15
|
+
private static sendReturnValue;
|
|
15
16
|
/**
|
|
16
17
|
* Adds a new route to the router with the specified HTTP methods, path, handler, and optional middlewares.
|
|
17
18
|
*
|
package/dist/express/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { t as CoreRouter } from "../router-CHaZi7NS.mjs";
|
|
2
|
+
import { n as resolveResponseMeta, r as responseWasSent, t as isFetchResponse } from "../responses-HOePPAl8.mjs";
|
|
2
3
|
|
|
3
4
|
//#region src/express/router.ts
|
|
4
5
|
/**
|
|
@@ -13,6 +14,31 @@ var Router = class Router extends CoreRouter {
|
|
|
13
14
|
static ensureRequestBodyAccessor(req) {
|
|
14
15
|
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
15
16
|
}
|
|
17
|
+
static async sendReturnValue(req, res, value, method, path) {
|
|
18
|
+
if (responseWasSent(res) || value === res || responseWasSent(value)) return;
|
|
19
|
+
const meta = resolveResponseMeta(value, {
|
|
20
|
+
headers: req.headers,
|
|
21
|
+
method,
|
|
22
|
+
path
|
|
23
|
+
});
|
|
24
|
+
if (!meta) return;
|
|
25
|
+
res.status(meta.status);
|
|
26
|
+
if (isFetchResponse(meta.body)) {
|
|
27
|
+
meta.body.headers.forEach((headerValue, key) => {
|
|
28
|
+
res.setHeader(key, headerValue);
|
|
29
|
+
});
|
|
30
|
+
res.status(meta.body.status);
|
|
31
|
+
const body = Buffer.from(await meta.body.arrayBuffer());
|
|
32
|
+
res.send(body);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (meta.contentType && !res.getHeader("Content-Type")) res.setHeader("Content-Type", meta.contentType);
|
|
36
|
+
if (meta.isEmpty) {
|
|
37
|
+
res.sendStatus(meta.status);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
res.send(meta.body);
|
|
41
|
+
}
|
|
16
42
|
/**
|
|
17
43
|
* Adds a new route to the router with the specified HTTP methods, path, handler, and optional middlewares.
|
|
18
44
|
*
|
|
@@ -177,7 +203,8 @@ var Router = class Router extends CoreRouter {
|
|
|
177
203
|
params: ctx.req.params
|
|
178
204
|
});
|
|
179
205
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
180
|
-
await Promise.resolve(result);
|
|
206
|
+
const resolved = await Promise.resolve(result);
|
|
207
|
+
await Router.sendReturnValue(req, res, resolved, method, route.path);
|
|
181
208
|
} catch (error) {
|
|
182
209
|
next(error);
|
|
183
210
|
}
|
|
@@ -206,7 +233,8 @@ var Router = class Router extends CoreRouter {
|
|
|
206
233
|
params: ctx.req.params
|
|
207
234
|
});
|
|
208
235
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
209
|
-
await Promise.resolve(result);
|
|
236
|
+
const resolved = await Promise.resolve(result);
|
|
237
|
+
await Router.sendReturnValue(req, res, resolved, method, route.path);
|
|
210
238
|
} catch (error) {
|
|
211
239
|
next(error);
|
|
212
240
|
}
|
package/dist/fastify/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_router = require('../router-C-c43ybe.cjs');
|
|
3
|
+
const require_responses = require('../responses-r-O3jS-S.cjs');
|
|
3
4
|
|
|
4
5
|
//#region src/fastify/router.ts
|
|
5
6
|
/**
|
|
@@ -13,6 +14,26 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
13
14
|
static ensureRequestBodyAccessor(req) {
|
|
14
15
|
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
15
16
|
}
|
|
17
|
+
static async sendReturnValue(req, reply, value, method, path) {
|
|
18
|
+
if (require_responses.responseWasSent(reply) || value === reply || require_responses.responseWasSent(value)) return value;
|
|
19
|
+
const meta = require_responses.resolveResponseMeta(value, {
|
|
20
|
+
headers: req.headers,
|
|
21
|
+
method,
|
|
22
|
+
path
|
|
23
|
+
});
|
|
24
|
+
if (!meta) return void 0;
|
|
25
|
+
reply.code(meta.status);
|
|
26
|
+
if (require_responses.isFetchResponse(meta.body)) {
|
|
27
|
+
meta.body.headers.forEach((headerValue, key) => {
|
|
28
|
+
reply.header(key, headerValue);
|
|
29
|
+
});
|
|
30
|
+
reply.code(meta.body.status);
|
|
31
|
+
return reply.send(Buffer.from(await meta.body.arrayBuffer()));
|
|
32
|
+
}
|
|
33
|
+
if (meta.contentType) reply.type(meta.contentType);
|
|
34
|
+
if (meta.isEmpty) return reply.send();
|
|
35
|
+
return reply.send(meta.body);
|
|
36
|
+
}
|
|
16
37
|
/**
|
|
17
38
|
* Add a route to the router
|
|
18
39
|
*
|
|
@@ -176,7 +197,8 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
176
197
|
params: ctx.req.params ?? {}
|
|
177
198
|
});
|
|
178
199
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
179
|
-
|
|
200
|
+
const resolved = await Promise.resolve(result);
|
|
201
|
+
return Router.sendReturnValue(req, reply, resolved, method, route.path);
|
|
180
202
|
}
|
|
181
203
|
});
|
|
182
204
|
if ([
|
|
@@ -201,7 +223,8 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
201
223
|
params: ctx.req.params ?? {}
|
|
202
224
|
});
|
|
203
225
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
204
|
-
|
|
226
|
+
const resolved = await Promise.resolve(result);
|
|
227
|
+
return Router.sendReturnValue(req, reply, resolved, method, route.path);
|
|
205
228
|
}
|
|
206
229
|
});
|
|
207
230
|
}
|
package/dist/fastify/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-
|
|
1
|
+
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-A-9JuZPi.cjs";
|
|
2
2
|
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
3
3
|
|
|
4
4
|
//#region types/fastify.d.ts
|
|
@@ -25,6 +25,7 @@ type FastifyApp = FastifyInstance;
|
|
|
25
25
|
declare class Router extends CoreRouter {
|
|
26
26
|
protected static routerStateNamespace: string;
|
|
27
27
|
private static ensureRequestBodyAccessor;
|
|
28
|
+
private static sendReturnValue;
|
|
28
29
|
/**
|
|
29
30
|
* Add a route to the router
|
|
30
31
|
*
|
package/dist/fastify/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-
|
|
1
|
+
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-CVeqd7Ro.mjs";
|
|
2
2
|
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
3
3
|
|
|
4
4
|
//#region types/fastify.d.ts
|
|
@@ -25,6 +25,7 @@ type FastifyApp = FastifyInstance;
|
|
|
25
25
|
declare class Router extends CoreRouter {
|
|
26
26
|
protected static routerStateNamespace: string;
|
|
27
27
|
private static ensureRequestBodyAccessor;
|
|
28
|
+
private static sendReturnValue;
|
|
28
29
|
/**
|
|
29
30
|
* Add a route to the router
|
|
30
31
|
*
|
package/dist/fastify/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { t as CoreRouter } from "../router-CHaZi7NS.mjs";
|
|
2
|
+
import { n as resolveResponseMeta, r as responseWasSent, t as isFetchResponse } from "../responses-HOePPAl8.mjs";
|
|
2
3
|
|
|
3
4
|
//#region src/fastify/router.ts
|
|
4
5
|
/**
|
|
@@ -12,6 +13,26 @@ var Router = class Router extends CoreRouter {
|
|
|
12
13
|
static ensureRequestBodyAccessor(req) {
|
|
13
14
|
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
14
15
|
}
|
|
16
|
+
static async sendReturnValue(req, reply, value, method, path) {
|
|
17
|
+
if (responseWasSent(reply) || value === reply || responseWasSent(value)) return value;
|
|
18
|
+
const meta = resolveResponseMeta(value, {
|
|
19
|
+
headers: req.headers,
|
|
20
|
+
method,
|
|
21
|
+
path
|
|
22
|
+
});
|
|
23
|
+
if (!meta) return void 0;
|
|
24
|
+
reply.code(meta.status);
|
|
25
|
+
if (isFetchResponse(meta.body)) {
|
|
26
|
+
meta.body.headers.forEach((headerValue, key) => {
|
|
27
|
+
reply.header(key, headerValue);
|
|
28
|
+
});
|
|
29
|
+
reply.code(meta.body.status);
|
|
30
|
+
return reply.send(Buffer.from(await meta.body.arrayBuffer()));
|
|
31
|
+
}
|
|
32
|
+
if (meta.contentType) reply.type(meta.contentType);
|
|
33
|
+
if (meta.isEmpty) return reply.send();
|
|
34
|
+
return reply.send(meta.body);
|
|
35
|
+
}
|
|
15
36
|
/**
|
|
16
37
|
* Add a route to the router
|
|
17
38
|
*
|
|
@@ -175,7 +196,8 @@ var Router = class Router extends CoreRouter {
|
|
|
175
196
|
params: ctx.req.params ?? {}
|
|
176
197
|
});
|
|
177
198
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
178
|
-
|
|
199
|
+
const resolved = await Promise.resolve(result);
|
|
200
|
+
return Router.sendReturnValue(req, reply, resolved, method, route.path);
|
|
179
201
|
}
|
|
180
202
|
});
|
|
181
203
|
if ([
|
|
@@ -200,7 +222,8 @@ var Router = class Router extends CoreRouter {
|
|
|
200
222
|
params: ctx.req.params ?? {}
|
|
201
223
|
});
|
|
202
224
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
203
|
-
|
|
225
|
+
const resolved = await Promise.resolve(result);
|
|
226
|
+
return Router.sendReturnValue(req, reply, resolved, method, route.path);
|
|
204
227
|
}
|
|
205
228
|
});
|
|
206
229
|
}
|
package/dist/h3/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_router = require('../router-C-c43ybe.cjs');
|
|
3
|
+
const require_responses = require('../responses-r-O3jS-S.cjs');
|
|
3
4
|
let h3 = require("h3");
|
|
4
5
|
|
|
5
6
|
//#region src/h3/router.ts
|
|
@@ -12,6 +13,21 @@ let h3 = require("h3");
|
|
|
12
13
|
var Router = class Router extends require_router.CoreRouter {
|
|
13
14
|
static routerStateNamespace = "clear-router:h3";
|
|
14
15
|
static bodyCache = /* @__PURE__ */ new WeakMap();
|
|
16
|
+
static toResponse(ctx, value, method, path) {
|
|
17
|
+
const meta = require_responses.resolveResponseMeta(value, {
|
|
18
|
+
headers: ctx.req.headers,
|
|
19
|
+
method,
|
|
20
|
+
path,
|
|
21
|
+
status: typeof ctx.res.status === "number" && ctx.res.status !== 200 ? ctx.res.status : void 0
|
|
22
|
+
});
|
|
23
|
+
if (!meta) return void 0;
|
|
24
|
+
if (meta.isNativeResponse) return meta.body;
|
|
25
|
+
if (meta.contentType?.startsWith("application/json")) return Response.json(meta.body, { status: meta.status });
|
|
26
|
+
return new Response(meta.isEmpty ? null : meta.body, {
|
|
27
|
+
status: meta.status,
|
|
28
|
+
headers: meta.contentType ? { "Content-Type": meta.contentType } : void 0
|
|
29
|
+
});
|
|
30
|
+
}
|
|
15
31
|
static async readBodyCached(ctx) {
|
|
16
32
|
if (this.bodyCache.has(ctx)) {
|
|
17
33
|
const cached = this.bodyCache.get(ctx);
|
|
@@ -191,7 +207,8 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
191
207
|
params: (0, h3.getRouterParams)(ctx, { decode: true })
|
|
192
208
|
});
|
|
193
209
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
194
|
-
|
|
210
|
+
const resolved = await Promise.resolve(result);
|
|
211
|
+
return Router.toResponse(ctx, resolved, method, route.path);
|
|
195
212
|
} catch (error) {
|
|
196
213
|
return error;
|
|
197
214
|
}
|
|
@@ -212,7 +229,8 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
212
229
|
params: (0, h3.getRouterParams)(ctx, { decode: true })
|
|
213
230
|
});
|
|
214
231
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
215
|
-
|
|
232
|
+
const resolved = await Promise.resolve(result);
|
|
233
|
+
return Router.toResponse(ctx, resolved, method, route.path);
|
|
216
234
|
} catch (error) {
|
|
217
235
|
return error;
|
|
218
236
|
}
|
package/dist/h3/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as Handler, d as ApiResourceMiddleware, f as ControllerAction, i as H3App, m as HttpMethod, o as HttpContext, r as Route, s as Middleware, t as CoreRouter } from "../router-
|
|
1
|
+
import { a as Handler, d as ApiResourceMiddleware, f as ControllerAction, i as H3App, m as HttpMethod, o as HttpContext, r as Route, s as Middleware, t as CoreRouter } from "../router-A-9JuZPi.cjs";
|
|
2
2
|
import { H3 } from "h3";
|
|
3
3
|
|
|
4
4
|
//#region src/h3/router.d.ts
|
|
@@ -11,6 +11,7 @@ import { H3 } from "h3";
|
|
|
11
11
|
declare class Router extends CoreRouter {
|
|
12
12
|
protected static routerStateNamespace: string;
|
|
13
13
|
private static readonly bodyCache;
|
|
14
|
+
private static toResponse;
|
|
14
15
|
private static readBodyCached;
|
|
15
16
|
/**
|
|
16
17
|
* Adds a new route to the router with the specified HTTP methods, path, handler, and optional middlewares.
|
package/dist/h3/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as Handler, d as ApiResourceMiddleware, f as ControllerAction, i as H3App, m as HttpMethod, o as HttpContext, r as Route, s as Middleware, t as CoreRouter } from "../router-
|
|
1
|
+
import { a as Handler, d as ApiResourceMiddleware, f as ControllerAction, i as H3App, m as HttpMethod, o as HttpContext, r as Route, s as Middleware, t as CoreRouter } from "../router-CVeqd7Ro.mjs";
|
|
2
2
|
import { H3 } from "h3";
|
|
3
3
|
|
|
4
4
|
//#region src/h3/router.d.ts
|
|
@@ -11,6 +11,7 @@ import { H3 } from "h3";
|
|
|
11
11
|
declare class Router extends CoreRouter {
|
|
12
12
|
protected static routerStateNamespace: string;
|
|
13
13
|
private static readonly bodyCache;
|
|
14
|
+
private static toResponse;
|
|
14
15
|
private static readBodyCached;
|
|
15
16
|
/**
|
|
16
17
|
* Adds a new route to the router with the specified HTTP methods, path, handler, and optional middlewares.
|
package/dist/h3/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { t as CoreRouter } from "../router-CHaZi7NS.mjs";
|
|
2
|
+
import { n as resolveResponseMeta } from "../responses-HOePPAl8.mjs";
|
|
2
3
|
import { getQuery, getRouterParams, readBody } from "h3";
|
|
3
4
|
|
|
4
5
|
//#region src/h3/router.ts
|
|
@@ -11,6 +12,21 @@ import { getQuery, getRouterParams, readBody } from "h3";
|
|
|
11
12
|
var Router = class Router extends CoreRouter {
|
|
12
13
|
static routerStateNamespace = "clear-router:h3";
|
|
13
14
|
static bodyCache = /* @__PURE__ */ new WeakMap();
|
|
15
|
+
static toResponse(ctx, value, method, path) {
|
|
16
|
+
const meta = resolveResponseMeta(value, {
|
|
17
|
+
headers: ctx.req.headers,
|
|
18
|
+
method,
|
|
19
|
+
path,
|
|
20
|
+
status: typeof ctx.res.status === "number" && ctx.res.status !== 200 ? ctx.res.status : void 0
|
|
21
|
+
});
|
|
22
|
+
if (!meta) return void 0;
|
|
23
|
+
if (meta.isNativeResponse) return meta.body;
|
|
24
|
+
if (meta.contentType?.startsWith("application/json")) return Response.json(meta.body, { status: meta.status });
|
|
25
|
+
return new Response(meta.isEmpty ? null : meta.body, {
|
|
26
|
+
status: meta.status,
|
|
27
|
+
headers: meta.contentType ? { "Content-Type": meta.contentType } : void 0
|
|
28
|
+
});
|
|
29
|
+
}
|
|
14
30
|
static async readBodyCached(ctx) {
|
|
15
31
|
if (this.bodyCache.has(ctx)) {
|
|
16
32
|
const cached = this.bodyCache.get(ctx);
|
|
@@ -190,7 +206,8 @@ var Router = class Router extends CoreRouter {
|
|
|
190
206
|
params: getRouterParams(ctx, { decode: true })
|
|
191
207
|
});
|
|
192
208
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
193
|
-
|
|
209
|
+
const resolved = await Promise.resolve(result);
|
|
210
|
+
return Router.toResponse(ctx, resolved, method, route.path);
|
|
194
211
|
} catch (error) {
|
|
195
212
|
return error;
|
|
196
213
|
}
|
|
@@ -211,7 +228,8 @@ var Router = class Router extends CoreRouter {
|
|
|
211
228
|
params: getRouterParams(ctx, { decode: true })
|
|
212
229
|
});
|
|
213
230
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
214
|
-
|
|
231
|
+
const resolved = await Promise.resolve(result);
|
|
232
|
+
return Router.toResponse(ctx, resolved, method, route.path);
|
|
215
233
|
} catch (error) {
|
|
216
234
|
return error;
|
|
217
235
|
}
|
package/dist/hono/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_router = require('../router-C-c43ybe.cjs');
|
|
3
|
+
const require_responses = require('../responses-r-O3jS-S.cjs');
|
|
3
4
|
|
|
4
5
|
//#region src/hono/router.ts
|
|
5
6
|
/**
|
|
@@ -11,13 +12,17 @@ const require_router = require('../router-C-c43ybe.cjs');
|
|
|
11
12
|
var Router = class Router extends require_router.CoreRouter {
|
|
12
13
|
static routerStateNamespace = "clear-router:hono";
|
|
13
14
|
static bodyCache = /* @__PURE__ */ new WeakMap();
|
|
14
|
-
static toResponse(ctx, value) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
static toResponse(ctx, value, method, path) {
|
|
16
|
+
const meta = require_responses.resolveResponseMeta(value, {
|
|
17
|
+
headers: ctx.req.header(),
|
|
18
|
+
method,
|
|
19
|
+
path
|
|
20
|
+
});
|
|
21
|
+
if (!meta) return void 0;
|
|
22
|
+
if (meta.isNativeResponse) return meta.body;
|
|
23
|
+
if (meta.isEmpty) return ctx.body(null, meta.status);
|
|
24
|
+
if (meta.contentType?.startsWith("application/json")) return ctx.json(meta.body, meta.status);
|
|
25
|
+
return ctx.body(meta.body, meta.status, meta.contentType ? { "Content-Type": meta.contentType } : void 0);
|
|
21
26
|
}
|
|
22
27
|
static getParams(ctx) {
|
|
23
28
|
try {
|
|
@@ -198,7 +203,7 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
198
203
|
});
|
|
199
204
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
200
205
|
const resolved = await Promise.resolve(result);
|
|
201
|
-
return Router.toResponse(ctx, resolved);
|
|
206
|
+
return Router.toResponse(ctx, resolved, method, route.path);
|
|
202
207
|
});
|
|
203
208
|
if ([
|
|
204
209
|
"put",
|
|
@@ -216,7 +221,7 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
216
221
|
});
|
|
217
222
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
218
223
|
const resolved = await Promise.resolve(result);
|
|
219
|
-
return Router.toResponse(ctx, resolved);
|
|
224
|
+
return Router.toResponse(ctx, resolved, method, route.path);
|
|
220
225
|
});
|
|
221
226
|
}
|
|
222
227
|
}
|
package/dist/hono/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-
|
|
1
|
+
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-A-9JuZPi.cjs";
|
|
2
2
|
import { Context, HonoRequest, MiddlewareHandler } from "hono";
|
|
3
3
|
|
|
4
4
|
//#region types/hono.d.ts
|
package/dist/hono/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-
|
|
1
|
+
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-CVeqd7Ro.mjs";
|
|
2
2
|
import { Context, HonoRequest, MiddlewareHandler } from "hono";
|
|
3
3
|
|
|
4
4
|
//#region types/hono.d.ts
|
package/dist/hono/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { t as CoreRouter } from "../router-CHaZi7NS.mjs";
|
|
2
|
+
import { n as resolveResponseMeta } from "../responses-HOePPAl8.mjs";
|
|
2
3
|
|
|
3
4
|
//#region src/hono/router.ts
|
|
4
5
|
/**
|
|
@@ -10,13 +11,17 @@ import { t as CoreRouter } from "../router-CHaZi7NS.mjs";
|
|
|
10
11
|
var Router = class Router extends CoreRouter {
|
|
11
12
|
static routerStateNamespace = "clear-router:hono";
|
|
12
13
|
static bodyCache = /* @__PURE__ */ new WeakMap();
|
|
13
|
-
static toResponse(ctx, value) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
static toResponse(ctx, value, method, path) {
|
|
15
|
+
const meta = resolveResponseMeta(value, {
|
|
16
|
+
headers: ctx.req.header(),
|
|
17
|
+
method,
|
|
18
|
+
path
|
|
19
|
+
});
|
|
20
|
+
if (!meta) return void 0;
|
|
21
|
+
if (meta.isNativeResponse) return meta.body;
|
|
22
|
+
if (meta.isEmpty) return ctx.body(null, meta.status);
|
|
23
|
+
if (meta.contentType?.startsWith("application/json")) return ctx.json(meta.body, meta.status);
|
|
24
|
+
return ctx.body(meta.body, meta.status, meta.contentType ? { "Content-Type": meta.contentType } : void 0);
|
|
20
25
|
}
|
|
21
26
|
static getParams(ctx) {
|
|
22
27
|
try {
|
|
@@ -197,7 +202,7 @@ var Router = class Router extends CoreRouter {
|
|
|
197
202
|
});
|
|
198
203
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
199
204
|
const resolved = await Promise.resolve(result);
|
|
200
|
-
return Router.toResponse(ctx, resolved);
|
|
205
|
+
return Router.toResponse(ctx, resolved, method, route.path);
|
|
201
206
|
});
|
|
202
207
|
if ([
|
|
203
208
|
"put",
|
|
@@ -215,7 +220,7 @@ var Router = class Router extends CoreRouter {
|
|
|
215
220
|
});
|
|
216
221
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
217
222
|
const resolved = await Promise.resolve(result);
|
|
218
|
-
return Router.toResponse(ctx, resolved);
|
|
223
|
+
return Router.toResponse(ctx, resolved, method, route.path);
|
|
219
224
|
});
|
|
220
225
|
}
|
|
221
226
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
//#region src/core/responses.ts
|
|
2
|
+
function isFetchResponse(value) {
|
|
3
|
+
return typeof Response !== "undefined" && value instanceof Response;
|
|
4
|
+
}
|
|
5
|
+
function isH3Response(value) {
|
|
6
|
+
return Boolean(value && typeof value === "object" && value.constructor?.name === "HTTPResponse");
|
|
7
|
+
}
|
|
8
|
+
function responseWasSent(target) {
|
|
9
|
+
return Boolean(target?.headersSent || target?.sent || target?.raw?.headersSent);
|
|
10
|
+
}
|
|
11
|
+
function getHeader(headers, name) {
|
|
12
|
+
if (!headers) return "";
|
|
13
|
+
if (headers instanceof Headers) return headers.get(name) || "";
|
|
14
|
+
const lowerName = name.toLowerCase();
|
|
15
|
+
const value = headers[name] ?? headers[lowerName];
|
|
16
|
+
return Array.isArray(value) ? String(value[0] ?? "") : String(value ?? "");
|
|
17
|
+
}
|
|
18
|
+
function isApiRequest(headers, path) {
|
|
19
|
+
const accept = getHeader(headers, "accept").toLowerCase();
|
|
20
|
+
const requestedWith = getHeader(headers, "x-requested-with").toLowerCase();
|
|
21
|
+
return Boolean(requestedWith === "xmlhttprequest" || accept.includes("application/json") || accept.includes("application/xml") || path?.startsWith("/api/"));
|
|
22
|
+
}
|
|
23
|
+
function resolveResponseMeta(value, options = {}) {
|
|
24
|
+
if (typeof value === "undefined") return void 0;
|
|
25
|
+
if (isFetchResponse(value) || isH3Response(value)) return {
|
|
26
|
+
body: value,
|
|
27
|
+
status: getStatus(value, options.method, options.status),
|
|
28
|
+
isEmpty: false,
|
|
29
|
+
isNativeResponse: true
|
|
30
|
+
};
|
|
31
|
+
const status = getStatus(value, String(options.method || "get").toLowerCase(), options.status);
|
|
32
|
+
if (value === null) return {
|
|
33
|
+
body: null,
|
|
34
|
+
status,
|
|
35
|
+
isEmpty: true,
|
|
36
|
+
isNativeResponse: false
|
|
37
|
+
};
|
|
38
|
+
if (typeof value === "string") return {
|
|
39
|
+
body: value,
|
|
40
|
+
status,
|
|
41
|
+
contentType: inferStringContentType(value, isApiRequest(options.headers, options.path)),
|
|
42
|
+
isEmpty: false,
|
|
43
|
+
isNativeResponse: false
|
|
44
|
+
};
|
|
45
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") return {
|
|
46
|
+
body: String(value),
|
|
47
|
+
status,
|
|
48
|
+
contentType: "text/plain; charset=utf-8",
|
|
49
|
+
isEmpty: false,
|
|
50
|
+
isNativeResponse: false
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
body: value,
|
|
54
|
+
status,
|
|
55
|
+
contentType: "application/json; charset=utf-8",
|
|
56
|
+
isEmpty: false,
|
|
57
|
+
isNativeResponse: false
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function getStatus(value, method, explicitStatus) {
|
|
61
|
+
if (typeof value?.status === "number" && value.status >= 100 && value.status <= 999) return value.status;
|
|
62
|
+
if (typeof explicitStatus === "number" && explicitStatus >= 100 && explicitStatus <= 999) return explicitStatus;
|
|
63
|
+
return String(method || "").toLowerCase() === "post" ? 201 : 200;
|
|
64
|
+
}
|
|
65
|
+
function inferStringContentType(value, apiRequest) {
|
|
66
|
+
if (apiRequest) return "text/plain; charset=utf-8";
|
|
67
|
+
const trimmed = value.trimStart().toLowerCase();
|
|
68
|
+
if (trimmed.startsWith("<!doctype html") || trimmed.startsWith("<html") || trimmed.startsWith("<")) return "text/html; charset=utf-8";
|
|
69
|
+
return "text/plain; charset=utf-8";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
export { resolveResponseMeta as n, responseWasSent as r, isFetchResponse as t };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/core/responses.ts
|
|
3
|
+
function isFetchResponse(value) {
|
|
4
|
+
return typeof Response !== "undefined" && value instanceof Response;
|
|
5
|
+
}
|
|
6
|
+
function isH3Response(value) {
|
|
7
|
+
return Boolean(value && typeof value === "object" && value.constructor?.name === "HTTPResponse");
|
|
8
|
+
}
|
|
9
|
+
function responseWasSent(target) {
|
|
10
|
+
return Boolean(target?.headersSent || target?.sent || target?.raw?.headersSent);
|
|
11
|
+
}
|
|
12
|
+
function getHeader(headers, name) {
|
|
13
|
+
if (!headers) return "";
|
|
14
|
+
if (headers instanceof Headers) return headers.get(name) || "";
|
|
15
|
+
const lowerName = name.toLowerCase();
|
|
16
|
+
const value = headers[name] ?? headers[lowerName];
|
|
17
|
+
return Array.isArray(value) ? String(value[0] ?? "") : String(value ?? "");
|
|
18
|
+
}
|
|
19
|
+
function isApiRequest(headers, path) {
|
|
20
|
+
const accept = getHeader(headers, "accept").toLowerCase();
|
|
21
|
+
const requestedWith = getHeader(headers, "x-requested-with").toLowerCase();
|
|
22
|
+
return Boolean(requestedWith === "xmlhttprequest" || accept.includes("application/json") || accept.includes("application/xml") || path?.startsWith("/api/"));
|
|
23
|
+
}
|
|
24
|
+
function resolveResponseMeta(value, options = {}) {
|
|
25
|
+
if (typeof value === "undefined") return void 0;
|
|
26
|
+
if (isFetchResponse(value) || isH3Response(value)) return {
|
|
27
|
+
body: value,
|
|
28
|
+
status: getStatus(value, options.method, options.status),
|
|
29
|
+
isEmpty: false,
|
|
30
|
+
isNativeResponse: true
|
|
31
|
+
};
|
|
32
|
+
const status = getStatus(value, String(options.method || "get").toLowerCase(), options.status);
|
|
33
|
+
if (value === null) return {
|
|
34
|
+
body: null,
|
|
35
|
+
status,
|
|
36
|
+
isEmpty: true,
|
|
37
|
+
isNativeResponse: false
|
|
38
|
+
};
|
|
39
|
+
if (typeof value === "string") return {
|
|
40
|
+
body: value,
|
|
41
|
+
status,
|
|
42
|
+
contentType: inferStringContentType(value, isApiRequest(options.headers, options.path)),
|
|
43
|
+
isEmpty: false,
|
|
44
|
+
isNativeResponse: false
|
|
45
|
+
};
|
|
46
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") return {
|
|
47
|
+
body: String(value),
|
|
48
|
+
status,
|
|
49
|
+
contentType: "text/plain; charset=utf-8",
|
|
50
|
+
isEmpty: false,
|
|
51
|
+
isNativeResponse: false
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
body: value,
|
|
55
|
+
status,
|
|
56
|
+
contentType: "application/json; charset=utf-8",
|
|
57
|
+
isEmpty: false,
|
|
58
|
+
isNativeResponse: false
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function getStatus(value, method, explicitStatus) {
|
|
62
|
+
if (typeof value?.status === "number" && value.status >= 100 && value.status <= 999) return value.status;
|
|
63
|
+
if (typeof explicitStatus === "number" && explicitStatus >= 100 && explicitStatus <= 999) return explicitStatus;
|
|
64
|
+
return String(method || "").toLowerCase() === "post" ? 201 : 200;
|
|
65
|
+
}
|
|
66
|
+
function inferStringContentType(value, apiRequest) {
|
|
67
|
+
if (apiRequest) return "text/plain; charset=utf-8";
|
|
68
|
+
const trimmed = value.trimStart().toLowerCase();
|
|
69
|
+
if (trimmed.startsWith("<!doctype html") || trimmed.startsWith("<html") || trimmed.startsWith("<")) return "text/html; charset=utf-8";
|
|
70
|
+
return "text/plain; charset=utf-8";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
Object.defineProperty(exports, 'isFetchResponse', {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
get: function () {
|
|
77
|
+
return isFetchResponse;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(exports, 'resolveResponseMeta', {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
get: function () {
|
|
83
|
+
return resolveResponseMeta;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
Object.defineProperty(exports, 'responseWasSent', {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
get: function () {
|
|
89
|
+
return responseWasSent;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
@@ -68,7 +68,7 @@ type Handler$1 = RouteHandler$1 | ControllerHandler;
|
|
|
68
68
|
type Middleware$1 = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
|
|
69
69
|
//#endregion
|
|
70
70
|
//#region types/h3.d.ts
|
|
71
|
-
type H3App = Omit<H3
|
|
71
|
+
type H3App = Omit<H3, 'fetch'> & {
|
|
72
72
|
fetch: (request: TypedServerRequest<EventHandlerRequest>) => Promise<Response>;
|
|
73
73
|
};
|
|
74
74
|
interface HttpRequest extends TypedServerRequest<EventHandlerRequest> {
|
|
@@ -68,7 +68,7 @@ type Handler$1 = RouteHandler$1 | ControllerHandler;
|
|
|
68
68
|
type Middleware$1 = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
|
|
69
69
|
//#endregion
|
|
70
70
|
//#region types/h3.d.ts
|
|
71
|
-
type H3App = Omit<H3
|
|
71
|
+
type H3App = Omit<H3, 'fetch'> & {
|
|
72
72
|
fetch: (request: TypedServerRequest<EventHandlerRequest>) => Promise<Response>;
|
|
73
73
|
};
|
|
74
74
|
interface HttpRequest extends TypedServerRequest<EventHandlerRequest> {
|
package/dist/types/h3.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ import { ClearRequest } from "./ClearRequest.mjs";
|
|
|
3
3
|
import { EventHandlerRequest, H3, H3Event, Middleware, TypedServerRequest } from "h3";
|
|
4
4
|
|
|
5
5
|
//#region types/h3.d.ts
|
|
6
|
-
type H3App = Omit<H3
|
|
6
|
+
type H3App = Omit<H3, 'fetch'> & {
|
|
7
7
|
fetch: (request: TypedServerRequest<EventHandlerRequest>) => Promise<Response>;
|
|
8
8
|
};
|
|
9
9
|
type MaybePromise<T = unknown> = T | Promise<T>;
|
package/package.json
CHANGED