@vladimirdev635/gql-client 0.0.22 → 0.0.24

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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/parser.d.ts +6 -2
  3. package/parser.js +12 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vladimirdev635/gql-client",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",
package/parser.d.ts CHANGED
@@ -1,2 +1,6 @@
1
- import { ClientParser } from './types.js';
2
- export declare function createParser<TContext>(): ClientParser<TContext>;
1
+ import { ClientParser, Operation } from './types.js';
2
+ interface CreateParserOptions<TContext> {
3
+ onErrors: <T extends Operation>(context: TContext, operation: T, response: Response, errors: any[]) => Promise<void> | void;
4
+ }
5
+ export declare function createParser<TContext>(options?: CreateParserOptions<TContext>): ClientParser<TContext>;
6
+ export {};
package/parser.js CHANGED
@@ -1,7 +1,16 @@
1
- export function createParser() {
1
+ const defaultParserOptions = {
2
+ onErrors(_, __, ___, errors) {
3
+ throw new Error(JSON.stringify(errors));
4
+ },
5
+ };
6
+ export function createParser(options = defaultParserOptions) {
2
7
  return {
3
- parseBody: async (_, operation, response) => {
4
- return operation.resultSchema.parse(await response.json());
8
+ parseBody: async (context, operation, response) => {
9
+ const json = await response.json();
10
+ if (json.errors) {
11
+ await options.onErrors(context, operation, response, json.errors);
12
+ }
13
+ return operation.resultSchema.parse(json.data);
5
14
  },
6
15
  };
7
16
  }