@go-avro/avro-js 0.0.2-beta.15 → 0.0.2-beta.150

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 (62) hide show
  1. package/README.md +1 -0
  2. package/dist/auth/AuthManager.d.ts +14 -5
  3. package/dist/auth/AuthManager.js +59 -23
  4. package/dist/auth/storage.d.ts +8 -8
  5. package/dist/auth/storage.js +19 -14
  6. package/dist/client/AvroQueryClientProvider.d.ts +14 -0
  7. package/dist/client/AvroQueryClientProvider.js +32 -0
  8. package/dist/client/QueryClient.d.ts +492 -16
  9. package/dist/client/QueryClient.js +396 -214
  10. package/dist/client/core/fetch.d.ts +1 -0
  11. package/dist/client/core/fetch.js +64 -0
  12. package/dist/client/core/utils.d.ts +1 -0
  13. package/dist/client/core/utils.js +14 -0
  14. package/dist/client/core/xhr.d.ts +1 -0
  15. package/dist/client/core/xhr.js +90 -0
  16. package/dist/client/hooks/analytics.d.ts +1 -0
  17. package/dist/client/hooks/analytics.js +26 -0
  18. package/dist/client/hooks/avro.d.ts +1 -0
  19. package/dist/client/hooks/avro.js +9 -0
  20. package/dist/client/hooks/bills.d.ts +1 -0
  21. package/dist/client/hooks/bills.js +165 -0
  22. package/dist/client/hooks/chats.d.ts +1 -0
  23. package/dist/client/hooks/chats.js +37 -0
  24. package/dist/client/hooks/companies.d.ts +1 -0
  25. package/dist/client/hooks/companies.js +143 -0
  26. package/dist/client/hooks/events.d.ts +1 -0
  27. package/dist/client/hooks/events.js +308 -0
  28. package/dist/client/hooks/groups.d.ts +1 -0
  29. package/dist/client/hooks/groups.js +130 -0
  30. package/dist/client/hooks/jobs.d.ts +1 -0
  31. package/dist/client/hooks/jobs.js +213 -0
  32. package/dist/client/hooks/labels.d.ts +1 -0
  33. package/dist/client/hooks/labels.js +130 -0
  34. package/dist/client/hooks/messages.d.ts +1 -0
  35. package/dist/client/hooks/messages.js +30 -0
  36. package/dist/client/hooks/months.d.ts +1 -0
  37. package/dist/client/hooks/months.js +93 -0
  38. package/dist/client/hooks/plans.d.ts +1 -0
  39. package/dist/client/hooks/plans.js +8 -0
  40. package/dist/client/hooks/proposal.d.ts +1 -0
  41. package/dist/client/hooks/proposal.js +22 -0
  42. package/dist/client/hooks/root.d.ts +1 -0
  43. package/dist/client/hooks/root.js +8 -0
  44. package/dist/client/hooks/routes.d.ts +1 -0
  45. package/dist/client/hooks/routes.js +167 -0
  46. package/dist/client/hooks/sessions.d.ts +1 -0
  47. package/dist/client/hooks/sessions.js +175 -0
  48. package/dist/client/hooks/skills.d.ts +1 -0
  49. package/dist/client/hooks/skills.js +123 -0
  50. package/dist/client/hooks/teams.d.ts +1 -0
  51. package/dist/client/hooks/teams.js +128 -0
  52. package/dist/client/hooks/users.d.ts +1 -0
  53. package/dist/client/hooks/users.js +118 -0
  54. package/dist/index.d.ts +26 -1
  55. package/dist/index.js +26 -1
  56. package/dist/types/api.d.ts +146 -38
  57. package/dist/types/api.js +10 -1
  58. package/dist/types/auth.d.ts +6 -5
  59. package/dist/types/auth.js +5 -1
  60. package/dist/types/cache.d.ts +9 -0
  61. package/dist/types/cache.js +1 -0
  62. package/package.json +6 -4
