@go-avro/avro-js 0.0.2-beta.36 → 0.0.2-beta.38

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 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,8 +1,8 @@
1
1
  import { AuthManager } from '../auth/AuthManager';
2
- import { _Event, Bill, Company, Job, LineItem, ServiceMonth } from '../types/api';
2
+ import { _Event, Bill, Company, Job, LineItem, ServiceMonth, User } from '../types/api';
3
3
  import { CancelToken, RetryStrategy } from '../types/client';
4
4
  import { StandardError } from '../types/error';
5
- import { InfiniteData, UseInfiniteQueryResult, useQuery, UseQueryResult } from '@tanstack/react-query';
5
+ import { InfiniteData, UseInfiniteQueryResult, useMutation, useQuery, UseQueryResult } from '@tanstack/react-query';
6
6
  export interface AvroQueryClientConfig {
7
7
  baseUrl: string;
8
8
  authManager: AuthManager;
@@ -15,16 +15,16 @@ declare module '../client/QueryClient' {
15
15
  _xhr<T>(method: string, path: string, body: any, cancelToken?: CancelToken, headers?: Record<string, string>, isIdempotent?: boolean, retryCount?: number): Promise<T>;
16
16
  _fetch<T>(method: string, path: string, body: any, cancelToken?: CancelToken, headers?: Record<string, string>, isIdempotent?: boolean, retryCount?: number): Promise<T>;
17
17
  getDelay(strategy: RetryStrategy, attempt: number): number;
18
- getRoot(): ReturnType<typeof useQuery>;
19
- getJobs(companyGuid: string, body: {
18
+ useGetRoot(): ReturnType<typeof useQuery>;
19
+ useGetJobs(companyGuid: string, body: {
20
20
  amt?: number;
21
21
  query?: string;
22
22
  }, total: number, onProgress?: (fraction: number) => void, isMainLoad?: boolean): UseQueryResult<Job[], StandardError>;
23
- getRoutes(companyGuid: string, body: {
23
+ useGetRoutes(companyGuid: string, body: {
24
24
  amt?: number;
25
25
  query?: string;
26
26
  }, total: number, onProgress?: (fraction: number) => void): UseQueryResult<any[], StandardError>;
27
- getEvents(companyGuid: string, body: {
27
+ useGetEvents(companyGuid: string, body: {
28
28
  amt?: number;
29
29
  known_ids?: string[];
30
30
  unknown_ids?: string[];
@@ -34,7 +34,7 @@ declare module '../client/QueryClient' {
34
34
  paid?: boolean;
35
35
  jobId?: string;
36
36
  }): UseInfiniteQueryResult<InfiniteData<_Event[], unknown>, Error>;
37
- getMonths(companyGuid: string, body: {
37
+ useGetMonths(companyGuid: string, body: {
38
38
  amt?: number;
39
39
  known_ids?: string[];
40
40
  unknown_ids?: string[];
@@ -44,18 +44,36 @@ declare module '../client/QueryClient' {
44
44
  paid?: boolean;
45
45
  jobId?: string;
46
46
  }): UseInfiniteQueryResult<InfiniteData<ServiceMonth[], unknown>, Error>;
47
- getBills(companyGuid: string, body: {
47
+ useGetBills(companyGuid: string, body: {
48
48
  amt?: number;
49
49
  known_ids?: string[];
50
50
  unknown_ids?: string[];
51
51
  query?: string;
52
52
  paid?: boolean;
53
53
  }): UseInfiniteQueryResult<InfiniteData<Bill[], unknown>, Error>;
54
- getCompanies(options?: {}): UseQueryResult<{
54
+ useGetCompanies(options?: {}): UseQueryResult<{
55
55
  name: string;
56
56
  id: string;
57
57
  }[], StandardError>;
58
- getCompany(companyId: string): UseQueryResult<Company, StandardError>;
58
+ useGetCompany(companyId: string): UseQueryResult<Company, StandardError>;
59
+ useGetJob(jobId: string): UseQueryResult<Job, StandardError>;
60
+ useGetEvent(eventId: string): UseQueryResult<_Event, StandardError>;
61
+ useGetUser(userId: string): UseQueryResult<User, StandardError>;
62
+ useGetSelf(): UseQueryResult<User, StandardError>;
63
+ useGetBill(billId: string): UseQueryResult<Bill, StandardError>;
64
+ useUpdateJob(): ReturnType<typeof useMutation<unknown, unknown, {
65
+ jobId: string;
66
+ updates: Partial<Job>;
67
+ }, unknown>>;
68
+ useDeleteJob(): ReturnType<typeof useMutation<unknown, unknown, {
69
+ jobId: string;
70
+ }, unknown>>;
71
+ useCreateJob(): ReturnType<typeof useMutation<{
72
+ id: string;
73
+ }, unknown, {
74
+ companyId: string;
75
+ jobData: Partial<Job>;
76
+ }, unknown>>;
59
77
  }
60
78
  }
61
79
  export declare class AvroQueryClient {
@@ -1,6 +1,6 @@
1
1
  import { AvroQueryClient } from '../../client/QueryClient';
2
- import { useQueryClient, useInfiniteQuery } from '@tanstack/react-query';
3
- AvroQueryClient.prototype.getBills = function (companyGuid, body) {
2
+ import { useQueryClient, useInfiniteQuery, useQuery } from '@tanstack/react-query';
3
+ AvroQueryClient.prototype.useGetBills = function (companyGuid, body) {
4
4
  const queryClient = useQueryClient();
5
5
  const result = useInfiniteQuery({
6
6
  queryKey: [
@@ -28,3 +28,10 @@ AvroQueryClient.prototype.getBills = function (companyGuid, body) {
28
28
  }
29
29
  return result;
30
30
  };
31
+ AvroQueryClient.prototype.useGetBill = function (billId) {
32
+ return useQuery({
33
+ queryKey: ['bill', billId],
34
+ queryFn: () => this.get(`/bill/${billId}`),
35
+ enabled: Boolean(billId),
36
+ });
37
+ };
@@ -1,13 +1,13 @@
1
1
  import { AvroQueryClient } from '../../client/QueryClient';
2
2
  import { useQuery } from '@tanstack/react-query';
3
- AvroQueryClient.prototype.getCompanies = function (options = {}) {
3
+ AvroQueryClient.prototype.useGetCompanies = function (options = {}) {
4
4
  return useQuery({
5
5
  queryKey: ['/company/list'],
6
6
  queryFn: () => this.get('/company/list'),
7
7
  ...options,
8
8
  });
9
9
  };
10
- AvroQueryClient.prototype.getCompany = function (companyId) {
10
+ AvroQueryClient.prototype.useGetCompany = function (companyId) {
11
11
  return useQuery({
12
12
  queryKey: ['company', companyId],
13
13
  queryFn: () => this.get(`/company/${companyId}`),
@@ -1,6 +1,6 @@
1
1
  import { AvroQueryClient } from '../../client/QueryClient';
2
- import { useQueryClient, useInfiniteQuery } from '@tanstack/react-query';
3
- AvroQueryClient.prototype.getEvents = function (companyGuid, body) {
2
+ import { useQueryClient, useInfiniteQuery, useQuery } from '@tanstack/react-query';
3
+ AvroQueryClient.prototype.useGetEvents = function (companyGuid, body) {
4
4
  const queryClient = useQueryClient();
5
5
  const result = useInfiniteQuery({
6
6
  queryKey: [
@@ -32,3 +32,10 @@ AvroQueryClient.prototype.getEvents = function (companyGuid, body) {
32
32
  }
33
33
  return result;
34
34
  };
35
+ AvroQueryClient.prototype.useGetEvent = function (eventId) {
36
+ return useQuery({
37
+ queryKey: ['event', eventId],
38
+ queryFn: () => this.get(`/event/${eventId}`),
39
+ enabled: Boolean(eventId),
40
+ });
41
+ };
@@ -1,6 +1,6 @@
1
1
  import { AvroQueryClient } from '../../client/QueryClient';
2
- import { useQuery, useQueryClient } from '@tanstack/react-query';
3
- AvroQueryClient.prototype.getJobs = function (companyGuid, body, total = 0, onProgress, isMainLoad = false) {
2
+ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
3
+ AvroQueryClient.prototype.useGetJobs = function (companyGuid, body, total = 0, onProgress, isMainLoad = false) {
4
4
  const queryClient = useQueryClient();
5
5
  return useQuery({
6
6
  queryKey: [isMainLoad ? 'main' : 'jobs', companyGuid, body.amt ?? 50, body.query ?? ""],
@@ -45,3 +45,140 @@ AvroQueryClient.prototype.getJobs = function (companyGuid, body, total = 0, onPr
45
45
  ...isMainLoad ? { staleTime: Infinity, cacheTime: Infinity } : {},
46
46
  });
47
47
  };
48
+ AvroQueryClient.prototype.useGetJob = function (jobId) {
49
+ return useQuery({
50
+ queryKey: ['job', jobId],
51
+ queryFn: () => this.get(`/job/${jobId}`),
52
+ enabled: Boolean(jobId),
53
+ });
54
+ };
55
+ AvroQueryClient.prototype.useCreateJob = function () {
56
+ const queryClient = useQueryClient();
57
+ return useMutation({
58
+ mutationFn: ({ companyId, jobData }) => {
59
+ return this.post(`/company/${companyId}/job`, JSON.stringify(jobData), undefined, {
60
+ "Content-Type": "application/json",
61
+ });
62
+ },
63
+ onMutate: async ({ companyId, jobData }) => {
64
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
65
+ const previousJobs = queryClient.getQueryData(['jobs']);
66
+ queryClient.setQueryData(['jobs'], (oldData) => {
67
+ if (!oldData)
68
+ return [jobData];
69
+ if (oldData.pages) {
70
+ const firstPage = oldData.pages[0] || [];
71
+ return {
72
+ ...oldData,
73
+ pages: [[jobData, ...firstPage], ...oldData.pages.slice(1)],
74
+ };
75
+ }
76
+ if (Array.isArray(oldData)) {
77
+ return [jobData, ...oldData];
78
+ }
79
+ return oldData;
80
+ });
81
+ return { previousJobs };
82
+ },
83
+ onError: (err, variables, context) => {
84
+ if (context?.previousJobs) {
85
+ queryClient.setQueryData(['jobs'], context.previousJobs);
86
+ }
87
+ },
88
+ onSettled: (data, error, variables) => {
89
+ const { id: jobId } = data ?? {};
90
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
91
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
92
+ },
93
+ });
94
+ };
95
+ AvroQueryClient.prototype.useUpdateJob = function () {
96
+ const queryClient = useQueryClient();
97
+ return useMutation({
98
+ mutationFn: ({ jobId, updates }) => {
99
+ return this.put(`/job/${jobId}`, JSON.stringify(updates), undefined, {
100
+ "Content-Type": "application/json",
101
+ });
102
+ },
103
+ onMutate: async ({ jobId, updates }) => {
104
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
105
+ await queryClient.cancelQueries({ queryKey: ['job', jobId] });
106
+ const previousJobs = queryClient.getQueryData(['jobs']);
107
+ const previousJob = queryClient.getQueryData(['job', jobId]);
108
+ queryClient.setQueryData(['job', jobId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
109
+ queryClient.setQueriesData({ queryKey: ['jobs'] }, (oldData) => {
110
+ if (!oldData)
111
+ return oldData;
112
+ if (oldData.pages) {
113
+ return {
114
+ ...oldData,
115
+ pages: oldData.pages.map((page) => page.map((job) => job.id === jobId ? { ...job, ...updates } : job)),
116
+ };
117
+ }
118
+ if (Array.isArray(oldData)) {
119
+ return oldData.map((job) => job.id === jobId ? { ...job, ...updates } : job);
120
+ }
121
+ return oldData;
122
+ });
123
+ return { previousJobs, previousJob };
124
+ },
125
+ onError: (err, variables, context) => {
126
+ const { jobId } = variables;
127
+ if (context?.previousJobs) {
128
+ queryClient.setQueryData(['jobs'], context.previousJobs);
129
+ }
130
+ if (context?.previousJob) {
131
+ queryClient.setQueryData(['job', jobId], context.previousJob);
132
+ }
133
+ },
134
+ onSettled: (data, error, variables) => {
135
+ const { jobId } = variables;
136
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
137
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
138
+ },
139
+ });
140
+ };
141
+ AvroQueryClient.prototype.useDeleteJob = function () {
142
+ const queryClient = useQueryClient();
143
+ return useMutation({
144
+ mutationFn: async ({ jobId }) => {
145
+ return this.delete(`/job/${jobId}`, undefined, {
146
+ "Content-Type": "application/json",
147
+ });
148
+ },
149
+ onMutate: async ({ jobId }) => {
150
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
151
+ await queryClient.cancelQueries({ queryKey: ['job', jobId] });
152
+ const previousJobs = queryClient.getQueryData(['jobs']);
153
+ const previousJob = queryClient.getQueryData(['job', jobId]);
154
+ queryClient.setQueryData(['job', jobId], undefined);
155
+ queryClient.setQueriesData({ queryKey: ['jobs'] }, (oldData) => {
156
+ if (!oldData)
157
+ return oldData;
158
+ if (oldData.pages) {
159
+ const updatedPages = oldData.pages.map((page) => page.filter((job) => job.id !== jobId));
160
+ return { ...oldData, pages: updatedPages };
161
+ }
162
+ if (Array.isArray(oldData)) {
163
+ return oldData.filter((job) => job.id !== jobId);
164
+ }
165
+ return oldData;
166
+ });
167
+ return { previousJobs, previousJob };
168
+ },
169
+ onError: (_err, variables, context) => {
170
+ const { jobId } = variables;
171
+ if (context?.previousJobs) {
172
+ queryClient.setQueryData(['jobs'], context.previousJobs);
173
+ }
174
+ if (context?.previousJob) {
175
+ queryClient.setQueryData(['job', jobId], context.previousJob);
176
+ }
177
+ },
178
+ onSettled: (_data, _error, variables) => {
179
+ const { jobId } = variables;
180
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
181
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
182
+ },
183
+ });
184
+ };
@@ -1,6 +1,6 @@
1
1
  import { AvroQueryClient } from '../../client/QueryClient';
2
2
  import { useQueryClient, useInfiniteQuery } from '@tanstack/react-query';
3
- AvroQueryClient.prototype.getMonths = function (companyGuid, body) {
3
+ AvroQueryClient.prototype.useGetMonths = function (companyGuid, body) {
4
4
  const queryClient = useQueryClient();
5
5
  const result = useInfiniteQuery({
6
6
  queryKey: [
@@ -1,6 +1,6 @@
1
1
  import { AvroQueryClient } from '../../client/QueryClient';
2
2
  import { useQuery } from '@tanstack/react-query';
3
- AvroQueryClient.prototype.getRoot = function () {
3
+ AvroQueryClient.prototype.useGetRoot = function () {
4
4
  return useQuery({
5
5
  queryKey: ['health'],
6
6
  queryFn: () => this.get('/', undefined) // your async fetch function
@@ -1,6 +1,6 @@
1
1
  import { AvroQueryClient } from '../../client/QueryClient';
2
2
  import { useQuery, useQueryClient } from '@tanstack/react-query';
3
- AvroQueryClient.prototype.getRoutes = function (companyGuid, body, total = 0, onProgress) {
3
+ AvroQueryClient.prototype.useGetRoutes = function (companyGuid, body, total = 0, onProgress) {
4
4
  const queryClient = useQueryClient();
5
5
  return useQuery({
6
6
  queryKey: ['routes', companyGuid, body.amt ?? 50, body.query ?? ""],
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import { AvroQueryClient } from "../../client/QueryClient";
2
+ import { useQuery } from "@tanstack/react-query";
3
+ AvroQueryClient.prototype.useGetUser = function (userId) {
4
+ return useQuery({
5
+ queryKey: ['user', userId],
6
+ queryFn: () => this.get(`/user/${userId}`),
7
+ enabled: Boolean(userId),
8
+ });
9
+ };
10
+ AvroQueryClient.prototype.useGetSelf = function () {
11
+ return useQuery({
12
+ queryKey: ['user'],
13
+ queryFn: () => this.get(`/user`),
14
+ enabled: Boolean(this),
15
+ });
16
+ };
package/dist/index.d.ts CHANGED
@@ -11,6 +11,7 @@ import './client/hooks/events';
11
11
  import './client/hooks/months';
12
12
  import './client/hooks/bills';
13
13
  import './client/hooks/companies';
14
+ import './client/hooks/users';
14
15
  export * from './types/api';
15
16
  export * from './types/error';
16
17
  export * from './types/client';
package/dist/index.js CHANGED
@@ -11,6 +11,7 @@ import './client/hooks/events';
11
11
  import './client/hooks/months';
12
12
  import './client/hooks/bills';
13
13
  import './client/hooks/companies';
14
+ import './client/hooks/users';
14
15
  export * from './types/api';
15
16
  export * from './types/error';
16
17
  export * from './types/client';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.2-beta.36",
3
+ "version": "0.0.2-beta.38",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",