@go-avro/avro-js 0.0.37 → 0.0.39
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 +2 -2
- package/dist/auth/AuthManager.js +37 -32
- package/dist/auth/storage.d.ts +1 -1
- package/dist/auth/storage.js +6 -6
- package/dist/client/AvroQueryClientProvider.js +1 -1
- package/dist/client/QueryClient.d.ts +35 -17
- package/dist/client/QueryClient.js +505 -391
- package/dist/client/core/fetch.js +16 -11
- package/dist/client/core/utils.js +5 -5
- package/dist/client/core/xhr.js +28 -23
- package/dist/client/hooks/analytics.js +14 -14
- package/dist/client/hooks/avro.js +2 -2
- package/dist/client/hooks/bills.js +66 -30
- package/dist/client/hooks/catalog_items.js +57 -22
- package/dist/client/hooks/chats.js +4 -4
- package/dist/client/hooks/companies.js +96 -39
- package/dist/client/hooks/email.js +1 -1
- package/dist/client/hooks/events.js +174 -63
- package/dist/client/hooks/groups.js +37 -22
- package/dist/client/hooks/jobs.js +69 -18
- package/dist/client/hooks/labels.js +36 -21
- package/dist/client/hooks/messages.js +9 -6
- package/dist/client/hooks/months.js +42 -22
- package/dist/client/hooks/plans.js +2 -2
- package/dist/client/hooks/prepayments.js +42 -22
- package/dist/client/hooks/proposal.js +21 -5
- package/dist/client/hooks/root.js +4 -4
- package/dist/client/hooks/routes.js +77 -32
- package/dist/client/hooks/sessions.js +66 -34
- package/dist/client/hooks/skills.js +33 -18
- package/dist/client/hooks/teams.js +36 -21
- package/dist/client/hooks/timecards.js +6 -0
- package/dist/client/hooks/users.js +61 -29
- package/dist/client/hooks/waivers.js +41 -19
- package/dist/index.d.ts +38 -38
- package/dist/index.js +37 -37
- package/dist/types/api/Bill.d.ts +1 -1
- package/dist/types/api/Bill.js +1 -1
- package/dist/types/api/Job.d.ts +1 -1
- package/dist/types/api/Job.js +14 -14
- package/dist/types/api/LineItem.d.ts +3 -3
- package/dist/types/api/LineItem.js +5 -2
- package/dist/types/api/PaymentType.d.ts +1 -1
- package/dist/types/api/Prepayment.d.ts +1 -1
- package/dist/types/api/Route.d.ts +3 -3
- package/dist/types/api/Route.js +4 -2
- package/dist/types/api/RouteJob.d.ts +1 -1
- package/dist/types/api/Task.d.ts +2 -2
- package/dist/types/api/Task.js +12 -7
- package/dist/types/api/Timecard.d.ts +1 -1
- package/dist/types/api/TimecardAction.d.ts +1 -1
- package/dist/types/api/UserCompanyAssociation.d.ts +2 -2
- package/dist/types/api/UserCompanyAssociation.js +1 -1
- package/dist/types/api/_Event.d.ts +1 -1
- package/dist/types/api/_Event.js +1 -1
- package/dist/types/api.d.ts +1 -1
- package/dist/types/auth.d.ts +1 -1
- package/dist/types/client.d.ts +1 -1
- package/package.json +3 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AuthState, Tokens } from
|
|
2
|
-
import { Cache, CacheData } from
|
|
1
|
+
import { AuthState, Tokens } from "../types/auth";
|
|
2
|
+
import { Cache, CacheData } from "../types/cache";
|
|
3
3
|
export declare class AuthManager {
|
|
4
4
|
private storages;
|
|
5
5
|
private baseUrl;
|
package/dist/auth/AuthManager.js
CHANGED
|
@@ -1,33 +1,36 @@
|
|
|
1
|
-
import { AuthState } from
|
|
2
|
-
import { StandardError } from
|
|
1
|
+
import { AuthState } from "../types/auth";
|
|
2
|
+
import { StandardError } from "../types/error";
|
|
3
3
|
export class AuthManager {
|
|
4
4
|
constructor({ baseUrl, storage, }) {
|
|
5
5
|
this.tokenRefreshedCallbacks = [];
|
|
6
6
|
this.tokenRefreshFailedCallbacks = [];
|
|
7
7
|
this.storages = Array.isArray(storage) ? storage : [storage];
|
|
8
8
|
if (this.storages.length === 0) {
|
|
9
|
-
throw new Error(
|
|
9
|
+
throw new Error("At least one token storage must be provided");
|
|
10
10
|
}
|
|
11
|
-
this.storages.forEach(storage => {
|
|
12
|
-
if (!storage ||
|
|
13
|
-
|
|
11
|
+
this.storages.forEach((storage) => {
|
|
12
|
+
if (!storage ||
|
|
13
|
+
typeof storage.get !== "function" ||
|
|
14
|
+
typeof storage.set !== "function" ||
|
|
15
|
+
typeof storage.clear !== "function") {
|
|
16
|
+
throw new Error("Invalid token storage provided");
|
|
14
17
|
}
|
|
15
18
|
});
|
|
16
19
|
this.baseUrl = baseUrl;
|
|
17
20
|
}
|
|
18
21
|
async isAuthenticated() {
|
|
19
22
|
if (!this.storages.length) {
|
|
20
|
-
throw new Error(
|
|
23
|
+
throw new Error("No token storages initialized");
|
|
21
24
|
}
|
|
22
25
|
for (const storage of this.storages) {
|
|
23
26
|
const cache = await storage.get();
|
|
24
|
-
if (cache && typeof cache !==
|
|
27
|
+
if (cache && typeof cache !== "string" && cache.access_token) {
|
|
25
28
|
try {
|
|
26
29
|
const response = await fetch(`${this.baseUrl}/validate`, {
|
|
27
|
-
method:
|
|
30
|
+
method: "POST",
|
|
28
31
|
headers: {
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
"Content-Type": "application/json",
|
|
33
|
+
Authorization: `Bearer ${cache.access_token}`,
|
|
31
34
|
},
|
|
32
35
|
});
|
|
33
36
|
if (response.ok) {
|
|
@@ -38,18 +41,20 @@ export class AuthManager {
|
|
|
38
41
|
const newTokens = await this.refreshTokens();
|
|
39
42
|
if (newTokens && newTokens.access_token) {
|
|
40
43
|
const retryResponse = await fetch(`${this.baseUrl}/validate`, {
|
|
41
|
-
method:
|
|
44
|
+
method: "POST",
|
|
42
45
|
headers: {
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
"Content-Type": "application/json",
|
|
47
|
+
Authorization: `Bearer ${newTokens.access_token}`,
|
|
45
48
|
},
|
|
46
49
|
});
|
|
47
|
-
return retryResponse.ok
|
|
50
|
+
return retryResponse.ok
|
|
51
|
+
? AuthState.AUTHENTICATED
|
|
52
|
+
: AuthState.UNAUTHENTICATED;
|
|
48
53
|
}
|
|
49
54
|
}
|
|
50
55
|
}
|
|
51
56
|
catch (error) {
|
|
52
|
-
console.error(
|
|
57
|
+
console.error("Error validating access token:", error);
|
|
53
58
|
}
|
|
54
59
|
}
|
|
55
60
|
}
|
|
@@ -58,15 +63,15 @@ export class AuthManager {
|
|
|
58
63
|
async fetchNewTokens() {
|
|
59
64
|
for (const storage of this.storages) {
|
|
60
65
|
const cache = await storage.get();
|
|
61
|
-
if (!cache || typeof cache ===
|
|
66
|
+
if (!cache || typeof cache === "string" || !cache.refresh_token)
|
|
62
67
|
continue;
|
|
63
68
|
try {
|
|
64
69
|
const response = await fetch(`${this.baseUrl}/refresh`, {
|
|
65
|
-
method:
|
|
70
|
+
method: "POST",
|
|
66
71
|
headers: {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
72
|
+
"Content-Type": "application/json",
|
|
73
|
+
Accept: "application/json",
|
|
74
|
+
Authorization: `Bearer ${cache.refresh_token}`,
|
|
70
75
|
},
|
|
71
76
|
});
|
|
72
77
|
if (response.ok) {
|
|
@@ -78,15 +83,15 @@ export class AuthManager {
|
|
|
78
83
|
storage.clear();
|
|
79
84
|
}
|
|
80
85
|
}
|
|
81
|
-
throw new StandardError(410,
|
|
86
|
+
throw new StandardError(410, "Failed to refresh tokens from all storages");
|
|
82
87
|
}
|
|
83
88
|
async accessToken() {
|
|
84
89
|
if (!this.storages.length) {
|
|
85
|
-
throw new Error(
|
|
90
|
+
throw new Error("No token storages initialized");
|
|
86
91
|
}
|
|
87
92
|
for (const storage of this.storages) {
|
|
88
93
|
const cache = await storage.get();
|
|
89
|
-
if (cache && typeof cache !==
|
|
94
|
+
if (cache && typeof cache !== "string" && cache.access_token)
|
|
90
95
|
return cache.access_token;
|
|
91
96
|
}
|
|
92
97
|
const newToken = await this.refreshTokens();
|
|
@@ -100,18 +105,18 @@ export class AuthManager {
|
|
|
100
105
|
}
|
|
101
106
|
async refreshTokens() {
|
|
102
107
|
const newToken = await this.fetchNewTokens();
|
|
103
|
-
await Promise.all(this.storages.map(s => s.set(newToken)));
|
|
104
|
-
this.tokenRefreshedCallbacks.forEach(cb => {
|
|
108
|
+
await Promise.all(this.storages.map((s) => s.set(newToken)));
|
|
109
|
+
this.tokenRefreshedCallbacks.forEach((cb) => {
|
|
105
110
|
if (newToken?.access_token)
|
|
106
111
|
cb(newToken.access_token);
|
|
107
112
|
});
|
|
108
113
|
return newToken;
|
|
109
114
|
}
|
|
110
115
|
async setTokens(tokens) {
|
|
111
|
-
await Promise.all(this.storages.map(s => s.set(tokens)));
|
|
116
|
+
await Promise.all(this.storages.map((s) => s.set(tokens)));
|
|
112
117
|
}
|
|
113
118
|
async setCache(data) {
|
|
114
|
-
await Promise.all(this.storages.map(s => s.set(data)));
|
|
119
|
+
await Promise.all(this.storages.map((s) => s.set(data)));
|
|
115
120
|
}
|
|
116
121
|
async getCache(key) {
|
|
117
122
|
if (!this.storages.length) {
|
|
@@ -125,20 +130,20 @@ export class AuthManager {
|
|
|
125
130
|
return null;
|
|
126
131
|
}
|
|
127
132
|
async clearCache() {
|
|
128
|
-
await Promise.all(this.storages.map(s => s.clear()));
|
|
133
|
+
await Promise.all(this.storages.map((s) => s.clear()));
|
|
129
134
|
}
|
|
130
135
|
async getCompanyId() {
|
|
131
136
|
if (!this.storages.length) {
|
|
132
137
|
return undefined;
|
|
133
138
|
}
|
|
134
139
|
for (const storage of this.storages) {
|
|
135
|
-
const companyId = await storage.get(
|
|
136
|
-
if (companyId && typeof companyId ===
|
|
140
|
+
const companyId = await storage.get("companyId");
|
|
141
|
+
if (companyId && typeof companyId === "string")
|
|
137
142
|
return companyId;
|
|
138
143
|
}
|
|
139
144
|
return undefined;
|
|
140
145
|
}
|
|
141
146
|
async setCompanyId(companyId) {
|
|
142
|
-
return Promise.all(this.storages.map(s => s.set({ companyId })));
|
|
147
|
+
return Promise.all(this.storages.map((s) => s.set({ companyId })));
|
|
143
148
|
}
|
|
144
149
|
}
|
package/dist/auth/storage.d.ts
CHANGED
package/dist/auth/storage.js
CHANGED
|
@@ -3,7 +3,7 @@ export class MemoryStorage {
|
|
|
3
3
|
this.data = null;
|
|
4
4
|
}
|
|
5
5
|
async get(key) {
|
|
6
|
-
return this.data ? key ? this.data[key] ?? null : this.data : null;
|
|
6
|
+
return this.data ? (key ? (this.data[key] ?? null) : this.data) : null;
|
|
7
7
|
}
|
|
8
8
|
async set(data) {
|
|
9
9
|
this.data = { ...this.data, ...data };
|
|
@@ -14,17 +14,17 @@ export class MemoryStorage {
|
|
|
14
14
|
}
|
|
15
15
|
export class LocalStorage {
|
|
16
16
|
constructor() {
|
|
17
|
-
this.key =
|
|
17
|
+
this.key = "cache_data";
|
|
18
18
|
}
|
|
19
19
|
async get(key) {
|
|
20
|
-
const item = JSON.parse(localStorage.getItem(this.key) ??
|
|
21
|
-
if (typeof item !==
|
|
20
|
+
const item = JSON.parse(localStorage.getItem(this.key) ?? "null");
|
|
21
|
+
if (typeof item !== "object" || item === null) {
|
|
22
22
|
return null;
|
|
23
23
|
}
|
|
24
|
-
return item ? key ? item[key] ?? null : item : null;
|
|
24
|
+
return item ? (key ? (item[key] ?? null) : item) : null;
|
|
25
25
|
}
|
|
26
26
|
async set(data) {
|
|
27
|
-
const current = await this.get() || {};
|
|
27
|
+
const current = (await this.get()) || {};
|
|
28
28
|
const updated = { ...current, ...data };
|
|
29
29
|
localStorage.setItem(this.key, JSON.stringify(updated));
|
|
30
30
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { createContext, useContext, useMemo, useEffect, useRef, useCallback } from "react";
|
|
1
|
+
import React, { createContext, useContext, useMemo, useEffect, useRef, useCallback, } from "react";
|
|
2
2
|
import { AvroQueryClient } from "./QueryClient";
|
|
3
3
|
const AvroQueryClientContext = createContext(null);
|
|
4
4
|
export const AvroQueryClientProvider = ({ baseUrl, authManager, queryClient, configOverrides, children, }) => {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { Socket } from
|
|
2
|
-
import { InfiniteData, QueryClient, UseInfiniteQueryResult, useMutation, UseQueryResult } from
|
|
3
|
-
import { AuthManager } from
|
|
4
|
-
import { _Event, ApiInfo, Avro, Bill, Break, Chat, Company, FinancialInsightData, RevenueInsightData, Job, EventInsightData, LoginResponse, Message, Plan, Route, ServiceMonth, Session, Team, User, UserCompanyAssociation, Skill, Group, Label, RouteScheduleConfig, CatalogItem, Prepayment, Timecard, TimecardActionType, TimecardStatus } from
|
|
5
|
-
import { AuthState, Tokens } from
|
|
6
|
-
import { CancelToken, RetryStrategy } from
|
|
7
|
-
import { StandardError } from
|
|
8
|
-
import { CacheData } from
|
|
9
|
-
import { Waiver } from
|
|
1
|
+
import { Socket } from "socket.io-client";
|
|
2
|
+
import { InfiniteData, QueryClient, UseInfiniteQueryResult, useMutation, UseQueryResult } from "@tanstack/react-query";
|
|
3
|
+
import { AuthManager } from "../auth/AuthManager";
|
|
4
|
+
import { _Event, ApiInfo, Avro, Bill, Break, Chat, Company, FinancialInsightData, RevenueInsightData, Job, EventInsightData, LoginResponse, Message, Plan, Route, ServiceMonth, Session, Team, User, UserCompanyAssociation, Skill, Group, Label, RouteScheduleConfig, CatalogItem, Prepayment, Timecard, TimecardActionType, TimecardStatus } from "../types/api";
|
|
5
|
+
import { AuthState, Tokens } from "../types/auth";
|
|
6
|
+
import { CancelToken, RetryStrategy } from "../types/client";
|
|
7
|
+
import { StandardError } from "../types/error";
|
|
8
|
+
import { CacheData } from "../types/cache";
|
|
9
|
+
import { Waiver } from "../types/api/Waiver";
|
|
10
10
|
import type { EmailSucceededPayload, EmailFailedPayload, EmailType } from "../types/api/EmailNotification";
|
|
11
11
|
/** Callbacks for a tracked email request. */
|
|
12
12
|
export interface TrackEmailOptions {
|
|
@@ -24,7 +24,7 @@ export interface AvroQueryClientConfig {
|
|
|
24
24
|
retryStrategy?: RetryStrategy;
|
|
25
25
|
timeout?: number;
|
|
26
26
|
}
|
|
27
|
-
declare module
|
|
27
|
+
declare module "../client/QueryClient" {
|
|
28
28
|
interface AvroQueryClient {
|
|
29
29
|
_xhr<T>(method: string, path: string, body: any, cancelToken?: CancelToken, headers?: Record<string, string>, isIdempotent?: boolean, retryCount?: number, progressUpdateCallback?: (loaded: number, total: number) => void): Promise<T>;
|
|
30
30
|
_fetch<T>(method: string, path: string, body: any, cancelToken?: CancelToken, headers?: Record<string, string>, isIdempotent?: boolean, retryCount?: number): Promise<T>;
|
|
@@ -146,14 +146,14 @@ declare module '../client/QueryClient' {
|
|
|
146
146
|
}>;
|
|
147
147
|
useGetProposal(proposal_id: string): UseQueryResult<any, StandardError>;
|
|
148
148
|
useGetAnalytics(): UseQueryResult<any, StandardError>;
|
|
149
|
-
useFinanceAnalytics({ periods, cumulative }: {
|
|
149
|
+
useFinanceAnalytics({ periods, cumulative, }: {
|
|
150
150
|
periods: number[][];
|
|
151
151
|
cumulative: boolean;
|
|
152
152
|
}): UseQueryResult<FinancialInsightData[], StandardError>;
|
|
153
|
-
useEventAnalytics({ periods }: {
|
|
153
|
+
useEventAnalytics({ periods, }: {
|
|
154
154
|
periods: number[][];
|
|
155
155
|
}): UseQueryResult<EventInsightData[], StandardError>;
|
|
156
|
-
useRevenueAnalytics({ periods }: {
|
|
156
|
+
useRevenueAnalytics({ periods, }: {
|
|
157
157
|
periods: number[][];
|
|
158
158
|
}): UseQueryResult<RevenueInsightData[], StandardError>;
|
|
159
159
|
useGetCompany(companyId: string): UseQueryResult<Company, StandardError>;
|
|
@@ -551,27 +551,27 @@ export declare class AvroQueryClient {
|
|
|
551
551
|
* Remove all invalidation listeners. Called on provider unmount.
|
|
552
552
|
*/
|
|
553
553
|
teardownSocketInvalidation(): void;
|
|
554
|
-
get<T>({ path, cancelToken, headers, progressUpdateCallback }: {
|
|
554
|
+
get<T>({ path, cancelToken, headers, progressUpdateCallback, }: {
|
|
555
555
|
path: string;
|
|
556
556
|
cancelToken?: CancelToken;
|
|
557
557
|
headers?: Record<string, string>;
|
|
558
558
|
progressUpdateCallback?: (loaded: number, total: number) => void;
|
|
559
559
|
}): Promise<T>;
|
|
560
|
-
post<T>({ path, data, cancelToken, headers, progressUpdateCallback }: {
|
|
560
|
+
post<T>({ path, data, cancelToken, headers, progressUpdateCallback, }: {
|
|
561
561
|
path: string;
|
|
562
562
|
data?: any;
|
|
563
563
|
cancelToken?: CancelToken;
|
|
564
564
|
headers?: Record<string, string>;
|
|
565
565
|
progressUpdateCallback?: (loaded: number, total: number) => void;
|
|
566
566
|
}): Promise<T>;
|
|
567
|
-
put<T>({ path, data, cancelToken, headers, progressUpdateCallback }: {
|
|
567
|
+
put<T>({ path, data, cancelToken, headers, progressUpdateCallback, }: {
|
|
568
568
|
path: string;
|
|
569
569
|
data?: any;
|
|
570
570
|
cancelToken?: CancelToken;
|
|
571
571
|
headers?: Record<string, string>;
|
|
572
572
|
progressUpdateCallback?: (loaded: number, total: number) => void;
|
|
573
573
|
}): Promise<T>;
|
|
574
|
-
delete<T>({ path, cancelToken, headers, progressUpdateCallback }: {
|
|
574
|
+
delete<T>({ path, cancelToken, headers, progressUpdateCallback, }: {
|
|
575
575
|
path: string;
|
|
576
576
|
cancelToken?: CancelToken;
|
|
577
577
|
headers?: Record<string, string>;
|
|
@@ -618,6 +618,24 @@ export declare class AvroQueryClient {
|
|
|
618
618
|
getAuthState(): AuthState;
|
|
619
619
|
getAuthStateAsync(): Promise<AuthState>;
|
|
620
620
|
getQueryClient(): QueryClient;
|
|
621
|
+
/**
|
|
622
|
+
* Fetch an entity from the API, optionally construct it, and surgically
|
|
623
|
+
* update all matching React-Query caches (individual + list + infinite).
|
|
624
|
+
*
|
|
625
|
+
* Shared by socket handlers and mutation `onSuccess` callbacks so the
|
|
626
|
+
* sender gets an immediate cache sync and everyone else gets the socket
|
|
627
|
+
* update — both use the identical code path.
|
|
628
|
+
*
|
|
629
|
+
* @returns The fetched (and optionally constructed) item, or `undefined`
|
|
630
|
+
* for deletes / entities without a fetchPath.
|
|
631
|
+
*/
|
|
632
|
+
_syncEntity(queryClient: QueryClient, params: {
|
|
633
|
+
action: "create" | "update" | "delete";
|
|
634
|
+
entityKey: string;
|
|
635
|
+
id: string;
|
|
636
|
+
fetchPath?: string;
|
|
637
|
+
construct?: (raw: any) => any;
|
|
638
|
+
}): Promise<any>;
|
|
621
639
|
useLogout(): ReturnType<typeof useMutation<void, StandardError, CancelToken | undefined>>;
|
|
622
640
|
fetchJobs(body?: {
|
|
623
641
|
amt?: number;
|