@kerebron/extension-server-hono 0.4.5 → 0.4.6
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/esm/HonoYjsMemAdapter.d.ts +2 -1
- package/esm/HonoYjsMemAdapter.d.ts.map +1 -1
- package/esm/HonoYjsMemAdapter.js +1 -0
- package/esm/_dnt.polyfills.d.ts +7 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/compose.d.ts +15 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/compose.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/compose.js +64 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/context.d.ts +452 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/context.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/context.js +513 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/helper/websocket/index.d.ts +70 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/helper/websocket/index.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/helper/websocket/index.js +95 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/hono-base.d.ts +221 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/hono-base.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/hono-base.js +498 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/http-exception.d.ts +60 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/http-exception.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/http-exception.js +71 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/request/constants.d.ts +2 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/request/constants.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/request/constants.js +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/request.d.ts +314 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/request.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/request.js +370 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/router.d.ts +98 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/router.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/router.js +25 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/types.d.ts +563 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/types.js +11 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/body.d.ts +80 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/body.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/body.js +111 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/constants.d.ts +5 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/constants.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/constants.js +4 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/headers.d.ts +9 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/headers.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/headers.js +5 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/html.d.ts +45 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/html.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/html.js +122 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/http-status.d.ts +33 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/http-status.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/http-status.js +5 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/mime.d.ts +71 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/mime.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/mime.js +83 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/types.d.ts +75 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/types.js +5 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/url.d.ts +30 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/url.d.ts.map +1 -0
- package/esm/deps/jsr.io/@hono/hono/4.10.7/src/utils/url.js +255 -0
- package/esm/mod.d.ts +2 -1
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +1 -1
- package/package.json +3 -1
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
* This module is the base module for the Hono object.
|
|
4
|
+
*/
|
|
5
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
6
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
7
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
8
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
9
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
12
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
13
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
14
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
15
|
+
};
|
|
16
|
+
var _Hono_instances, _a, _Hono_path, _Hono_clone, _Hono_notFoundHandler, _Hono_addRoute, _Hono_handleError, _Hono_dispatch;
|
|
17
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
18
|
+
import { compose } from './compose.js';
|
|
19
|
+
import { Context } from './context.js';
|
|
20
|
+
import { METHODS, METHOD_NAME_ALL, METHOD_NAME_ALL_LOWERCASE } from './router.js';
|
|
21
|
+
import { COMPOSED_HANDLER } from './utils/constants.js';
|
|
22
|
+
import { getPath, getPathNoStrict, mergePath } from './utils/url.js';
|
|
23
|
+
const notFoundHandler = (c) => {
|
|
24
|
+
return c.text('404 Not Found', 404);
|
|
25
|
+
};
|
|
26
|
+
const errorHandler = (err, c) => {
|
|
27
|
+
if ('getResponse' in err) {
|
|
28
|
+
const res = err.getResponse();
|
|
29
|
+
return c.newResponse(res.body, res);
|
|
30
|
+
}
|
|
31
|
+
console.error(err);
|
|
32
|
+
return c.text('Internal Server Error', 500);
|
|
33
|
+
};
|
|
34
|
+
class Hono {
|
|
35
|
+
constructor(options = {}) {
|
|
36
|
+
_Hono_instances.add(this);
|
|
37
|
+
Object.defineProperty(this, "get", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
writable: true,
|
|
41
|
+
value: void 0
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(this, "post", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
writable: true,
|
|
47
|
+
value: void 0
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(this, "put", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
value: void 0
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(this, "delete", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: void 0
|
|
60
|
+
});
|
|
61
|
+
Object.defineProperty(this, "options", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: void 0
|
|
66
|
+
});
|
|
67
|
+
Object.defineProperty(this, "patch", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: void 0
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(this, "all", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: void 0
|
|
78
|
+
});
|
|
79
|
+
Object.defineProperty(this, "on", {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
configurable: true,
|
|
82
|
+
writable: true,
|
|
83
|
+
value: void 0
|
|
84
|
+
});
|
|
85
|
+
Object.defineProperty(this, "use", {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
configurable: true,
|
|
88
|
+
writable: true,
|
|
89
|
+
value: void 0
|
|
90
|
+
});
|
|
91
|
+
/*
|
|
92
|
+
This class is like an abstract class and does not have a router.
|
|
93
|
+
To use it, inherit the class and implement router in the constructor.
|
|
94
|
+
*/
|
|
95
|
+
Object.defineProperty(this, "router", {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true,
|
|
99
|
+
value: void 0
|
|
100
|
+
});
|
|
101
|
+
Object.defineProperty(this, "getPath", {
|
|
102
|
+
enumerable: true,
|
|
103
|
+
configurable: true,
|
|
104
|
+
writable: true,
|
|
105
|
+
value: void 0
|
|
106
|
+
});
|
|
107
|
+
// Cannot use `#` because it requires visibility at JavaScript runtime.
|
|
108
|
+
Object.defineProperty(this, "_basePath", {
|
|
109
|
+
enumerable: true,
|
|
110
|
+
configurable: true,
|
|
111
|
+
writable: true,
|
|
112
|
+
value: '/'
|
|
113
|
+
});
|
|
114
|
+
_Hono_path.set(this, '/');
|
|
115
|
+
Object.defineProperty(this, "routes", {
|
|
116
|
+
enumerable: true,
|
|
117
|
+
configurable: true,
|
|
118
|
+
writable: true,
|
|
119
|
+
value: []
|
|
120
|
+
});
|
|
121
|
+
_Hono_notFoundHandler.set(this, notFoundHandler
|
|
122
|
+
// Cannot use `#` because it requires visibility at JavaScript runtime.
|
|
123
|
+
);
|
|
124
|
+
// Cannot use `#` because it requires visibility at JavaScript runtime.
|
|
125
|
+
Object.defineProperty(this, "errorHandler", {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
configurable: true,
|
|
128
|
+
writable: true,
|
|
129
|
+
value: errorHandler
|
|
130
|
+
});
|
|
131
|
+
/**
|
|
132
|
+
* `.onError()` handles an error and returns a customized Response.
|
|
133
|
+
*
|
|
134
|
+
* @see {@link https://hono.dev/docs/api/hono#error-handling}
|
|
135
|
+
*
|
|
136
|
+
* @param {ErrorHandler} handler - request Handler for error
|
|
137
|
+
* @returns {Hono} changed Hono instance
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* app.onError((err, c) => {
|
|
142
|
+
* console.error(`${err}`)
|
|
143
|
+
* return c.text('Custom Error Message', 500)
|
|
144
|
+
* })
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
Object.defineProperty(this, "onError", {
|
|
148
|
+
enumerable: true,
|
|
149
|
+
configurable: true,
|
|
150
|
+
writable: true,
|
|
151
|
+
value: (handler) => {
|
|
152
|
+
this.errorHandler = handler;
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
/**
|
|
157
|
+
* `.notFound()` allows you to customize a Not Found Response.
|
|
158
|
+
*
|
|
159
|
+
* @see {@link https://hono.dev/docs/api/hono#not-found}
|
|
160
|
+
*
|
|
161
|
+
* @param {NotFoundHandler} handler - request handler for not-found
|
|
162
|
+
* @returns {Hono} changed Hono instance
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* app.notFound((c) => {
|
|
167
|
+
* return c.text('Custom 404 Message', 404)
|
|
168
|
+
* })
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
Object.defineProperty(this, "notFound", {
|
|
172
|
+
enumerable: true,
|
|
173
|
+
configurable: true,
|
|
174
|
+
writable: true,
|
|
175
|
+
value: (handler) => {
|
|
176
|
+
__classPrivateFieldSet(this, _Hono_notFoundHandler, handler, "f");
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
/**
|
|
181
|
+
* `.fetch()` will be entry point of your app.
|
|
182
|
+
*
|
|
183
|
+
* @see {@link https://hono.dev/docs/api/hono#fetch}
|
|
184
|
+
*
|
|
185
|
+
* @param {Request} request - request Object of request
|
|
186
|
+
* @param {Env} Env - env Object
|
|
187
|
+
* @param {ExecutionContext} - context of execution
|
|
188
|
+
* @returns {Response | Promise<Response>} response of request
|
|
189
|
+
*
|
|
190
|
+
*/
|
|
191
|
+
Object.defineProperty(this, "fetch", {
|
|
192
|
+
enumerable: true,
|
|
193
|
+
configurable: true,
|
|
194
|
+
writable: true,
|
|
195
|
+
value: (request, ...rest) => {
|
|
196
|
+
return __classPrivateFieldGet(this, _Hono_instances, "m", _Hono_dispatch).call(this, request, rest[1], rest[0], request.method);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
/**
|
|
200
|
+
* `.request()` is a useful method for testing.
|
|
201
|
+
* You can pass a URL or pathname to send a GET request.
|
|
202
|
+
* app will return a Response object.
|
|
203
|
+
* ```ts
|
|
204
|
+
* test('GET /hello is ok', async () => {
|
|
205
|
+
* const res = await app.request('/hello')
|
|
206
|
+
* expect(res.status).toBe(200)
|
|
207
|
+
* })
|
|
208
|
+
* ```
|
|
209
|
+
* @see https://hono.dev/docs/api/hono#request
|
|
210
|
+
*/
|
|
211
|
+
Object.defineProperty(this, "request", {
|
|
212
|
+
enumerable: true,
|
|
213
|
+
configurable: true,
|
|
214
|
+
writable: true,
|
|
215
|
+
value: (input, requestInit, Env, executionCtx) => {
|
|
216
|
+
if (input instanceof Request) {
|
|
217
|
+
return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
218
|
+
}
|
|
219
|
+
input = input.toString();
|
|
220
|
+
return this.fetch(new Request(/^https?:\/\//.test(input) ? input : `http://localhost${mergePath('/', input)}`, requestInit), Env, executionCtx);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
/**
|
|
224
|
+
* `.fire()` automatically adds a global fetch event listener.
|
|
225
|
+
* This can be useful for environments that adhere to the Service Worker API, such as non-ES module Cloudflare Workers.
|
|
226
|
+
* @deprecated
|
|
227
|
+
* Use `fire` from `hono/service-worker` instead.
|
|
228
|
+
* ```ts
|
|
229
|
+
* import { Hono } from 'hono'
|
|
230
|
+
* import { fire } from 'hono/service-worker'
|
|
231
|
+
*
|
|
232
|
+
* const app = new Hono()
|
|
233
|
+
* // ...
|
|
234
|
+
* fire(app)
|
|
235
|
+
* ```
|
|
236
|
+
* @see https://hono.dev/docs/api/hono#fire
|
|
237
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
|
|
238
|
+
* @see https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/
|
|
239
|
+
*/
|
|
240
|
+
Object.defineProperty(this, "fire", {
|
|
241
|
+
enumerable: true,
|
|
242
|
+
configurable: true,
|
|
243
|
+
writable: true,
|
|
244
|
+
value: () => {
|
|
245
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
246
|
+
// @ts-ignore
|
|
247
|
+
addEventListener('fetch', (event) => {
|
|
248
|
+
event.respondWith(__classPrivateFieldGet(this, _Hono_instances, "m", _Hono_dispatch).call(this, event.request, event, undefined, event.request.method));
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
// Implementation of app.get(...handlers[]) or app.get(path, ...handlers[])
|
|
253
|
+
const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE];
|
|
254
|
+
allMethods.forEach((method) => {
|
|
255
|
+
this[method] = (args1, ...args) => {
|
|
256
|
+
if (typeof args1 === 'string') {
|
|
257
|
+
__classPrivateFieldSet(this, _Hono_path, args1, "f");
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
__classPrivateFieldGet(this, _Hono_instances, "m", _Hono_addRoute).call(this, method, __classPrivateFieldGet(this, _Hono_path, "f"), args1);
|
|
261
|
+
}
|
|
262
|
+
args.forEach((handler) => {
|
|
263
|
+
__classPrivateFieldGet(this, _Hono_instances, "m", _Hono_addRoute).call(this, method, __classPrivateFieldGet(this, _Hono_path, "f"), handler);
|
|
264
|
+
});
|
|
265
|
+
return this;
|
|
266
|
+
};
|
|
267
|
+
});
|
|
268
|
+
// Implementation of app.on(method, path, ...handlers[])
|
|
269
|
+
this.on = (method, path, ...handlers) => {
|
|
270
|
+
for (const p of [path].flat()) {
|
|
271
|
+
__classPrivateFieldSet(this, _Hono_path, p, "f");
|
|
272
|
+
for (const m of [method].flat()) {
|
|
273
|
+
handlers.map((handler) => {
|
|
274
|
+
__classPrivateFieldGet(this, _Hono_instances, "m", _Hono_addRoute).call(this, m.toUpperCase(), __classPrivateFieldGet(this, _Hono_path, "f"), handler);
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return this;
|
|
279
|
+
};
|
|
280
|
+
// Implementation of app.use(...handlers[]) or app.use(path, ...handlers[])
|
|
281
|
+
this.use = (arg1, ...handlers) => {
|
|
282
|
+
if (typeof arg1 === 'string') {
|
|
283
|
+
__classPrivateFieldSet(this, _Hono_path, arg1, "f");
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
__classPrivateFieldSet(this, _Hono_path, '*', "f");
|
|
287
|
+
handlers.unshift(arg1);
|
|
288
|
+
}
|
|
289
|
+
handlers.forEach((handler) => {
|
|
290
|
+
__classPrivateFieldGet(this, _Hono_instances, "m", _Hono_addRoute).call(this, METHOD_NAME_ALL, __classPrivateFieldGet(this, _Hono_path, "f"), handler);
|
|
291
|
+
});
|
|
292
|
+
return this;
|
|
293
|
+
};
|
|
294
|
+
const { strict, ...optionsWithoutStrict } = options;
|
|
295
|
+
Object.assign(this, optionsWithoutStrict);
|
|
296
|
+
this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* `.route()` allows grouping other Hono instance in routes.
|
|
300
|
+
*
|
|
301
|
+
* @see {@link https://hono.dev/docs/api/routing#grouping}
|
|
302
|
+
*
|
|
303
|
+
* @param {string} path - base Path
|
|
304
|
+
* @param {Hono} app - other Hono instance
|
|
305
|
+
* @returns {Hono} routed Hono instance
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* ```ts
|
|
309
|
+
* const app = new Hono()
|
|
310
|
+
* const app2 = new Hono()
|
|
311
|
+
*
|
|
312
|
+
* app2.get("/user", (c) => c.text("user"))
|
|
313
|
+
* app.route("/api", app2) // GET /api/user
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
route(path, app) {
|
|
317
|
+
const subApp = this.basePath(path);
|
|
318
|
+
app.routes.map((r) => {
|
|
319
|
+
let handler;
|
|
320
|
+
if (app.errorHandler === errorHandler) {
|
|
321
|
+
handler = r.handler;
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
handler = async (c, next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res;
|
|
325
|
+
handler[COMPOSED_HANDLER] = r.handler;
|
|
326
|
+
}
|
|
327
|
+
__classPrivateFieldGet(subApp, _Hono_instances, "m", _Hono_addRoute).call(subApp, r.method, r.path, handler);
|
|
328
|
+
});
|
|
329
|
+
return this;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* `.basePath()` allows base paths to be specified.
|
|
333
|
+
*
|
|
334
|
+
* @see {@link https://hono.dev/docs/api/routing#base-path}
|
|
335
|
+
*
|
|
336
|
+
* @param {string} path - base Path
|
|
337
|
+
* @returns {Hono} changed Hono instance
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```ts
|
|
341
|
+
* const api = new Hono().basePath('/api')
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
344
|
+
basePath(path) {
|
|
345
|
+
const subApp = __classPrivateFieldGet(this, _Hono_instances, "m", _Hono_clone).call(this);
|
|
346
|
+
subApp._basePath = mergePath(this._basePath, path);
|
|
347
|
+
return subApp;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* `.mount()` allows you to mount applications built with other frameworks into your Hono application.
|
|
351
|
+
*
|
|
352
|
+
* @see {@link https://hono.dev/docs/api/hono#mount}
|
|
353
|
+
*
|
|
354
|
+
* @param {string} path - base Path
|
|
355
|
+
* @param {Function} applicationHandler - other Request Handler
|
|
356
|
+
* @param {MountOptions} [options] - options of `.mount()`
|
|
357
|
+
* @returns {Hono} mounted Hono instance
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```ts
|
|
361
|
+
* import { Router as IttyRouter } from 'itty-router'
|
|
362
|
+
* import { Hono } from 'hono'
|
|
363
|
+
* // Create itty-router application
|
|
364
|
+
* const ittyRouter = IttyRouter()
|
|
365
|
+
* // GET /itty-router/hello
|
|
366
|
+
* ittyRouter.get('/hello', () => new Response('Hello from itty-router'))
|
|
367
|
+
*
|
|
368
|
+
* const app = new Hono()
|
|
369
|
+
* app.mount('/itty-router', ittyRouter.handle)
|
|
370
|
+
* ```
|
|
371
|
+
*
|
|
372
|
+
* @example
|
|
373
|
+
* ```ts
|
|
374
|
+
* const app = new Hono()
|
|
375
|
+
* // Send the request to another application without modification.
|
|
376
|
+
* app.mount('/app', anotherApp, {
|
|
377
|
+
* replaceRequest: (req) => req,
|
|
378
|
+
* })
|
|
379
|
+
* ```
|
|
380
|
+
*/
|
|
381
|
+
mount(path, applicationHandler, options) {
|
|
382
|
+
// handle options
|
|
383
|
+
let replaceRequest;
|
|
384
|
+
let optionHandler;
|
|
385
|
+
if (options) {
|
|
386
|
+
if (typeof options === 'function') {
|
|
387
|
+
optionHandler = options;
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
optionHandler = options.optionHandler;
|
|
391
|
+
if (options.replaceRequest === false) {
|
|
392
|
+
replaceRequest = (request) => request;
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
replaceRequest = options.replaceRequest;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
// prepare handlers for request
|
|
400
|
+
const getOptions = optionHandler
|
|
401
|
+
? (c) => {
|
|
402
|
+
const options = optionHandler(c);
|
|
403
|
+
return Array.isArray(options) ? options : [options];
|
|
404
|
+
}
|
|
405
|
+
: (c) => {
|
|
406
|
+
let executionContext = undefined;
|
|
407
|
+
try {
|
|
408
|
+
executionContext = c.executionCtx;
|
|
409
|
+
}
|
|
410
|
+
catch { } // Do nothing
|
|
411
|
+
return [c.env, executionContext];
|
|
412
|
+
};
|
|
413
|
+
replaceRequest ||= (() => {
|
|
414
|
+
const mergedPath = mergePath(this._basePath, path);
|
|
415
|
+
const pathPrefixLength = mergedPath === '/' ? 0 : mergedPath.length;
|
|
416
|
+
return (request) => {
|
|
417
|
+
const url = new URL(request.url);
|
|
418
|
+
url.pathname = url.pathname.slice(pathPrefixLength) || '/';
|
|
419
|
+
return new Request(url, request);
|
|
420
|
+
};
|
|
421
|
+
})();
|
|
422
|
+
const handler = async (c, next) => {
|
|
423
|
+
const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c));
|
|
424
|
+
if (res) {
|
|
425
|
+
return res;
|
|
426
|
+
}
|
|
427
|
+
await next();
|
|
428
|
+
};
|
|
429
|
+
__classPrivateFieldGet(this, _Hono_instances, "m", _Hono_addRoute).call(this, METHOD_NAME_ALL, mergePath(path, '*'), handler);
|
|
430
|
+
return this;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
_a = Hono, _Hono_path = new WeakMap(), _Hono_notFoundHandler = new WeakMap(), _Hono_instances = new WeakSet(), _Hono_clone = function _Hono_clone() {
|
|
434
|
+
const clone = new _a({
|
|
435
|
+
router: this.router,
|
|
436
|
+
getPath: this.getPath,
|
|
437
|
+
});
|
|
438
|
+
clone.errorHandler = this.errorHandler;
|
|
439
|
+
__classPrivateFieldSet(clone, _Hono_notFoundHandler, __classPrivateFieldGet(this, _Hono_notFoundHandler, "f"), "f");
|
|
440
|
+
clone.routes = this.routes;
|
|
441
|
+
return clone;
|
|
442
|
+
}, _Hono_addRoute = function _Hono_addRoute(method, path, handler) {
|
|
443
|
+
method = method.toUpperCase();
|
|
444
|
+
path = mergePath(this._basePath, path);
|
|
445
|
+
const r = { basePath: this._basePath, path, method, handler };
|
|
446
|
+
this.router.add(method, path, [handler, r]);
|
|
447
|
+
this.routes.push(r);
|
|
448
|
+
}, _Hono_handleError = function _Hono_handleError(err, c) {
|
|
449
|
+
if (err instanceof Error) {
|
|
450
|
+
return this.errorHandler(err, c);
|
|
451
|
+
}
|
|
452
|
+
throw err;
|
|
453
|
+
}, _Hono_dispatch = function _Hono_dispatch(request, executionCtx, env, method) {
|
|
454
|
+
// Handle HEAD method
|
|
455
|
+
if (method === 'HEAD') {
|
|
456
|
+
return (async () => new Response(null, await __classPrivateFieldGet(this, _Hono_instances, "m", _Hono_dispatch).call(this, request, executionCtx, env, 'GET')))();
|
|
457
|
+
}
|
|
458
|
+
const path = this.getPath(request, { env });
|
|
459
|
+
const matchResult = this.router.match(method, path);
|
|
460
|
+
const c = new Context(request, {
|
|
461
|
+
path,
|
|
462
|
+
matchResult,
|
|
463
|
+
env,
|
|
464
|
+
executionCtx,
|
|
465
|
+
notFoundHandler: __classPrivateFieldGet(this, _Hono_notFoundHandler, "f"),
|
|
466
|
+
});
|
|
467
|
+
// Do not `compose` if it has only one handler
|
|
468
|
+
if (matchResult[0].length === 1) {
|
|
469
|
+
let res;
|
|
470
|
+
try {
|
|
471
|
+
res = matchResult[0][0][0][0](c, async () => {
|
|
472
|
+
c.res = await __classPrivateFieldGet(this, _Hono_notFoundHandler, "f").call(this, c);
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
catch (err) {
|
|
476
|
+
return __classPrivateFieldGet(this, _Hono_instances, "m", _Hono_handleError).call(this, err, c);
|
|
477
|
+
}
|
|
478
|
+
return res instanceof Promise
|
|
479
|
+
? res
|
|
480
|
+
.then((resolved) => resolved || (c.finalized ? c.res : __classPrivateFieldGet(this, _Hono_notFoundHandler, "f").call(this, c)))
|
|
481
|
+
.catch((err) => __classPrivateFieldGet(this, _Hono_instances, "m", _Hono_handleError).call(this, err, c))
|
|
482
|
+
: res ?? __classPrivateFieldGet(this, _Hono_notFoundHandler, "f").call(this, c);
|
|
483
|
+
}
|
|
484
|
+
const composed = compose(matchResult[0], this.errorHandler, __classPrivateFieldGet(this, _Hono_notFoundHandler, "f"));
|
|
485
|
+
return (async () => {
|
|
486
|
+
try {
|
|
487
|
+
const context = await composed(c);
|
|
488
|
+
if (!context.finalized) {
|
|
489
|
+
throw new Error('Context is not finalized. Did you forget to return a Response object or `await next()`?');
|
|
490
|
+
}
|
|
491
|
+
return context.res;
|
|
492
|
+
}
|
|
493
|
+
catch (err) {
|
|
494
|
+
return __classPrivateFieldGet(this, _Hono_instances, "m", _Hono_handleError).call(this, err, c);
|
|
495
|
+
}
|
|
496
|
+
})();
|
|
497
|
+
};
|
|
498
|
+
export { Hono as HonoBase };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
* This module provides the `HTTPException` class.
|
|
4
|
+
*/
|
|
5
|
+
import type { ContentfulStatusCode } from './utils/http-status.js';
|
|
6
|
+
/**
|
|
7
|
+
* Options for creating an `HTTPException`.
|
|
8
|
+
* @property res - Optional response object to use.
|
|
9
|
+
* @property message - Optional custom error message.
|
|
10
|
+
* @property cause - Optional cause of the error.
|
|
11
|
+
*/
|
|
12
|
+
type HTTPExceptionOptions = {
|
|
13
|
+
res?: Response;
|
|
14
|
+
message?: string;
|
|
15
|
+
cause?: unknown;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* `HTTPException` must be used when a fatal error such as authentication failure occurs.
|
|
19
|
+
*
|
|
20
|
+
* @see {@link https://hono.dev/docs/api/exception}
|
|
21
|
+
*
|
|
22
|
+
* @param {StatusCode} status - status code of HTTPException
|
|
23
|
+
* @param {HTTPExceptionOptions} options - options of HTTPException
|
|
24
|
+
* @param {HTTPExceptionOptions["res"]} options.res - response of options of HTTPException
|
|
25
|
+
* @param {HTTPExceptionOptions["message"]} options.message - message of options of HTTPException
|
|
26
|
+
* @param {HTTPExceptionOptions["cause"]} options.cause - cause of options of HTTPException
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { HTTPException } from 'hono/http-exception'
|
|
31
|
+
*
|
|
32
|
+
* // ...
|
|
33
|
+
*
|
|
34
|
+
* app.post('/auth', async (c, next) => {
|
|
35
|
+
* // authentication
|
|
36
|
+
* if (authorized === false) {
|
|
37
|
+
* throw new HTTPException(401, { message: 'Custom error message' })
|
|
38
|
+
* }
|
|
39
|
+
* await next()
|
|
40
|
+
* })
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare class HTTPException extends Error {
|
|
44
|
+
readonly res?: Response;
|
|
45
|
+
readonly status: ContentfulStatusCode;
|
|
46
|
+
/**
|
|
47
|
+
* Creates an instance of `HTTPException`.
|
|
48
|
+
* @param status - HTTP status code for the exception. Defaults to 500.
|
|
49
|
+
* @param options - Additional options for the exception.
|
|
50
|
+
*/
|
|
51
|
+
constructor(status?: ContentfulStatusCode, options?: HTTPExceptionOptions);
|
|
52
|
+
/**
|
|
53
|
+
* Returns the response object associated with the exception.
|
|
54
|
+
* If a response object is not provided, a new response is created with the error message and status code.
|
|
55
|
+
* @returns The response object.
|
|
56
|
+
*/
|
|
57
|
+
getResponse(): Response;
|
|
58
|
+
}
|
|
59
|
+
export {};
|
|
60
|
+
//# sourceMappingURL=http-exception.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-exception.d.ts","sourceRoot":"","sources":["../../../../../../../src/deps/jsr.io/@hono/hono/4.10.7/src/http-exception.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAElE;;;;;GAKG;AACH,KAAK,oBAAoB,GAAG;IAC1B,GAAG,CAAC,EAAE,QAAQ,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,aAAc,SAAQ,KAAK;IACtC,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAA;IACvB,QAAQ,CAAC,MAAM,EAAE,oBAAoB,CAAA;IAErC;;;;OAIG;gBACS,MAAM,GAAE,oBAA0B,EAAE,OAAO,CAAC,EAAE,oBAAoB;IAM9E;;;;OAIG;IACH,WAAW,IAAI,QAAQ;CAYxB"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
* This module provides the `HTTPException` class.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* `HTTPException` must be used when a fatal error such as authentication failure occurs.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://hono.dev/docs/api/exception}
|
|
9
|
+
*
|
|
10
|
+
* @param {StatusCode} status - status code of HTTPException
|
|
11
|
+
* @param {HTTPExceptionOptions} options - options of HTTPException
|
|
12
|
+
* @param {HTTPExceptionOptions["res"]} options.res - response of options of HTTPException
|
|
13
|
+
* @param {HTTPExceptionOptions["message"]} options.message - message of options of HTTPException
|
|
14
|
+
* @param {HTTPExceptionOptions["cause"]} options.cause - cause of options of HTTPException
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { HTTPException } from 'hono/http-exception'
|
|
19
|
+
*
|
|
20
|
+
* // ...
|
|
21
|
+
*
|
|
22
|
+
* app.post('/auth', async (c, next) => {
|
|
23
|
+
* // authentication
|
|
24
|
+
* if (authorized === false) {
|
|
25
|
+
* throw new HTTPException(401, { message: 'Custom error message' })
|
|
26
|
+
* }
|
|
27
|
+
* await next()
|
|
28
|
+
* })
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export class HTTPException extends Error {
|
|
32
|
+
/**
|
|
33
|
+
* Creates an instance of `HTTPException`.
|
|
34
|
+
* @param status - HTTP status code for the exception. Defaults to 500.
|
|
35
|
+
* @param options - Additional options for the exception.
|
|
36
|
+
*/
|
|
37
|
+
constructor(status = 500, options) {
|
|
38
|
+
super(options?.message, { cause: options?.cause });
|
|
39
|
+
Object.defineProperty(this, "res", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
Object.defineProperty(this, "status", {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
configurable: true,
|
|
48
|
+
writable: true,
|
|
49
|
+
value: void 0
|
|
50
|
+
});
|
|
51
|
+
this.res = options?.res;
|
|
52
|
+
this.status = status;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns the response object associated with the exception.
|
|
56
|
+
* If a response object is not provided, a new response is created with the error message and status code.
|
|
57
|
+
* @returns The response object.
|
|
58
|
+
*/
|
|
59
|
+
getResponse() {
|
|
60
|
+
if (this.res) {
|
|
61
|
+
const newResponse = new Response(this.res.body, {
|
|
62
|
+
status: this.status,
|
|
63
|
+
headers: this.res.headers,
|
|
64
|
+
});
|
|
65
|
+
return newResponse;
|
|
66
|
+
}
|
|
67
|
+
return new Response(this.message, {
|
|
68
|
+
status: this.status,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../../../../../src/deps/jsr.io/@hono/hono/4.10.7/src/request/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,EAAE,MAAiB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const GET_MATCH_RESULT = Symbol();
|