@stephenchenorg/astro 1.3.0 → 1.5.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.
- package/dist/api/error.d.ts +33 -10
- package/dist/api/error.js +24 -1
- package/dist/api/errorResponse.d.ts +1 -0
- package/dist/api/errorResponse.js +9 -0
- package/dist/api/fetch.d.ts +2 -2
- package/dist/api/fetch.js +29 -14
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/package.json +2 -2
package/dist/api/error.d.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
interface
|
|
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
|
-
|
|
11
|
-
|
|
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
|
-
|
|
22
|
-
|
|
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;
|
package/dist/api/fetch.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
|
2
2
|
export interface CreateGraphQLAPIOptions {
|
|
3
3
|
endpoint: string;
|
|
4
4
|
defaultVariables?: Record<string, any> | (() => Record<string, any>);
|
|
5
|
-
|
|
5
|
+
fetchOptions?: RequestInit | (() => RequestInit);
|
|
6
6
|
}
|
|
7
|
-
export declare function createGraphQLAPI(options: CreateGraphQLAPIOptions): <TData extends Record<string, any>, TVariables extends Record<string, any> = Record<string, any>>(query: TypedDocumentNode<TData, TVariables>, variables?: TVariables) => Promise<TData>;
|
|
7
|
+
export declare function createGraphQLAPI(options: CreateGraphQLAPIOptions): <TData extends Record<string, any>, TVariables extends Record<string, any> = Record<string, any>>(query: TypedDocumentNode<TData, TVariables>, variables?: TVariables, fetchOptions?: RequestInit) => Promise<TData>;
|
|
8
8
|
export { gql } from 'graphql-tag';
|
package/dist/api/fetch.js
CHANGED
|
@@ -1,28 +1,43 @@
|
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const defaultVariables = typeof options.defaultVariables === "function" ? options.defaultVariables() : options.defaultVariables
|
|
11
|
-
const
|
|
12
|
-
headers: typeof options.headers === "function" ? options.headers() : options.headers
|
|
13
|
-
};
|
|
5
|
+
const client = new AwesomeGraphQLClient({
|
|
6
|
+
endpoint: options.endpoint,
|
|
7
|
+
formatQuery: (query) => print(query)
|
|
8
|
+
});
|
|
9
|
+
return function graphQLAPI(query, variables, fetchOptions) {
|
|
10
|
+
const defaultVariables = typeof options.defaultVariables === "function" ? options.defaultVariables() : options.defaultVariables;
|
|
11
|
+
const defaultFetchOptions = typeof options.fetchOptions === "function" ? options.fetchOptions() : options.fetchOptions;
|
|
14
12
|
return new Promise((resolve, reject) => {
|
|
15
13
|
client.request(query, {
|
|
16
14
|
...defaultVariables,
|
|
17
15
|
...variables
|
|
18
|
-
},
|
|
16
|
+
}, {
|
|
17
|
+
...defaultFetchOptions,
|
|
18
|
+
...fetchOptions,
|
|
19
|
+
headers: {
|
|
20
|
+
...defaultFetchOptions?.headers,
|
|
21
|
+
...fetchOptions?.headers
|
|
22
|
+
}
|
|
23
|
+
}).then((data) => resolve(data)).catch((error) => {
|
|
19
24
|
if (error instanceof AwesomeGraphQLRequestError) {
|
|
20
|
-
|
|
25
|
+
const fieldError = error.fieldErrors?.[0];
|
|
26
|
+
const code = fieldError?.code;
|
|
27
|
+
const errorProps = {
|
|
28
|
+
code,
|
|
21
29
|
message: error.message,
|
|
22
30
|
query: error.query,
|
|
23
31
|
variables: error.variables,
|
|
24
|
-
extensions: error.extensions
|
|
25
|
-
|
|
32
|
+
extensions: error.extensions,
|
|
33
|
+
fieldErrors: error.fieldErrors
|
|
34
|
+
};
|
|
35
|
+
if (code === 404) {
|
|
36
|
+
reject(new GraphQLNotFoundError(errorProps));
|
|
37
|
+
} else if (code === 422) {
|
|
38
|
+
reject(new GraphQLValidationError(errorProps));
|
|
39
|
+
}
|
|
40
|
+
reject(new GraphQLRequestError(errorProps));
|
|
26
41
|
} else {
|
|
27
42
|
reject(error);
|
|
28
43
|
}
|
package/dist/api/index.d.ts
CHANGED
package/dist/api/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stephenchenorg/astro",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.5.0",
|
|
5
5
|
"description": "Stephenchenorg Astro 前端通用套件",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://stephenchenorg-astro.netlify.app",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "mkdist --declaration --ext=js",
|
|
59
|
-
"lint": "eslint
|
|
59
|
+
"lint": "eslint \"*.{js,ts,json}\" \"src/**/*.ts\"",
|
|
60
60
|
"code-check": "astro check && npm run lint",
|
|
61
61
|
"prepack": "npm run build",
|
|
62
62
|
"release": "bumpp --commit \"Release v%s\" && npm publish"
|