@go-avro/avro-js 0.0.2-beta.13 → 0.0.2-beta.131
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/README.md +1 -0
- package/dist/auth/AuthManager.d.ts +12 -3
- package/dist/auth/AuthManager.js +56 -12
- package/dist/auth/storage.d.ts +8 -8
- package/dist/auth/storage.js +12 -10
- package/dist/client/QueryClient.d.ts +409 -15
- package/dist/client/QueryClient.js +343 -203
- package/dist/client/core/fetch.d.ts +1 -0
- package/dist/client/core/fetch.js +62 -0
- package/dist/client/core/utils.d.ts +1 -0
- package/dist/client/core/utils.js +14 -0
- package/dist/client/core/xhr.d.ts +1 -0
- package/dist/client/core/xhr.js +84 -0
- package/dist/client/hooks/analytics.d.ts +1 -0
- package/dist/client/hooks/analytics.js +26 -0
- package/dist/client/hooks/avro.d.ts +1 -0
- package/dist/client/hooks/avro.js +9 -0
- package/dist/client/hooks/bills.d.ts +1 -0
- package/dist/client/hooks/bills.js +165 -0
- package/dist/client/hooks/chats.d.ts +1 -0
- package/dist/client/hooks/chats.js +37 -0
- package/dist/client/hooks/companies.d.ts +1 -0
- package/dist/client/hooks/companies.js +137 -0
- package/dist/client/hooks/events.d.ts +1 -0
- package/dist/client/hooks/events.js +308 -0
- package/dist/client/hooks/jobs.d.ts +1 -0
- package/dist/client/hooks/jobs.js +215 -0
- package/dist/client/hooks/messages.d.ts +1 -0
- package/dist/client/hooks/messages.js +30 -0
- package/dist/client/hooks/months.d.ts +1 -0
- package/dist/client/hooks/months.js +93 -0
- package/dist/client/hooks/plans.d.ts +1 -0
- package/dist/client/hooks/plans.js +8 -0
- package/dist/client/hooks/root.d.ts +1 -0
- package/dist/client/hooks/root.js +8 -0
- package/dist/client/hooks/routes.d.ts +1 -0
- package/dist/client/hooks/routes.js +167 -0
- package/dist/client/hooks/sessions.d.ts +1 -0
- package/dist/client/hooks/sessions.js +175 -0
- package/dist/client/hooks/skills.d.ts +1 -0
- package/dist/client/hooks/skills.js +123 -0
- package/dist/client/hooks/teams.d.ts +1 -0
- package/dist/client/hooks/teams.js +128 -0
- package/dist/client/hooks/users.d.ts +1 -0
- package/dist/client/hooks/users.js +104 -0
- package/dist/index.d.ts +22 -1
- package/dist/index.js +22 -1
- package/dist/types/api.d.ts +124 -32
- package/dist/types/api.js +10 -1
- package/dist/types/auth.d.ts +0 -5
- package/dist/types/cache.d.ts +9 -0
- package/dist/types/cache.js +1 -0
- package/package.json +6 -4
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { StandardError } from '../../types/error';
|
|
3
|
+
AvroQueryClient.prototype._fetch = async function (method, path, body, cancelToken, headers = {}, isIdempotent = false, retryCount = 0) {
|
|
4
|
+
const checkCancelled = () => {
|
|
5
|
+
try {
|
|
6
|
+
if (cancelToken?.isCancelled()) {
|
|
7
|
+
throw new StandardError(0, 'Request cancelled');
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
throw new StandardError(0, `Error checking cancellation: ${error.message}`);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
try {
|
|
15
|
+
checkCancelled();
|
|
16
|
+
const token = await this.config.authManager.accessToken();
|
|
17
|
+
checkCancelled();
|
|
18
|
+
const url = this.config.baseUrl + path;
|
|
19
|
+
const requestHeaders = {
|
|
20
|
+
Authorization: `Bearer ${token}`,
|
|
21
|
+
...headers,
|
|
22
|
+
};
|
|
23
|
+
const options = {
|
|
24
|
+
method,
|
|
25
|
+
headers: requestHeaders,
|
|
26
|
+
body,
|
|
27
|
+
};
|
|
28
|
+
const response = await fetch(url, options);
|
|
29
|
+
if (response.ok) {
|
|
30
|
+
const text = await response.text();
|
|
31
|
+
if (!text) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
return JSON.parse(text);
|
|
35
|
+
}
|
|
36
|
+
if (response.status === 401 && this.config.authManager.refreshTokens && retryCount === 0) {
|
|
37
|
+
await this.config.authManager.refreshTokens();
|
|
38
|
+
return this._fetch(method, path, body, cancelToken, headers, isIdempotent, 1);
|
|
39
|
+
}
|
|
40
|
+
if (retryCount < this.config.maxRetries) {
|
|
41
|
+
const delay = this.getDelay(this.config.retryStrategy, retryCount);
|
|
42
|
+
await this.sleep(delay);
|
|
43
|
+
return this._fetch(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1);
|
|
44
|
+
}
|
|
45
|
+
let errorMessage = response.statusText;
|
|
46
|
+
try {
|
|
47
|
+
const parsedError = await response.json();
|
|
48
|
+
errorMessage = parsedError.message ?? parsedError.msg ?? errorMessage;
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
console.error('Ignoring:', e);
|
|
52
|
+
}
|
|
53
|
+
throw new StandardError(response.status, errorMessage);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (error instanceof StandardError) {
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
60
|
+
throw new StandardError(0, `Request failed: ${message}`);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
AvroQueryClient.prototype.getDelay = function (strategy, attempt) {
|
|
3
|
+
if (typeof strategy === 'function') {
|
|
4
|
+
return strategy(attempt);
|
|
5
|
+
}
|
|
6
|
+
else if (strategy === 'fixed') {
|
|
7
|
+
return 1000;
|
|
8
|
+
}
|
|
9
|
+
else if (strategy === 'exponential') {
|
|
10
|
+
return Math.pow(2, attempt) * 100;
|
|
11
|
+
}
|
|
12
|
+
throw new Error(`Invalid retry strategy: ${strategy}`);
|
|
13
|
+
};
|
|
14
|
+
AvroQueryClient.prototype.sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { StandardError } from '../../types/error';
|
|
3
|
+
AvroQueryClient.prototype._xhr = async function (method, path, body, cancelToken, headers = {}, isIdempotent = false, retryCount = 0, progressUpdateCallback) {
|
|
4
|
+
const checkCancelled = () => {
|
|
5
|
+
if (cancelToken?.isCancelled()) {
|
|
6
|
+
throw new StandardError(0, 'Request cancelled');
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
for (let attempt = 0; attempt <= this.config.maxRetries; attempt++) {
|
|
10
|
+
try {
|
|
11
|
+
checkCancelled();
|
|
12
|
+
const token = await this.config.authManager.accessToken();
|
|
13
|
+
checkCancelled();
|
|
14
|
+
const result = await new Promise((resolve, reject) => {
|
|
15
|
+
const xhr = new XMLHttpRequest();
|
|
16
|
+
const url = this.config.baseUrl + path;
|
|
17
|
+
xhr.open(method, url, true);
|
|
18
|
+
if (token)
|
|
19
|
+
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
|
|
20
|
+
Object.entries(headers).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
|
21
|
+
xhr.onload = () => {
|
|
22
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
23
|
+
try {
|
|
24
|
+
const responseText = xhr.responseText;
|
|
25
|
+
if (!responseText) {
|
|
26
|
+
return resolve(undefined);
|
|
27
|
+
}
|
|
28
|
+
resolve(JSON.parse(responseText));
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
reject(new StandardError(0, `Failed to parse successful response: ${e.message}`));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
let msg = xhr.responseText || xhr.statusText;
|
|
36
|
+
try {
|
|
37
|
+
const parsed = JSON.parse(xhr.responseText);
|
|
38
|
+
msg = parsed.message ?? parsed.msg ?? msg;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
console.error('Ignoring:', e);
|
|
42
|
+
}
|
|
43
|
+
reject(new StandardError(xhr.status, msg));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
xhr.onerror = () => reject(new StandardError(0, 'Network Error'));
|
|
47
|
+
if (this.config.timeout) {
|
|
48
|
+
xhr.timeout = this.config.timeout;
|
|
49
|
+
xhr.ontimeout = () => reject(new StandardError(0, 'Request timed out'));
|
|
50
|
+
}
|
|
51
|
+
if (progressUpdateCallback && xhr.upload) {
|
|
52
|
+
xhr.upload.onprogress = (event) => {
|
|
53
|
+
if (event.lengthComputable) {
|
|
54
|
+
progressUpdateCallback(event.loaded, event.total);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
xhr.send(body);
|
|
59
|
+
});
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (!(error instanceof StandardError)) {
|
|
64
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
65
|
+
throw new StandardError(0, `An unexpected error occurred: ${message}`);
|
|
66
|
+
}
|
|
67
|
+
if (error.status === 401 && this.config.authManager.refreshTokens && attempt === 0) {
|
|
68
|
+
try {
|
|
69
|
+
await this.config.authManager.refreshTokens();
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
catch (refreshError) {
|
|
73
|
+
throw new StandardError(401, 'Unauthorized (refresh failed)');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (attempt >= this.config.maxRetries) {
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
const delay = this.getDelay(this.config.retryStrategy, attempt);
|
|
80
|
+
await this.sleep(delay);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
throw new StandardError(0, 'Request failed after maximum retries.');
|
|
84
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useQuery } from "@tanstack/react-query";
|
|
2
|
+
import { AvroQueryClient } from "../../client/QueryClient";
|
|
3
|
+
AvroQueryClient.prototype.useGetAnalytics = function () {
|
|
4
|
+
return useQuery({
|
|
5
|
+
queryKey: ['analytics'],
|
|
6
|
+
queryFn: () => this.post('/avro/analytics', null),
|
|
7
|
+
enabled: true,
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
AvroQueryClient.prototype.useFinanceAnalytics = function ({ periods, cumulative }) {
|
|
11
|
+
return useQuery({
|
|
12
|
+
queryKey: ['analytics', 'finance', this.companyId, periods, cumulative],
|
|
13
|
+
queryFn: () => this.post(`/company/${this.companyId}/analytics/finance`, JSON.stringify({ periods, cumulative }), undefined, {
|
|
14
|
+
'Content-Type': 'application/json',
|
|
15
|
+
}),
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
AvroQueryClient.prototype.useEventAnalytics = function ({ periods }) {
|
|
19
|
+
return useQuery({
|
|
20
|
+
queryKey: ['analytics', 'events', this.companyId, periods],
|
|
21
|
+
queryFn: () => this.post(`/company/${this.companyId}/analytics/events`, JSON.stringify({ periods }), undefined, {
|
|
22
|
+
'Content-Type': 'application/json',
|
|
23
|
+
}),
|
|
24
|
+
enabled: Boolean(this.companyId),
|
|
25
|
+
});
|
|
26
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useQuery } from "@tanstack/react-query";
|
|
2
|
+
import { AvroQueryClient } from "../../client/QueryClient";
|
|
3
|
+
AvroQueryClient.prototype.useGetAvro = function () {
|
|
4
|
+
return useQuery({
|
|
5
|
+
queryKey: ['avro'],
|
|
6
|
+
queryFn: () => this.get('/avro'),
|
|
7
|
+
enabled: true,
|
|
8
|
+
});
|
|
9
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { useInfiniteQuery, useQuery, useMutation } from '@tanstack/react-query';
|
|
2
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
3
|
+
AvroQueryClient.prototype.useGetBills = function (body) {
|
|
4
|
+
const queryClient = this.getQueryClient();
|
|
5
|
+
const result = useInfiniteQuery({
|
|
6
|
+
queryKey: [
|
|
7
|
+
'bills',
|
|
8
|
+
this.companyId,
|
|
9
|
+
body.query ?? "",
|
|
10
|
+
body.known_ids ?? [],
|
|
11
|
+
body.unknown_ids ?? [],
|
|
12
|
+
body.paid ?? false,
|
|
13
|
+
],
|
|
14
|
+
initialPageParam: 0,
|
|
15
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
16
|
+
if (lastPage.length < (body.amt ?? 50))
|
|
17
|
+
return undefined;
|
|
18
|
+
return allPages.flat().length; // next offset
|
|
19
|
+
},
|
|
20
|
+
queryFn: ({ pageParam = 0 }) => this.fetchBills({ ...body, offset: pageParam }),
|
|
21
|
+
});
|
|
22
|
+
if (result.data) {
|
|
23
|
+
result.data.pages.forEach((data_page) => {
|
|
24
|
+
data_page.forEach((bill) => {
|
|
25
|
+
queryClient.setQueryData(['bill', bill.id], bill);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
AvroQueryClient.prototype.useGetBill = function (billId) {
|
|
32
|
+
return useQuery({
|
|
33
|
+
queryKey: ['bill', billId],
|
|
34
|
+
queryFn: () => this.get(`/bill/${billId}`),
|
|
35
|
+
enabled: Boolean(billId),
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
AvroQueryClient.prototype.useCreateBill = function () {
|
|
39
|
+
const queryClient = this.getQueryClient();
|
|
40
|
+
return useMutation({
|
|
41
|
+
mutationFn: async ({ data }) => {
|
|
42
|
+
const body = {
|
|
43
|
+
events: data.events ?? [],
|
|
44
|
+
months: data.months ?? [],
|
|
45
|
+
line_items: data.line_items ?? [],
|
|
46
|
+
manual_emails: data.manual_emails,
|
|
47
|
+
users: data.users,
|
|
48
|
+
due_date: data.due_date,
|
|
49
|
+
};
|
|
50
|
+
return this.post(`/company/${this.companyId}/bill`, JSON.stringify(body), undefined, {
|
|
51
|
+
'Content-Type': 'application/json',
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
onMutate: async () => {
|
|
55
|
+
await queryClient.cancelQueries({ queryKey: ['bills', this.companyId] });
|
|
56
|
+
const previousBills = queryClient.getQueryData(['bills', this.companyId]);
|
|
57
|
+
// TODO: Create a fake bill object for optimistic update and update events and months accordingly
|
|
58
|
+
return { previousBills };
|
|
59
|
+
},
|
|
60
|
+
onError: (_err, _variables, context) => {
|
|
61
|
+
if (context?.previousBills) {
|
|
62
|
+
queryClient.setQueryData(['bills'], context.previousBills);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
onSettled: (_data, _error) => {
|
|
66
|
+
queryClient.invalidateQueries({ queryKey: ['bills', this.companyId] });
|
|
67
|
+
queryClient.invalidateQueries({ queryKey: ['events', this.companyId] });
|
|
68
|
+
queryClient.invalidateQueries({ queryKey: ['months', this.companyId] });
|
|
69
|
+
queryClient.invalidateQueries({ queryKey: ['company'] });
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
AvroQueryClient.prototype.useUpdateBill = function () {
|
|
74
|
+
const queryClient = this.getQueryClient();
|
|
75
|
+
return useMutation({
|
|
76
|
+
mutationFn: ({ billId, updates }) => {
|
|
77
|
+
return this.put(`/bill/${billId}`, JSON.stringify(updates), undefined, {
|
|
78
|
+
"Content-Type": "application/json",
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
onMutate: async ({ billId, updates }) => {
|
|
82
|
+
await queryClient.cancelQueries({ queryKey: ['bills'] });
|
|
83
|
+
await queryClient.cancelQueries({ queryKey: ['bill', billId] });
|
|
84
|
+
const previousBills = queryClient.getQueryData(['bills']);
|
|
85
|
+
const previousBill = queryClient.getQueryData(['bill', billId]);
|
|
86
|
+
queryClient.setQueryData(['bill', billId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
|
|
87
|
+
queryClient.setQueriesData({ queryKey: ['bills'] }, (oldData) => {
|
|
88
|
+
if (!oldData)
|
|
89
|
+
return oldData;
|
|
90
|
+
if (oldData.pages) {
|
|
91
|
+
return {
|
|
92
|
+
...oldData,
|
|
93
|
+
pages: oldData.pages.map((page) => page.map((bill) => bill.id === billId ? { ...bill, ...updates } : bill)),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (Array.isArray(oldData)) {
|
|
97
|
+
return oldData.map((bill) => bill.id === billId ? { ...bill, ...updates } : bill);
|
|
98
|
+
}
|
|
99
|
+
return oldData;
|
|
100
|
+
});
|
|
101
|
+
return { previousBills, previousBill };
|
|
102
|
+
},
|
|
103
|
+
onError: (err, variables, context) => {
|
|
104
|
+
const { billId } = variables;
|
|
105
|
+
if (context?.previousBills) {
|
|
106
|
+
queryClient.setQueryData(['bills'], context.previousBills);
|
|
107
|
+
}
|
|
108
|
+
if (context?.previousBill) {
|
|
109
|
+
queryClient.setQueryData(['bill', billId], context.previousBill);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
onSettled: (data, error, variables) => {
|
|
113
|
+
const { billId } = variables;
|
|
114
|
+
queryClient.invalidateQueries({ queryKey: ['bills'] });
|
|
115
|
+
queryClient.invalidateQueries({ queryKey: ['bill', billId] });
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
AvroQueryClient.prototype.useDeleteBill = function () {
|
|
120
|
+
const queryClient = this.getQueryClient();
|
|
121
|
+
return useMutation({
|
|
122
|
+
mutationFn: async ({ billId }) => {
|
|
123
|
+
return this.delete(`/bill/${billId}`);
|
|
124
|
+
},
|
|
125
|
+
onMutate: async ({ billId }) => {
|
|
126
|
+
await queryClient.cancelQueries({ queryKey: ['bills'] });
|
|
127
|
+
const previousBills = queryClient.getQueryData(['bills']);
|
|
128
|
+
// TODO: Create a fake bill object for optimistic update and update events and months accordingly
|
|
129
|
+
return { previousBills };
|
|
130
|
+
},
|
|
131
|
+
onError: (_err, _variables, context) => {
|
|
132
|
+
if (context?.previousBills) {
|
|
133
|
+
queryClient.setQueryData(['bills'], context.previousBills);
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
onSettled: (_data, _error) => {
|
|
137
|
+
queryClient.invalidateQueries({ queryKey: ['bills'] });
|
|
138
|
+
queryClient.invalidateQueries({ queryKey: ['events'] });
|
|
139
|
+
queryClient.invalidateQueries({ queryKey: ['months'] });
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
AvroQueryClient.prototype.useSyncBillToIntuit = function () {
|
|
144
|
+
const queryClient = this.getQueryClient();
|
|
145
|
+
return useMutation({
|
|
146
|
+
mutationFn: async ({ billId }) => {
|
|
147
|
+
return this.post(`/company/${this.companyId}/bill/${billId}/intuit/sync`, null);
|
|
148
|
+
},
|
|
149
|
+
onMutate: async ({ billId }) => {
|
|
150
|
+
await queryClient.cancelQueries({ queryKey: ['bill', billId] });
|
|
151
|
+
const previousBill = queryClient.getQueryData(['bill', billId]);
|
|
152
|
+
return { previousBill };
|
|
153
|
+
},
|
|
154
|
+
onError: (_err, variables, context) => {
|
|
155
|
+
const { billId } = variables;
|
|
156
|
+
if (context?.previousBill) {
|
|
157
|
+
queryClient.setQueryData(['bill', billId], context.previousBill);
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
onSettled: (_data, _error, variables) => {
|
|
161
|
+
const { billId } = variables;
|
|
162
|
+
queryClient.invalidateQueries({ queryKey: ['bill', billId] });
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useInfiniteQuery, useQuery } from "@tanstack/react-query";
|
|
2
|
+
import { AvroQueryClient } from "../../client/QueryClient";
|
|
3
|
+
AvroQueryClient.prototype.useGetChats = function (body) {
|
|
4
|
+
const queryClient = this.getQueryClient();
|
|
5
|
+
const result = useInfiniteQuery({
|
|
6
|
+
queryKey: [
|
|
7
|
+
'chats',
|
|
8
|
+
this.companyId,
|
|
9
|
+
body.amt ?? 50,
|
|
10
|
+
body.known_ids ?? [],
|
|
11
|
+
body.unknown_ids ?? [],
|
|
12
|
+
body.query ?? '',
|
|
13
|
+
],
|
|
14
|
+
initialPageParam: 0,
|
|
15
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
16
|
+
if (lastPage.length < (body.amt ?? 50))
|
|
17
|
+
return undefined;
|
|
18
|
+
return allPages.flat().length; // next offset
|
|
19
|
+
},
|
|
20
|
+
queryFn: ({ pageParam = 0 }) => this.fetchChats({ ...body, offset: pageParam }),
|
|
21
|
+
});
|
|
22
|
+
if (result.data) {
|
|
23
|
+
result.data.pages.forEach((data_page) => {
|
|
24
|
+
data_page.forEach((chat) => {
|
|
25
|
+
queryClient.setQueryData(['chat', chat.id], chat);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
AvroQueryClient.prototype.useGetChat = function (chatId) {
|
|
32
|
+
return useQuery({
|
|
33
|
+
queryKey: ['chat', chatId],
|
|
34
|
+
queryFn: () => this.get(`/chat/${chatId}`),
|
|
35
|
+
enabled: Boolean(chatId),
|
|
36
|
+
});
|
|
37
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
2
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
3
|
+
AvroQueryClient.prototype.useGetCompanies = function (options = {}) {
|
|
4
|
+
return useQuery({
|
|
5
|
+
queryKey: ['/company/list'],
|
|
6
|
+
queryFn: () => this.get('/company/list'),
|
|
7
|
+
...options,
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
AvroQueryClient.prototype.useGetCompany = function (companyId) {
|
|
11
|
+
return useQuery({
|
|
12
|
+
queryKey: ['company', companyId],
|
|
13
|
+
queryFn: () => this.get(`/company/${companyId}`),
|
|
14
|
+
enabled: Boolean(companyId),
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
AvroQueryClient.prototype.useGetCurrentCompany = function () {
|
|
18
|
+
return useQuery({
|
|
19
|
+
queryKey: ['company', 'current'],
|
|
20
|
+
queryFn: async () => {
|
|
21
|
+
if (!this.companyId) {
|
|
22
|
+
const companyList = await this.get(`/company/list`);
|
|
23
|
+
if (companyList.length > 0) {
|
|
24
|
+
this.companyId = companyList[0].id;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
throw new Error("No company ID set and no companies available");
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
this.company = await this.get(`/company/${this.companyId}`);
|
|
31
|
+
return this.company;
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
AvroQueryClient.prototype.useCreateCompany = function () {
|
|
36
|
+
const queryClient = this.getQueryClient();
|
|
37
|
+
return useMutation({
|
|
38
|
+
mutationFn: async ({ companyData }) => {
|
|
39
|
+
return this.post(`/company`, JSON.stringify(companyData), undefined, { "Content-Type": "application/json" });
|
|
40
|
+
},
|
|
41
|
+
onSettled: () => {
|
|
42
|
+
queryClient.invalidateQueries({ queryKey: ['/company/list'] });
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
AvroQueryClient.prototype.useUpdateCompany = function () {
|
|
47
|
+
const queryClient = this.getQueryClient();
|
|
48
|
+
return useMutation({
|
|
49
|
+
mutationFn: async ({ companyId, companyData, }) => {
|
|
50
|
+
return this.put(`/company/${companyId}`, JSON.stringify(companyData), undefined, { "Content-Type": "application/json" });
|
|
51
|
+
},
|
|
52
|
+
onMutate: async ({ companyId, companyData }) => {
|
|
53
|
+
await queryClient.cancelQueries({ queryKey: ['company', companyId] });
|
|
54
|
+
await queryClient.cancelQueries({ queryKey: ['/company/list'] });
|
|
55
|
+
const previousCompany = queryClient.getQueryData(['company', companyId]);
|
|
56
|
+
const previousCompanyList = queryClient.getQueryData(['/company/list']);
|
|
57
|
+
queryClient.setQueryData(['company', companyId], (oldData) => oldData ? { ...oldData, ...companyData } : undefined);
|
|
58
|
+
queryClient.setQueryData(['/company/list'], (oldList) => {
|
|
59
|
+
if (!oldList)
|
|
60
|
+
return oldList;
|
|
61
|
+
return oldList.map((company) => company.id === companyId ? { ...company, ...companyData } : company);
|
|
62
|
+
});
|
|
63
|
+
return { previousCompany, previousCompanyList };
|
|
64
|
+
},
|
|
65
|
+
onError: (_err, variables, context) => {
|
|
66
|
+
const { companyId } = variables;
|
|
67
|
+
if (context?.previousCompany) {
|
|
68
|
+
queryClient.setQueryData(['company', companyId], context.previousCompany);
|
|
69
|
+
}
|
|
70
|
+
if (context?.previousCompanyList) {
|
|
71
|
+
queryClient.setQueryData(['/company/list'], context.previousCompanyList);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
onSettled: (_data, _error, variables) => {
|
|
75
|
+
const { companyId } = variables;
|
|
76
|
+
queryClient.invalidateQueries({ queryKey: ['company', companyId] });
|
|
77
|
+
queryClient.invalidateQueries({ queryKey: ['/company/list'] });
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
AvroQueryClient.prototype.useRemoveUserCompany = function () {
|
|
82
|
+
const queryClient = this.getQueryClient();
|
|
83
|
+
return useMutation({
|
|
84
|
+
mutationFn: async ({ userId }) => {
|
|
85
|
+
return this.delete(`/company/${this.companyId}/user/${userId}`);
|
|
86
|
+
},
|
|
87
|
+
onMutate: async ({ userId }) => {
|
|
88
|
+
await queryClient.cancelQueries({ queryKey: ['company', this.companyId] });
|
|
89
|
+
const previousCompany = queryClient.getQueryData(['company', this.companyId]);
|
|
90
|
+
queryClient.setQueryData(['company', this.companyId], (oldData) => {
|
|
91
|
+
if (!oldData)
|
|
92
|
+
return oldData;
|
|
93
|
+
return {
|
|
94
|
+
...oldData,
|
|
95
|
+
users: oldData.users.filter((user) => user.id !== userId),
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
return { previousCompany };
|
|
99
|
+
},
|
|
100
|
+
onError: (err, variables, context) => {
|
|
101
|
+
if (context?.previousCompany) {
|
|
102
|
+
queryClient.setQueryData(['company', this.companyId], context.previousCompany);
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
onSettled: (_data, _error, variables) => {
|
|
106
|
+
queryClient.invalidateQueries({ queryKey: ['company', this.companyId] });
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
AvroQueryClient.prototype.useDeleteCompany = function () {
|
|
111
|
+
const queryClient = this.getQueryClient();
|
|
112
|
+
return useMutation({
|
|
113
|
+
mutationFn: async ({ companyId }) => {
|
|
114
|
+
return this.delete(`/company/${companyId}`);
|
|
115
|
+
},
|
|
116
|
+
onMutate: async ({ companyId }) => {
|
|
117
|
+
await queryClient.cancelQueries({ queryKey: ['/company/list'] });
|
|
118
|
+
await queryClient.cancelQueries({ queryKey: ['company', companyId] });
|
|
119
|
+
const previousCompanyList = queryClient.getQueryData(['/company/list']);
|
|
120
|
+
queryClient.setQueryData(['/company/list'], (oldList) => {
|
|
121
|
+
if (!oldList)
|
|
122
|
+
return oldList;
|
|
123
|
+
return oldList.filter((company) => company.id !== companyId);
|
|
124
|
+
});
|
|
125
|
+
return { previousCompanyList };
|
|
126
|
+
},
|
|
127
|
+
onError: (err, companyId, context) => {
|
|
128
|
+
if (context?.previousCompanyList) {
|
|
129
|
+
queryClient.setQueryData(['/company/list'], context.previousCompanyList);
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
onSettled: (_data, _error, companyId) => {
|
|
133
|
+
queryClient.invalidateQueries({ queryKey: ['/company/list'] });
|
|
134
|
+
queryClient.invalidateQueries({ queryKey: ['company', companyId] });
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|