hono 0.5.6 → 0.5.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/dist/context.d.ts CHANGED
@@ -11,6 +11,7 @@ export declare class Context<RequestParamKeyType = string> {
11
11
  private _headers;
12
12
  private _status;
13
13
  private _statusText;
14
+ private _pretty;
14
15
  render: (template: string, params?: object, options?: object) => Promise<Response>;
15
16
  notFound: () => Response | Promise<Response>;
16
17
  constructor(req: Request<RequestParamKeyType>, opts?: {
@@ -20,6 +21,7 @@ export declare class Context<RequestParamKeyType = string> {
20
21
  });
21
22
  header(name: string, value: string): void;
22
23
  status(number: number): void;
24
+ pretty(prettyJSON: boolean): void;
23
25
  newResponse(data: Data, init?: ResponseInit): Response;
24
26
  body(data: Data, status?: number, headers?: Headers): Response;
25
27
  text(text: string, status?: number, headers?: Headers): Response;
package/dist/context.js CHANGED
@@ -34,6 +34,9 @@ class Context {
34
34
  this._status = number;
35
35
  this._statusText = (0, http_status_1.getStatusText)(number);
36
36
  }
37
+ pretty(prettyJSON) {
38
+ this._pretty = prettyJSON;
39
+ }
37
40
  newResponse(data, init = {}) {
38
41
  init.status = init.status || this._status || 200;
39
42
  init.statusText = init.statusText || this._statusText || (0, http_status_1.getStatusText)(init.status);
@@ -69,7 +72,7 @@ class Context {
69
72
  if (typeof object !== 'object') {
70
73
  throw new TypeError('json method arg must be a object!');
71
74
  }
72
- const body = JSON.stringify(object);
75
+ const body = this._pretty ? JSON.stringify(object, null, 2) : JSON.stringify(object);
73
76
  headers['Content-Type'] || (headers['Content-Type'] = 'application/json; charset=UTF-8');
74
77
  return this.body(body, status, headers);
75
78
  }
@@ -1,5 +1,6 @@
1
1
  import type { Context } from '../../context';
2
- import type { GraphQLSchema, ValidationRule } from 'graphql';
2
+ import { GraphQLError } from 'graphql';
3
+ import type { GraphQLSchema, ValidationRule, GraphQLFormattedError } from 'graphql';
3
4
  declare type Options = {
4
5
  schema: GraphQLSchema;
5
6
  rootValue?: unknown;
@@ -16,7 +17,9 @@ export interface GraphQLParams {
16
17
  raw: boolean;
17
18
  }
18
19
  export declare const getGraphQLParams: (request: Request) => Promise<GraphQLParams>;
19
- export declare const errorMessages: (messages: string[]) => {
20
+ export declare const errorMessages: (messages: string[], graphqlErrors?: readonly GraphQLError[] | readonly GraphQLFormattedError[]) => {
21
+ errors: readonly GraphQLError[] | readonly GraphQLFormattedError[];
22
+ } | {
20
23
  errors: {
21
24
  message: string;
22
25
  }[];
@@ -27,7 +27,7 @@ const graphqlServer = (options) => {
27
27
  }
28
28
  catch (e) {
29
29
  if (e instanceof Error) {
30
- c.res = c.json((0, exports.errorMessages)([e.message]), 400);
30
+ c.res = c.json((0, exports.errorMessages)([e.message], [e]), 400);
31
31
  }
32
32
  return;
33
33
  }
@@ -39,7 +39,7 @@ const graphqlServer = (options) => {
39
39
  const schemaValidationErrors = (0, graphql_1.validateSchema)(schema);
40
40
  if (schemaValidationErrors.length > 0) {
41
41
  // Return 500: Internal Server Error if invalid schema.
42
- c.res = c.json((0, exports.errorMessages)(['GraphQL schema validation error.']), 500);
42
+ c.res = c.json((0, exports.errorMessages)(['GraphQL schema validation error.'], schemaValidationErrors), 500);
43
43
  return;
44
44
  }
45
45
  let documentAST;
@@ -48,14 +48,19 @@ const graphqlServer = (options) => {
48
48
  }
49
49
  catch (syntaxError) {
50
50
  // Return 400: Bad Request if any syntax errors errors exist.
51
- c.res = c.json((0, exports.errorMessages)(['GraphQL syntax error.']), 400);
51
+ if (syntaxError instanceof Error) {
52
+ const e = new graphql_1.GraphQLError(syntaxError.message, {
53
+ originalError: syntaxError,
54
+ });
55
+ c.res = c.json((0, exports.errorMessages)(['GraphQL syntax error.'], [e]), 400);
56
+ }
52
57
  return;
53
58
  }
54
59
  // Validate AST, reporting any errors.
55
60
  const validationErrors = (0, graphql_1.validate)(schema, documentAST, [...graphql_1.specifiedRules, ...validationRules]);
56
61
  if (validationErrors.length > 0) {
57
62
  // Return 400: Bad Request if any validation errors exist.
58
- c.res = c.json((0, exports.errorMessages)(['GraphQL validation error.']), 400);
63
+ c.res = c.json((0, exports.errorMessages)(['GraphQL validation error.'], validationErrors), 400);
59
64
  return;
60
65
  }
61
66
  if (c.req.method === 'GET') {
@@ -85,13 +90,19 @@ const graphqlServer = (options) => {
85
90
  operationName: operationName,
86
91
  });
87
92
  }
88
- catch (error) {
89
- // Return 400: Bad Request if any execution context errors exist.
90
- c.res = c.json((0, exports.errorMessages)(['GraphQL execution context error.']), 400);
93
+ catch (contextError) {
94
+ if (contextError instanceof Error) {
95
+ const e = new graphql_1.GraphQLError(contextError.message, {
96
+ originalError: contextError,
97
+ nodes: documentAST,
98
+ });
99
+ // Return 400: Bad Request if any execution context errors exist.
100
+ c.res = c.json((0, exports.errorMessages)(['GraphQL execution context error.'], [e]), 400);
101
+ }
91
102
  return;
92
103
  }
93
104
  if (result.data == null) {
94
- c.res = c.json((0, exports.errorMessages)([result.errors.toString()]), 500);
105
+ c.res = c.json((0, exports.errorMessages)([result.errors.toString()], result.errors), 500);
95
106
  return;
96
107
  }
97
108
  /*
@@ -149,14 +160,18 @@ const getGraphQLParams = async (request) => {
149
160
  return params;
150
161
  };
151
162
  exports.getGraphQLParams = getGraphQLParams;
152
- const errorMessages = (messages) => {
153
- const errors = messages.map((message) => {
163
+ const errorMessages = (messages, graphqlErrors) => {
164
+ if (graphqlErrors) {
154
165
  return {
155
- message: message,
166
+ errors: graphqlErrors,
156
167
  };
157
- });
168
+ }
158
169
  return {
159
- errors: errors,
170
+ errors: messages.map((message) => {
171
+ return {
172
+ message: message,
173
+ };
174
+ }),
160
175
  };
161
176
  };
162
177
  exports.errorMessages = errorMessages;
@@ -6,5 +6,6 @@ declare type Data = string | object | boolean;
6
6
  export declare const sha256: (data: Data) => Promise<string>;
7
7
  export declare const sha1: (data: Data) => Promise<string>;
8
8
  export declare const createHash: (data: Data, algorithm: Algorithm) => Promise<string>;
9
+ export declare const encodeBase64: (str: string) => any;
9
10
  export declare const decodeBase64: (str: string) => any;
10
11
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decodeBase64 = exports.createHash = exports.sha1 = exports.sha256 = void 0;
3
+ exports.decodeBase64 = exports.encodeBase64 = exports.createHash = exports.sha1 = exports.sha256 = void 0;
4
4
  const sha256 = async (data) => {
5
5
  const algorithm = { name: 'SHA-256', alias: 'sha256' };
6
6
  const hash = await (0, exports.createHash)(data, algorithm);
@@ -34,6 +34,28 @@ const createHash = async (data, algorithm) => {
34
34
  }
35
35
  };
36
36
  exports.createHash = createHash;
37
+ const encodeBase64 = (str) => {
38
+ try {
39
+ const encoder = new TextEncoder();
40
+ const bytes = encoder.encode(str);
41
+ const length = bytes.byteLength;
42
+ let binary = '';
43
+ for (let i = 0; i < length; i++) {
44
+ binary += String.fromCharCode(bytes[i]);
45
+ }
46
+ return btoa(binary);
47
+ }
48
+ catch (_a) { }
49
+ try {
50
+ const { Buffer } = require('buffer');
51
+ return Buffer.from(str).toString('base64');
52
+ }
53
+ catch (e) {
54
+ console.error('If you want to do "encodeBase64", polyfill "buffer" module.');
55
+ throw e;
56
+ }
57
+ };
58
+ exports.encodeBase64 = encodeBase64;
37
59
  const decodeBase64 = (str) => {
38
60
  try {
39
61
  const text = atob(str);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "0.5.6",
3
+ "version": "0.5.9",
4
4
  "description": "[炎] Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -27,7 +27,10 @@
27
27
  "./powered-by": "./dist/middleware/powered-by/powered-by.js",
28
28
  "./serve-static": "./dist/middleware/serve-static/serve-static.js",
29
29
  "./router/trie-router": "./dist/router/trie-router/router.js",
30
- "./router/reg-exp-router": "./dist/router/reg-exp-router/index.js"
30
+ "./router/reg-exp-router": "./dist/router/reg-exp-router/index.js",
31
+ "./utils/cloudflare": "./dist/utils/cloudflare.js",
32
+ "./utils/crypto": "./dist/utils/crypto.js",
33
+ "./utils/mime": "./dist/utils/mime.js"
31
34
  },
32
35
  "typesVersions": {
33
36
  "*": {
@@ -66,6 +69,15 @@
66
69
  ],
67
70
  "router/reg-exp-router": [
68
71
  "./dist/router/reg-exp-router/router.d.ts"
72
+ ],
73
+ "utils/cloudflare": [
74
+ "./dist/utils/cloudflare.d.ts"
75
+ ],
76
+ "utils/crypto": [
77
+ "./dist/utils/crypto.d.ts"
78
+ ],
79
+ "utils/mime": [
80
+ "./dist/utils/mime.d.ts"
69
81
  ]
70
82
  }
71
83
  },