@jsb188/graphql 1.1.8 → 1.1.11

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 (122) hide show
  1. package/cache/config.ts +66 -42
  2. package/cache/index.test.ts +881 -0
  3. package/cache/index.ts +1815 -979
  4. package/cache/persist.ts +721 -0
  5. package/cache/store.ts +437 -0
  6. package/client/data-table-realtime-subscriptions.test.ts +740 -0
  7. package/client/data-table-realtime-subscriptions.ts +955 -0
  8. package/client/data-table-realtime.test.ts +792 -0
  9. package/client/data-table-realtime.ts +1218 -0
  10. package/client/fragment-realtime-sequence.ts +52 -0
  11. package/client/index.ts +4 -2
  12. package/client/open-sheet.ts +137 -0
  13. package/client/request-managed-session.test.ts +574 -0
  14. package/client/request-no-store.test.ts +157 -0
  15. package/client/request.ts +973 -143
  16. package/client/sse-ticket.test.ts +26 -0
  17. package/deno.lock +170 -0
  18. package/deno.test.json +10 -0
  19. package/drafts/index.ts +149 -0
  20. package/drafts/store.ts +397 -0
  21. package/gql/eagerQueryMap.ts +12 -0
  22. package/gql/fragments/accountAuthFragments.ts +5 -0
  23. package/gql/fragments/accountFragments.ts +1 -3
  24. package/gql/fragments/accountSessionFragments.ts +16 -0
  25. package/gql/fragments/agentDocFragments.ts +46 -0
  26. package/gql/fragments/agentMemoryFragments.ts +19 -0
  27. package/gql/fragments/aiChatFragments.ts +17 -0
  28. package/gql/fragments/billingFragments.ts +0 -11
  29. package/gql/fragments/campaignFragments.ts +277 -0
  30. package/gql/fragments/dataTableFragments.ts +102 -0
  31. package/gql/fragments/emailFragments.ts +36 -0
  32. package/gql/fragments/fieldDocFragments.ts +82 -0
  33. package/gql/fragments/inboundContactFragments.ts +15 -0
  34. package/gql/fragments/integrationFragments.ts +22 -0
  35. package/gql/fragments/logFragments.ts +33 -64
  36. package/gql/fragments/organizationFragments.ts +94 -21
  37. package/gql/fragments/scheduleFragments.ts +155 -0
  38. package/gql/fragments/sheetFragments.ts +675 -0
  39. package/gql/fragments/storageFragments.ts +21 -1
  40. package/gql/fragments/workflowFragments.ts +67 -0
  41. package/gql/mutations/accountMutations.ts +24 -4
  42. package/gql/mutations/agentDocMutations.ts +177 -0
  43. package/gql/mutations/agentMemoryMutations.ts +36 -0
  44. package/gql/mutations/aiChatMutations.ts +43 -3
  45. package/gql/mutations/authMutations.ts +160 -19
  46. package/gql/mutations/campaignMutations.ts +215 -0
  47. package/gql/mutations/dataTableMutations.ts +106 -0
  48. package/gql/mutations/fieldDocMutations.ts +24 -0
  49. package/gql/mutations/inboundContactMutations.ts +24 -0
  50. package/gql/mutations/index.ts +17 -8
  51. package/gql/mutations/integrationMutations.ts +25 -0
  52. package/gql/mutations/logMutations.ts +10 -19
  53. package/gql/mutations/organizationMutations.ts +160 -2
  54. package/gql/mutations/scheduleMutations.ts +423 -0
  55. package/gql/mutations/sheetMutations.ts +385 -0
  56. package/gql/mutations/storageMutations.ts +21 -2
  57. package/gql/mutations/workflowMutations.ts +42 -0
  58. package/gql/queries/agentDocQueries.ts +120 -0
  59. package/gql/queries/agentMemoryQueries.ts +22 -0
  60. package/gql/queries/aiChatQueries.ts +18 -2
  61. package/gql/queries/authQueries.ts +41 -0
  62. package/gql/queries/billingQueries.ts +24 -1
  63. package/gql/queries/campaignQueries.ts +127 -0
  64. package/gql/queries/dataTableQueries.ts +134 -0
  65. package/gql/queries/emailQueries.ts +96 -0
  66. package/gql/queries/fieldDocQueries.ts +158 -0
  67. package/gql/queries/inboundContactQueries.ts +40 -0
  68. package/gql/queries/index.ts +17 -6
  69. package/gql/queries/logQueries.ts +42 -32
  70. package/gql/queries/organizationQueries.ts +113 -8
  71. package/gql/queries/scheduleQueries.ts +315 -0
  72. package/gql/queries/sheetQueries.ts +383 -0
  73. package/gql/queries/storageQueries.ts +38 -0
  74. package/gql/queries/workflowQueries.ts +99 -0
  75. package/hooks/index.ts +2104 -860
  76. package/hooks/use-account-mtn.ts +91 -48
  77. package/hooks/use-agentDoc-mtn.ts +285 -0
  78. package/hooks/use-agentDoc-qry.ts +208 -0
  79. package/hooks/use-agentMemory-mtn.ts +73 -0
  80. package/hooks/use-agentMemory-qry.ts +44 -0
  81. package/hooks/use-aiChat-mtn.ts +248 -97
  82. package/hooks/use-aiChat-qry.ts +501 -191
  83. package/hooks/use-app-qry.ts +14 -11
  84. package/hooks/use-auth-mtn.ts +410 -256
  85. package/hooks/use-auth-qry.ts +51 -0
  86. package/hooks/use-campaign-mtn.ts +377 -0
  87. package/hooks/use-campaign-qry.ts +257 -0
  88. package/hooks/use-dataTable-mtn.ts +170 -0
  89. package/hooks/use-dataTable-qry.ts +996 -0
  90. package/hooks/use-email-qry.ts +227 -0
  91. package/hooks/use-fieldDoc-mtn.ts +50 -0
  92. package/hooks/use-fieldDoc-qry.ts +180 -0
  93. package/hooks/use-inboundContact-mtn.ts +34 -0
  94. package/hooks/use-inboundContact-qry.ts +96 -0
  95. package/hooks/use-integration-mtn.ts +77 -0
  96. package/hooks/use-integration-qry.ts +30 -0
  97. package/hooks/use-log-mtn.ts +77 -61
  98. package/hooks/use-log-qry.ts +99 -72
  99. package/hooks/use-organization-mtn.ts +409 -80
  100. package/hooks/use-organization-qry.ts +211 -92
  101. package/hooks/use-schedule-mtn.ts +416 -0
  102. package/hooks/use-schedule-qry.ts +157 -0
  103. package/hooks/use-sheet-mtn.ts +367 -0
  104. package/hooks/use-sheet-qry.ts +440 -0
  105. package/hooks/use-storage-mtn.ts +83 -42
  106. package/hooks/use-storage-qry.ts +104 -13
  107. package/hooks/use-workflow-mtn.ts +55 -0
  108. package/hooks/use-workflow-qry.ts +230 -0
  109. package/loaders/campaign.ts +137 -0
  110. package/package.json +94 -52
  111. package/sse.ts +158 -105
  112. package/tsconfig.json +13 -0
  113. package/types.d.ts +60 -29
  114. package/utils.ts +77 -50
  115. package/gql/fragments/actionFragments.ts +0 -16
  116. package/gql/fragments/productFragments.ts +0 -63
  117. package/gql/fragments/reportFragments.ts +0 -48
  118. package/gql/mutations/billingMutations.ts +0 -34
  119. package/gql/queries/productQueries.ts +0 -70
  120. package/gql/queries/reportQueries.ts +0 -66
  121. package/hooks/use-product-qry.ts +0 -109
  122. package/hooks/use-report-qry.ts +0 -69
