@go-avro/avro-js 0.0.2-beta.34 → 0.0.2-beta.36
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/dist/client/QueryClient.d.ts +59 -5
- package/dist/client/QueryClient.js +16 -160
- package/dist/client/core/fetch.d.ts +1 -0
- package/dist/client/core/fetch.js +67 -0
- package/dist/client/core/utils.d.ts +1 -0
- package/dist/client/core/utils.js +13 -0
- package/dist/client/core/xhr.d.ts +1 -0
- package/dist/client/core/xhr.js +80 -0
- package/dist/client/hooks/bills.d.ts +1 -0
- package/dist/client/hooks/bills.js +30 -0
- package/dist/client/hooks/companies.d.ts +1 -0
- package/dist/client/hooks/companies.js +16 -0
- package/dist/client/hooks/events.d.ts +1 -0
- package/dist/client/hooks/events.js +34 -0
- package/dist/client/hooks/jobs.d.ts +1 -0
- package/dist/client/hooks/jobs.js +47 -0
- package/dist/client/hooks/months.d.ts +1 -0
- package/dist/client/hooks/months.js +34 -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 +31 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/types/api.d.ts +1 -0
- package/package.json +5 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { AuthManager } from '../auth/AuthManager';
|
|
2
|
-
import { LineItem } from '../types/api';
|
|
2
|
+
import { _Event, Bill, Company, Job, LineItem, ServiceMonth } from '../types/api';
|
|
3
3
|
import { CancelToken, RetryStrategy } from '../types/client';
|
|
4
|
+
import { StandardError } from '../types/error';
|
|
5
|
+
import { InfiniteData, UseInfiniteQueryResult, useQuery, UseQueryResult } from '@tanstack/react-query';
|
|
4
6
|
export interface AvroQueryClientConfig {
|
|
5
7
|
baseUrl: string;
|
|
6
8
|
authManager: AuthManager;
|
|
@@ -8,12 +10,57 @@ export interface AvroQueryClientConfig {
|
|
|
8
10
|
retryStrategy?: RetryStrategy;
|
|
9
11
|
timeout?: number;
|
|
10
12
|
}
|
|
13
|
+
declare module '../client/QueryClient' {
|
|
14
|
+
interface AvroQueryClient {
|
|
15
|
+
_xhr<T>(method: string, path: string, body: any, cancelToken?: CancelToken, headers?: Record<string, string>, isIdempotent?: boolean, retryCount?: number): Promise<T>;
|
|
16
|
+
_fetch<T>(method: string, path: string, body: any, cancelToken?: CancelToken, headers?: Record<string, string>, isIdempotent?: boolean, retryCount?: number): Promise<T>;
|
|
17
|
+
getDelay(strategy: RetryStrategy, attempt: number): number;
|
|
18
|
+
getRoot(): ReturnType<typeof useQuery>;
|
|
19
|
+
getJobs(companyGuid: string, body: {
|
|
20
|
+
amt?: number;
|
|
21
|
+
query?: string;
|
|
22
|
+
}, total: number, onProgress?: (fraction: number) => void, isMainLoad?: boolean): UseQueryResult<Job[], StandardError>;
|
|
23
|
+
getRoutes(companyGuid: string, body: {
|
|
24
|
+
amt?: number;
|
|
25
|
+
query?: string;
|
|
26
|
+
}, total: number, onProgress?: (fraction: number) => void): UseQueryResult<any[], StandardError>;
|
|
27
|
+
getEvents(companyGuid: string, body: {
|
|
28
|
+
amt?: number;
|
|
29
|
+
known_ids?: string[];
|
|
30
|
+
unknown_ids?: string[];
|
|
31
|
+
query?: string;
|
|
32
|
+
unbilled?: boolean;
|
|
33
|
+
billed?: boolean;
|
|
34
|
+
paid?: boolean;
|
|
35
|
+
jobId?: string;
|
|
36
|
+
}): UseInfiniteQueryResult<InfiniteData<_Event[], unknown>, Error>;
|
|
37
|
+
getMonths(companyGuid: string, body: {
|
|
38
|
+
amt?: number;
|
|
39
|
+
known_ids?: string[];
|
|
40
|
+
unknown_ids?: string[];
|
|
41
|
+
query?: string;
|
|
42
|
+
unbilled?: boolean;
|
|
43
|
+
billed?: boolean;
|
|
44
|
+
paid?: boolean;
|
|
45
|
+
jobId?: string;
|
|
46
|
+
}): UseInfiniteQueryResult<InfiniteData<ServiceMonth[], unknown>, Error>;
|
|
47
|
+
getBills(companyGuid: string, body: {
|
|
48
|
+
amt?: number;
|
|
49
|
+
known_ids?: string[];
|
|
50
|
+
unknown_ids?: string[];
|
|
51
|
+
query?: string;
|
|
52
|
+
paid?: boolean;
|
|
53
|
+
}): UseInfiniteQueryResult<InfiniteData<Bill[], unknown>, Error>;
|
|
54
|
+
getCompanies(options?: {}): UseQueryResult<{
|
|
55
|
+
name: string;
|
|
56
|
+
id: string;
|
|
57
|
+
}[], StandardError>;
|
|
58
|
+
getCompany(companyId: string): UseQueryResult<Company, StandardError>;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
11
61
|
export declare class AvroQueryClient {
|
|
12
|
-
|
|
62
|
+
protected config: Required<AvroQueryClientConfig>;
|
|
13
63
|
constructor(config: AvroQueryClientConfig);
|
|
14
|
-
getDelay(strategy: RetryStrategy, attempt: number): number;
|
|
15
|
-
private _xhr;
|
|
16
|
-
private _fetch;
|
|
17
64
|
get<T>(path: string, cancelToken?: CancelToken, headers?: Record<string, string>): Promise<T>;
|
|
18
65
|
post<T>(path: string, data: any, cancelToken?: CancelToken, headers?: Record<string, string>): Promise<T>;
|
|
19
66
|
put<T>(path: string, data: any, cancelToken?: CancelToken, headers?: Record<string, string>): Promise<T>;
|
|
@@ -60,6 +107,13 @@ export declare class AvroQueryClient {
|
|
|
60
107
|
offset?: number;
|
|
61
108
|
paid?: boolean;
|
|
62
109
|
}, cancelToken?: CancelToken, headers?: Record<string, string>): Promise<any>;
|
|
110
|
+
fetchRoutes(companyGuid: string, body?: {
|
|
111
|
+
amt?: number;
|
|
112
|
+
known_ids?: string[];
|
|
113
|
+
unknown_ids?: string[];
|
|
114
|
+
query?: string;
|
|
115
|
+
offset?: number;
|
|
116
|
+
}, cancelToken?: CancelToken, headers?: Record<string, string>): Promise<any>;
|
|
63
117
|
sendEmail(emailId: string, formData: FormData): Promise<void>;
|
|
64
118
|
createBill(companyGuid: string, data: {
|
|
65
119
|
line_items: LineItem[];
|
|
@@ -9,166 +9,6 @@ export class AvroQueryClient {
|
|
|
9
9
|
timeout: config.timeout ?? 0,
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
|
-
getDelay(strategy, attempt) {
|
|
13
|
-
if (typeof strategy === 'function') {
|
|
14
|
-
return strategy(attempt);
|
|
15
|
-
}
|
|
16
|
-
else if (strategy === 'fixed') {
|
|
17
|
-
return 1000;
|
|
18
|
-
}
|
|
19
|
-
else if (strategy === 'exponential') {
|
|
20
|
-
return Math.pow(2, attempt) * 100;
|
|
21
|
-
}
|
|
22
|
-
throw new Error(`Invalid retry strategy: ${strategy}`);
|
|
23
|
-
}
|
|
24
|
-
_xhr(method, path, body, cancelToken, headers = {}, isIdempotent = false, retryCount = 0) {
|
|
25
|
-
const checkCancelled = () => {
|
|
26
|
-
if (cancelToken?.isCancelled()) {
|
|
27
|
-
return new StandardError(0, 'Request cancelled');
|
|
28
|
-
}
|
|
29
|
-
return null;
|
|
30
|
-
};
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
this.config.authManager.accessToken().then(token => {
|
|
33
|
-
const cancelErr = checkCancelled();
|
|
34
|
-
if (cancelErr)
|
|
35
|
-
return reject(cancelErr);
|
|
36
|
-
const xhr = new XMLHttpRequest();
|
|
37
|
-
const url = this.config.baseUrl + path;
|
|
38
|
-
xhr.open(method, url, true);
|
|
39
|
-
if (token) {
|
|
40
|
-
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
|
|
41
|
-
}
|
|
42
|
-
Object.entries(headers).forEach(([key, value]) => {
|
|
43
|
-
xhr.setRequestHeader(key, value);
|
|
44
|
-
});
|
|
45
|
-
xhr.onload = () => {
|
|
46
|
-
const cancelErr = checkCancelled();
|
|
47
|
-
if (cancelErr)
|
|
48
|
-
return reject(cancelErr);
|
|
49
|
-
if (xhr.status === 401 && this.config.authManager.refreshTokens && retryCount === 0) {
|
|
50
|
-
this.config.authManager
|
|
51
|
-
.refreshTokens()
|
|
52
|
-
.then(() => {
|
|
53
|
-
this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
|
|
54
|
-
})
|
|
55
|
-
.catch(() => {
|
|
56
|
-
reject(new StandardError(401, 'Unauthorized (refresh failed)'));
|
|
57
|
-
});
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
if (xhr.status >= 200 && xhr.status < 300) {
|
|
61
|
-
try {
|
|
62
|
-
resolve(JSON.parse(xhr.responseText));
|
|
63
|
-
}
|
|
64
|
-
catch {
|
|
65
|
-
resolve(xhr.responseText);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
if (retryCount < this.config.maxRetries) {
|
|
70
|
-
const delay = this.getDelay(this.config.retryStrategy, retryCount);
|
|
71
|
-
setTimeout(() => {
|
|
72
|
-
this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
|
|
73
|
-
}, delay);
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
let msg = xhr.statusText;
|
|
77
|
-
try {
|
|
78
|
-
const parsed = JSON.parse(xhr.responseText);
|
|
79
|
-
msg = parsed.msg || msg;
|
|
80
|
-
}
|
|
81
|
-
catch {
|
|
82
|
-
console.warn('Failed to parse error response:', xhr.responseText);
|
|
83
|
-
}
|
|
84
|
-
reject(new StandardError(xhr.status, msg));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
xhr.onerror = () => {
|
|
89
|
-
if (retryCount < this.config.maxRetries) {
|
|
90
|
-
const delay = this.getDelay(this.config.retryStrategy, retryCount);
|
|
91
|
-
setTimeout(() => {
|
|
92
|
-
this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
|
|
93
|
-
}, delay);
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
reject(new StandardError(0, 'Network Error'));
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
if (this.config.timeout) {
|
|
100
|
-
xhr.timeout = this.config.timeout;
|
|
101
|
-
xhr.ontimeout = () => reject(new StandardError(0, 'Request timed out'));
|
|
102
|
-
}
|
|
103
|
-
xhr.send(body);
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
_fetch(method, path, body, cancelToken, headers = {}, isIdempotent = false, retryCount = 0) {
|
|
108
|
-
const checkCancelled = () => {
|
|
109
|
-
try {
|
|
110
|
-
if (cancelToken?.isCancelled()) {
|
|
111
|
-
return new StandardError(0, 'Request cancelled');
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
catch (error) {
|
|
115
|
-
throw new StandardError(0, `Error checking cancellation (${typeof cancelToken}): ${error}`);
|
|
116
|
-
}
|
|
117
|
-
return null;
|
|
118
|
-
};
|
|
119
|
-
return this.config.authManager.accessToken().then(token => {
|
|
120
|
-
const cancelErr = checkCancelled();
|
|
121
|
-
if (cancelErr)
|
|
122
|
-
return Promise.reject(cancelErr);
|
|
123
|
-
const url = this.config.baseUrl + path;
|
|
124
|
-
const requestHeaders = {
|
|
125
|
-
'Content-Type': 'application/json',
|
|
126
|
-
...headers,
|
|
127
|
-
};
|
|
128
|
-
if (token) {
|
|
129
|
-
requestHeaders['Authorization'] = `Bearer ${token}`;
|
|
130
|
-
}
|
|
131
|
-
const options = {
|
|
132
|
-
method,
|
|
133
|
-
headers: requestHeaders,
|
|
134
|
-
body: body ? JSON.stringify(body) : null,
|
|
135
|
-
};
|
|
136
|
-
return fetch(url, options).then(response => {
|
|
137
|
-
if (response.status === 401 && this.config.authManager.refreshTokens && retryCount === 0) {
|
|
138
|
-
return this.config.authManager
|
|
139
|
-
.refreshTokens()
|
|
140
|
-
.then(() => this._fetch(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1))
|
|
141
|
-
.catch(() => Promise.reject(new StandardError(401, 'Unauthorized (refresh failed)')));
|
|
142
|
-
}
|
|
143
|
-
if (!response.ok) {
|
|
144
|
-
if (retryCount < this.config.maxRetries) {
|
|
145
|
-
const delay = this.getDelay(this.config.retryStrategy, retryCount);
|
|
146
|
-
return new Promise((resolve, reject) => {
|
|
147
|
-
setTimeout(() => {
|
|
148
|
-
this._fetch(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1)
|
|
149
|
-
.then(resolve)
|
|
150
|
-
.catch(reject);
|
|
151
|
-
}, delay);
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
return response.text().then(text => {
|
|
156
|
-
let msg = response.statusText;
|
|
157
|
-
try {
|
|
158
|
-
const parsed = JSON.parse(text);
|
|
159
|
-
msg = parsed.message || msg;
|
|
160
|
-
}
|
|
161
|
-
catch {
|
|
162
|
-
console.warn('Failed to parse error response:', text);
|
|
163
|
-
}
|
|
164
|
-
throw new StandardError(response.status, msg);
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return response.json();
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
12
|
get(path, cancelToken, headers = {}) {
|
|
173
13
|
return this._xhr('GET', path, null, cancelToken, headers, true);
|
|
174
14
|
}
|
|
@@ -266,6 +106,22 @@ export class AvroQueryClient {
|
|
|
266
106
|
throw new StandardError(500, 'Failed to fetch bills');
|
|
267
107
|
});
|
|
268
108
|
}
|
|
109
|
+
fetchRoutes(companyGuid, body = {}, cancelToken, headers = {}) {
|
|
110
|
+
if (!companyGuid || companyGuid.trim() === '') {
|
|
111
|
+
return Promise.reject(new StandardError(400, 'Company GUID is required'));
|
|
112
|
+
}
|
|
113
|
+
return this._fetch('POST', `/company/${companyGuid}/routes`, body, cancelToken, headers)
|
|
114
|
+
.then(response => {
|
|
115
|
+
if (!response || !Array.isArray(response)) {
|
|
116
|
+
throw new StandardError(400, 'Invalid routes response');
|
|
117
|
+
}
|
|
118
|
+
return response;
|
|
119
|
+
})
|
|
120
|
+
.catch(err => {
|
|
121
|
+
console.error('Failed to fetch routes:', err);
|
|
122
|
+
throw new StandardError(500, 'Failed to fetch routes');
|
|
123
|
+
});
|
|
124
|
+
}
|
|
269
125
|
sendEmail(emailId, formData) {
|
|
270
126
|
try {
|
|
271
127
|
return this._xhr('POST', `/email/${emailId}`, formData);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { StandardError } from '../../types/error';
|
|
3
|
+
AvroQueryClient.prototype._fetch = function (method, path, body, cancelToken, headers = {}, isIdempotent = false, retryCount = 0) {
|
|
4
|
+
const checkCancelled = () => {
|
|
5
|
+
try {
|
|
6
|
+
if (cancelToken?.isCancelled()) {
|
|
7
|
+
return new StandardError(0, 'Request cancelled');
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
throw new StandardError(0, `Error checking cancellation (${typeof cancelToken}): ${error}`);
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
};
|
|
15
|
+
return this.config.authManager.accessToken().then(token => {
|
|
16
|
+
const cancelErr = checkCancelled();
|
|
17
|
+
if (cancelErr)
|
|
18
|
+
return Promise.reject(cancelErr);
|
|
19
|
+
const url = this.config.baseUrl + path;
|
|
20
|
+
const requestHeaders = {
|
|
21
|
+
'Content-Type': 'application/json',
|
|
22
|
+
...headers,
|
|
23
|
+
};
|
|
24
|
+
if (token) {
|
|
25
|
+
requestHeaders['Authorization'] = `Bearer ${token}`;
|
|
26
|
+
}
|
|
27
|
+
const options = {
|
|
28
|
+
method,
|
|
29
|
+
headers: requestHeaders,
|
|
30
|
+
body: body ? JSON.stringify(body) : null,
|
|
31
|
+
};
|
|
32
|
+
return fetch(url, options).then(response => {
|
|
33
|
+
if (response.status === 401 && this.config.authManager.refreshTokens && retryCount === 0) {
|
|
34
|
+
return this.config.authManager
|
|
35
|
+
.refreshTokens()
|
|
36
|
+
.then(() => this._fetch(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1))
|
|
37
|
+
.catch(() => Promise.reject(new StandardError(401, 'Unauthorized (refresh failed)')));
|
|
38
|
+
}
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
if (retryCount < this.config.maxRetries) {
|
|
41
|
+
const delay = this.getDelay(this.config.retryStrategy, retryCount);
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
setTimeout(() => {
|
|
44
|
+
this._fetch(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1)
|
|
45
|
+
.then(resolve)
|
|
46
|
+
.catch(reject);
|
|
47
|
+
}, delay);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return response.text().then(text => {
|
|
52
|
+
let msg = response.statusText;
|
|
53
|
+
try {
|
|
54
|
+
const parsed = JSON.parse(text);
|
|
55
|
+
msg = parsed.message || msg;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
console.warn('Failed to parse error response:', text);
|
|
59
|
+
}
|
|
60
|
+
throw new StandardError(response.status, msg);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return response.json();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { StandardError } from '../../types/error';
|
|
3
|
+
AvroQueryClient.prototype._xhr = function (method, path, body, cancelToken, headers = {}, isIdempotent = false, retryCount = 0) {
|
|
4
|
+
const checkCancelled = () => {
|
|
5
|
+
if (cancelToken?.isCancelled()) {
|
|
6
|
+
return new StandardError(0, 'Request cancelled');
|
|
7
|
+
}
|
|
8
|
+
return null;
|
|
9
|
+
};
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
this.config.authManager.accessToken().then(token => {
|
|
12
|
+
const cancelErr = checkCancelled();
|
|
13
|
+
if (cancelErr)
|
|
14
|
+
return reject(cancelErr);
|
|
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
|
+
const cancelErr = checkCancelled();
|
|
23
|
+
if (cancelErr)
|
|
24
|
+
return reject(cancelErr);
|
|
25
|
+
if (xhr.status === 401 && this.config.authManager.refreshTokens && retryCount === 0) {
|
|
26
|
+
this.config.authManager
|
|
27
|
+
.refreshTokens()
|
|
28
|
+
.then(() => {
|
|
29
|
+
this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
|
|
30
|
+
})
|
|
31
|
+
.catch(() => reject(new StandardError(401, 'Unauthorized (refresh failed)')));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
35
|
+
try {
|
|
36
|
+
resolve(JSON.parse(xhr.responseText));
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
resolve(xhr.responseText);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
if (retryCount < this.config.maxRetries) {
|
|
44
|
+
const delay = this.getDelay(this.config.retryStrategy, retryCount);
|
|
45
|
+
setTimeout(() => {
|
|
46
|
+
this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
|
|
47
|
+
}, delay);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
let msg = xhr.statusText;
|
|
51
|
+
try {
|
|
52
|
+
const parsed = JSON.parse(xhr.responseText);
|
|
53
|
+
msg = parsed.msg || msg;
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
console.warn('Failed to parse error response:', xhr.responseText);
|
|
57
|
+
}
|
|
58
|
+
reject(new StandardError(xhr.status, msg));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
xhr.onerror = () => {
|
|
63
|
+
if (retryCount < this.config.maxRetries) {
|
|
64
|
+
const delay = this.getDelay(this.config.retryStrategy, retryCount);
|
|
65
|
+
setTimeout(() => {
|
|
66
|
+
this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
|
|
67
|
+
}, delay);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
reject(new StandardError(0, 'Network Error'));
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
if (this.config.timeout) {
|
|
74
|
+
xhr.timeout = this.config.timeout;
|
|
75
|
+
xhr.ontimeout = () => reject(new StandardError(0, 'Request timed out'));
|
|
76
|
+
}
|
|
77
|
+
xhr.send(body);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useQueryClient, useInfiniteQuery } from '@tanstack/react-query';
|
|
3
|
+
AvroQueryClient.prototype.getBills = function (companyGuid, body) {
|
|
4
|
+
const queryClient = useQueryClient();
|
|
5
|
+
const result = useInfiniteQuery({
|
|
6
|
+
queryKey: [
|
|
7
|
+
'bills',
|
|
8
|
+
companyGuid,
|
|
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(companyGuid, { ...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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useQuery } from '@tanstack/react-query';
|
|
3
|
+
AvroQueryClient.prototype.getCompanies = function (options = {}) {
|
|
4
|
+
return useQuery({
|
|
5
|
+
queryKey: ['/company/list'],
|
|
6
|
+
queryFn: () => this.get('/company/list'),
|
|
7
|
+
...options,
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
AvroQueryClient.prototype.getCompany = function (companyId) {
|
|
11
|
+
return useQuery({
|
|
12
|
+
queryKey: ['company', companyId],
|
|
13
|
+
queryFn: () => this.get(`/company/${companyId}`),
|
|
14
|
+
enabled: Boolean(companyId),
|
|
15
|
+
});
|
|
16
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useQueryClient, useInfiniteQuery } from '@tanstack/react-query';
|
|
3
|
+
AvroQueryClient.prototype.getEvents = function (companyGuid, body) {
|
|
4
|
+
const queryClient = useQueryClient();
|
|
5
|
+
const result = useInfiniteQuery({
|
|
6
|
+
queryKey: [
|
|
7
|
+
'events',
|
|
8
|
+
companyGuid,
|
|
9
|
+
body.amt ?? 50,
|
|
10
|
+
body.known_ids ?? [],
|
|
11
|
+
body.unknown_ids ?? [],
|
|
12
|
+
body.query ?? '',
|
|
13
|
+
body.unbilled ?? true,
|
|
14
|
+
body.billed ?? true,
|
|
15
|
+
body.paid ?? true,
|
|
16
|
+
body.jobId ?? '',
|
|
17
|
+
],
|
|
18
|
+
initialPageParam: 0,
|
|
19
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
20
|
+
if (lastPage.length < (body.amt ?? 50))
|
|
21
|
+
return undefined;
|
|
22
|
+
return allPages.flat().length; // next offset
|
|
23
|
+
},
|
|
24
|
+
queryFn: ({ pageParam = 0 }) => this.fetchEvents(companyGuid, { ...body, offset: pageParam }),
|
|
25
|
+
});
|
|
26
|
+
if (result.data) {
|
|
27
|
+
result.data.pages.forEach((data_page) => {
|
|
28
|
+
data_page.forEach((event) => {
|
|
29
|
+
queryClient.setQueryData(['event', event.id], event);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
|
+
AvroQueryClient.prototype.getJobs = function (companyGuid, body, total = 0, onProgress, isMainLoad = false) {
|
|
4
|
+
const queryClient = useQueryClient();
|
|
5
|
+
return useQuery({
|
|
6
|
+
queryKey: [isMainLoad ? 'main' : 'jobs', companyGuid, body.amt ?? 50, body.query ?? ""],
|
|
7
|
+
queryFn: async () => {
|
|
8
|
+
if (total === 0) {
|
|
9
|
+
onProgress?.(1);
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
onProgress?.(0);
|
|
13
|
+
const pageCount = body.amt ? Math.ceil(total / body.amt) : 0;
|
|
14
|
+
let completed = 0;
|
|
15
|
+
const promises = Array.from({ length: pageCount }, (_, i) => this.fetchJobs(companyGuid, {
|
|
16
|
+
...body,
|
|
17
|
+
offset: i * (body.amt ?? 0),
|
|
18
|
+
}));
|
|
19
|
+
const trackedPromises = promises.map((promise) => promise.then((result) => {
|
|
20
|
+
completed++;
|
|
21
|
+
const fraction = completed / pageCount;
|
|
22
|
+
onProgress?.(fraction);
|
|
23
|
+
return result;
|
|
24
|
+
}));
|
|
25
|
+
const pages = await Promise.all(trackedPromises);
|
|
26
|
+
const jobs = pages.flat();
|
|
27
|
+
jobs.forEach((job) => {
|
|
28
|
+
job.last_event = job.tasks.reduce((latest, task) => {
|
|
29
|
+
return task.last_event && (!latest || task.last_event.time_started > latest.time_started) ? task.last_event : latest;
|
|
30
|
+
}, null);
|
|
31
|
+
job.last_completed_event = job.tasks.reduce((latest, task) => {
|
|
32
|
+
return task.last_completed_event && (!latest || task.last_completed_event.time_started > latest.time_started) ? task.last_completed_event : latest;
|
|
33
|
+
}, null);
|
|
34
|
+
job.overdue_time = job.tasks.reduce((maxOverdue, task) => {
|
|
35
|
+
return task.overdue_time && task.overdue_time > maxOverdue ? task.overdue_time : maxOverdue;
|
|
36
|
+
}, 0);
|
|
37
|
+
queryClient.setQueryData(['job', job.id], job);
|
|
38
|
+
});
|
|
39
|
+
if (isMainLoad) {
|
|
40
|
+
queryClient.setQueryData(['jobs', companyGuid, body.amt ?? 50, body.query ?? ""], jobs);
|
|
41
|
+
}
|
|
42
|
+
return jobs;
|
|
43
|
+
},
|
|
44
|
+
enabled: Boolean(companyGuid) && companyGuid.length > 0 && Boolean(total) && total >= 0,
|
|
45
|
+
...isMainLoad ? { staleTime: Infinity, cacheTime: Infinity } : {},
|
|
46
|
+
});
|
|
47
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useQueryClient, useInfiniteQuery } from '@tanstack/react-query';
|
|
3
|
+
AvroQueryClient.prototype.getMonths = function (companyGuid, body) {
|
|
4
|
+
const queryClient = useQueryClient();
|
|
5
|
+
const result = useInfiniteQuery({
|
|
6
|
+
queryKey: [
|
|
7
|
+
'months',
|
|
8
|
+
companyGuid,
|
|
9
|
+
body.amt ?? 50,
|
|
10
|
+
body.known_ids ?? [],
|
|
11
|
+
body.unknown_ids ?? [],
|
|
12
|
+
body.query ?? '',
|
|
13
|
+
body.unbilled ?? true,
|
|
14
|
+
body.billed ?? true,
|
|
15
|
+
body.paid ?? true,
|
|
16
|
+
body.jobId ?? '',
|
|
17
|
+
],
|
|
18
|
+
initialPageParam: 0,
|
|
19
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
20
|
+
if (lastPage.length < (body.amt ?? 50))
|
|
21
|
+
return undefined;
|
|
22
|
+
return allPages.flat().length; // next offset
|
|
23
|
+
},
|
|
24
|
+
queryFn: ({ pageParam = 0 }) => this.fetchMonths(companyGuid, { ...body, offset: pageParam }),
|
|
25
|
+
});
|
|
26
|
+
if (result.data) {
|
|
27
|
+
result.data.pages.forEach((data_page) => {
|
|
28
|
+
data_page.forEach((month) => {
|
|
29
|
+
queryClient.setQueryData(['month', month.id], month);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useQuery } from '@tanstack/react-query';
|
|
3
|
+
AvroQueryClient.prototype.getRoot = function () {
|
|
4
|
+
return useQuery({
|
|
5
|
+
queryKey: ['health'],
|
|
6
|
+
queryFn: () => this.get('/', undefined) // your async fetch function
|
|
7
|
+
});
|
|
8
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
|
+
AvroQueryClient.prototype.getRoutes = function (companyGuid, body, total = 0, onProgress) {
|
|
4
|
+
const queryClient = useQueryClient();
|
|
5
|
+
return useQuery({
|
|
6
|
+
queryKey: ['routes', companyGuid, body.amt ?? 50, body.query ?? ""],
|
|
7
|
+
queryFn: async () => {
|
|
8
|
+
if (total === 0) {
|
|
9
|
+
onProgress?.(1);
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
onProgress?.(0);
|
|
13
|
+
const pageCount = body.amt ? Math.ceil(total / body.amt) : 0;
|
|
14
|
+
let completed = 0;
|
|
15
|
+
const promises = Array.from({ length: pageCount }, (_, i) => this.fetchRoutes(companyGuid, {
|
|
16
|
+
...body,
|
|
17
|
+
offset: i * (body.amt ?? 0),
|
|
18
|
+
}));
|
|
19
|
+
const trackedPromises = promises.map((promise) => promise.then((result) => {
|
|
20
|
+
completed++;
|
|
21
|
+
const fraction = completed / pageCount;
|
|
22
|
+
onProgress?.(fraction);
|
|
23
|
+
return result;
|
|
24
|
+
}));
|
|
25
|
+
const pages = await Promise.all(trackedPromises);
|
|
26
|
+
const routes = pages.flat();
|
|
27
|
+
return routes;
|
|
28
|
+
},
|
|
29
|
+
enabled: Boolean(companyGuid) && companyGuid.length > 0 && Boolean(total) && total >= 0,
|
|
30
|
+
});
|
|
31
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
export { AvroQueryClientConfig, AvroQueryClient } from './client/QueryClient';
|
|
2
2
|
export { AuthManager } from './auth/AuthManager';
|
|
3
3
|
export { MemoryStorage, LocalStorage } from './auth/storage';
|
|
4
|
+
import './client/core/xhr';
|
|
5
|
+
import './client/core/fetch';
|
|
6
|
+
import './client/core/utils';
|
|
7
|
+
import './client/hooks/root';
|
|
8
|
+
import './client/hooks/jobs';
|
|
9
|
+
import './client/hooks/routes';
|
|
10
|
+
import './client/hooks/events';
|
|
11
|
+
import './client/hooks/months';
|
|
12
|
+
import './client/hooks/bills';
|
|
13
|
+
import './client/hooks/companies';
|
|
4
14
|
export * from './types/api';
|
|
5
15
|
export * from './types/error';
|
|
6
16
|
export * from './types/client';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
export { AvroQueryClient } from './client/QueryClient';
|
|
2
2
|
export { AuthManager } from './auth/AuthManager';
|
|
3
3
|
export { MemoryStorage, LocalStorage } from './auth/storage';
|
|
4
|
+
import './client/core/xhr';
|
|
5
|
+
import './client/core/fetch';
|
|
6
|
+
import './client/core/utils';
|
|
7
|
+
import './client/hooks/root';
|
|
8
|
+
import './client/hooks/jobs';
|
|
9
|
+
import './client/hooks/routes';
|
|
10
|
+
import './client/hooks/events';
|
|
11
|
+
import './client/hooks/months';
|
|
12
|
+
import './client/hooks/bills';
|
|
13
|
+
import './client/hooks/companies';
|
|
4
14
|
export * from './types/api';
|
|
5
15
|
export * from './types/error';
|
|
6
16
|
export * from './types/client';
|
package/dist/types/api.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@go-avro/avro-js",
|
|
3
|
-
"version": "0.0.2-beta.
|
|
3
|
+
"version": "0.0.2-beta.36",
|
|
4
4
|
"description": "JS client for Avro backend integration.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"license": "CC-BY-SA-4.0",
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/jest": "^29.0.0",
|
|
36
|
+
"@types/react": "^19.2.2",
|
|
36
37
|
"@typescript-eslint/eslint-plugin": "^8.38.0",
|
|
37
38
|
"@typescript-eslint/parser": "^8.38.0",
|
|
38
39
|
"eslint": "^8.57.1",
|
|
@@ -50,5 +51,8 @@
|
|
|
50
51
|
],
|
|
51
52
|
"publishConfig": {
|
|
52
53
|
"access": "public"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@tanstack/react-query": "^5.90.2"
|
|
53
57
|
}
|
|
54
58
|
}
|