@devticon-os/graphql-codegen-axios 0.3.13 → 0.3.14
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/package.json +1 -1
- package/templates/helpers.ts +37 -6
package/package.json
CHANGED
package/templates/helpers.ts
CHANGED
|
@@ -10,14 +10,45 @@ type GraphqlRequestParams = {
|
|
|
10
10
|
query: string;
|
|
11
11
|
variables?: any;
|
|
12
12
|
};
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
|
|
14
|
+
const get = (key: string, data: any) => {
|
|
15
|
+
const p = key.split('.');
|
|
16
|
+
let d = data;
|
|
17
|
+
while (p.length) {
|
|
18
|
+
const k = p.shift();
|
|
19
|
+
d = d[k];
|
|
20
|
+
if (p.length === 0) {
|
|
21
|
+
return d;
|
|
22
|
+
}
|
|
23
|
+
if (d === undefined) {
|
|
24
|
+
throw new Error(`Invalid path ${key} missing ${k}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const set = (key: string, value: any, data: any) => {
|
|
30
|
+
const p = key.split('.');
|
|
31
|
+
let d = data;
|
|
32
|
+
while (p.length) {
|
|
33
|
+
const k = p.shift();
|
|
34
|
+
if (p.length === 0) {
|
|
35
|
+
d[k] = value;
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
d = d[k];
|
|
39
|
+
if (d === undefined) {
|
|
40
|
+
throw new Error(`Invalid path ${key} missing ${k}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
export const first = (key: string) => (data: any) => {
|
|
45
|
+
set(key, get(key + '.0', data), data);
|
|
15
46
|
return data;
|
|
16
47
|
};
|
|
17
48
|
|
|
18
|
-
const firstOrFail = (key: string) => (data: any) => {
|
|
19
|
-
|
|
20
|
-
if (!data
|
|
49
|
+
export const firstOrFail = (key: string) => (data: any) => {
|
|
50
|
+
first(key)(data);
|
|
51
|
+
if (!get(key, data)) {
|
|
21
52
|
throw {
|
|
22
53
|
message: `Empty list for ${key} `,
|
|
23
54
|
code: 'EMPTY_LIST',
|
|
@@ -75,7 +106,7 @@ const execute = (
|
|
|
75
106
|
return d;
|
|
76
107
|
});
|
|
77
108
|
|
|
78
|
-
export const singleResult = (key: string) => (data: any) => data
|
|
109
|
+
export const singleResult = (key: string) => (data: any) => get(key, data);
|
|
79
110
|
|
|
80
111
|
export class GraphqlError extends Error {
|
|
81
112
|
constructor(message: string, public gqlErrors: GraphQLError[]) {
|