@kiyasov/platform-hono 1.4.3 → 1.4.4
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/cjs/src/adapters/hono-adapter.js +15 -1
- package/dist/cjs/src/adapters/hono-adapter.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/adapters/hono-adapter.js +15 -1
- package/dist/esm/src/adapters/hono-adapter.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/adapters/hono-adapter.ts +21 -12
package/package.json
CHANGED
|
@@ -15,17 +15,17 @@ import { AbstractHttpAdapter } from '@nestjs/core/adapters/http-adapter';
|
|
|
15
15
|
import { Context, Next, Hono } from 'hono';
|
|
16
16
|
import { bodyLimit } from 'hono/body-limit';
|
|
17
17
|
import { cors } from 'hono/cors';
|
|
18
|
+
import { Data } from 'hono/dist/types/context';
|
|
18
19
|
import { RedirectStatusCode, StatusCode } from 'hono/utils/http-status';
|
|
19
20
|
import * as http from 'http';
|
|
20
|
-
import
|
|
21
|
+
import http2 from 'http2';
|
|
21
22
|
import * as https from 'https';
|
|
22
|
-
import { Server } from 'node:net';
|
|
23
23
|
|
|
24
24
|
import { HonoRequest, TypeBodyParser } from '../interfaces';
|
|
25
25
|
|
|
26
26
|
type HonoHandler = RequestHandler<HonoRequest, Context>;
|
|
27
27
|
|
|
28
|
-
type ServerType = Server | Http2Server | Http2SecureServer;
|
|
28
|
+
type ServerType = http.Server | http2.Http2Server | http2.Http2SecureServer;
|
|
29
29
|
type Ctx = Context | (() => Promise<Context>);
|
|
30
30
|
|
|
31
31
|
/**
|
|
@@ -56,23 +56,25 @@ export class HonoAdapter extends AbstractHttpAdapter<
|
|
|
56
56
|
): [string, HonoHandler] {
|
|
57
57
|
const path = typeof pathOrHandler === 'function' ? '' : pathOrHandler;
|
|
58
58
|
handler = typeof pathOrHandler === 'function' ? pathOrHandler : handler;
|
|
59
|
+
|
|
59
60
|
return [path, handler];
|
|
60
61
|
}
|
|
61
62
|
|
|
62
63
|
private createRouteHandler(routeHandler: HonoHandler) {
|
|
63
64
|
return async (ctx: Context, next: Next) => {
|
|
64
65
|
ctx.req['params'] = ctx.req.param();
|
|
66
|
+
|
|
65
67
|
await routeHandler(ctx.req, ctx, next);
|
|
66
|
-
|
|
68
|
+
|
|
69
|
+
return ctx.res;
|
|
67
70
|
};
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
private async
|
|
73
|
+
private async getBody(ctx: Ctx, body?: Data) {
|
|
71
74
|
if (typeof ctx === 'function') {
|
|
72
75
|
ctx = await ctx();
|
|
73
76
|
}
|
|
74
77
|
|
|
75
|
-
const body = ctx.get('body');
|
|
76
78
|
let responseContentType = await this.getHeader(ctx, 'Content-Type');
|
|
77
79
|
|
|
78
80
|
if (!responseContentType || responseContentType.startsWith('text/plain')) {
|
|
@@ -90,6 +92,8 @@ export class HonoAdapter extends AbstractHttpAdapter<
|
|
|
90
92
|
typeof body === 'object'
|
|
91
93
|
) {
|
|
92
94
|
return ctx.json(body);
|
|
95
|
+
} else if (body === undefined) {
|
|
96
|
+
return ctx.newResponse(null);
|
|
93
97
|
}
|
|
94
98
|
|
|
95
99
|
return ctx.body(body);
|
|
@@ -108,6 +112,7 @@ export class HonoAdapter extends AbstractHttpAdapter<
|
|
|
108
112
|
pathOrHandler,
|
|
109
113
|
handler,
|
|
110
114
|
);
|
|
115
|
+
|
|
111
116
|
this.instance.get(routePath, this.createRouteHandler(routeHandler));
|
|
112
117
|
}
|
|
113
118
|
|
|
@@ -165,7 +170,9 @@ export class HonoAdapter extends AbstractHttpAdapter<
|
|
|
165
170
|
ctx = await ctx();
|
|
166
171
|
}
|
|
167
172
|
|
|
168
|
-
if (statusCode)
|
|
173
|
+
if (statusCode) {
|
|
174
|
+
ctx.status(statusCode);
|
|
175
|
+
}
|
|
169
176
|
|
|
170
177
|
const responseContentType = await this.getHeader(ctx, 'Content-Type');
|
|
171
178
|
|
|
@@ -179,7 +186,7 @@ export class HonoAdapter extends AbstractHttpAdapter<
|
|
|
179
186
|
this.setHeader(ctx, 'Content-Type', 'application/json');
|
|
180
187
|
}
|
|
181
188
|
|
|
182
|
-
ctx.
|
|
189
|
+
ctx.res = await this.getBody(ctx, body);
|
|
183
190
|
}
|
|
184
191
|
|
|
185
192
|
public async status(ctx: Ctx, statusCode: StatusCode) {
|
|
@@ -187,7 +194,7 @@ export class HonoAdapter extends AbstractHttpAdapter<
|
|
|
187
194
|
ctx = await ctx();
|
|
188
195
|
}
|
|
189
196
|
|
|
190
|
-
ctx.status(statusCode);
|
|
197
|
+
return ctx.status(statusCode);
|
|
191
198
|
}
|
|
192
199
|
|
|
193
200
|
public async end() {
|
|
@@ -203,20 +210,22 @@ export class HonoAdapter extends AbstractHttpAdapter<
|
|
|
203
210
|
ctx = await ctx();
|
|
204
211
|
}
|
|
205
212
|
|
|
206
|
-
ctx.redirect(url, statusCode);
|
|
213
|
+
ctx.res = ctx.redirect(url, statusCode);
|
|
207
214
|
}
|
|
208
215
|
|
|
209
216
|
public setErrorHandler(handler: ErrorHandler) {
|
|
210
217
|
this.instance.onError(async (err: Error, ctx: Context) => {
|
|
211
218
|
await handler(err, ctx.req, ctx);
|
|
212
|
-
|
|
219
|
+
|
|
220
|
+
return this.getBody(ctx);
|
|
213
221
|
});
|
|
214
222
|
}
|
|
215
223
|
|
|
216
224
|
public setNotFoundHandler(handler: RequestHandler) {
|
|
217
225
|
this.instance.notFound(async (ctx: Context) => {
|
|
218
226
|
await handler(ctx.req, ctx);
|
|
219
|
-
|
|
227
|
+
|
|
228
|
+
return this.getBody(ctx);
|
|
220
229
|
});
|
|
221
230
|
}
|
|
222
231
|
|