@iotready/nextjs-components-library 1.0.0-preview1

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 (47) hide show
  1. package/components/accounts/AccountMenu.d.ts +9 -0
  2. package/components/accounts/AccountMenu.js +37 -0
  3. package/components/accounts/AccountProfile.d.ts +15 -0
  4. package/components/accounts/AccountProfile.js +153 -0
  5. package/components/accounts/index.d.ts +2 -0
  6. package/components/accounts/index.js +2 -0
  7. package/components/charts/TrendChart.d.ts +17 -0
  8. package/components/charts/TrendChart.js +454 -0
  9. package/components/charts/index.d.ts +1 -0
  10. package/components/charts/index.js +1 -0
  11. package/components/groups/GroupUpdate.d.ts +24 -0
  12. package/components/groups/GroupUpdate.js +134 -0
  13. package/components/groups/GroupsDevices.d.ts +37 -0
  14. package/components/groups/GroupsDevices.js +299 -0
  15. package/components/groups/Map.d.ts +14 -0
  16. package/components/groups/Map.js +17 -0
  17. package/components/groups/index.d.ts +3 -0
  18. package/components/groups/index.js +3 -0
  19. package/components/index.d.ts +5 -0
  20. package/components/index.js +5 -0
  21. package/components/settings/DynamicMenu.d.ts +17 -0
  22. package/components/settings/DynamicMenu.js +42 -0
  23. package/components/settings/index.d.ts +1 -0
  24. package/components/settings/index.js +1 -0
  25. package/components/users/UserUpdate.d.ts +11 -0
  26. package/components/users/UserUpdate.js +26 -0
  27. package/components/users/UsersDataGrid.d.ts +23 -0
  28. package/components/users/UsersDataGrid.js +76 -0
  29. package/components/users/index.d.ts +2 -0
  30. package/components/users/index.js +2 -0
  31. package/index.d.ts +3 -0
  32. package/index.js +4 -0
  33. package/package.json +45 -0
  34. package/server-actions/groups.d.ts +22 -0
  35. package/server-actions/groups.js +109 -0
  36. package/server-actions/index.d.ts +4 -0
  37. package/server-actions/index.js +5 -0
  38. package/server-actions/influx.d.ts +13 -0
  39. package/server-actions/influx.js +145 -0
  40. package/server-actions/logto.d.ts +39 -0
  41. package/server-actions/logto.js +194 -0
  42. package/server-actions/trackle.d.ts +35 -0
  43. package/server-actions/trackle.js +158 -0
  44. package/types/index.d.ts +1 -0
  45. package/types/index.js +1 -0
  46. package/types/user.d.ts +12 -0
  47. package/types/user.js +1 -0
