@go-avro/avro-js 0.0.2-beta.15 → 0.0.2-beta.151
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 +14 -5
- package/dist/auth/AuthManager.js +59 -23
- 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 +492 -16
- package/dist/client/QueryClient.js +396 -214
- package/dist/client/core/fetch.d.ts +1 -0
- package/dist/client/core/fetch.js +64 -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 +90 -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 +152 -0
- package/dist/client/hooks/events.d.ts +1 -0
- package/dist/client/hooks/events.js +308 -0
- package/dist/client/hooks/groups.d.ts +1 -0
- package/dist/client/hooks/groups.js +130 -0
- package/dist/client/hooks/jobs.d.ts +1 -0
- package/dist/client/hooks/jobs.js +213 -0
- package/dist/client/hooks/labels.d.ts +1 -0
- package/dist/client/hooks/labels.js +130 -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/proposal.d.ts +1 -0
- package/dist/client/hooks/proposal.js +22 -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 +118 -0
- package/dist/index.d.ts +26 -1
- package/dist/index.js +26 -1
- package/dist/types/api.d.ts +146 -38
- package/dist/types/api.js +10 -1
- 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 +6 -4
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ This SDK provides:
|
|
|
10
10
|
- Typed API interfaces for Avro entities (users, jobs, teams, etc.)
|
|
11
11
|
- Pluggable token storage (in-memory, localStorage, SecureStore, etc.)
|
|
12
12
|
- XHR-based requests for compatibility with React Native WebViews and older environments
|
|
13
|
+
- Mutation and Data Hooks for easy plug-in use in React and React Native projects.
|
|
13
14
|
|
|
14
15
|
## Installation
|
|
15
16
|
|
|
@@ -1,15 +1,24 @@
|
|
|
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
|
+
private tokenRefreshedCallbacks;
|
|
7
|
+
private tokenRefreshFailedCallbacks;
|
|
5
8
|
constructor({ baseUrl, storage, }: {
|
|
6
9
|
baseUrl: string;
|
|
7
|
-
storage:
|
|
10
|
+
storage: Cache | Cache[];
|
|
8
11
|
});
|
|
9
|
-
isAuthenticated(): Promise<
|
|
12
|
+
isAuthenticated(): Promise<AuthState>;
|
|
10
13
|
fetchNewTokens(): Promise<Tokens>;
|
|
11
14
|
accessToken(): Promise<string | undefined>;
|
|
12
|
-
|
|
15
|
+
onTokenRefreshed(callback: (accessToken: string) => void): void;
|
|
16
|
+
onTokenRefreshFailed(callback: () => void): void;
|
|
17
|
+
refreshTokens(): Promise<Tokens>;
|
|
13
18
|
setTokens(tokens: Tokens): Promise<void>;
|
|
14
|
-
|
|
19
|
+
setCache(data: Partial<CacheData>): Promise<void>;
|
|
20
|
+
getCache(key?: keyof CacheData): Promise<CacheData | string | null>;
|
|
21
|
+
clearCache(): Promise<void>;
|
|
22
|
+
getCompanyId(): Promise<string | null>;
|
|
23
|
+
setCompanyId(companyId: string): Promise<void[]>;
|
|
15
24
|
}
|
package/dist/auth/AuthManager.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { AuthState } from '../types/auth';
|
|
2
|
+
import { StandardError } from '../types/error';
|
|
1
3
|
export class AuthManager {
|
|
2
4
|
constructor({ baseUrl, storage, }) {
|
|
5
|
+
this.tokenRefreshedCallbacks = [];
|
|
6
|
+
this.tokenRefreshFailedCallbacks = [];
|
|
3
7
|
this.storages = Array.isArray(storage) ? storage : [storage];
|
|
4
8
|
if (this.storages.length === 0) {
|
|
5
9
|
throw new Error('At least one token storage must be provided');
|
|
@@ -16,18 +20,18 @@ export class AuthManager {
|
|
|
16
20
|
throw new Error('No token storages initialized');
|
|
17
21
|
}
|
|
18
22
|
for (const storage of this.storages) {
|
|
19
|
-
const
|
|
20
|
-
if (
|
|
23
|
+
const cache = await storage.get();
|
|
24
|
+
if (cache && typeof cache !== 'string' && cache.access_token) {
|
|
21
25
|
try {
|
|
22
26
|
const response = await fetch(`${this.baseUrl}/validate`, {
|
|
23
27
|
method: 'POST',
|
|
24
28
|
headers: {
|
|
25
29
|
'Content-Type': 'application/json',
|
|
26
|
-
'Authorization': `Bearer ${
|
|
30
|
+
'Authorization': `Bearer ${cache.access_token}`,
|
|
27
31
|
},
|
|
28
32
|
});
|
|
29
33
|
if (response.ok) {
|
|
30
|
-
return
|
|
34
|
+
return AuthState.AUTHENTICATED;
|
|
31
35
|
}
|
|
32
36
|
else {
|
|
33
37
|
// Attempt token refresh if validation fails
|
|
@@ -40,7 +44,7 @@ export class AuthManager {
|
|
|
40
44
|
'Authorization': `Bearer ${newTokens.access_token}`,
|
|
41
45
|
},
|
|
42
46
|
});
|
|
43
|
-
return retryResponse.ok;
|
|
47
|
+
return retryResponse.ok ? AuthState.AUTHENTICATED : AuthState.UNAUTHENTICATED;
|
|
44
48
|
}
|
|
45
49
|
}
|
|
46
50
|
}
|
|
@@ -49,12 +53,12 @@ export class AuthManager {
|
|
|
49
53
|
}
|
|
50
54
|
}
|
|
51
55
|
}
|
|
52
|
-
return
|
|
56
|
+
return AuthState.UNAUTHENTICATED;
|
|
53
57
|
}
|
|
54
58
|
async fetchNewTokens() {
|
|
55
59
|
for (const storage of this.storages) {
|
|
56
|
-
const
|
|
57
|
-
if (!
|
|
60
|
+
const cache = await storage.get();
|
|
61
|
+
if (!cache || typeof cache === 'string' || !cache.refresh_token)
|
|
58
62
|
continue;
|
|
59
63
|
try {
|
|
60
64
|
const response = await fetch(`${this.baseUrl}/refresh`, {
|
|
@@ -62,7 +66,7 @@ export class AuthManager {
|
|
|
62
66
|
headers: {
|
|
63
67
|
'Content-Type': 'application/json',
|
|
64
68
|
'Accept': 'application/json',
|
|
65
|
-
'Authorization': `Bearer ${
|
|
69
|
+
'Authorization': `Bearer ${cache.refresh_token}`,
|
|
66
70
|
},
|
|
67
71
|
});
|
|
68
72
|
if (response.ok) {
|
|
@@ -74,35 +78,67 @@ export class AuthManager {
|
|
|
74
78
|
storage.clear();
|
|
75
79
|
}
|
|
76
80
|
}
|
|
77
|
-
throw new
|
|
81
|
+
throw new StandardError(410, 'Failed to refresh tokens from all storages');
|
|
78
82
|
}
|
|
79
83
|
async accessToken() {
|
|
80
84
|
if (!this.storages.length) {
|
|
81
85
|
throw new Error('No token storages initialized');
|
|
82
86
|
}
|
|
83
87
|
for (const storage of this.storages) {
|
|
84
|
-
const
|
|
85
|
-
if (
|
|
86
|
-
return
|
|
88
|
+
const cache = await storage.get();
|
|
89
|
+
if (cache && typeof cache !== 'string' && cache.access_token)
|
|
90
|
+
return cache.access_token;
|
|
87
91
|
}
|
|
88
92
|
const newToken = await this.refreshTokens();
|
|
89
93
|
return newToken?.access_token;
|
|
90
94
|
}
|
|
95
|
+
onTokenRefreshed(callback) {
|
|
96
|
+
this.tokenRefreshedCallbacks.push(callback);
|
|
97
|
+
}
|
|
98
|
+
onTokenRefreshFailed(callback) {
|
|
99
|
+
this.tokenRefreshFailedCallbacks.push(callback);
|
|
100
|
+
}
|
|
91
101
|
async refreshTokens() {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
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;
|
|
101
109
|
}
|
|
102
110
|
async setTokens(tokens) {
|
|
103
111
|
await Promise.all(this.storages.map(s => s.set(tokens)));
|
|
104
112
|
}
|
|
105
|
-
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() {
|
|
106
128
|
await Promise.all(this.storages.map(s => s.clear()));
|
|
107
129
|
}
|
|
130
|
+
async getCompanyId() {
|
|
131
|
+
if (!this.storages.length) {
|
|
132
|
+
return null;
|
|
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 null;
|
|
140
|
+
}
|
|
141
|
+
async setCompanyId(companyId) {
|
|
142
|
+
return Promise.all(this.storages.map(s => s.set({ companyId })));
|
|
143
|
+
}
|
|
108
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;
|