package/sse.ts CHANGED
@@ -1,143 +1,196 @@
1
- import { addFragmentToCache, updateFragment, updateQuery } from './cache/';
2
- import type { UpdateObserversFn } from './types.d';
1
+ import {
2
+ addFragmentToCache,
3
+ updateFragment,
4
+ updateQuery,
5
+ } from './cache/index.ts';
6
+ import type { UpdateObserversFn } from './types.d.ts';
3
7
 
4
8
  /**
5
9
  * Types
6
10
  */
7
11
 
8
- type ValidSSEType = (
9
- // Default SSE events
10
- 'CONNECTED' |
11
- // Query handlers
12
- 'QUERY_APPEND' | 'QUERY_PREPEND' |
13
- // Fragment handlers
14
- 'DATA_APPEND' | 'DATA_PREPEND' | 'DATA_UPDATE'
15
- );
12
+ type ValidSSEType =
13
+ // Default SSE events
14
+ | 'CONNECTED'
15
+ // Query handlers
16
+ | 'QUERY_APPEND'
17
+ | 'QUERY_PREPEND'
18
+ // Fragment handlers
19
+ | 'DATA_APPEND'
20
+ | 'DATA_PREPEND'
21
+ | 'DATA_UPDATE';
16
22
 
17
23
  export interface PublishPayload {
18
24
  __type: ValidSSEType;
19
25
  fragmentKey: string;
20
26
  fragmentNamespace?: string;
21
- dataFragmentKey?: string;
27
+ dataFragmentKey?: string;
22
28
  data: Record<string, any>;
23
- otherData?: Record<string, any>;
29
+ otherData?: Record<string, any>;
24
30
  }
