@kiyasov/platform-hono 1.4.2 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kiyasov/platform-hono",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "Nest adapter for Hono",
5
5
  "author": "Islam Kiiasov",
6
6
  "repository": {
@@ -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 { Http2SecureServer, Http2Server } from 'http2';
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
- return this.send(ctx);
68
+
69
+ return ctx.res;
67
70
  };
68
71
  }
69
72
 
70
- private async send(ctx: Ctx) {
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) ctx.status(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.set('body', body);
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
- return this.send(ctx);
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
- return this.send(ctx);
227
+
228
+ return this.getBody(ctx);
220
229
  });
221
230
  }
222
231
 
@@ -24,27 +24,41 @@ export const handleMultipartMultipleFiles = async (
24
24
 
25
25
  try {
26
26
  for await (const [partFieldName, part] of Object.entries(parts)) {
27
- if (!(part instanceof File)) {
27
+ if (!(part instanceof File || Array.isArray(part))) {
28
28
  body[partFieldName] = part;
29
29
  continue;
30
30
  }
31
31
 
32
- if (partFieldName !== fieldname) {
33
- throw new BadRequestException(
34
- `Field ${partFieldName} doesn't accept files`,
35
- );
36
- }
32
+ const partArray = Array.isArray(part) ? part : [part];
37
33
 
38
- if (files.length + 1 > maxCount) {
39
- throw new BadRequestException(
40
- `Field ${partFieldName} accepts max ${maxCount} files`,
41
- );
42
- }
34
+ for (const singlePart of partArray) {
35
+ if (!(singlePart instanceof File)) {
36
+ throw new BadRequestException(
37
+ `Field ${partFieldName} contains invalid file data`,
38
+ );
39
+ }
43
40
 
44
- const file = await options.storage!.handleFile(part, req, partFieldName);
41
+ if (partFieldName !== fieldname) {
42
+ throw new BadRequestException(
43
+ `Field ${partFieldName} doesn't accept files`,
44
+ );
45
+ }
46
+
47
+ if (files.length >= maxCount) {
48
+ throw new BadRequestException(
49
+ `Field ${partFieldName} accepts max ${maxCount} files`,
50
+ );
51
+ }
52
+
53
+ const file = await options.storage!.handleFile(
54
+ singlePart,
55
+ req,
56
+ partFieldName,
57
+ );
45
58
 
46
- if (await filterUpload(options, req, file)) {
47
- files.push(file);
59
+ if (await filterUpload(options, req, file)) {
60
+ files.push(file);
61
+ }
48
62
  }
49
63
  }
50
64
  } catch (error) {