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

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 +331 -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 +67 -0
  8. package/dist/client/core/utils.d.ts +1 -0
  9. package/dist/client/core/utils.js +13 -0
  10. package/dist/client/core/xhr.d.ts +1 -0
  11. package/dist/client/core/xhr.js +87 -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,87 @@
1
+ import { AvroQueryClient } from '../../client/QueryClient';
2
+ import { StandardError } from '../../types/error';
3
+ AvroQueryClient.prototype._xhr = function (method, path, body, cancelToken, headers = {}, isIdempotent = false, retryCount = 0, progressUpdateCallback) {
4
+ const checkCancelled = () => {
5
+ if (cancelToken?.isCancelled()) {
6
+ return new StandardError(0, 'Request cancelled');
7
+ }
8
+ return null;
9
+ };
10
+ return new Promise((resolve, reject) => {
11
+ this.config.authManager.accessToken().then(token => {
12
+ const cancelErr = checkCancelled();
13
+ if (cancelErr)
14
+ return reject(cancelErr);
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
+ const cancelErr = checkCancelled();
23
+ if (cancelErr)
24
+ return reject(cancelErr);
25
+ if (xhr.status === 401 && this.config.authManager.refreshTokens && retryCount === 0) {
26
+ this.config.authManager
27
+ .refreshTokens()
28
+ .then(() => {
29
+ this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
30
+ })
31
+ .catch(() => reject(new StandardError(401, 'Unauthorized (refresh failed)')));
32
+ return;
33
+ }
34
+ if (xhr.status >= 200 && xhr.status < 300) {
35
+ try {
36
+ resolve(JSON.parse(xhr.responseText));
37
+ }
38
+ catch {
39
+ resolve(xhr.responseText);
40
+ }
41
+ }
42
+ else {
43
+ if (retryCount < this.config.maxRetries) {
44
+ const delay = this.getDelay(this.config.retryStrategy, retryCount);
45
+ setTimeout(() => {
46
+ this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
47
+ }, delay);
48
+ }
49
+ else {
50
+ let msg = xhr.statusText;
51
+ try {
52
+ const parsed = JSON.parse(xhr.responseText);
53
+ msg = parsed.msg || msg;
54
+ }
55
+ catch {
56
+ console.warn('Failed to parse error response:', xhr.responseText);
57
+ }
58
+ reject(new StandardError(xhr.status, msg));
59
+ }
60
+ }
61
+ };
62
+ if (progressUpdateCallback && xhr.upload) {
63
+ xhr.upload.onprogress = (event) => {
64
+ if (event.lengthComputable) {
65
+ progressUpdateCallback(event.loaded, event.total);
66
+ }
67
+ };
68
+ }
69
+ xhr.onerror = () => {
70
+ if (retryCount < this.config.maxRetries) {
71
+ const delay = this.getDelay(this.config.retryStrategy, retryCount);
72
+ setTimeout(() => {
73
+ this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
74
+ }, delay);
75
+ }
76
+ else {
77
+ reject(new StandardError(0, 'Network Error'));
78
+ }
79
+ };
80
+ if (this.config.timeout) {
81
+ xhr.timeout = this.config.timeout;
82
+ xhr.ontimeout = () => reject(new StandardError(0, 'Request timed out'));
83
+ }
84
+ xhr.send(body);
85
+ });
86
+ });
87
+ };
@@ -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 {};