25
31
 
26
32
  /**
27
33
  * Append or prepend data to a query
28
34
  */
29
35
 
30
- function handleQueryConcat(payload: PublishPayload, updateObservers?: UpdateObserversFn) {
31
-
32
- const { __type, fragmentKey, fragmentNamespace, dataFragmentKey, data, otherData } = payload;
33
-
34
- if (fragmentKey && fragmentNamespace && dataFragmentKey && data) {
35
- const isAppend = __type === 'QUERY_APPEND';
36
- const updateData = (currentData: any) => {
37
- const currentList = currentData?.[fragmentNamespace]?.data;
38
- if (
39
- currentData?.[fragmentNamespace]?.__list &&
40
- Array.isArray(currentList) &&
41
- !currentList.find(a => a[0] === dataFragmentKey)
42
- ) {
43
- addFragmentToCache(dataFragmentKey, data);
44
- const updatedList = isAppend ? [...currentList, [dataFragmentKey]] : [[dataFragmentKey], ...currentList];
45
-
46
- // console.log('UPDATED !!!!')
47
- // console.log('UPDATED !!!!')
48
- // console.log('UPDATED !!!!')
49
- // console.log('UPDATED !!!!')
50
- // console.log(fragmentNamespace, {
51
- // ...currentData?.[fragmentNamespace],
52
- // data: updatedList
53
- // });
54
-
55
- return {
56
- ...currentData,
57
- ...otherData,
58
- [fragmentNamespace]: {
59
- ...currentData?.[fragmentNamespace],
60
- data: updatedList
61
- }
62
- };
63
- }
64
- return currentData;
65
- }
66
-
67
- updateQuery(
68
- fragmentKey,
69
- updateData,
70
- false,
71
- updateObservers
72
- );
73
- }
74
-
75
- return null;
36
+ function handleQueryConcat(
37
+ payload: PublishPayload,
38
+ updateObservers?: UpdateObserversFn,
39
+ ) {
40
+ const {
41
+ __type,
42
+ fragmentKey,
43
+ fragmentNamespace,
44
+ dataFragmentKey,
45
+ data,
46
+ otherData,
47
+ } = payload;
48
+
49
+ if (fragmentKey && fragmentNamespace && dataFragmentKey && data) {
50
+ const isAppend = __type === 'QUERY_APPEND';
51
+ addFragmentToCache(dataFragmentKey, data);
52
+
53
+ const updateData = (currentData: any) => {
54
+ const currentList = currentData?.[fragmentNamespace]?.data;
55
+ if (
56
+ currentData?.[fragmentNamespace]?.__list &&
57
+ Array.isArray(currentList) &&
58
+ !currentList.find((a) => a[0] === dataFragmentKey)
59
+ ) {
60
+ const updatedList = isAppend
61
+ ? [...currentList, [dataFragmentKey]]
62
+ : [[dataFragmentKey], ...currentList];
63
+
64
+ // console.log('UPDATED !!!!')
65
+ // console.log('UPDATED !!!!')
66
+ // console.log('UPDATED !!!!')
67
+ // console.log('UPDATED !!!!')
68
+ // console.log(fragmentNamespace, {
69
+ // ...currentData?.[fragmentNamespace],
70
+ // data: updatedList
71
+ // });
72
+
73
+ return {
74
+ ...currentData,
75
+ ...otherData,
76
+ [fragmentNamespace]: {
77
+ ...currentData?.[fragmentNamespace],
78
+ data: updatedList,
79
+ },
80
+ };
81
+ }
82
+ return currentData;
83
+ };
84
+
85
+ updateQuery(fragmentKey, updateData, false, updateObservers);
86
+ }
87
+
88
+ return null;
76
89
  }
77
90
 
78
91
  /**
79
92
  * Append or prepend data to a fragment
80
93
  */
81
94
 
82
- function handleFragmentConcat(payload: PublishPayload, updateObservers?: UpdateObserversFn) {
83
- const { __type, fragmentKey, fragmentNamespace, data, otherData } = payload;
84
- const isAppend = __type === 'DATA_APPEND';
85
-
86
- if (fragmentKey && fragmentNamespace && data) {
87
- const namespaceType = typeof fragmentNamespace;
88
- const updateData = (currentData: any) => {
89
- if (typeof currentData[fragmentNamespace] === namespaceType) {
90
- let nextValue;
91
- if (namespaceType === 'string') {
92
- nextValue = isAppend ? `${currentData[fragmentNamespace]}${data}` : `${data}${currentData[fragmentNamespace]}`;
93
- } else if (Array.isArray(currentData[fragmentNamespace])) {
94
- nextValue = isAppend ? [...currentData[fragmentNamespace], data] : [data, ...currentData[fragmentNamespace]];
95
- }
96
- return {
97
- ...currentData,
98
- ...otherData,
99
- [fragmentNamespace]: nextValue
100
- }
101
- }
102
- return currentData;
103
- };
104
-
105
- updateFragment(fragmentKey, updateData, null, false, updateObservers);
106
- }
107
-
95
+ function handleFragmentConcat(
96
+ payload: PublishPayload,
97
+ updateObservers?: UpdateObserversFn,
98
+ ) {
99
+ const { __type, fragmentKey, fragmentNamespace, data, otherData } = payload;
100
+ const isAppend = __type === 'DATA_APPEND';
101
+
102
+ if (fragmentKey && fragmentNamespace && data) {
103
+ const namespaceType = typeof fragmentNamespace;
104
+ const seedData = {
105
+ ...(otherData && typeof otherData === 'object' ? otherData : {}),
106
+ [fragmentNamespace]: data,
107
+ };
108
+ const updateData = (currentData: any) => {
109
+ if (typeof currentData[fragmentNamespace] === namespaceType) {
110
+ let nextValue;
111
+ if (namespaceType === 'string') {
112
+ nextValue = isAppend
113
+ ? `${currentData[fragmentNamespace]}${data}`
114
+ : `${data}${currentData[fragmentNamespace]}`;
115
+ } else if (Array.isArray(currentData[fragmentNamespace])) {
116
+ nextValue = isAppend
117
+ ? [...currentData[fragmentNamespace], data]
118
+ : [data, ...currentData[fragmentNamespace]];
119
+ }
120
+ return {
121
+ ...currentData,
122
+ ...otherData,
123
+ [fragmentNamespace]: nextValue,
124
+ };
125
+ }
126
+ return currentData;
127
+ };
128
+
129
+ const updateFragmentKey = updateFragment(
130
+ fragmentKey,
131
+ updateData,
132
+ null,
133
+ false,
134
+ updateObservers,
135
+ );
136
+ if (!updateFragmentKey) {
137
+ addFragmentToCache(fragmentKey, seedData);
138
+ updateObservers?.({
139
+ fragmentIds: [fragmentKey],
140
+ });
141
+ }
142
+ }
108
143
  }
109
144
 
110
145
  /**
111
146
  * Update data in a fragment
112
147
  */
113
148
 
114
- function handleFragmentUpdate(payload: PublishPayload, updateObservers?: UpdateObserversFn) {
115
- const { fragmentKey, dataFragmentKey, data } = payload;
116
- updateFragment(dataFragmentKey || fragmentKey, data, null, false, updateObservers);
149
+ function handleFragmentUpdate(
150
+ payload: PublishPayload,
151
+ updateObservers?: UpdateObserversFn,
152
+ ) {
153
+ const { fragmentKey, dataFragmentKey, data } = payload;
154
+ const updateFragmentKey = dataFragmentKey || fragmentKey;
155
+ const updatedFragmentKey = updateFragment(
156
+ updateFragmentKey,
157
+ data,
158
+ null,
159
+ false,
160
+ updateObservers,
161
+ );
162
+ if (!updatedFragmentKey) {
163
+ addFragmentToCache(updateFragmentKey, data);
164
+ updateObservers?.({
165
+ fragmentIds: [updateFragmentKey],
166
+ });
167
+ }
117
168
  }
118
169
 
119
170
  /**
120
171
  * Data handler for SSE
121
172
  */
122
173
 
123
- function handleSSEData(payload: PublishPayload, updateObservers?: UpdateObserversFn) {
124
-
125
- switch (payload.__type) {
126
- case 'CONNECTED':
127
- // Do nothing
128
- break;
129
- case 'QUERY_APPEND':
130
- case 'QUERY_PREPEND':
131
- return handleQueryConcat(payload, updateObservers)
132
- case 'DATA_APPEND':
133
- case 'DATA_PREPEND':
134
- return handleFragmentConcat(payload, updateObservers)
135
- case 'DATA_UPDATE':
136
- return handleFragmentUpdate(payload, updateObservers)
137
- default:
138
- console.log('Unknown {__type} in handleSSEData():', payload.__type);
139
- break;
140
- }
174
+ function handleSSEData(
175
+ payload: PublishPayload,
176
+ updateObservers?: UpdateObserversFn,
177
+ ) {
178
+ switch (payload.__type) {
179
+ case 'CONNECTED':
180
+ // Do nothing
181
+ break;
182
+ case 'QUERY_APPEND':
183
+ case 'QUERY_PREPEND':
184
+ return handleQueryConcat(payload, updateObservers);
185
+ case 'DATA_APPEND':
186
+ case 'DATA_PREPEND':
187
+ return handleFragmentConcat(payload, updateObservers);
188
+ case 'DATA_UPDATE':
189
+ return handleFragmentUpdate(payload, updateObservers);
190
+ default:
191
+ console.log('Unknown {__type} in handleSSEData():', payload.__type);
192
+ break;
193
+ }
141
194
  }
142
195
 
143
196
  export default handleSSEData;
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+ "allowImportingTsExtensions": true,
7
+ "noEmit": true,
8
+ "strict": true,
9
+ "esModuleInterop": true
10
+ },
11
+ "include": ["**/*.ts", "**/*.tsx", "**/*.d.ts"],
12
+ "exclude": ["**/*.test.ts"]
13
+ }
package/types.d.ts CHANGED
@@ -1,26 +1,34 @@
1
- import type { ServerErrorObj, SimpleErrorType } from '@jsb188/app/types/app.d';
1
+ import type {
2
+ ServerErrorObj,
3
+ SimpleErrorType,
4
+ } from '@jsb188/app/types/app.d.ts';
5
+ import type { QueryResetTarget } from '@jsb188/app/utils/queryReset.ts';
2
6
 
3
7
  /**
4
8
  * GraphQL API requests
5
9
  */
6
10
 
7
11
  export interface APIRequestOutput {
8
- aborted?: boolean;
9
- error: ServerErrorObj | null;
10
- [apiName: string]: any; // Different name & data; it depends on API
12
+ aborted?: boolean;
13
+ error: ServerErrorObj | null;
14
+ [apiName: string]: any; // Different name & data; it depends on API
11
15
  }
12
16
 
13
17
  export interface GraphQLHandlers {
14
- resetErrors: () => void;
15
- setError: (error: SimpleErrorType) => void;
16
- setSaving: (nextSaving: boolean, resetInMS?: number) => void;
17
- refetch: () => void;
18
+ resetErrors: () => void;
19
+ setError: (error: SimpleErrorType) => void;
20
+ setSaving: (nextSaving: boolean, resetInMS?: number) => void;
21
+ refetch: () => void;
18
22
  }
19
23
 
20
24
  export interface UpdateDataObserverArgs {
21
- queryId?: string | null;
22
- fragmentIds?: (string | null)[];
23
- forceRefetch?: boolean;
25
+ queryId?: string | null;
26
+ // Set when the reset came from a structured target; observers match against
27
+ // this instead of parsing queryId, which is only a stable identity string
28
+ queryTarget?: QueryResetTarget | null;
29
+ fragmentIds?: (string | null)[];
30
+ forceRefetch?: boolean;
31
+ resetOnly?: boolean;
24
32
  }
25
33
 
26
34
  /**
@@ -29,46 +37,69 @@ export interface UpdateDataObserverArgs {
29
37
 
30
38
  export type AnyModalPopUpFn = (data: any, err?: ServerErrorObj) => void;
31
39
  export type UpdateObserversFn = (args: UpdateDataObserverArgs) => void;
32
- export type OnCompletedGQLFn = (data: any, error: ServerErrorObj | null, variables?: any) => void;
33
- export type OnErrorGQLFn = (error: ServerErrorObj | null, variables: any) => void;
40
+ export type OnCompletedGQLFn = (
41
+ data: any,
42
+ error: ServerErrorObj | null,
43
+ variables?: any,
44
+ ) => void;
45
+ export type OnErrorGQLFn = (
46
+ error: ServerErrorObj | null,
47
+ variables: any,
48
+ ) => void;
34
49
 
35
50
  /**
36
51
  * GraphQL query hook params
37
52
  */
38
53
 
39
54
  export interface GraphQLQueryOptions {
40
- name?: string;
41
- eagerFragmentKeyMap?: any;
42
- retryCount?: number;
43
- variables?: Record<string, any> | null;
44
- cacheMap?: any;
45
- skip?: boolean;
46
- testMode?: boolean;
47
- openModalPopUp?: AnyModalPopUpFn | null;
48
- onCompleted?: OnCompletedGQLFn
49
- onError?: OnErrorGQLFn;
55
+ authToken?: string | null;
56
+ name?: string;
57
+ eagerFragmentKeyMap?: any;
58
+ retryCount?: number;
59
+ rejectedResultRetryCount?: number;
60
+ retryTransientErrors?: boolean;
61
+ variables?: Record<string, any> | null;
62
+ cacheMap?: any;
63
+ skip?: boolean;
64
+ // Freshness window in ms: a complete cached result younger than this is
65
+ // served without any request; an older one is served instantly and then
66
+ // revalidated in the background (stale-while-revalidate). Defaults to the
67
+ // query's QUERY_EXPIRE_TIMES entry.
68
+ persistTime?: number;
69
+ testMode?: boolean;
70
+ openModalPopUp?: AnyModalPopUpFn | null;
71
+ onCompleted?: OnCompletedGQLFn;
72
+ onError?: OnErrorGQLFn;
73
+ shouldAcceptResult?: (
74
+ data: any,
75
+ variables?: Record<string, any> | null,
76
+ ) => boolean;
50
77
  }
51
78
 
52
79
  // Params for most GraphQL (query) hooks
53
- export type UseQueryParams = Omit<GraphQLQueryOptions, 'name' | 'retryCount' | 'cacheMap'>;
80
+ export type UseQueryParams = Omit<
81
+ GraphQLQueryOptions,
82
+ 'name' | 'retryCount' | 'cacheMap'
83
+ >;
54
84
 
55
85
  // Params for most GraphQL (mutation) hooks
56
86
  export type UseMutationParams = Partial<{
57
- onCompleted: OnCompletedGQLFn;
58
- onError: OnErrorGQLFn;
87
+ onCompleted: OnCompletedGQLFn;
88
+ onError: OnErrorGQLFn;
59
89
  }>;
60
90
 
61
91
  // Params for most GraphQL hooks with these options
62
92
  export type UseMutationParamsExtra = Partial<{
63
- checkMountedBeforeCallback?: boolean;
64
- }> & UseMutationParams;
93
+ checkMountedBeforeCallback?: boolean;
94
+ }> &
95
+ UseMutationParams;
65
96
 
66
97
  /**
67
98
  * Common query patterns
68
99
  */
69
100
 
70
101
  export type PaginationArgs = Partial<{
71
- cursor: [number | null, number | null];
102
+ cursor: [number | null, number | null, string?] | string | null;
72
103
  after: boolean;
73
104
  limit: number;
74
105
  doNotThrowError: boolean;
package/utils.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { getENVVariable } from '@jsb188/app';
2
- import type { ServerErrorObj } from '@jsb188/app/types/app.d';
2
+ import type { ServerErrorObj } from '@jsb188/app/types/app.d.ts';
3
3
 
4
4
  const TIME_TRACKER = new Map();
5
5
 
@@ -7,30 +7,35 @@ const TIME_TRACKER = new Map();
7
7
  * Check if data should be refetched based on time
8
8
  */
9
9
 
10
- export function checkIfNeedsRefetch(qryName: string, id: string, threshold: number = 3.6e+6): boolean {
11
- const lastFetched = TIME_TRACKER.get(`${qryName}_${id}`);
12
- if (!lastFetched) {
13
- return true;
14
- }
10
+ export function checkIfNeedsRefetch(
11
+ qryName: string,
12
+ id: string,
13
+ threshold: number = 3.6e6,
14
+ ): boolean {
15
+ const lastFetched = TIME_TRACKER.get(`${qryName}_${id}`);
16
+ if (!lastFetched) {
17
+ return true;
18
+ }
15
19
 
16
- const now = Date.now();
17
- const diff = now - lastFetched;
20
+ const now = Date.now();
21
+ const diff = now - lastFetched;
18
22
 
19
- return diff > threshold;
23
+ return diff > threshold;
20
24
  }
21
25
 
22
26
  /**
23
27
  * Set fetched time
24
28
  */
25
29
 
26
- export function setFetchedTime(qryName: string, id: string, setOnlyIfUndefined: boolean = false) {
27
- const key = `${qryName}_${id}`;
28
- if (
29
- !setOnlyIfUndefined ||
30
- !TIME_TRACKER.get(key)
31
- ) {
32
- TIME_TRACKER.set(key, Date.now());
33
- }
30
+ export function setFetchedTime(
31
+ qryName: string,
32
+ id: string,
33
+ setOnlyIfUndefined: boolean = false,
34
+ ) {
35
+ const key = `${qryName}_${id}`;
36
+ if (!setOnlyIfUndefined || !TIME_TRACKER.get(key)) {
37
+ TIME_TRACKER.set(key, Date.now());
38
+ }
34
39
  }
35
40
 
36
41
  /**
@@ -38,11 +43,27 @@ export function setFetchedTime(qryName: string, id: string, setOnlyIfUndefined:
38
43
  */
39
44
 
40
45
  export function isServerError(error: ServerErrorObj): boolean {
41
- return !!error && (
42
- error.statusCode === 500 ||
43
- error.errorCode === '30000' ||
44
- error.errorCode === 'network_error'
45
- );
46
+ return (
47
+ !!error &&
48
+ (error.statusCode === 500 ||
49
+ error.errorCode === '30000' ||
50
+ error.errorCode === 'network_error')
51
+ );
52
+ }
53
+
54
+ /**
55
+ * Return whether a request failed for a temporary transport or server reason
56
+ * that can be retried without changing the user's authentication context.
57
+ */
58
+ export function isTransientServerError(error?: ServerErrorObj | null): boolean {
59
+ const statusCode = Number(error?.statusCode || 0);
60
+ const errorCode = String(error?.errorCode || '');
61
+
62
+ return (
63
+ errorCode === 'network_error' ||
64
+ errorCode === '30000' ||
65
+ [500, 502, 503, 504].includes(statusCode)
66
+ );
46
67
  }
47
68
 
48
69
  /**
@@ -50,38 +71,44 @@ export function isServerError(error: ServerErrorObj): boolean {
50
71
  */
51
72
 
52
73
  export function isServerErrorGQL(apiResult: any): boolean {
53
- return (
54
- apiResult?.errors?.find((e: ServerErrorObj) => e.errorCode === '30000') ||
55
- apiResult?.error?.errorCode === 'network_error'
56
- );
74
+ return (
75
+ apiResult?.errors?.find((e: ServerErrorObj) => e.errorCode === '30000') ||
76
+ apiResult?.error?.errorCode === 'network_error'
77
+ );
57
78
  }
58
79
 
59
80
  /**
60
81
  * Headers for all network requests
61
82
  */
62
83
 
63
- export function apiRequestHeaders(authorization?: string | null, ipAddress?: string | null): Headers {
64
- const headers: Record<string, string> = {
65
- 'Content-Type': 'application/json; charset=utf-8',
66
- api_key: String(getENVVariable('GQL_API_KEY') || ''),
67
- origin: String(getENVVariable('APP_URL') || ''),
68
- 'Access-Control-Allow-Origin': String(getENVVariable('APP_URL') || ''),
69
- // recaptcha: ? // Not sure if we're going to do this later any more
70
- };
71
-
72
- if (!getENVVariable('IS_BROWSER')) {
73
- headers.ssr = '1';
74
-
75
- if (ipAddress) {
76
- // Do not allow undefined or null, it becomes a String type
77
- headers['x-ssr-ip'] = ipAddress;
78
- }
79
- }
80
-
81
- if (authorization) {
82
- // Do not send undefined or null, it becomes a String type
83
- headers.authorization = authorization;
84
- }
85
-
86
- return new Headers(headers);
84
+ export function apiRequestHeaders(
85
+ authorization?: string | null,
86
+ ipAddress?: string | null,
87
+ userAgent?: string | null,
88
+ ): Headers {
89
+ const headers: Record<string, string> = {
90
+ 'Content-Type': 'application/json; charset=utf-8',
91
+ 'x-api-key': String(getENVVariable('GQL_API_KEY') || ''),
92
+ // recaptcha: ? // Not sure if we're going to do this later any more
93
+ };
94
+
95
+ if (!getENVVariable('IS_BROWSER')) {
96
+ headers.ssr = '1';
97
+
98
+ if (ipAddress) {
99
+ // Do not allow undefined or null, it becomes a String type
100
+ headers['x-ssr-ip'] = ipAddress;
101
+ }
102
+
103
+ if (userAgent) {
104
+ headers['x-client-user-agent'] = userAgent;
105
+ }
106
+ }
107
+
108
+ if (authorization) {
109
+ // Do not send undefined or null, it becomes a String type
110
+ headers.authorization = authorization;
111
+ }
112
+
113
+ return new Headers(headers);
87
114
  }
@@ -1,16 +0,0 @@
1
-
2
- export const actionTaskFragment = `fragment actionTaskFragment on ActionTask {
3
- id
4
- logId
5
-
6
- title
7
- message
8
- instruction
9
-
10
- scheduledAt
11
- delay
12
- completed
13
-
14
- createdAt
15
- updatedAt
16
- }`;