@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.
- package/cache/config.ts +66 -42
- package/cache/index.test.ts +881 -0
- package/cache/index.ts +1815 -979
- package/cache/persist.ts +721 -0
- package/cache/store.ts +437 -0
- package/client/data-table-realtime-subscriptions.test.ts +740 -0
- package/client/data-table-realtime-subscriptions.ts +955 -0
- package/client/data-table-realtime.test.ts +792 -0
- package/client/data-table-realtime.ts +1218 -0
- package/client/fragment-realtime-sequence.ts +52 -0
- package/client/index.ts +4 -2
- package/client/open-sheet.ts +137 -0
- package/client/request-managed-session.test.ts +574 -0
- package/client/request-no-store.test.ts +157 -0
- package/client/request.ts +973 -143
- package/client/sse-ticket.test.ts +26 -0
- package/deno.lock +170 -0
- package/deno.test.json +10 -0
- package/drafts/index.ts +149 -0
- package/drafts/store.ts +397 -0
- package/gql/eagerQueryMap.ts +12 -0
- package/gql/fragments/accountAuthFragments.ts +5 -0
- package/gql/fragments/accountFragments.ts +1 -3
- package/gql/fragments/accountSessionFragments.ts +16 -0
- package/gql/fragments/agentDocFragments.ts +46 -0
- package/gql/fragments/agentMemoryFragments.ts +19 -0
- package/gql/fragments/aiChatFragments.ts +17 -0
- package/gql/fragments/billingFragments.ts +0 -11
- package/gql/fragments/campaignFragments.ts +277 -0
- package/gql/fragments/dataTableFragments.ts +102 -0
- package/gql/fragments/emailFragments.ts +36 -0
- package/gql/fragments/fieldDocFragments.ts +82 -0
- package/gql/fragments/inboundContactFragments.ts +15 -0
- package/gql/fragments/integrationFragments.ts +22 -0
- package/gql/fragments/logFragments.ts +33 -64
- package/gql/fragments/organizationFragments.ts +94 -21
- package/gql/fragments/scheduleFragments.ts +155 -0
- package/gql/fragments/sheetFragments.ts +675 -0
- package/gql/fragments/storageFragments.ts +21 -1
- package/gql/fragments/workflowFragments.ts +67 -0
- package/gql/mutations/accountMutations.ts +24 -4
- package/gql/mutations/agentDocMutations.ts +177 -0
- package/gql/mutations/agentMemoryMutations.ts +36 -0
- package/gql/mutations/aiChatMutations.ts +43 -3
- package/gql/mutations/authMutations.ts +160 -19
- package/gql/mutations/campaignMutations.ts +215 -0
- package/gql/mutations/dataTableMutations.ts +106 -0
- package/gql/mutations/fieldDocMutations.ts +24 -0
- package/gql/mutations/inboundContactMutations.ts +24 -0
- package/gql/mutations/index.ts +17 -8
- package/gql/mutations/integrationMutations.ts +25 -0
- package/gql/mutations/logMutations.ts +10 -19
- package/gql/mutations/organizationMutations.ts +160 -2
- package/gql/mutations/scheduleMutations.ts +423 -0
- package/gql/mutations/sheetMutations.ts +385 -0
- package/gql/mutations/storageMutations.ts +21 -2
- package/gql/mutations/workflowMutations.ts +42 -0
- package/gql/queries/agentDocQueries.ts +120 -0
- package/gql/queries/agentMemoryQueries.ts +22 -0
- package/gql/queries/aiChatQueries.ts +18 -2
- package/gql/queries/authQueries.ts +41 -0
- package/gql/queries/billingQueries.ts +24 -1
- package/gql/queries/campaignQueries.ts +127 -0
- package/gql/queries/dataTableQueries.ts +134 -0
- package/gql/queries/emailQueries.ts +96 -0
- package/gql/queries/fieldDocQueries.ts +158 -0
- package/gql/queries/inboundContactQueries.ts +40 -0
- package/gql/queries/index.ts +17 -6
- package/gql/queries/logQueries.ts +42 -32
- package/gql/queries/organizationQueries.ts +113 -8
- package/gql/queries/scheduleQueries.ts +315 -0
- package/gql/queries/sheetQueries.ts +383 -0
- package/gql/queries/storageQueries.ts +38 -0
- package/gql/queries/workflowQueries.ts +99 -0
- package/hooks/index.ts +2104 -860
- package/hooks/use-account-mtn.ts +91 -48
- package/hooks/use-agentDoc-mtn.ts +285 -0
- package/hooks/use-agentDoc-qry.ts +208 -0
- package/hooks/use-agentMemory-mtn.ts +73 -0
- package/hooks/use-agentMemory-qry.ts +44 -0
- package/hooks/use-aiChat-mtn.ts +248 -97
- package/hooks/use-aiChat-qry.ts +501 -191
- package/hooks/use-app-qry.ts +14 -11
- package/hooks/use-auth-mtn.ts +410 -256
- package/hooks/use-auth-qry.ts +51 -0
- package/hooks/use-campaign-mtn.ts +377 -0
- package/hooks/use-campaign-qry.ts +257 -0
- package/hooks/use-dataTable-mtn.ts +170 -0
- package/hooks/use-dataTable-qry.ts +996 -0
- package/hooks/use-email-qry.ts +227 -0
- package/hooks/use-fieldDoc-mtn.ts +50 -0
- package/hooks/use-fieldDoc-qry.ts +180 -0
- package/hooks/use-inboundContact-mtn.ts +34 -0
- package/hooks/use-inboundContact-qry.ts +96 -0
- package/hooks/use-integration-mtn.ts +77 -0
- package/hooks/use-integration-qry.ts +30 -0
- package/hooks/use-log-mtn.ts +77 -61
- package/hooks/use-log-qry.ts +99 -72
- package/hooks/use-organization-mtn.ts +409 -80
- package/hooks/use-organization-qry.ts +211 -92
- package/hooks/use-schedule-mtn.ts +416 -0
- package/hooks/use-schedule-qry.ts +157 -0
- package/hooks/use-sheet-mtn.ts +367 -0
- package/hooks/use-sheet-qry.ts +440 -0
- package/hooks/use-storage-mtn.ts +83 -42
- package/hooks/use-storage-qry.ts +104 -13
- package/hooks/use-workflow-mtn.ts +55 -0
- package/hooks/use-workflow-qry.ts +230 -0
- package/loaders/campaign.ts +137 -0
- package/package.json +94 -52
- package/sse.ts +158 -105
- package/tsconfig.json +13 -0
- package/types.d.ts +60 -29
- package/utils.ts +77 -50
- package/gql/fragments/actionFragments.ts +0 -16
- package/gql/fragments/productFragments.ts +0 -63
- package/gql/fragments/reportFragments.ts +0 -48
- package/gql/mutations/billingMutations.ts +0 -34
- package/gql/queries/productQueries.ts +0 -70
- package/gql/queries/reportQueries.ts +0 -66
- package/hooks/use-product-qry.ts +0 -109
- package/hooks/use-report-qry.ts +0 -69
package/sse.ts
CHANGED
|
@@ -1,143 +1,196 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
27
|
+
dataFragmentKey?: string;
|
|
22
28
|
data: Record<string, any>;
|
|
23
|
-
|
|
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(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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(
|
|
115
|
-
|
|
116
|
-
|
|
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(
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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 {
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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 = (
|
|
33
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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<
|
|
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
|
-
|
|
58
|
-
|
|
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
|
-
|
|
64
|
-
}> &
|
|
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(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
17
|
-
|
|
20
|
+
const now = Date.now();
|
|
21
|
+
const diff = now - lastFetched;
|
|
18
22
|
|
|
19
|
-
|
|
23
|
+
return diff > threshold;
|
|
20
24
|
}
|
|
21
25
|
|
|
22
26
|
/**
|
|
23
27
|
* Set fetched time
|
|
24
28
|
*/
|
|
25
29
|
|
|
26
|
-
export function setFetchedTime(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
}
|