@stephenchenorg/astro 1.3.0 → 1.4.0

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.
@@ -1,4 +1,15 @@
1
- interface GraphQLRequestExtensions {
1
+ export interface GraphQLFieldError {
2
+ code?: number;
3
+ message: string;
4
+ locations?: {
5
+ line: number;
6
+ column: number;
7
+ }[];
8
+ path?: (string | number)[];
9
+ extensions?: GraphQLRequestExtensions;
10
+ data?: Record<string, any>;
11
+ }
12
+ export interface GraphQLRequestExtensions {
2
13
  debugMessage?: string;
3
14
  file?: string;
4
15
  line?: number;
@@ -7,25 +18,37 @@ interface GraphQLRequestExtensions {
7
18
  line: number;
8
19
  call: number;
9
20
  }[];
10
- validation?: Record<string, string[]>;
11
- category?: string;
21
+ }
22
+ export interface GraphQLRequestErrorOptions {
23
+ code?: number;
24
+ message: string;
25
+ query: string;
26
+ variables?: Record<string, any>;
27
+ extensions?: GraphQLRequestExtensions;
28
+ fieldErrors?: GraphQLFieldError[];
12
29
  }
13
30
  export declare class GraphQLRequestError extends Error {
14
31
  type: string;
15
32
  name: string;
16
33
  title: string;
34
+ code: number | undefined;
17
35
  originalMessage: string;
18
36
  query: string;
19
37
  variables: Record<string, any> | undefined;
20
38
  extensions: GraphQLRequestExtensions | undefined;
21
- constructor(props: {
22
- message: string;
23
- query: string;
24
- variables?: Record<string, any>;
25
- extensions?: GraphQLRequestExtensions;
26
- }, options?: ErrorOptions);
39
+ fieldErrors: GraphQLFieldError[] | undefined;
40
+ constructor(props: GraphQLRequestErrorOptions, options?: ErrorOptions);
27
41
  isNotFound(): boolean;
28
42
  private buildMessage;
29
43
  static is(err: unknown): err is GraphQLRequestError;
30
44
  }
31
- export {};
45
+ export declare class GraphQLNotFoundError extends GraphQLRequestError {
46
+ type: string;
47
+ name: string;
48
+ }
49
+ export declare class GraphQLValidationError extends GraphQLRequestError {
50
+ type: string;
51
+ name: string;
52
+ errors: Record<string, string[]>;
53
+ constructor(props: GraphQLRequestErrorOptions, options?: ErrorOptions);
54
+ }
package/dist/api/error.js CHANGED
@@ -2,19 +2,23 @@ export class GraphQLRequestError extends Error {
2
2
  type = "GraphQLRequestError";
3
3
  name = "GraphQLRequestError";
4
4
  title = "GraphQL request error.";
5
+ code;
5
6
  originalMessage;
6
7
  query;
7
8
  variables;
8
9
  extensions;
10
+ fieldErrors;
9
11
  constructor(props, options) {
10
- const { message, query, variables, extensions } = props;
12
+ const { code, message, query, variables, extensions, fieldErrors } = props;
11
13
  super("GraphQL request error.", options);
12
14
  const originalStack = this.stack;
13
15
  this.title = "GraphQL request error.";
16
+ this.code = code;
14
17
  this.originalMessage = message;
15
18
  this.query = query;
16
19
  this.variables = variables;
17
20
  this.extensions = extensions;
21
+ this.fieldErrors = fieldErrors;
18
22
  this.message = this.buildMessage();
19
23
  this.stack = originalStack;
20
24
  }
@@ -24,6 +28,12 @@ export class GraphQLRequestError extends Error {
24
28
  buildMessage() {
25
29
  let message = `${this.originalMessage}
26
30
  `;
31
+ if (this.code) {
32
+ message += "\n";
33
+ message += `[code]
34
+ ${this.code}
35
+ `;
36
+ }
27
37
  if (this.extensions) {
28
38
  if (this.extensions.debugMessage) {
29
39
  message += "\n";
@@ -72,3 +82,16 @@ ${JSON.stringify(this.variables, null, 2)}
72
82
  return err.type === "GraphQLRequestError";
73
83
  }
74
84
  }
85
+ export class GraphQLNotFoundError extends GraphQLRequestError {
86
+ type = "GraphQLNotFoundError";
87
+ name = "GraphQLNotFoundError";
88
+ }
89
+ export class GraphQLValidationError extends GraphQLRequestError {
90
+ type = "GraphQLValidationError";
91
+ name = "GraphQLValidationError";
92
+ errors;
93
+ constructor(props, options) {
94
+ super(props, options);
95
+ this.errors = this.fieldErrors?.[0]?.data?.validation || {};
96
+ }
97
+ }
@@ -0,0 +1 @@
1
+ export declare function handleErrorResponse(e: unknown): Response | undefined;
@@ -0,0 +1,9 @@
1
+ import { GraphQLNotFoundError } from "./error.js";
2
+ export function handleErrorResponse(e) {
3
+ if (e instanceof GraphQLNotFoundError) {
4
+ return new Response(null, {
5
+ status: 404,
6
+ statusText: "Not found"
7
+ });
8
+ }
9
+ }
package/dist/api/fetch.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AwesomeGraphQLClient, GraphQLRequestError as AwesomeGraphQLRequestError } from "awesome-graphql-client";
2
2
  import { print } from "graphql/language/printer";
3
- import { GraphQLRequestError } from "./error.js";
3
+ import { GraphQLNotFoundError, GraphQLRequestError, GraphQLValidationError } from "./error.js";
4
4
  export function createGraphQLAPI(options) {
5
5
  return function graphQLAPI(query, variables = {}) {
6
6
  const client = new AwesomeGraphQLClient({
@@ -17,12 +17,22 @@ export function createGraphQLAPI(options) {
17
17
  ...variables
18
18
  }, fetchOptions).then((data) => resolve(data)).catch((error) => {
19
19
  if (error instanceof AwesomeGraphQLRequestError) {
20
- reject(new GraphQLRequestError({
20
+ const fieldError = error.fieldErrors?.[0];
21
+ const code = fieldError?.code;
22
+ const errorProps = {
23
+ code,
21
24
  message: error.message,
22
25
  query: error.query,
23
26
  variables: error.variables,
24
- extensions: error.extensions
25
- }));
27
+ extensions: error.extensions,
28
+ fieldErrors: error.fieldErrors
29
+ };
30
+ if (code === 404) {
31
+ reject(new GraphQLNotFoundError(errorProps));
32
+ } else if (code === 422) {
33
+ reject(new GraphQLValidationError(errorProps));
34
+ }
35
+ reject(new GraphQLRequestError(errorProps));
26
36
  } else {
27
37
  reject(error);
28
38
  }
@@ -1,2 +1,3 @@
1
1
  export * from './fetch';
2
2
  export * from './error';
3
+ export * from './errorResponse';
package/dist/api/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./fetch.js";
2
2
  export * from "./error.js";
3
+ export * from "./errorResponse.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stephenchenorg/astro",
3
3
  "type": "module",
4
- "version": "1.3.0",
4
+ "version": "1.4.0",
5
5
  "description": "Stephenchenorg Astro 前端通用套件",
6
6
  "license": "MIT",
7
7
  "homepage": "https://stephenchenorg-astro.netlify.app",