@@ -0,0 +1,123 @@
1
+ import { useMutation, useQuery } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.useCreateSkill = function () {
4
+ const queryClient = this.getQueryClient();
5
+ return useMutation({
6
+ mutationFn: async ({ skillData }) => {
7
+ return this.post(`/company/${this.companyId}/skill`, JSON.stringify(skillData), undefined, {
8
+ "Content-Type": "application/json",
9
+ });
10
+ },
11
+ onSettled: () => {
12
+ queryClient.invalidateQueries({ queryKey: ['company'] });
13
+ queryClient.invalidateQueries({ queryKey: ['skills'] });
14
+ },
15
+ });
16
+ };
17
+ AvroQueryClient.prototype.useGetSkills = function (body, total, onProgress) {
18
+ return useQuery({
19
+ queryKey: ['skills', this.companyId, body.amt ?? 50, body.query ?? "", total ?? "all"],
20
+ queryFn: async () => {
21
+ if (typeof total !== "number") {
22
+ return this.fetchSkills({ ...body, offset: 0 });
23
+ }
24
+ onProgress?.(0);
25
+ const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
26
+ let completed = 0;
27
+ const promises = Array.from({ length: pageCount }, (_, i) => this.fetchSkills({
28
+ ...body,
29
+ offset: i * (body.amt ?? 1),
30
+ }));
31
+ const trackedPromises = promises.map((promise) => promise.then((result) => {
32
+ completed++;
33
+ const fraction = completed / pageCount;
34
+ onProgress?.(fraction);
35
+ return result;
36
+ }));
37
+ const pages = await Promise.all(trackedPromises);
38
+ const skills = pages.flat();
39
+ return skills;
40
+ },
41
+ });
42
+ };
43
+ AvroQueryClient.prototype.useUpdateSkill = function () {
44
+ const queryClient = this.getQueryClient();
45
+ return useMutation({
46
+ mutationFn: async ({ skillId, updates }) => {
47
+ return this.put(`/skill/${skillId}`, JSON.stringify(updates), undefined, {
48
+ "Content-Type": "application/json",
49
+ });
50
+ },
51
+ onMutate: async ({ skillId, updates }) => {
52
+ await queryClient.cancelQueries({ queryKey: ['skills'] });
53
+ await queryClient.cancelQueries({ queryKey: ['skill', skillId] });
54
+ const previousSkills = queryClient.getQueryData(['skills']);
55
+ const previousSkill = queryClient.getQueryData(['skill', skillId]);
56
+ queryClient.setQueryData(['skill', skillId], (oldData) => {
57
+ if (!oldData)
58
+ return oldData;
59
+ return { ...oldData, ...updates };
60
+ });
61
+ queryClient.setQueriesData({ queryKey: ['skills'] }, (oldData) => {
62
+ if (!oldData)
63
+ return oldData;
64
+ if (oldData.pages) {
65
+ const updatedPages = oldData.pages.map((page) => page.map((skill) => skill.id === skillId ? { ...skill, ...updates } : skill));
66
+ return { ...oldData, pages: updatedPages };
67
+ }
68
+ if (Array.isArray(oldData)) {
69
+ return oldData.map((skill) => skill.id === skillId ? { ...skill, ...updates } : skill);
70
+ }
71
+ return oldData;
72
+ });
73
+ return { previousSkills, previousSkill };
74
+ },
75
+ onError: (_err, variables, context) => {
76
+ const { skillId } = variables;
77
+ if (context?.previousSkills) {
78
+ queryClient.setQueryData(['skills'], context.previousSkills);
79
+ }
80
+ if (context?.previousSkill) {
81
+ queryClient.setQueryData(['skill', skillId], context.previousSkill);
82
+ }
83
+ },
84
+ onSettled: (_data, _error, variables) => {
85
+ const { skillId } = variables;
86
+ queryClient.invalidateQueries({ queryKey: ['skills'] });
87
+ queryClient.invalidateQueries({ queryKey: ['skill', skillId] });
88
+ },
89
+ });
90
+ };
91
+ AvroQueryClient.prototype.useDeleteSkill = function () {
92
+ const queryClient = this.getQueryClient();
93
+ return useMutation({
94
+ mutationFn: async ({ skillId }) => {
95
+ return this.delete(`/skill/${skillId}`);
96
+ },
97
+ onMutate: async ({ skillId }) => {
98
+ await queryClient.cancelQueries({ queryKey: ['skills'] });
99
+ const previousSkills = queryClient.getQueryData(['skills']);
100
+ queryClient.setQueriesData({ queryKey: ['skills'] }, (oldData) => {
101
+ if (!oldData)
102
+ return oldData;
103
+ if (oldData.pages) {
104
+ const updatedPages = oldData.pages.map((page) => page.filter((skill) => skill.id !== skillId));
105
+ return { ...oldData, pages: updatedPages };
106
+ }
107
+ if (Array.isArray(oldData)) {
108
+ return oldData.filter((skill) => skill.id !== skillId);
109
+ }
110
+ return oldData;
111
+ });
112
+ return { previousSkills };
113
+ },
114
+ onError: (_err, variables, context) => {
115
+ if (context?.previousSkills) {
116
+ queryClient.setQueryData(['skills'], context.previousSkills);
117
+ }
118
+ },
119
+ onSettled: () => {
120
+ queryClient.invalidateQueries({ queryKey: ['skills'] });
121
+ },
122
+ });
123
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,128 @@
1
+ import { useMutation, useQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ AvroQueryClient.prototype.useGetTeams = function (body, total, onProgress) {
4
+ return useQuery({
5
+ queryKey: ['teams', this.companyId, body.amt ?? 50, body.query ?? "", total ?? "all"],
6
+ queryFn: async () => {
7
+ if (typeof total !== "number") {
8
+ return this.fetchTeams({ ...body, offset: 0 });
9
+ }
10
+ onProgress?.(0);
11
+ const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
12
+ let completed = 0;
13
+ const promises = Array.from({ length: pageCount }, (_, i) => this.fetchTeams({
14
+ ...body,
15
+ offset: i * (body.amt ?? 1),
16
+ }));
17
+ const trackedPromises = promises.map((promise) => promise.then((result) => {
18
+ completed++;
19
+ const fraction = completed / pageCount;
20
+ onProgress?.(fraction);
21
+ return result;
22
+ }));
23
+ const pages = await Promise.all(trackedPromises);
24
+ const teams = pages.flat();
25
+ return teams;
26
+ },
27
+ });
28
+ };
29
+ AvroQueryClient.prototype.useCreateTeam = function () {
30
+ const queryClient = this.getQueryClient();
31
+ return useMutation({
32
+ mutationFn: async ({ teamData }) => {
33
+ return this.post(`/company/${this.companyId}/team`, JSON.stringify(teamData), undefined, { "Content-Type": "application/json" });
34
+ },
35
+ onSettled: () => {
36
+ queryClient.invalidateQueries({ queryKey: ['company'] });
37
+ queryClient.invalidateQueries({ queryKey: ['teams'] });
38
+ },
39
+ });
40
+ };
41
+ AvroQueryClient.prototype.useUpdateTeam = function () {
42
+ const queryClient = this.getQueryClient();
43
+ return useMutation({
44
+ mutationFn: async ({ teamId, teamData }) => {
45
+ return this.put(`/team/${teamId}`, JSON.stringify(teamData), undefined, { "Content-Type": "application/json" });
46
+ },
47
+ onMutate: async ({ teamId, teamData }) => {
48
+ await queryClient.cancelQueries({ queryKey: ['teams'] });
49
+ await queryClient.cancelQueries({ queryKey: ['team', teamId] });
50
+ const previousTeams = queryClient.getQueryData(['teams']);
51
+ const previousTeam = queryClient.getQueryData(['team', teamId]);
52
+ queryClient.setQueryData(['team', teamId], (oldData) => {
53
+ if (!oldData)
54
+ return oldData;
55
+ return { ...oldData, ...teamData };
56
+ });
57
+ queryClient.setQueriesData({ queryKey: ['teams'] }, (oldData) => {
58
+ if (!oldData)
59
+ return oldData;
60
+ if (oldData.pages) {
61
+ const updatedPages = oldData.pages.map((page) => page.map((team) => team.id === teamId ? { ...team, ...teamData } : team));
62
+ return { ...oldData, pages: updatedPages };
63
+ }
64
+ if (Array.isArray(oldData)) {
65
+ return oldData.map((team) => team.id === teamId ? { ...team, ...teamData } : team);
66
+ }
67
+ return oldData;
68
+ });
69
+ return { previousTeams, previousTeam };
70
+ },
71
+ onError: (_err, variables, context) => {
72
+ const { teamId } = variables;
73
+ if (context?.previousTeams) {
74
+ queryClient.setQueryData(['teams'], context.previousTeams);
75
+ }
76
+ if (context?.previousTeam) {
77
+ queryClient.setQueryData(['team', teamId], context.previousTeam);
78
+ }
79
+ },
80
+ onSettled: (_data, _error, variables) => {
81
+ const { teamId } = variables;
82
+ queryClient.invalidateQueries({ queryKey: ['teams'] });
83
+ queryClient.invalidateQueries({ queryKey: ['team', teamId] });
84
+ },
85
+ });
86
+ };
87
+ AvroQueryClient.prototype.useDeleteTeam = function () {
88
+ const queryClient = this.getQueryClient();
89
+ return useMutation({
90
+ mutationFn: async ({ teamId, }) => {
91
+ return this.delete(`/team/${teamId}`);
92
+ },
93
+ onMutate: async ({ teamId }) => {
94
+ await queryClient.cancelQueries({ queryKey: ['teams'] });
95
+ await queryClient.cancelQueries({ queryKey: ['team', teamId] });
96
+ const previousRoutes = queryClient.getQueryData(['teams']);
97
+ const previousRoute = queryClient.getQueryData(['team', teamId]);
98
+ queryClient.setQueryData(['team', teamId], undefined);
99
+ queryClient.setQueriesData({ queryKey: ['teams'] }, (oldData) => {
100
+ if (!oldData)
101
+ return oldData;
102
+ if (oldData.pages) {
103
+ const updatedPages = oldData.pages.map((page) => page.filter((team) => team.id !== teamId));
104
+ return { ...oldData, pages: updatedPages };
105
+ }
106
+ if (Array.isArray(oldData)) {
107
+ return oldData.filter((team) => team.id !== teamId);
108
+ }
109
+ return oldData;
110
+ });
111
+ return { previousRoutes, previousRoute };
112
+ },
113
+ onError: (_err, variables, context) => {
114
+ const { teamId } = variables;
115
+ if (context?.previousRoutes) {
116
+ queryClient.setQueryData(['teams'], context.previousRoutes);
117
+ }
118
+ if (context?.previousRoute) {
119
+ queryClient.setQueryData(['team', teamId], context.previousRoute);
120
+ }
121
+ },
122
+ onSettled: (_data, _error, variables) => {
123
+ const { teamId } = variables;
124
+ queryClient.invalidateQueries({ queryKey: ['teams'] });
125
+ queryClient.invalidateQueries({ queryKey: ['team', teamId] });
126
+ },
127
+ });
128
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,118 @@
1
+ import { useMutation, useQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ import { AuthState } from "../../types/auth";
4
+ AvroQueryClient.prototype.useGetUser = function (userId) {
5
+ return useQuery({
6
+ queryKey: ['user', userId],
7
+ queryFn: () => this.get(`/user/${userId}`),
8
+ enabled: Boolean(userId),
9
+ });
10
+ };
11
+ AvroQueryClient.prototype.useSearchUsers = function (searchUsername) {
12
+ return useQuery({
13
+ queryKey: ['user', 'search', searchUsername],
14
+ queryFn: () => {
15
+ if (!searchUsername)
16
+ return Promise.resolve([]);
17
+ return this.get(`/user/search/${searchUsername}`);
18
+ },
19
+ enabled: Boolean(searchUsername),
20
+ });
21
+ };
22
+ AvroQueryClient.prototype.useGetSelf = function () {
23
+ return useQuery({
24
+ queryKey: ['user'],
25
+ queryFn: () => this.get(`/user`),
26
+ enabled: this.getAuthState() === AuthState.AUTHENTICATED,
27
+ });
28
+ };
29
+ AvroQueryClient.prototype.useCreateSelf = function () {
30
+ const queryClient = this.getQueryClient();
31
+ return useMutation({
32
+ mutationFn: async (data) => {
33
+ return this.post('/user', JSON.stringify(data), undefined, { "Content-Type": "application/json" });
34
+ },
35
+ onSettled: () => {
36
+ queryClient.invalidateQueries({ queryKey: ['user'] });
37
+ },
38
+ });
39
+ };
40
+ AvroQueryClient.prototype.useUpdateSelf = function () {
41
+ const queryClient = this.getQueryClient();
42
+ return useMutation({
43
+ mutationFn: async (data) => {
44
+ return this.put(`/user`, JSON.stringify(data), undefined, { "Content-Type": "application/json" });
45
+ },
46
+ // Optimistically update the user data in the cache
47
+ onMutate: async (data) => {
48
+ await queryClient.cancelQueries({ queryKey: ['user'] });
49
+ const previousUser = queryClient.getQueryData(['user']);
50
+ if (previousUser) {
51
+ queryClient.setQueryData(['user'], { ...previousUser, ...data });
52
+ }
53
+ return { previousUser };
54
+ },
55
+ onError: (err, _, context) => {
56
+ if (context?.previousUser) {
57
+ queryClient.setQueryData(['user'], context.previousUser);
58
+ }
59
+ },
60
+ onSettled: () => {
61
+ queryClient.invalidateQueries({ queryKey: ['user'] });
62
+ },
63
+ });
64
+ };
65
+ AvroQueryClient.prototype.useUpdateUserCompany = function () {
66
+ const queryClient = this.getQueryClient();
67
+ return useMutation({
68
+ mutationFn: async ({ user_id, data }) => {
69
+ return this.put(`/company/${this.companyId}/user/${user_id}`, JSON.stringify(data), undefined, { "Content-Type": "application/json" });
70
+ },
71
+ // Optimistically update the user data and company data in the cache
72
+ onMutate: async (data) => {
73
+ await queryClient.cancelQueries({ queryKey: ['user'] });
74
+ await queryClient.cancelQueries({ queryKey: ['company', this.companyId] });
75
+ const previousCompany = queryClient.getQueryData(['company', this.companyId]);
76
+ if (previousCompany) {
77
+ let oldCompanyUser = previousCompany.users?.find((u) => u.user.id === data.user_id);
78
+ if (oldCompanyUser) {
79
+ oldCompanyUser = { ...oldCompanyUser, ...data.data };
80
+ const newUsers = previousCompany.users?.map((u) => u.user.id === data.user_id ? oldCompanyUser : u) ?? [];
81
+ queryClient.setQueryData(['company', this.companyId], { ...previousCompany, users: newUsers });
82
+ }
83
+ }
84
+ return { previousCompany };
85
+ },
86
+ onError: (err, _, context) => {
87
+ if (context?.previousCompany) {
88
+ queryClient.setQueryData(['company', this.companyId], context.previousCompany);
89
+ }
90
+ },
91
+ onSettled: (data, error, variables) => {
92
+ queryClient.invalidateQueries({ queryKey: ['user'] });
93
+ queryClient.invalidateQueries({ queryKey: ['company', this.companyId] });
94
+ },
95
+ });
96
+ };
97
+ AvroQueryClient.prototype.useDeleteSelf = function () {
98
+ const queryClient = this.getQueryClient();
99
+ return useMutation({
100
+ mutationFn: async () => {
101
+ return this.delete(`/user`);
102
+ },
103
+ onMutate: async () => {
104
+ await queryClient.cancelQueries({ queryKey: ['user'] });
105
+ const previousUser = queryClient.getQueryData(['user']);
106
+ queryClient.removeQueries({ queryKey: ['user'] });
107
+ return { previousUser };
108
+ },
109
+ onError: (err, _, context) => {
110
+ if (context?.previousUser) {
111
+ queryClient.setQueryData(['user'], context.previousUser);
112
+ }
113
+ },
114
+ onSettled: () => {
115
+ queryClient.invalidateQueries({ queryKey: ['user'] });
116
+ },
117
+ });
118
+ };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,31 @@
1
1
  export { AvroQueryClientConfig, AvroQueryClient } from './client/QueryClient';
2
+ export { AvroQueryClientProvider, useAvroQueryClient } from './client/AvroQueryClientProvider';
2
3
  export { AuthManager } from './auth/AuthManager';
3
4
  export { MemoryStorage, LocalStorage } from './auth/storage';
5
+ import './client/core/xhr';
6
+ import './client/core/fetch';
7
+ import './client/core/utils';
8
+ import './client/hooks/root';
9
+ import './client/hooks/jobs';
10
+ import './client/hooks/routes';
11
+ import './client/hooks/events';
12
+ import './client/hooks/months';
13
+ import './client/hooks/bills';
14
+ import './client/hooks/companies';
15
+ import './client/hooks/users';
16
+ import './client/hooks/sessions';
17
+ import './client/hooks/chats';
18
+ import './client/hooks/messages';
19
+ import './client/hooks/plans';
20
+ import './client/hooks/analytics';
21
+ import './client/hooks/avro';
22
+ import './client/hooks/teams';
23
+ import './client/hooks/labels';
24
+ import './client/hooks/groups';
25
+ import './client/hooks/skills';
26
+ import './client/hooks/proposal';
4
27
  export * from './types/api';
5
- export * from './types/error';
28
+ export * from './types/auth';
29
+ export * from './types/cache';
6
30
  export * from './types/client';
31
+ export * from './types/error';
package/dist/index.js CHANGED
@@ -1,6 +1,31 @@
1
1
  export { AvroQueryClient } from './client/QueryClient';
2
+ export { AvroQueryClientProvider, useAvroQueryClient } from './client/AvroQueryClientProvider';
2
3
  export { AuthManager } from './auth/AuthManager';
3
4
  export { MemoryStorage, LocalStorage } from './auth/storage';
5
+ import './client/core/xhr';
6
+ import './client/core/fetch';
7
+ import './client/core/utils';
8
+ import './client/hooks/root';
9
+ import './client/hooks/jobs';
10
+ import './client/hooks/routes';
11
+ import './client/hooks/events';
12
+ import './client/hooks/months';
13
+ import './client/hooks/bills';
14
+ import './client/hooks/companies';
15
+ import './client/hooks/users';
16
+ import './client/hooks/sessions';
17
+ import './client/hooks/chats';
18
+ import './client/hooks/messages';
19
+ import './client/hooks/plans';
20
+ import './client/hooks/analytics';
21
+ import './client/hooks/avro';
22
+ import './client/hooks/teams';
23
+ import './client/hooks/labels';
24
+ import './client/hooks/groups';
25
+ import './client/hooks/skills';
26
+ import './client/hooks/proposal';
4
27
  export * from './types/api';
5
- export * from './types/error';
28
+ export * from './types/auth';
29
+ export * from './types/cache';
6
30
  export * from './types/client';
31
+ export * from './types/error';