hono 1.4.0 → 1.4.1

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.
@@ -8,7 +8,7 @@ declare type Options = {
8
8
  pretty?: boolean;
9
9
  validationRules?: ReadonlyArray<ValidationRule>;
10
10
  };
11
- export declare const graphqlServer: (options: Options) => (c: Context, next: Next) => Promise<void>;
11
+ export declare const graphqlServer: (options: Options) => (c: Context, next: Next) => Promise<Response>;
12
12
  export interface GraphQLParams {
13
13
  query: string | null;
14
14
  variables: {
@@ -13,13 +13,11 @@ const graphqlServer = (options) => {
13
13
  const validationRules = (_b = options.validationRules) !== null && _b !== void 0 ? _b : [];
14
14
  // const showGraphiQL = options.graphiql ?? false
15
15
  return async (c, next) => {
16
- await next();
17
16
  // GraphQL HTTP only supports GET and POST methods.
18
17
  if (c.req.method !== 'GET' && c.req.method !== 'POST') {
19
- c.res = c.json((0, exports.errorMessages)(['GraphQL only supports GET and POST requests.']), 405, {
18
+ return c.json((0, exports.errorMessages)(['GraphQL only supports GET and POST requests.']), 405, {
20
19
  Allow: 'GET, POST',
21
20
  });
22
- return;
23
21
  }
24
22
  let params;
25
23
  try {
@@ -28,20 +26,17 @@ const graphqlServer = (options) => {
28
26
  catch (e) {
29
27
  if (e instanceof Error) {
30
28
  console.error(`${e.stack || e.message}`);
31
- c.res = c.json((0, exports.errorMessages)([e.message], [e]), 400);
29
+ return c.json((0, exports.errorMessages)([e.message], [e]), 400);
32
30
  }
33
- return;
34
31
  }
35
32
  const { query, variables, operationName } = params;
36
33
  if (query == null) {
37
- c.res = c.json((0, exports.errorMessages)(['Must provide query string.']), 400);
38
- return;
34
+ return c.json((0, exports.errorMessages)(['Must provide query string.']), 400);
39
35
  }
40
36
  const schemaValidationErrors = (0, graphql_1.validateSchema)(schema);
41
37
  if (schemaValidationErrors.length > 0) {
42
38
  // Return 500: Internal Server Error if invalid schema.
43
- c.res = c.json((0, exports.errorMessages)(['GraphQL schema validation error.'], schemaValidationErrors), 500);
44
- return;
39
+ return c.json((0, exports.errorMessages)(['GraphQL schema validation error.'], schemaValidationErrors), 500);
45
40
  }
46
41
  let documentAST;
47
42
  try {
@@ -54,16 +49,14 @@ const graphqlServer = (options) => {
54
49
  const e = new graphql_1.GraphQLError(syntaxError.message, {
55
50
  originalError: syntaxError,
56
51
  });
57
- c.res = c.json((0, exports.errorMessages)(['GraphQL syntax error.'], [e]), 400);
52
+ return c.json((0, exports.errorMessages)(['GraphQL syntax error.'], [e]), 400);
58
53
  }
59
- return;
60
54
  }
61
55
  // Validate AST, reporting any errors.
62
56
  const validationErrors = (0, graphql_1.validate)(schema, documentAST, [...graphql_1.specifiedRules, ...validationRules]);
63
57
  if (validationErrors.length > 0) {
64
58
  // Return 400: Bad Request if any validation errors exist.
65
- c.res = c.json((0, exports.errorMessages)(['GraphQL validation error.'], validationErrors), 400);
66
- return;
59
+ return c.json((0, exports.errorMessages)(['GraphQL validation error.'], validationErrors), 400);
67
60
  }
68
61
  if (c.req.method === 'GET') {
69
62
  // Determine if this GET request will perform a non-query.
@@ -76,10 +69,9 @@ const graphqlServer = (options) => {
76
69
  }
77
70
  */
78
71
  // Otherwise, report a 405: Method Not Allowed error.
79
- c.res = c.json((0, exports.errorMessages)([
72
+ return c.json((0, exports.errorMessages)([
80
73
  `Can only perform a ${operationAST.operation} operation from a POST request.`,
81
74
  ]), 405, { Allow: 'POST' });
82
- return;
83
75
  }
84
76
  }
85
77
  let result;
@@ -100,13 +92,11 @@ const graphqlServer = (options) => {
100
92
  nodes: documentAST,
101
93
  });
102
94
  // Return 400: Bad Request if any execution context errors exist.
103
- c.res = c.json((0, exports.errorMessages)(['GraphQL execution context error.'], [e]), 400);
95
+ return c.json((0, exports.errorMessages)(['GraphQL execution context error.'], [e]), 400);
104
96
  }
105
- return;
106
97
  }
107
98
  if (result.data == null) {
108
- c.res = c.json((0, exports.errorMessages)([result.errors.toString()], result.errors), 500);
109
- return;
99
+ return c.json((0, exports.errorMessages)([result.errors.toString()], result.errors), 500);
110
100
  }
111
101
  /*
112
102
  Now, does not support GraphiQL
@@ -115,14 +105,14 @@ const graphqlServer = (options) => {
115
105
  */
116
106
  if (pretty) {
117
107
  const payload = JSON.stringify(result, null, pretty ? 2 : 0);
118
- c.res = c.text(payload, 200, {
108
+ return c.text(payload, 200, {
119
109
  'Content-Type': 'application/json',
120
110
  });
121
111
  }
122
112
  else {
123
- c.res = c.json(result);
113
+ return c.json(result);
124
114
  }
125
- return;
115
+ await next(); // XXX
126
116
  };
127
117
  };
128
118
  exports.graphqlServer = graphqlServer;
@@ -158,7 +158,7 @@ class RegExpRouter {
158
158
  const handlerWithSortIndex = {
159
159
  index,
160
160
  handler,
161
- componentsLength: hint.components.length,
161
+ componentsLength: hint.components.length || 1,
162
162
  };
163
163
  for (let i = 0, len = routes.length; i < len; i++) {
164
164
  if (routes[i].method === method && routes[i].path === path) {
@@ -315,7 +315,7 @@ class RegExpRouter {
315
315
  }
316
316
  }
317
317
  if (routes[j].hint.components.length < routes[i].hint.components.length) {
318
- const componentsLength = routes[j].hint.components.length;
318
+ const componentsLength = routes[j].hint.components.length || 1;
319
319
  routes[j].middleware.push(...routes[i].handlers.map((h) => ({
320
320
  componentsLength,
321
321
  index: h.index,
@@ -70,7 +70,7 @@ class Node {
70
70
  score = score + this.order * 0.01;
71
71
  }
72
72
  else {
73
- score = parts.length + 1 + this.order * 0.01;
73
+ score = parts.length + this.order * 0.01;
74
74
  }
75
75
  if (!curNode.methods.length) {
76
76
  curNode.methods = [];
@@ -107,11 +107,7 @@ class Node {
107
107
  if (pattern === '*') {
108
108
  const astNode = node.children['*'];
109
109
  if (astNode) {
110
- let wildcard = false;
111
- if (!Object.keys(astNode.children).length) {
112
- wildcard = true;
113
- }
114
- handlerSets.push(...this.getHandlerSets(astNode, method, wildcard));
110
+ handlerSets.push(...this.getHandlerSets(astNode, method));
115
111
  nextNodes.push(astNode);
116
112
  }
117
113
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",