@kiyasov/platform-hono 1.3.8 → 1.3.9

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.3.8",
3
+ "version": "1.3.9",
4
4
  "description": "Nest adapter for Hono",
5
5
  "author": "Islam Kiiasov",
6
6
  "repository": {
@@ -139,8 +139,19 @@ export class HonoAdapter extends AbstractHttpAdapter<
139
139
  ctx.set("body", body);
140
140
  }
141
141
 
142
- public status(ctx: Context, statusCode: StatusCode) {
143
- ctx.status(statusCode);
142
+ public async status(
143
+ ctx: Context | (() => Promise<Context>),
144
+ statusCode: StatusCode
145
+ ) {
146
+ if (typeof ctx === "function") {
147
+ ctx = await ctx();
148
+ }
149
+
150
+ if (ctx && typeof ctx.status === "function") {
151
+ ctx.status(statusCode);
152
+ } else {
153
+ throw new Error("Invalid context: status method not found.");
154
+ }
144
155
  }
145
156
 
146
157
  public async end() {
@@ -151,8 +162,20 @@ export class HonoAdapter extends AbstractHttpAdapter<
151
162
  throw new Error("Method not implemented.");
152
163
  }
153
164
 
154
- public redirect(ctx: Context, statusCode: RedirectStatusCode, url: string) {
155
- ctx.redirect(url, statusCode);
165
+ public async redirect(
166
+ ctx: Context | (() => Promise<Context>),
167
+ statusCode: RedirectStatusCode,
168
+ url: string
169
+ ) {
170
+ if (typeof ctx === "function") {
171
+ ctx = await ctx();
172
+ }
173
+
174
+ if (ctx && typeof ctx.redirect === "function") {
175
+ ctx.redirect(url, statusCode);
176
+ } else {
177
+ throw new Error("Invalid context: redirect method not found.");
178
+ }
156
179
  }
157
180
 
158
181
  public setErrorHandler(handler: ErrorHandler) {
@@ -186,12 +209,36 @@ export class HonoAdapter extends AbstractHttpAdapter<
186
209
  return ctx.req.header(name);
187
210
  }
188
211
 
189
- public setHeader(ctx: Context, name: string, value: string) {
190
- ctx.header(name, value);
212
+ public async setHeader(
213
+ ctx: Context | (() => Promise<Context>),
214
+ name: string,
215
+ value: string
216
+ ) {
217
+ if (typeof ctx === "function") {
218
+ ctx = await ctx();
219
+ }
220
+
221
+ if (ctx && typeof ctx.header === "function") {
222
+ ctx.header(name, value);
223
+ } else {
224
+ throw new Error("Invalid context: set method not found on context.");
225
+ }
191
226
  }
192
227
 
193
- public appendHeader?(ctx: Context, name: string, value: string) {
194
- ctx.res.headers.append(name, value);
228
+ public async appendHeader?(
229
+ ctx: Context | (() => Promise<Context>),
230
+ name: string,
231
+ value: string
232
+ ) {
233
+ if (typeof ctx === "function") {
234
+ ctx = await ctx();
235
+ }
236
+
237
+ if (ctx && typeof ctx.res.headers.append === "function") {
238
+ ctx.res.headers.append(name, value);
239
+ } else {
240
+ throw new Error("Invalid context: append method not found on context.");
241
+ }
195
242
  }
196
243
 
197
244
  public getRequestHostname(ctx: Context): string {