@@ -0,0 +1,158 @@
1
+ "use server";
2
+ import wretch from "wretch";
3
+ import { cookies } from "next/headers";
4
+ import AbortAddon from "wretch/addons/abort";
5
+ const wretchApi = (trackleConfig, uid) => {
6
+ const accessToken = cookies().get("__trackle")?.value;
7
+ return wretch(trackleConfig.apiUrl)
8
+ .addon(AbortAddon())
9
+ .auth(`Bearer ${accessToken}`)
10
+ .catcher(401, async (err, originalRequest) => {
11
+ // Renew credentials
12
+ const token = await getToken(trackleConfig, uid);
13
+ return await originalRequest
14
+ .addon(AbortAddon())
15
+ .auth(`Bearer ${token}`)
16
+ .fetch()
17
+ .setTimeout(trackleConfig.apiTimeout)
18
+ // Redefine authorized to prevent infinite loops in case or multiple 401 errors
19
+ .unauthorized((err) => {
20
+ throw err;
21
+ })
22
+ .json();
23
+ });
24
+ };
25
+ const getToken = async (trackleConfig, uid) => {
26
+ const data = await wretch(trackleConfig.tokenUrl)
27
+ .headers({
28
+ Authorization: `Basic ${btoa(`${trackleConfig.clientId}:${trackleConfig.clientSecret}`)}`
29
+ })
30
+ .post({
31
+ grant_type: "client_credentials",
32
+ scope: uid ? `customer=${uid}` : undefined
33
+ })
34
+ .json();
35
+ const accessToken = data.access_token;
36
+ // set cookie
37
+ cookies().set({
38
+ name: trackleConfig.cookieName,
39
+ value: accessToken,
40
+ httpOnly: true,
41
+ sameSite: "lax",
42
+ secure: trackleConfig.cookieSecure
43
+ });
44
+ return accessToken;
45
+ };
46
+ export async function logOut(trackleConfig) {
47
+ // delete cookie
48
+ cookies().delete(trackleConfig.cookieName);
49
+ }
50
+ export async function createCustomer(trackleConfig, uid) {
51
+ return await wretch(trackleConfig.createCustomerUrl)
52
+ .headers({
53
+ Authorization: `Basic ${btoa(`${trackleConfig.clientId}:${trackleConfig.clientSecret}`)}`
54
+ })
55
+ .post({ uid })
56
+ .json();
57
+ }
58
+ export async function getDevices(trackleConfig, productId, uid, group, selected) {
59
+ let queryParams = "last_handshake_at!=null";
60
+ if (group !== "all") {
61
+ queryParams = `last_handshake_at!=null&state.groups=/${group}/`;
62
+ if (!uid) {
63
+ queryParams += "&quarantined=false";
64
+ }
65
+ }
66
+ else if (selected !== "all") {
67
+ queryParams = `last_handshake_at!=null&state.groups!=/${selected}/`;
68
+ if (!uid) {
69
+ queryParams += "&quarantined=false";
70
+ }
71
+ }
72
+ const api = uid ? wretchApi(trackleConfig, uid) : wretchApi(trackleConfig);
73
+ const response = await api
74
+ .get(uid
75
+ ? `/devices?${queryParams}`
76
+ : `/products/${productId}/devices?${queryParams}`)
77
+ .setTimeout(trackleConfig.apiTimeout)
78
+ .json();
79
+ return (uid ? { devices: response } : response);
80
+ }
81
+ export async function getDevice(trackleConfig, id, productId, uid) {
82
+ const api = uid ? wretchApi(trackleConfig, uid) : wretchApi(trackleConfig);
83
+ return await api
84
+ .get(uid ? `/devices/${id}` : `/products/${productId}/devices/${id}`)
85
+ .setTimeout(trackleConfig.apiTimeout)
86
+ .json();
87
+ }
88
+ export async function updateDevice(trackleConfig, id, body, productId, uid) {
89
+ const api = uid ? wretchApi(trackleConfig, uid) : wretchApi(trackleConfig);
90
+ return await api
91
+ .put(body, uid ? `/devices/${id}` : `/products/${productId}/devices/${id}`)
92
+ .setTimeout(trackleConfig.apiTimeout)
93
+ .json();
94
+ }
95
+ export async function get(trackleConfig, id, endpoint, params, productId, uid) {
96
+ const api = uid ? wretchApi(trackleConfig, uid) : wretchApi(trackleConfig);
97
+ return await api
98
+ .get(uid
99
+ ? `/devices/${id}/${endpoint}`
100
+ : `/products/${productId}/devices/${id}/${endpoint}` +
101
+ (params ? `?args=${params}` : ""))
102
+ .setTimeout(trackleConfig.apiTimeout)
103
+ .json();
104
+ }
105
+ export async function post(trackleConfig, id, endpoint, args, productId, uid) {
106
+ const api = uid ? wretchApi(trackleConfig, uid) : wretchApi(trackleConfig);
107
+ return await api
108
+ .post({ args }, uid
109
+ ? `/devices/${id}/${endpoint}`
110
+ : `/products/${productId}/devices/${id}/${endpoint}`)
111
+ .setTimeout(trackleConfig.apiTimeout)
112
+ .json();
113
+ }
114
+ export async function put(trackleConfig, id, endpoint, value, productId, uid) {
115
+ const api = uid ? wretchApi(trackleConfig, uid) : wretchApi(trackleConfig);
116
+ return await api
117
+ .put({ value }, uid
118
+ ? `/devices/${id}/${endpoint}`
119
+ : `/products/${productId}/devices/${id}/${endpoint}`)
120
+ .setTimeout(trackleConfig.apiTimeout)
121
+ .json();
122
+ }
123
+ export async function addDevicesToGroup(trackleConfig, productId, uid, group, devicesToPatch) {
124
+ const devicesPatched = [];
125
+ for (const device of devicesToPatch) {
126
+ const newGroups = device?.groups && device?.groups instanceof Array ? device?.groups : [];
127
+ if (!newGroups.includes(group)) {
128
+ newGroups.push(group);
129
+ await wretchApi(trackleConfig, uid)
130
+ .url(`/products/${productId}/devices/${device.id}/groups`)
131
+ .put({
132
+ value: newGroups
133
+ })
134
+ .setTimeout(trackleConfig.apiTimeout)
135
+ .res();
136
+ devicesPatched.push(device.id);
137
+ }
138
+ }
139
+ return devicesPatched;
140
+ }
141
+ export async function removeDevicesFromGroup(trackleConfig, productId, uid, group, devicesToPatch) {
142
+ const devicesPatched = [];
143
+ for (const device of devicesToPatch) {
144
+ let newGroups = device?.groups && device?.groups instanceof Array ? device?.groups : [];
145
+ if (newGroups.includes(group)) {
146
+ newGroups = newGroups.filter((newGroup) => newGroup !== group);
147
+ await wretchApi(trackleConfig, uid)
148
+ .url(`/products/${productId}/devices/${device.id}/groups`)
149
+ .put({
150
+ value: newGroups
151
+ })
152
+ .setTimeout(trackleConfig.apiTimeout)
153
+ .res();
154
+ devicesPatched.push(device.id);
155
+ }
156
+ }
157
+ return devicesPatched;
158
+ }
@@ -0,0 +1 @@
1
+ export * from "./user";
package/types/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./user";
@@ -0,0 +1,12 @@
1
+ export type UserType = {
2
+ id: string;
3
+ email: string;
4
+ name: string;
5
+ picture?: string;
6
+ role: string;
7
+ lastSignInAt?: number;
8
+ firstname?: string;
9
+ lastname?: string;
10
+ uid: string;
11
+ sign?: string;
12
+ };
package/types/user.js ADDED
@@ -0,0 +1 @@
1
+ export {};