@stonecrop/graphql-client 0.2.21 → 0.2.22

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stonecrop/graphql-client",
3
- "version": "0.2.21",
3
+ "version": "0.2.22",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "author": {
@@ -30,9 +30,10 @@
30
30
  "src/*"
31
31
  ],
32
32
  "dependencies": {
33
+ "decimal.js": "^10.4.3",
33
34
  "graphql": "~16.6.0",
34
35
  "graphql-request": "~6.0.0",
35
- "@stonecrop/stonecrop": "0.2.21"
36
+ "@stonecrop/stonecrop": "0.2.22"
36
37
  },
37
38
  "devDependencies": {
38
39
  "@miragejs/graphql": "^0.1.13",
package/src/index.ts CHANGED
@@ -1,10 +1,55 @@
1
- import { request } from 'graphql-request'
1
+ import { Decimal } from 'decimal.js'
2
+ import { GraphQLClient } from 'graphql-request'
2
3
 
3
4
  import { queries } from './queries'
5
+ import { Meta, MetaParser } from 'types/index'
6
+
7
+ /**
8
+ * Parse the response from the GraphQL server. Converts the stringified JSON to JSON and converts the stringified numbers to Decimal.
9
+ * @param obj - The response from the GraphQL server
10
+ * @returns The parsed response
11
+ * @example
12
+ * const response = '{"data":{"getMeta":{"id":"Issue","name":"Issue","workflow":"{\"machineId\":null,\"name\":\"save\",\"id\":\"1\"}","schema":"[{\"label\":\"Subject\",\"id\":\"1\"}]","actions":"[{\"eventName\":\"save\",\"id\":\"1\"}]"}}}'
13
+ * const parsedResponse = metaParser(response)
14
+ * console.log(parsedResponse)
15
+ * /* Output: {"id": "Issue", "name": "Issue", "workflow": { "machineId": null, "name": "save", "id": "1" }, "schema": [{ "label": "Subject", "id": "1" }], "actions": [{ "eventName": "save", "id": "1" }]}
16
+ */
17
+ const metaParser = (obj: string): MetaParser => {
18
+ return JSON.parse(obj, (key, value) => {
19
+ if (typeof value === 'string') {
20
+ try {
21
+ return JSON.parse(value, (key, value) => {
22
+ if (typeof value === 'string' && !isNaN(Number(value))) {
23
+ return new Decimal(value)
24
+ }
25
+
26
+ return value
27
+ })
28
+ } catch (error) {
29
+ // if the value is not a stringified JSON, return as it is
30
+ return value
31
+ }
32
+ } else if (!isNaN(Number(value))) {
33
+ return new Decimal(value)
34
+ }
35
+ return value
36
+ })
37
+ }
4
38
 
5
39
  export const methods = {
6
- getMeta: async (doctype: string, url?: string) => {
7
- const data: any = await request(url || '/graphql', queries.getMeta, { doctype })
8
- return data
40
+ getMeta: async (doctype: string, url?: string): Promise<Meta['response']['getMeta']> => {
41
+ const client = new GraphQLClient(url || '/graphql', {
42
+ jsonSerializer: {
43
+ stringify: obj => JSON.stringify(obj), // process the request object before sending; leave as default JSON
44
+ parse: metaParser, // process the response meta object
45
+ },
46
+ })
47
+
48
+ const { getMeta } = await client.request<Meta['response'], Meta['variables']>({
49
+ document: queries.getMeta,
50
+ variables: { doctype },
51
+ })
52
+
53
+ return getMeta
9
54
  },
10
55
  }