@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.
Files changed (44) hide show
  1. package/dist/auth/AuthManager.d.ts +10 -5
  2. package/dist/auth/AuthManager.js +49 -29
  3. package/dist/auth/storage.d.ts +8 -8
  4. package/dist/auth/storage.js +19 -14
  5. package/dist/client/AvroQueryClientProvider.d.ts +14 -0
  6. package/dist/client/AvroQueryClientProvider.js +32 -0
  7. package/dist/client/QueryClient.d.ts +232 -57
  8. package/dist/client/QueryClient.js +192 -88
  9. package/dist/client/core/fetch.js +4 -2
  10. package/dist/client/core/xhr.js +8 -2
  11. package/dist/client/hooks/analytics.js +18 -2
  12. package/dist/client/hooks/bills.js +46 -21
  13. package/dist/client/hooks/catalog_items.d.ts +1 -0
  14. package/dist/client/hooks/catalog_items.js +90 -0
  15. package/dist/client/hooks/chats.js +5 -5
  16. package/dist/client/hooks/companies.js +102 -7
  17. package/dist/client/hooks/events.js +29 -27
  18. package/dist/client/hooks/groups.d.ts +1 -0
  19. package/dist/client/hooks/groups.js +130 -0
  20. package/dist/client/hooks/jobs.js +140 -31
  21. package/dist/client/hooks/labels.d.ts +1 -0
  22. package/dist/client/hooks/labels.js +130 -0
  23. package/dist/client/hooks/messages.js +7 -7
  24. package/dist/client/hooks/months.js +14 -13
  25. package/dist/client/hooks/plans.js +1 -2
  26. package/dist/client/hooks/prepayments.d.ts +1 -0
  27. package/dist/client/hooks/prepayments.js +34 -0
  28. package/dist/client/hooks/proposal.d.ts +1 -0
  29. package/dist/client/hooks/proposal.js +22 -0
  30. package/dist/client/hooks/routes.js +27 -19
  31. package/dist/client/hooks/sessions.js +13 -14
  32. package/dist/client/hooks/skills.d.ts +1 -0
  33. package/dist/client/hooks/skills.js +123 -0
  34. package/dist/client/hooks/teams.js +21 -10
  35. package/dist/client/hooks/users.js +38 -24
  36. package/dist/index.d.ts +9 -1
  37. package/dist/index.js +9 -1
  38. package/dist/types/api.d.ts +195 -39
  39. package/dist/types/api.js +63 -0
  40. package/dist/types/auth.d.ts +6 -5
  41. package/dist/types/auth.js +5 -1
  42. package/dist/types/cache.d.ts +9 -0
  43. package/dist/types/cache.js +1 -0
  44. package/package.json +1 -4
@@ -1,4 +1,5 @@
1
- import { TokenStorage, Tokens } from '../types/auth';
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: TokenStorage | TokenStorage[];
10
+ storage: Cache | Cache[];
10
11
  });
11
- isAuthenticated(): Promise<boolean>;
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 | null>;
17
+ refreshTokens(): Promise<Tokens>;
17
18
  setTokens(tokens: Tokens): Promise<void>;
18
- clearTokens(): Promise<void>;
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
  }
@@ -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 tokens = await storage.get();
23
- if (tokens && tokens.access_token) {
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 ${tokens.access_token}`,
30
+ 'Authorization': `Bearer ${cache.access_token}`,
30
31
  },
31
32
  });
32
33
  if (response.ok) {
33
- return true;
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 false;
56
+ return AuthState.UNAUTHENTICATED;
56
57
  }
57
58
  async fetchNewTokens() {
58
59
  for (const storage of this.storages) {
59
- const tokens = await storage.get();
60
- if (!tokens || !tokens.refresh_token)
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 ${tokens.refresh_token}`,
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 token = await storage.get();
88
- if (token)
89
- return token.access_token;
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
- try {
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;
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 clearTokens() {
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
  }
@@ -1,13 +1,13 @@
1
- import { Tokens, TokenStorage } from '../types/auth';
2
- export declare class MemoryStorage implements TokenStorage {
3
- private tokens;
4
- get(): Promise<Tokens | null>;
5
- set(tokens: Tokens): Promise<void>;
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 TokenStorage {
8
+ export declare class LocalStorage implements Cache {
9
9
  private key;
10
- get(): Promise<Tokens | null>;
11
- set(tokens: Tokens): Promise<void>;
10
+ get(key?: string): Promise<CacheData | null>;
11
+ set(data: Partial<CacheData>): Promise<void>;
12
12
  clear(): Promise<void>;
13
13
  }
@@ -1,27 +1,32 @@
1
1
  export class MemoryStorage {
2
2
  constructor() {
3
- this.tokens = null;
3
+ this.data = null;
4
4
  }
5
- async get() {
6
- return this.tokens ? this.tokens : null;
5
+ async get(key) {
6
+ return this.data ? key ? this.data[key] ?? null : this.data : null;
7
7
  }
8
- async set(tokens) {
9
- this.tokens = tokens;
8
+ async set(data) {
9
+ this.data = { ...this.data, ...data };
10
10
  }
11
11
  async clear() {
12
- this.tokens = null;
12
+ this.data = null;
13
13
  }
14
14
  }
15
15
  export class LocalStorage {
16
16
  constructor() {
17
- this.key = 'auth_tokens';
18
- }
19
- async get() {
20
- const item = localStorage.getItem(this.key);
21
- return item ? JSON.parse(item) : null;
22
- }
23
- async set(tokens) {
24
- localStorage.setItem(this.key, JSON.stringify(tokens));
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;