@go-avro/avro-js 0.0.2-beta.98 → 0.0.3
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/AuthManager.d.ts +10 -5
- package/dist/auth/AuthManager.js +49 -29
- package/dist/auth/storage.d.ts +8 -8
- package/dist/auth/storage.js +19 -14
- package/dist/client/AvroQueryClientProvider.d.ts +14 -0
- package/dist/client/AvroQueryClientProvider.js +32 -0
- package/dist/client/QueryClient.d.ts +232 -57
- package/dist/client/QueryClient.js +192 -88
- package/dist/client/core/fetch.js +4 -2
- package/dist/client/core/xhr.js +8 -2
- package/dist/client/hooks/analytics.js +18 -2
- package/dist/client/hooks/bills.js +46 -21
- package/dist/client/hooks/catalog_items.d.ts +1 -0
- package/dist/client/hooks/catalog_items.js +90 -0
- package/dist/client/hooks/chats.js +5 -5
- package/dist/client/hooks/companies.js +102 -7
- package/dist/client/hooks/events.js +29 -27
- package/dist/client/hooks/groups.d.ts +1 -0
- package/dist/client/hooks/groups.js +130 -0
- package/dist/client/hooks/jobs.js +140 -31
- package/dist/client/hooks/labels.d.ts +1 -0
- package/dist/client/hooks/labels.js +130 -0
- package/dist/client/hooks/messages.js +7 -7
- package/dist/client/hooks/months.js +14 -13
- package/dist/client/hooks/plans.js +1 -2
- package/dist/client/hooks/prepayments.d.ts +1 -0
- package/dist/client/hooks/prepayments.js +34 -0
- package/dist/client/hooks/proposal.d.ts +1 -0
- package/dist/client/hooks/proposal.js +22 -0
- package/dist/client/hooks/routes.js +27 -19
- package/dist/client/hooks/sessions.js +13 -14
- package/dist/client/hooks/skills.d.ts +1 -0
- package/dist/client/hooks/skills.js +123 -0
- package/dist/client/hooks/teams.js +21 -10
- package/dist/client/hooks/users.js +38 -24
- package/dist/index.d.ts +9 -1
- package/dist/index.js +9 -1
- package/dist/types/api.d.ts +195 -39
- package/dist/types/api.js +63 -0
- package/dist/types/auth.d.ts +6 -5
- package/dist/types/auth.js +5 -1
- package/dist/types/cache.d.ts +9 -0
- package/dist/types/cache.js +1 -0
- package/package.json +1 -4
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AuthState, Tokens } from '../types/auth';
|
|
2
|
+
import { Cache, CacheData } from '../types/cache';
|
|
2
3
|
export declare class AuthManager {
|
|
3
4
|
private storages;
|
|
4
5
|
private baseUrl;
|
|
@@ -6,14 +7,18 @@ export declare class AuthManager {
|
|
|
6
7
|
private tokenRefreshFailedCallbacks;
|
|
7
8
|
constructor({ baseUrl, storage, }: {
|
|
8
9
|
baseUrl: string;
|
|
9
|
-
storage:
|
|
10
|
+
storage: Cache | Cache[];
|
|
10
11
|
});
|
|
11
|
-
isAuthenticated(): Promise<
|
|
12
|
+
isAuthenticated(): Promise<AuthState>;
|
|
12
13
|
fetchNewTokens(): Promise<Tokens>;
|
|
13
14
|
accessToken(): Promise<string | undefined>;
|
|
14
15
|
onTokenRefreshed(callback: (accessToken: string) => void): void;
|
|
15
16
|
onTokenRefreshFailed(callback: () => void): void;
|
|
16
|
-
refreshTokens(): Promise<Tokens
|
|
17
|
+
refreshTokens(): Promise<Tokens>;
|
|
17
18
|
setTokens(tokens: Tokens): Promise<void>;
|
|
18
|
-
|
|
19
|
+
setCache(data: Partial<CacheData>): Promise<void>;
|
|
20
|
+
getCache(key?: keyof CacheData): Promise<CacheData | string | null>;
|
|
21
|
+
clearCache(): Promise<void>;
|
|
22
|
+
getCompanyId(): Promise<string | undefined>;
|
|
23
|
+
setCompanyId(companyId: string): Promise<void[]>;
|
|
19
24
|
}
|
package/dist/auth/AuthManager.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AuthState } from '../types/auth';
|
|
1
2
|
import { StandardError } from '../types/error';
|
|
2
3
|
export class AuthManager {
|
|
3
4
|
constructor({ baseUrl, storage, }) {
|
|
@@ -19,18 +20,18 @@ export class AuthManager {
|
|
|
19
20
|
throw new Error('No token storages initialized');
|
|
20
21
|
}
|
|
21
22
|
for (const storage of this.storages) {
|
|
22
|
-
const
|
|
23
|
-
if (
|
|
23
|
+
const cache = await storage.get();
|
|
24
|
+
if (cache && typeof cache !== 'string' && cache.access_token) {
|
|
24
25
|
try {
|
|
25
26
|
const response = await fetch(`${this.baseUrl}/validate`, {
|
|
26
27
|
method: 'POST',
|
|
27
28
|
headers: {
|
|
28
29
|
'Content-Type': 'application/json',
|
|
29
|
-
'Authorization': `Bearer ${
|
|
30
|
+
'Authorization': `Bearer ${cache.access_token}`,
|
|
30
31
|
},
|
|
31
32
|
});
|
|
32
33
|
if (response.ok) {
|
|
33
|
-
return
|
|
34
|
+
return AuthState.AUTHENTICATED;
|
|
34
35
|
}
|
|
35
36
|
else {
|
|
36
37
|
// Attempt token refresh if validation fails
|
|
@@ -43,7 +44,7 @@ export class AuthManager {
|
|
|
43
44
|
'Authorization': `Bearer ${newTokens.access_token}`,
|
|
44
45
|
},
|
|
45
46
|
});
|
|
46
|
-
return retryResponse.ok;
|
|
47
|
+
return retryResponse.ok ? AuthState.AUTHENTICATED : AuthState.UNAUTHENTICATED;
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
50
|
}
|
|
@@ -52,12 +53,12 @@ export class AuthManager {
|
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
|
-
return
|
|
56
|
+
return AuthState.UNAUTHENTICATED;
|
|
56
57
|
}
|
|
57
58
|
async fetchNewTokens() {
|
|
58
59
|
for (const storage of this.storages) {
|
|
59
|
-
const
|
|
60
|
-
if (!
|
|
60
|
+
const cache = await storage.get();
|
|
61
|
+
if (!cache || typeof cache === 'string' || !cache.refresh_token)
|
|
61
62
|
continue;
|
|
62
63
|
try {
|
|
63
64
|
const response = await fetch(`${this.baseUrl}/refresh`, {
|
|
@@ -65,7 +66,7 @@ export class AuthManager {
|
|
|
65
66
|
headers: {
|
|
66
67
|
'Content-Type': 'application/json',
|
|
67
68
|
'Accept': 'application/json',
|
|
68
|
-
'Authorization': `Bearer ${
|
|
69
|
+
'Authorization': `Bearer ${cache.refresh_token}`,
|
|
69
70
|
},
|
|
70
71
|
});
|
|
71
72
|
if (response.ok) {
|
|
@@ -84,9 +85,9 @@ export class AuthManager {
|
|
|
84
85
|
throw new Error('No token storages initialized');
|
|
85
86
|
}
|
|
86
87
|
for (const storage of this.storages) {
|
|
87
|
-
const
|
|
88
|
-
if (
|
|
89
|
-
return
|
|
88
|
+
const cache = await storage.get();
|
|
89
|
+
if (cache && typeof cache !== 'string' && cache.access_token)
|
|
90
|
+
return cache.access_token;
|
|
90
91
|
}
|
|
91
92
|
const newToken = await this.refreshTokens();
|
|
92
93
|
return newToken?.access_token;
|
|
@@ -98,27 +99,46 @@ export class AuthManager {
|
|
|
98
99
|
this.tokenRefreshFailedCallbacks.push(callback);
|
|
99
100
|
}
|
|
100
101
|
async refreshTokens() {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
return newToken;
|
|
109
|
-
}
|
|
110
|
-
catch (error) {
|
|
111
|
-
this.tokenRefreshFailedCallbacks.forEach(cb => cb());
|
|
112
|
-
if (error?.status !== 410) {
|
|
113
|
-
console.error('Failed to refresh tokens:', error);
|
|
114
|
-
}
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
102
|
+
const newToken = await this.fetchNewTokens();
|
|
103
|
+
await Promise.all(this.storages.map(s => s.set(newToken)));
|
|
104
|
+
this.tokenRefreshedCallbacks.forEach(cb => {
|
|
105
|
+
if (newToken?.access_token)
|
|
106
|
+
cb(newToken.access_token);
|
|
107
|
+
});
|
|
108
|
+
return newToken;
|
|
117
109
|
}
|
|
118
110
|
async setTokens(tokens) {
|
|
119
111
|
await Promise.all(this.storages.map(s => s.set(tokens)));
|
|
120
112
|
}
|
|
121
|
-
async
|
|
113
|
+
async setCache(data) {
|
|
114
|
+
await Promise.all(this.storages.map(s => s.set(data)));
|
|
115
|
+
}
|
|
116
|
+
async getCache(key) {
|
|
117
|
+
if (!this.storages.length) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
for (const storage of this.storages) {
|
|
121
|
+
const cache = await storage.get(key);
|
|
122
|
+
if (cache)
|
|
123
|
+
return cache;
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
async clearCache() {
|
|
122
128
|
await Promise.all(this.storages.map(s => s.clear()));
|
|
123
129
|
}
|
|
130
|
+
async getCompanyId() {
|
|
131
|
+
if (!this.storages.length) {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
for (const storage of this.storages) {
|
|
135
|
+
const companyId = await storage.get('companyId');
|
|
136
|
+
if (companyId && typeof companyId === 'string')
|
|
137
|
+
return companyId;
|
|
138
|
+
}
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
async setCompanyId(companyId) {
|
|
142
|
+
return Promise.all(this.storages.map(s => s.set({ companyId })));
|
|
143
|
+
}
|
|
124
144
|
}
|
package/dist/auth/storage.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class MemoryStorage implements
|
|
3
|
-
private
|
|
4
|
-
get(): Promise<
|
|
5
|
-
set(
|
|
1
|
+
import { Cache, CacheData } from '../types/cache';
|
|
2
|
+
export declare class MemoryStorage implements Cache {
|
|
3
|
+
private data;
|
|
4
|
+
get(key?: keyof CacheData): Promise<CacheData | string | null>;
|
|
5
|
+
set(data: Partial<CacheData>): Promise<void>;
|
|
6
6
|
clear(): Promise<void>;
|
|
7
7
|
}
|
|
8
|
-
export declare class LocalStorage implements
|
|
8
|
+
export declare class LocalStorage implements Cache {
|
|
9
9
|
private key;
|
|
10
|
-
get(): Promise<
|
|
11
|
-
set(
|
|
10
|
+
get(key?: string): Promise<CacheData | null>;
|
|
11
|
+
set(data: Partial<CacheData>): Promise<void>;
|
|
12
12
|
clear(): Promise<void>;
|
|
13
13
|
}
|
package/dist/auth/storage.js
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
1
|
export class MemoryStorage {
|
|
2
2
|
constructor() {
|
|
3
|
-
this.
|
|
3
|
+
this.data = null;
|
|
4
4
|
}
|
|
5
|
-
async get() {
|
|
6
|
-
return this.
|
|
5
|
+
async get(key) {
|
|
6
|
+
return this.data ? key ? this.data[key] ?? null : this.data : null;
|
|
7
7
|
}
|
|
8
|
-
async set(
|
|
9
|
-
this.
|
|
8
|
+
async set(data) {
|
|
9
|
+
this.data = { ...this.data, ...data };
|
|
10
10
|
}
|
|
11
11
|
async clear() {
|
|
12
|
-
this.
|
|
12
|
+
this.data = null;
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
export class LocalStorage {
|
|
16
16
|
constructor() {
|
|
17
|
-
this.key = '
|
|
18
|
-
}
|
|
19
|
-
async get() {
|
|
20
|
-
const item = localStorage.getItem(this.key);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
this.key = 'cache_data';
|
|
18
|
+
}
|
|
19
|
+
async get(key) {
|
|
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;
|
|
25
|
+
}
|
|
26
|
+
async set(data) {
|
|
27
|
+
const current = await this.get() || {};
|
|
28
|
+
const updated = { ...current, ...data };
|
|
29
|
+
localStorage.setItem(this.key, JSON.stringify(updated));
|
|
25
30
|
}
|
|
26
31
|
async clear() {
|
|
27
32
|
localStorage.removeItem(this.key);
|
|
@@ -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;
|