@go-avro/avro-js 0.0.2-beta.8 → 0.0.2-beta.81

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 (46) hide show
  1. package/README.md +1 -0
  2. package/dist/auth/AuthManager.d.ts +4 -0
  3. package/dist/auth/AuthManager.js +13 -0
  4. package/dist/client/QueryClient.d.ts +332 -14
  5. package/dist/client/QueryClient.js +251 -197
  6. package/dist/client/core/fetch.d.ts +1 -0
  7. package/dist/client/core/fetch.js +63 -0
  8. package/dist/client/core/utils.d.ts +1 -0
  9. package/dist/client/core/utils.js +14 -0
  10. package/dist/client/core/xhr.d.ts +1 -0
  11. package/dist/client/core/xhr.js +84 -0
  12. package/dist/client/hooks/analytics.d.ts +1 -0
  13. package/dist/client/hooks/analytics.js +10 -0
  14. package/dist/client/hooks/avro.d.ts +1 -0
  15. package/dist/client/hooks/avro.js +9 -0
  16. package/dist/client/hooks/bills.d.ts +1 -0
  17. package/dist/client/hooks/bills.js +141 -0
  18. package/dist/client/hooks/chats.d.ts +1 -0
  19. package/dist/client/hooks/chats.js +37 -0
  20. package/dist/client/hooks/companies.d.ts +1 -0
  21. package/dist/client/hooks/companies.js +79 -0
  22. package/dist/client/hooks/events.d.ts +1 -0
  23. package/dist/client/hooks/events.js +307 -0
  24. package/dist/client/hooks/jobs.d.ts +1 -0
  25. package/dist/client/hooks/jobs.js +184 -0
  26. package/dist/client/hooks/messages.d.ts +1 -0
  27. package/dist/client/hooks/messages.js +30 -0
  28. package/dist/client/hooks/months.d.ts +1 -0
  29. package/dist/client/hooks/months.js +92 -0
  30. package/dist/client/hooks/plans.d.ts +1 -0
  31. package/dist/client/hooks/plans.js +9 -0
  32. package/dist/client/hooks/root.d.ts +1 -0
  33. package/dist/client/hooks/root.js +8 -0
  34. package/dist/client/hooks/routes.d.ts +1 -0
  35. package/dist/client/hooks/routes.js +123 -0
  36. package/dist/client/hooks/sessions.d.ts +1 -0
  37. package/dist/client/hooks/sessions.js +154 -0
  38. package/dist/client/hooks/teams.d.ts +1 -0
  39. package/dist/client/hooks/teams.js +117 -0
  40. package/dist/client/hooks/users.d.ts +1 -0
  41. package/dist/client/hooks/users.js +104 -0
  42. package/dist/index.d.ts +19 -0
  43. package/dist/index.js +19 -0
  44. package/dist/types/api.d.ts +118 -29
  45. package/dist/types/api.js +10 -1
  46. package/package.json +6 -1
