@go-avro/avro-js 0.0.2-beta.133 → 0.0.2-beta.135
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/auth/storage.js +5 -2
- package/dist/client/AvroQueryClientProvider.d.ts +14 -0
- package/dist/client/AvroQueryClientProvider.js +32 -0
- package/dist/client/QueryClient.d.ts +8 -1
- package/dist/client/QueryClient.js +52 -7
- package/dist/client/hooks/companies.js +5 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
package/dist/auth/storage.js
CHANGED
|
@@ -17,8 +17,11 @@ export class LocalStorage {
|
|
|
17
17
|
this.key = 'cache_data';
|
|
18
18
|
}
|
|
19
19
|
async get(key) {
|
|
20
|
-
const item = localStorage.getItem(this.key);
|
|
21
|
-
|
|
20
|
+
const item = JSON.parse(localStorage.getItem(this.key) ?? 'null');
|
|
21
|
+
if (typeof item !== 'object' || item === null) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
return item ? key ? item[key] ?? null : item : null;
|
|
22
25
|
}
|
|
23
26
|
async set(data) {
|
|
24
27
|
const current = await this.get() || {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React, { ReactNode } from "react";
|
|
2
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
3
|
+
import { AvroQueryClient, AvroQueryClientConfig } from "./QueryClient";
|
|
4
|
+
import { AuthManager } from "../auth/AuthManager";
|
|
5
|
+
export interface AvroQueryClientProviderProps {
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
authManager: AuthManager;
|
|
8
|
+
queryClient?: QueryClient;
|
|
9
|
+
configOverrides?: Partial<AvroQueryClientConfig>;
|
|
10
|
+
children: ReactNode;
|
|
11
|
+
}
|
|
12
|
+
export declare const AvroQueryClientProvider: ({ baseUrl, authManager, configOverrides, children, }: AvroQueryClientProviderProps) => React.JSX.Element;
|
|
13
|
+
export declare const useAvroQueryClient: () => AvroQueryClient;
|
|
14
|
+
export default AvroQueryClientProvider;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React, { createContext, useContext, useMemo, useEffect } from "react";
|
|
2
|
+
import { AvroQueryClient } from "./QueryClient";
|
|
3
|
+
const AvroQueryClientContext = createContext(null);
|
|
4
|
+
export const AvroQueryClientProvider = ({ baseUrl, authManager, configOverrides, children, }) => {
|
|
5
|
+
const client = useMemo(() => {
|
|
6
|
+
const cfg = {
|
|
7
|
+
baseUrl,
|
|
8
|
+
authManager,
|
|
9
|
+
...(configOverrides || {}),
|
|
10
|
+
};
|
|
11
|
+
return new AvroQueryClient(cfg);
|
|
12
|
+
}, [baseUrl, authManager, configOverrides]);
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
return () => {
|
|
15
|
+
try {
|
|
16
|
+
client.socket?.disconnect();
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
// ignore
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}, [client]);
|
|
23
|
+
return (React.createElement(AvroQueryClientContext.Provider, { value: client }, children));
|
|
24
|
+
};
|
|
25
|
+
export const useAvroQueryClient = () => {
|
|
26
|
+
const ctx = useContext(AvroQueryClientContext);
|
|
27
|
+
if (!ctx) {
|
|
28
|
+
throw new Error("useAvroQueryClient must be used within <AvroQueryClientProvider>");
|
|
29
|
+
}
|
|
30
|
+
return ctx;
|
|
31
|
+
};
|
|
32
|
+
export default AvroQueryClientProvider;
|
|
@@ -9,7 +9,6 @@ import { CacheData } from '../types/cache';
|
|
|
9
9
|
export interface AvroQueryClientConfig {
|
|
10
10
|
baseUrl: string;
|
|
11
11
|
authManager: AuthManager;
|
|
12
|
-
queryClient: QueryClient;
|
|
13
12
|
maxRetries?: number;
|
|
14
13
|
retryStrategy?: RetryStrategy;
|
|
15
14
|
timeout?: number;
|
|
@@ -299,7 +298,14 @@ export declare class AvroQueryClient {
|
|
|
299
298
|
_isAuthenticated: boolean;
|
|
300
299
|
companyId: string | null;
|
|
301
300
|
company: Company | null;
|
|
301
|
+
private initialized;
|
|
302
|
+
private initializedListeners;
|
|
302
303
|
constructor(config: AvroQueryClientConfig);
|
|
304
|
+
private computeInitialized;
|
|
305
|
+
private updateInitialized;
|
|
306
|
+
onInitializedChange(cb: (v: boolean) => void): () => void;
|
|
307
|
+
offInitializedChange(cb: (v: boolean) => void): void;
|
|
308
|
+
isInitialized(): boolean;
|
|
303
309
|
emit(eventName: string, data: unknown): void;
|
|
304
310
|
on<T>(eventName: string, callback: (data: T) => void): void;
|
|
305
311
|
off(eventName: string, callback?: Function): void;
|
|
@@ -335,6 +341,7 @@ export declare class AvroQueryClient {
|
|
|
335
341
|
setCache(data: Partial<CacheData>): Promise<void>;
|
|
336
342
|
getCache(key?: keyof CacheData | undefined): Promise<CacheData | string | null>;
|
|
337
343
|
setCompanyId(companyId: string): Promise<void[]>;
|
|
344
|
+
getCompanyId(): Promise<string | null>;
|
|
338
345
|
clearCache(): Promise<void>;
|
|
339
346
|
isAuthenticated(): boolean;
|
|
340
347
|
isAuthenticatedAsync(): Promise<boolean>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import io from 'socket.io-client';
|
|
2
|
-
import { useMutation } from '@tanstack/react-query';
|
|
2
|
+
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
3
3
|
import { LoginResponse } from '../types/api';
|
|
4
4
|
import { StandardError } from '../types/error';
|
|
5
5
|
export class AvroQueryClient {
|
|
@@ -7,21 +7,23 @@ export class AvroQueryClient {
|
|
|
7
7
|
this._isAuthenticated = false;
|
|
8
8
|
this.companyId = null;
|
|
9
9
|
this.company = null;
|
|
10
|
+
this.initialized = false;
|
|
11
|
+
this.initializedListeners = [];
|
|
10
12
|
this.config = {
|
|
11
13
|
baseUrl: config.baseUrl,
|
|
12
14
|
authManager: config.authManager,
|
|
13
|
-
queryClient: config.queryClient,
|
|
14
15
|
maxRetries: config.maxRetries ?? 3,
|
|
15
16
|
retryStrategy: config.retryStrategy ?? 'fixed',
|
|
16
17
|
timeout: config.timeout ?? 0,
|
|
17
18
|
};
|
|
19
|
+
this.socket = io(config.baseUrl, { autoConnect: false, transports: ["websocket"], });
|
|
18
20
|
config.authManager.isAuthenticated().then(isAuth => {
|
|
19
21
|
this._isAuthenticated = isAuth;
|
|
22
|
+
this.getCompanyId().then(id => {
|
|
23
|
+
this.companyId = id;
|
|
24
|
+
this.updateInitialized();
|
|
25
|
+
});
|
|
20
26
|
});
|
|
21
|
-
config.authManager.getCompanyId().then(companyId => {
|
|
22
|
-
this.companyId = companyId;
|
|
23
|
-
});
|
|
24
|
-
this.socket = io(config.baseUrl, { autoConnect: false, transports: ["websocket"], });
|
|
25
27
|
if (!this.socket.connected) {
|
|
26
28
|
this.config.authManager.accessToken().then(token => {
|
|
27
29
|
console.log('Initializing socket connection with token...');
|
|
@@ -32,9 +34,11 @@ export class AvroQueryClient {
|
|
|
32
34
|
this.socket.on('connect', () => {
|
|
33
35
|
this._isAuthenticated = true;
|
|
34
36
|
console.log(`Socket connected with ID: ${this.socket?.id}`);
|
|
37
|
+
this.updateInitialized();
|
|
35
38
|
});
|
|
36
39
|
this.socket.on('disconnect', (reason) => {
|
|
37
40
|
console.log(`Socket disconnected: ${reason}`);
|
|
41
|
+
this.updateInitialized();
|
|
38
42
|
});
|
|
39
43
|
this.socket.on('connect_error', (err) => {
|
|
40
44
|
console.error(`Socket connection error: ${err.message}`);
|
|
@@ -54,6 +58,40 @@ export class AvroQueryClient {
|
|
|
54
58
|
}
|
|
55
59
|
});
|
|
56
60
|
}
|
|
61
|
+
computeInitialized() {
|
|
62
|
+
return !!(this.socket?.connected && this.companyId !== null);
|
|
63
|
+
}
|
|
64
|
+
updateInitialized() {
|
|
65
|
+
const next = this.computeInitialized();
|
|
66
|
+
if (this.initialized !== next) {
|
|
67
|
+
this.initialized = next;
|
|
68
|
+
this.initializedListeners.forEach(cb => {
|
|
69
|
+
try {
|
|
70
|
+
cb(next);
|
|
71
|
+
}
|
|
72
|
+
catch (_) {
|
|
73
|
+
console.error(_);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
onInitializedChange(cb) {
|
|
79
|
+
this.initializedListeners.push(cb);
|
|
80
|
+
// call immediately with current value
|
|
81
|
+
try {
|
|
82
|
+
cb(this.initialized);
|
|
83
|
+
}
|
|
84
|
+
catch (_) {
|
|
85
|
+
console.error(_);
|
|
86
|
+
}
|
|
87
|
+
return () => this.offInitializedChange(cb);
|
|
88
|
+
}
|
|
89
|
+
offInitializedChange(cb) {
|
|
90
|
+
this.initializedListeners = this.initializedListeners.filter(c => c !== cb);
|
|
91
|
+
}
|
|
92
|
+
isInitialized() {
|
|
93
|
+
return this.initialized;
|
|
94
|
+
}
|
|
57
95
|
emit(eventName, data) {
|
|
58
96
|
if (!this.socket?.connected) {
|
|
59
97
|
console.error('Socket is not connected. Cannot emit event.');
|
|
@@ -175,8 +213,15 @@ export class AvroQueryClient {
|
|
|
175
213
|
}
|
|
176
214
|
setCompanyId(companyId) {
|
|
177
215
|
this.companyId = companyId;
|
|
216
|
+
this.updateInitialized();
|
|
178
217
|
return this.config.authManager.setCompanyId(companyId);
|
|
179
218
|
}
|
|
219
|
+
getCompanyId() {
|
|
220
|
+
if (this.companyId) {
|
|
221
|
+
return Promise.resolve(this.companyId);
|
|
222
|
+
}
|
|
223
|
+
return this.config.authManager.getCompanyId();
|
|
224
|
+
}
|
|
180
225
|
clearCache() {
|
|
181
226
|
return this.config.authManager.clearCache();
|
|
182
227
|
}
|
|
@@ -187,7 +232,7 @@ export class AvroQueryClient {
|
|
|
187
232
|
return this.config.authManager.isAuthenticated();
|
|
188
233
|
}
|
|
189
234
|
getQueryClient() {
|
|
190
|
-
return
|
|
235
|
+
return useQueryClient();
|
|
191
236
|
}
|
|
192
237
|
useLogout() {
|
|
193
238
|
const queryClient = this.getQueryClient();
|
|
@@ -18,6 +18,9 @@ AvroQueryClient.prototype.useGetCurrentCompany = function () {
|
|
|
18
18
|
return useQuery({
|
|
19
19
|
queryKey: ['company', 'current'],
|
|
20
20
|
queryFn: async () => {
|
|
21
|
+
if (!this.companyId) {
|
|
22
|
+
this.companyId = await this.config.authManager.getCompanyId();
|
|
23
|
+
}
|
|
21
24
|
if (!this.companyId) {
|
|
22
25
|
const companyList = await this.get(`/company/list`);
|
|
23
26
|
if (companyList.length > 0) {
|
|
@@ -58,7 +61,7 @@ AvroQueryClient.prototype.useUpdateCompany = function () {
|
|
|
58
61
|
queryClient.setQueryData(['/company/list'], (oldList) => {
|
|
59
62
|
if (!oldList)
|
|
60
63
|
return oldList;
|
|
61
|
-
return oldList.map((company) => company
|
|
64
|
+
return oldList.map((company) => company?.id === companyId ? { ...company, ...companyData } : company);
|
|
62
65
|
});
|
|
63
66
|
return { previousCompany, previousCompanyList };
|
|
64
67
|
},
|
|
@@ -120,7 +123,7 @@ AvroQueryClient.prototype.useDeleteCompany = function () {
|
|
|
120
123
|
queryClient.setQueryData(['/company/list'], (oldList) => {
|
|
121
124
|
if (!oldList)
|
|
122
125
|
return oldList;
|
|
123
|
-
return oldList.filter((company) => company
|
|
126
|
+
return oldList.filter((company) => company?.id !== companyId);
|
|
124
127
|
});
|
|
125
128
|
return { previousCompanyList };
|
|
126
129
|
},
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { AvroQueryClientConfig, AvroQueryClient } from './client/QueryClient';
|
|
2
|
+
export { AvroQueryClientProvider } from './client/AvroQueryClientProvider';
|
|
2
3
|
export { AuthManager } from './auth/AuthManager';
|
|
3
4
|
export { MemoryStorage, LocalStorage } from './auth/storage';
|
|
4
5
|
import './client/core/xhr';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { AvroQueryClient } from './client/QueryClient';
|
|
2
|
+
export { AvroQueryClientProvider } from './client/AvroQueryClientProvider';
|
|
2
3
|
export { AuthManager } from './auth/AuthManager';
|
|
3
4
|
export { MemoryStorage, LocalStorage } from './auth/storage';
|
|
4
5
|
import './client/core/xhr';
|