@kiyasov/platform-hono 1.0.2 → 1.0.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.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Nest adapter for Hono",
5
5
  "author": "Islam Kiiasov",
6
6
  "license": "MIT",
@@ -1,13 +1,13 @@
1
- import { ApolloServer, BaseContext, HeaderMap } from '@apollo/server';
2
- import { AbstractGraphQLDriver } from '@nestjs/graphql';
3
- import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
4
- import { ApolloDriverConfig } from '@nestjs/apollo';
5
- import { Context, HonoRequest } from 'hono';
6
- import { StatusCode } from 'hono/utils/http-status';
7
- import { Logger } from '@nestjs/common';
1
+ import { ApolloServer, BaseContext, HeaderMap } from "@apollo/server";
2
+ import { AbstractGraphQLDriver } from "@nestjs/graphql";
3
+ import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
4
+ import { ApolloDriverConfig } from "@nestjs/apollo";
5
+ import { Context, HonoRequest } from "hono";
6
+ import { StatusCode } from "hono/utils/http-status";
7
+ import { Logger } from "@nestjs/common";
8
8
 
9
9
  export class HonoGraphQLDriver<
10
- T extends Record<string, any> = ApolloDriverConfig,
10
+ T extends Record<string, any> = ApolloDriverConfig
11
11
  > extends AbstractGraphQLDriver {
12
12
  protected apolloServer: ApolloServer<BaseContext>;
13
13
 
@@ -19,8 +19,8 @@ export class HonoGraphQLDriver<
19
19
  const { httpAdapter } = this.httpAdapterHost;
20
20
  const platformName = httpAdapter.getType();
21
21
 
22
- if (platformName !== 'hono') {
23
- throw new Error('This driver is only compatible with the Hono platform');
22
+ if (platformName !== "hono") {
23
+ throw new Error("This driver is only compatible with the Hono platform");
24
24
  }
25
25
 
26
26
  return this.registerHono(options);
@@ -28,7 +28,7 @@ export class HonoGraphQLDriver<
28
28
 
29
29
  protected async registerHono(
30
30
  options: T,
31
- { preStartHook }: { preStartHook?: () => void } = {},
31
+ { preStartHook }: { preStartHook?: () => void } = {}
32
32
  ) {
33
33
  const { path, typeDefs, resolvers, schema } = options;
34
34
  const { httpAdapter } = this.httpAdapterHost;
@@ -52,6 +52,10 @@ export class HonoGraphQLDriver<
52
52
  app.use(path, async (ctx: Context) => {
53
53
  const bodyData = await this.parseBody(ctx.req);
54
54
 
55
+ const defaultContext = () => Promise.resolve({} as BaseContext);
56
+
57
+ const contextFunction = options?.context ?? defaultContext;
58
+
55
59
  const httpGraphQLResponse = await server.executeHTTPGraphQLRequest({
56
60
  httpGraphQLRequest: {
57
61
  body: bodyData,
@@ -59,7 +63,7 @@ export class HonoGraphQLDriver<
59
63
  headers: this.httpHeadersToMap(ctx.req.raw.headers),
60
64
  search: new URL(ctx.req.url).search,
61
65
  },
62
- context: options?.context ?? (() => Promise.resolve({} as BaseContext)),
66
+ context: () => contextFunction(ctx.req, ctx),
63
67
  });
64
68
 
65
69
  const { headers, body, status } = httpGraphQLResponse;
@@ -70,7 +74,7 @@ export class HonoGraphQLDriver<
70
74
 
71
75
  ctx.status(status === undefined ? 200 : (status as StatusCode));
72
76
 
73
- if (body.kind === 'complete') {
77
+ if (body.kind === "complete") {
74
78
  return ctx.body(body.string);
75
79
  }
76
80
 
@@ -84,7 +88,7 @@ export class HonoGraphQLDriver<
84
88
  });
85
89
 
86
90
  return new Response(readableStream, {
87
- headers: { 'Content-Type': 'application/octet-stream' },
91
+ headers: { "Content-Type": "application/octet-stream" },
88
92
  });
89
93
  });
90
94
 
@@ -102,12 +106,12 @@ export class HonoGraphQLDriver<
102
106
  }
103
107
 
104
108
  private async parseBody(req: HonoRequest): Promise<Record<string, unknown>> {
105
- const contentType = req.header('content-type');
106
- if (contentType === 'application/graphql')
109
+ const contentType = req.header("content-type");
110
+ if (contentType === "application/graphql")
107
111
  return { query: await req.text() };
108
- if (contentType === 'application/json')
112
+ if (contentType === "application/json")
109
113
  return req.json().catch(this.logError);
110
- if (contentType === 'application/x-www-form-urlencoded')
114
+ if (contentType === "application/x-www-form-urlencoded")
111
115
  return this.parseFormURL(req);
112
116
  return {};
113
117
  }