@@ -0,0 +1,84 @@
1
+ import { AvroQueryClient } from '../../client/QueryClient';
2
+ import { StandardError } from '../../types/error';
3
+ AvroQueryClient.prototype._xhr = async function (method, path, body, cancelToken, headers = {}, isIdempotent = false, retryCount = 0, progressUpdateCallback) {
4
+ const checkCancelled = () => {
5
+ if (cancelToken?.isCancelled()) {
6
+ throw new StandardError(0, 'Request cancelled');
7
+ }
8
+ };
9
+ for (let attempt = 0; attempt <= this.config.maxRetries; attempt++) {
10
+ try {
11
+ checkCancelled();
12
+ const token = await this.config.authManager.accessToken();
13
+ checkCancelled();
14
+ const result = await new Promise((resolve, reject) => {
15
+ const xhr = new XMLHttpRequest();
16
+ const url = this.config.baseUrl + path;
17
+ xhr.open(method, url, true);
18
+ if (token)
19
+ xhr.setRequestHeader('Authorization', `Bearer ${token}`);
20
+ Object.entries(headers).forEach(([key, value]) => xhr.setRequestHeader(key, value));
21
+ xhr.onload = () => {
22
+ if (xhr.status >= 200 && xhr.status < 300) {
23
+ try {
24
+ const responseText = xhr.responseText;
25
+ if (!responseText) {
26
+ return resolve(undefined);
27
+ }
28
+ resolve(JSON.parse(responseText));
29
+ }
30
+ catch (e) {
31
+ reject(new StandardError(0, `Failed to parse successful response: ${e.message}`));
32
+ }
33
+ }
34
+ else {
35
+ let msg = xhr.responseText || xhr.statusText;
36
+ try {
37
+ const parsed = JSON.parse(xhr.responseText);
38
+ msg = parsed.message ?? parsed.msg ?? msg;
39
+ }
40
+ catch (e) {
41
+ console.error('Ignoring:', e);
42
+ }
43
+ reject(new StandardError(xhr.status, msg));
44
+ }
45
+ };
46
+ xhr.onerror = () => reject(new StandardError(0, 'Network Error'));
47
+ if (this.config.timeout) {
48
+ xhr.timeout = this.config.timeout;
49
+ xhr.ontimeout = () => reject(new StandardError(0, 'Request timed out'));
50
+ }
51
+ if (progressUpdateCallback && xhr.upload) {
52
+ xhr.upload.onprogress = (event) => {
53
+ if (event.lengthComputable) {
54
+ progressUpdateCallback(event.loaded, event.total);
55
+ }
56
+ };
57
+ }
58
+ xhr.send(body);
59
+ });
60
+ return result;
61
+ }
62
+ catch (error) {
63
+ if (!(error instanceof StandardError)) {
64
+ const message = error instanceof Error ? error.message : String(error);
65
+ throw new StandardError(0, `An unexpected error occurred: ${message}`);
66
+ }
67
+ if (error.status === 401 && this.config.authManager.refreshTokens && attempt === 0) {
68
+ try {
69
+ await this.config.authManager.refreshTokens();
70
+ continue;
71
+ }
72
+ catch (refreshError) {
73
+ throw new StandardError(401, 'Unauthorized (refresh failed)');
74
+ }
75
+ }
76
+ if (attempt >= this.config.maxRetries) {
77
+ throw error;
78
+ }
79
+ const delay = this.getDelay(this.config.retryStrategy, attempt);
80
+ await this.sleep(delay);
81
+ }
82
+ }
83
+ throw new StandardError(0, 'Request failed after maximum retries.');
84
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { useQuery, useQueryClient } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ AvroQueryClient.prototype.useGetAnalytics = function () {
4
+ const queryClient = useQueryClient();
5
+ return useQuery({
6
+ queryKey: ['analytics'],
7
+ queryFn: () => this.post('/avro/analytics', null),
8
+ enabled: true,
9
+ });
10
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { useQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ AvroQueryClient.prototype.useGetAvro = function () {
4
+ return useQuery({
5
+ queryKey: ['avro'],
6
+ queryFn: () => this.get('/avro'),
7
+ enabled: true,
8
+ });
9
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,141 @@
1
+ import { useQueryClient, useInfiniteQuery, useQuery, useMutation } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.useGetBills = function (companyGuid, body) {
4
+ const queryClient = useQueryClient();
5
+ const result = useInfiniteQuery({
6
+ queryKey: [
7
+ 'bills',
8
+ companyGuid,
9
+ body.query ?? "",
10
+ body.known_ids ?? [],
11
+ body.unknown_ids ?? [],
12
+ body.paid ?? false,
13
+ ],
14
+ initialPageParam: 0,
15
+ getNextPageParam: (lastPage, allPages) => {
16
+ if (lastPage.length < (body.amt ?? 50))
17
+ return undefined;
18
+ return allPages.flat().length; // next offset
19
+ },
20
+ queryFn: ({ pageParam = 0 }) => this.fetchBills(companyGuid, { ...body, offset: pageParam }),
21
+ });
22
+ if (result.data) {
23
+ result.data.pages.forEach((data_page) => {
24
+ data_page.forEach((bill) => {
25
+ queryClient.setQueryData(['bill', bill.id], bill);
26
+ });
27
+ });
28
+ }
29
+ return result;
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
+ };
38
+ AvroQueryClient.prototype.useCreateBill = function () {
39
+ const queryClient = useQueryClient();
40
+ return useMutation({
41
+ mutationFn: async ({ companyId, data, }) => {
42
+ const body = {
43
+ events: data.line_items.filter(item => item.line_item_type === 'EVENT').map(item => item.id),
44
+ months: data.line_items.filter(item => item.line_item_type === 'SERVICE_MONTH').map(item => item.id),
45
+ line_items: data.line_items.filter(item => item.line_item_type === 'CUSTOM'),
46
+ manual_emails: data.custom_emails,
47
+ users: data.users,
48
+ due_date: data.due_date,
49
+ };
50
+ return this.post(`/company/${companyId}/bill`, JSON.stringify(body), undefined, {
51
+ 'Content-Type': 'application/json',
52
+ });
53
+ },
54
+ onMutate: async ({ companyId }) => {
55
+ await queryClient.cancelQueries({ queryKey: ['bills', companyId] });
56
+ const previousBills = queryClient.getQueryData(['bills', companyId]);
57
+ // TODO: Create a fake bill object for optimistic update and update events and months accordingly
58
+ return { previousBills };
59
+ },
60
+ onError: (_err, _variables, context) => {
61
+ if (context?.previousBills) {
62
+ queryClient.setQueryData(['bills'], context.previousBills);
63
+ }
64
+ },
65
+ onSettled: (_data, _error, variables) => {
66
+ queryClient.invalidateQueries({ queryKey: ['bills', variables.companyId] });
67
+ queryClient.invalidateQueries({ queryKey: ['events', variables.companyId] });
68
+ queryClient.invalidateQueries({ queryKey: ['months', variables.companyId] });
69
+ },
70
+ });
71
+ };
72
+ AvroQueryClient.prototype.useUpdateBill = function () {
73
+ const queryClient = useQueryClient();
74
+ return useMutation({
75
+ mutationFn: ({ billId, updates }) => {
76
+ return this.put(`/bill/${billId}`, JSON.stringify(updates), undefined, {
77
+ "Content-Type": "application/json",
78
+ });
79
+ },
80
+ onMutate: async ({ billId, updates }) => {
81
+ await queryClient.cancelQueries({ queryKey: ['bills'] });
82
+ await queryClient.cancelQueries({ queryKey: ['bill', billId] });
83
+ const previousBills = queryClient.getQueryData(['bills']);
84
+ const previousBill = queryClient.getQueryData(['bill', billId]);
85
+ queryClient.setQueryData(['bill', billId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
86
+ queryClient.setQueriesData({ queryKey: ['bills'] }, (oldData) => {
87
+ if (!oldData)
88
+ return oldData;
89
+ if (oldData.pages) {
90
+ return {
91
+ ...oldData,
92
+ pages: oldData.pages.map((page) => page.map((bill) => bill.id === billId ? { ...bill, ...updates } : bill)),
93
+ };
94
+ }
95
+ if (Array.isArray(oldData)) {
96
+ return oldData.map((bill) => bill.id === billId ? { ...bill, ...updates } : bill);
97
+ }
98
+ return oldData;
99
+ });
100
+ return { previousBills, previousBill };
101
+ },
102
+ onError: (err, variables, context) => {
103
+ const { billId } = variables;
104
+ if (context?.previousBills) {
105
+ queryClient.setQueryData(['bills'], context.previousBills);
106
+ }
107
+ if (context?.previousBill) {
108
+ queryClient.setQueryData(['bill', billId], context.previousBill);
109
+ }
110
+ },
111
+ onSettled: (data, error, variables) => {
112
+ const { billId } = variables;
113
+ queryClient.invalidateQueries({ queryKey: ['bills'] });
114
+ queryClient.invalidateQueries({ queryKey: ['bill', billId] });
115
+ },
116
+ });
117
+ };
118
+ AvroQueryClient.prototype.useDeleteBill = function () {
119
+ const queryClient = useQueryClient();
120
+ return useMutation({
121
+ mutationFn: async ({ billId }) => {
122
+ return this.delete(`/bill/${billId}`);
123
+ },
124
+ onMutate: async ({ billId }) => {
125
+ await queryClient.cancelQueries({ queryKey: ['bills'] });
126
+ const previousBills = queryClient.getQueryData(['bills']);
127
+ // TODO: Create a fake bill object for optimistic update and update events and months accordingly
128
+ return { previousBills };
129
+ },
130
+ onError: (_err, _variables, context) => {
131
+ if (context?.previousBills) {
132
+ queryClient.setQueryData(['bills'], context.previousBills);
133
+ }
134
+ },
135
+ onSettled: (_data, _error) => {
136
+ queryClient.invalidateQueries({ queryKey: ['bills'] });
137
+ queryClient.invalidateQueries({ queryKey: ['events'] });
138
+ queryClient.invalidateQueries({ queryKey: ['months'] });
139
+ },
140
+ });
141
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,37 @@
1
+ import { useInfiniteQuery, useQuery, useQueryClient } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ AvroQueryClient.prototype.useGetChats = function (companyGuid, body) {
4
+ const queryClient = useQueryClient();
5
+ const result = useInfiniteQuery({
6
+ queryKey: [
7
+ 'chats',
8
+ companyGuid,
9
+ body.amt ?? 50,
10
+ body.known_ids ?? [],
11
+ body.unknown_ids ?? [],
12
+ body.query ?? '',
13
+ ],
14
+ initialPageParam: 0,
15
+ getNextPageParam: (lastPage, allPages) => {
16
+ if (lastPage.length < (body.amt ?? 50))
17
+ return undefined;
18
+ return allPages.flat().length; // next offset
19
+ },
20
+ queryFn: ({ pageParam = 0 }) => this.fetchChats(companyGuid, { ...body, offset: pageParam }),
21
+ });
22
+ if (result.data) {
23
+ result.data.pages.forEach((data_page) => {
24
+ data_page.forEach((chat) => {
25
+ queryClient.setQueryData(['chat', chat.id], chat);
26
+ });
27
+ });
28
+ }
29
+ return result;
30
+ };
31
+ AvroQueryClient.prototype.useGetChat = function (chatId) {
32
+ return useQuery({
33
+ queryKey: ['chat', chatId],
34
+ queryFn: () => this.get(`/chat/${chatId}`),
35
+ enabled: Boolean(chatId),
36
+ });
37
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,79 @@
1
+ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.useGetCompanies = function (options = {}) {
4
+ return useQuery({
5
+ queryKey: ['/company/list'],
6
+ queryFn: () => this.get('/company/list'),
7
+ ...options,
8
+ });
9
+ };
10
+ AvroQueryClient.prototype.useGetCompany = function (companyId) {
11
+ return useQuery({
12
+ queryKey: ['company', companyId],
13
+ queryFn: () => this.get(`/company/${companyId}`),
14
+ enabled: Boolean(companyId),
15
+ });
16
+ };
17
+ AvroQueryClient.prototype.useUpdateCompany = function () {
18
+ const queryClient = useQueryClient();
19
+ return useMutation({
20
+ mutationFn: async ({ companyId, companyData, }) => {
21
+ return this.put(`/company/${companyId}`, JSON.stringify(companyData), undefined, { "Content-Type": "application/json" });
22
+ },
23
+ onMutate: async ({ companyId, companyData }) => {
24
+ await queryClient.cancelQueries({ queryKey: ['company', companyId] });
25
+ await queryClient.cancelQueries({ queryKey: ['/company/list'] });
26
+ const previousCompany = queryClient.getQueryData(['company', companyId]);
27
+ const previousCompanyList = queryClient.getQueryData(['/company/list']);
28
+ queryClient.setQueryData(['company', companyId], (oldData) => oldData ? { ...oldData, ...companyData } : undefined);
29
+ queryClient.setQueryData(['/company/list'], (oldList) => {
30
+ if (!oldList)
31
+ return oldList;
32
+ return oldList.map((company) => company.id === companyId ? { ...company, ...companyData } : company);
33
+ });
34
+ return { previousCompany, previousCompanyList };
35
+ },
36
+ onError: (_err, variables, context) => {
37
+ const { companyId } = variables;
38
+ if (context?.previousCompany) {
39
+ queryClient.setQueryData(['company', companyId], context.previousCompany);
40
+ }
41
+ if (context?.previousCompanyList) {
42
+ queryClient.setQueryData(['/company/list'], context.previousCompanyList);
43
+ }
44
+ },
45
+ onSettled: (_data, _error, variables) => {
46
+ const { companyId } = variables;
47
+ queryClient.invalidateQueries({ queryKey: ['company', companyId] });
48
+ queryClient.invalidateQueries({ queryKey: ['/company/list'] });
49
+ },
50
+ });
51
+ };
52
+ AvroQueryClient.prototype.useDeleteCompany = function () {
53
+ const queryClient = useQueryClient();
54
+ return useMutation({
55
+ mutationFn: async ({ companyId }) => {
56
+ return this.delete(`/company/${companyId}`);
57
+ },
58
+ onMutate: async ({ companyId }) => {
59
+ await queryClient.cancelQueries({ queryKey: ['/company/list'] });
60
+ await queryClient.cancelQueries({ queryKey: ['company', companyId] });
61
+ const previousCompanyList = queryClient.getQueryData(['/company/list']);
62
+ queryClient.setQueryData(['/company/list'], (oldList) => {
63
+ if (!oldList)
64
+ return oldList;
65
+ return oldList.filter((company) => company.id !== companyId);
66
+ });
67
+ return { previousCompanyList };
68
+ },
69
+ onError: (err, companyId, context) => {
70
+ if (context?.previousCompanyList) {
71
+ queryClient.setQueryData(['/company/list'], context.previousCompanyList);
72
+ }
73
+ },
74
+ onSettled: (_data, _error, companyId) => {
75
+ queryClient.invalidateQueries({ queryKey: ['/company/list'] });
76
+ queryClient.invalidateQueries({ queryKey: ['company', companyId] });
77
+ },
78
+ });
79
+ };
@@ -0,0 +1 @@
1
+ export {};