shelflife-react-hooks 1.0.5 → 1.0.6
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/dist/index.cjs.js +133 -123
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.cts +14 -3
- package/dist/index.d.ts +14 -3
- package/dist/index.esm.js +133 -123
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/context/AuthContext.tsx +12 -3
- package/src/context/api/authApi.ts +21 -6
- package/src/context/api/requestState.ts +3 -4
- package/src/type/auth.ts +13 -3
package/dist/index.cjs.js
CHANGED
|
@@ -73,10 +73,9 @@ var runWithRequestState = async (handlers, request) => {
|
|
|
73
73
|
try {
|
|
74
74
|
return await request();
|
|
75
75
|
} catch (err) {
|
|
76
|
-
const error = err instanceof Error ? err : new Error("Unknown error");
|
|
77
76
|
handlers.setIsError(true);
|
|
78
|
-
handlers.setError(
|
|
79
|
-
throw
|
|
77
|
+
handlers.setError(err);
|
|
78
|
+
throw err;
|
|
80
79
|
} finally {
|
|
81
80
|
handlers.setIsLoading(false);
|
|
82
81
|
}
|
|
@@ -94,7 +93,9 @@ var loginRequest = async (config, dto) => runWithRequestState(config, async () =
|
|
|
94
93
|
});
|
|
95
94
|
if (!response.ok) {
|
|
96
95
|
const error = await readJson(response);
|
|
97
|
-
|
|
96
|
+
if (error?.error)
|
|
97
|
+
throw error;
|
|
98
|
+
throw new Error("Login failed");
|
|
98
99
|
}
|
|
99
100
|
const payload = await readJson(response);
|
|
100
101
|
if (!payload || !("token" in payload)) {
|
|
@@ -122,7 +123,9 @@ var signupRequest = async (config, dto) => runWithRequestState(config, async ()
|
|
|
122
123
|
});
|
|
123
124
|
if (!response.ok) {
|
|
124
125
|
const error = await readJson(response);
|
|
125
|
-
|
|
126
|
+
if (error?.email || error?.password || error?.passwordRepeat || error?.username)
|
|
127
|
+
throw error;
|
|
128
|
+
throw new Error("Signup failed");
|
|
126
129
|
}
|
|
127
130
|
const payload = await readJson(response);
|
|
128
131
|
if (!payload) {
|
|
@@ -143,7 +146,9 @@ var changePasswordRequest = async (config, dto) => runWithRequestState(config, a
|
|
|
143
146
|
});
|
|
144
147
|
if (!response.ok) {
|
|
145
148
|
const error = await readJson(response);
|
|
146
|
-
|
|
149
|
+
if (error?.oldPassword || error?.newPasswordRepeat)
|
|
150
|
+
throw error;
|
|
151
|
+
throw new Error("Change password failed");
|
|
147
152
|
}
|
|
148
153
|
});
|
|
149
154
|
var getMeRequest = async (config) => runWithRequestState(
|
|
@@ -181,6 +186,111 @@ var logoutRequest = async (config) => runWithRequestState(
|
|
|
181
186
|
}
|
|
182
187
|
);
|
|
183
188
|
|
|
189
|
+
// src/context/api/userApi.ts
|
|
190
|
+
var updateById = (items, updated) => {
|
|
191
|
+
const index = items.findIndex((item) => item.id === updated.id);
|
|
192
|
+
if (index === -1) {
|
|
193
|
+
return [updated, ...items];
|
|
194
|
+
}
|
|
195
|
+
const next = [...items];
|
|
196
|
+
next[index] = updated;
|
|
197
|
+
return next;
|
|
198
|
+
};
|
|
199
|
+
var fetchUsersRequest = async (config) => runWithRequestState(
|
|
200
|
+
config,
|
|
201
|
+
async () => {
|
|
202
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
203
|
+
const response = await fetch(`${normalizedBaseUrl}/api/users`, {
|
|
204
|
+
headers: buildAuthHeaders(config.token)
|
|
205
|
+
});
|
|
206
|
+
if (!response.ok) {
|
|
207
|
+
throw new Error("Failed to fetch users");
|
|
208
|
+
}
|
|
209
|
+
const payload = await readJson(response);
|
|
210
|
+
if (payload) {
|
|
211
|
+
config.setUsers(payload);
|
|
212
|
+
return payload;
|
|
213
|
+
}
|
|
214
|
+
return [];
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
var fetchUserRequest = async (config, id) => runWithRequestState(config, async () => {
|
|
218
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
219
|
+
const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
|
|
220
|
+
headers: buildAuthHeaders(config.token)
|
|
221
|
+
});
|
|
222
|
+
if (response.status === 404) {
|
|
223
|
+
config.setUser(null);
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
if (!response.ok) {
|
|
227
|
+
throw new Error("Failed to fetch user");
|
|
228
|
+
}
|
|
229
|
+
const payload = await readJson(response);
|
|
230
|
+
if (!payload) {
|
|
231
|
+
throw new Error("User response missing data");
|
|
232
|
+
}
|
|
233
|
+
config.setUser(payload);
|
|
234
|
+
config.setUsers((previous) => updateById(previous, payload));
|
|
235
|
+
return payload;
|
|
236
|
+
});
|
|
237
|
+
var updateUserRequest = async (config, id, dto) => runWithRequestState(config, async () => {
|
|
238
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
239
|
+
const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
|
|
240
|
+
method: "PATCH",
|
|
241
|
+
headers: {
|
|
242
|
+
...buildAuthHeaders(config.token),
|
|
243
|
+
"Content-Type": "application/json"
|
|
244
|
+
},
|
|
245
|
+
body: JSON.stringify(dto)
|
|
246
|
+
});
|
|
247
|
+
if (!response.ok) {
|
|
248
|
+
throw new Error("Failed to update user");
|
|
249
|
+
}
|
|
250
|
+
const payload = await readJson(response);
|
|
251
|
+
if (!payload) {
|
|
252
|
+
throw new Error("Update user response missing data");
|
|
253
|
+
}
|
|
254
|
+
config.setUsers((previous) => updateById(previous, payload));
|
|
255
|
+
config.setUser(payload);
|
|
256
|
+
return payload;
|
|
257
|
+
});
|
|
258
|
+
var deleteUserRequest = async (config, id) => runWithRequestState(config, async () => {
|
|
259
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
260
|
+
const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
|
|
261
|
+
method: "DELETE",
|
|
262
|
+
headers: buildAuthHeaders(config.token)
|
|
263
|
+
});
|
|
264
|
+
if (!response.ok) {
|
|
265
|
+
throw new Error("Failed to delete user");
|
|
266
|
+
}
|
|
267
|
+
config.setUsers((previous) => previous.filter((item) => item.id !== id));
|
|
268
|
+
config.setUser((current) => current?.id === id ? null : current);
|
|
269
|
+
});
|
|
270
|
+
var getUserPfpRequest = async (config, id) => runWithRequestState(config, async () => {
|
|
271
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
272
|
+
const response = await fetch(`${normalizedBaseUrl}/api/users/${id}/pfp`, {
|
|
273
|
+
headers: buildAuthHeaders(config.token)
|
|
274
|
+
});
|
|
275
|
+
if (!response.ok) {
|
|
276
|
+
throw new Error("Failed to fetch user pfp");
|
|
277
|
+
}
|
|
278
|
+
return response.blob();
|
|
279
|
+
});
|
|
280
|
+
var uploadUserPfpRequest = async (config, id, file) => runWithRequestState(config, async () => {
|
|
281
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
282
|
+
const formData = new FormData();
|
|
283
|
+
formData.append("pfp", file);
|
|
284
|
+
const response = await fetch(`${normalizedBaseUrl}/api/users/${id}/pfp`, {
|
|
285
|
+
method: "POST",
|
|
286
|
+
headers: buildAuthHeaders(config.token),
|
|
287
|
+
body: formData
|
|
288
|
+
});
|
|
289
|
+
if (!response.ok) {
|
|
290
|
+
throw new Error("Failed to upload user pfp");
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
|
|
184
294
|
// src/context/AuthContext.tsx
|
|
185
295
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
186
296
|
var AuthContext = (0, import_react.createContext)(void 0);
|
|
@@ -237,6 +347,11 @@ var AuthProvider = ({
|
|
|
237
347
|
}), [baseUrl, token]);
|
|
238
348
|
const login = (0, import_react.useCallback)((dto) => loginRequest(apiConfig, dto), [apiConfig]);
|
|
239
349
|
const signup = (0, import_react.useCallback)((dto) => signupRequest(apiConfig, dto), [apiConfig]);
|
|
350
|
+
const changeMe = (0, import_react.useCallback)(
|
|
351
|
+
(dto) => updateUserRequest({ ...apiConfig, setUsers: () => {
|
|
352
|
+
}, setUser }, user.id, dto),
|
|
353
|
+
[apiConfig, user]
|
|
354
|
+
);
|
|
240
355
|
const changePassword = (0, import_react.useCallback)(
|
|
241
356
|
(dto) => changePasswordRequest(apiConfig, dto),
|
|
242
357
|
[apiConfig]
|
|
@@ -251,11 +366,13 @@ var AuthProvider = ({
|
|
|
251
366
|
error,
|
|
252
367
|
login,
|
|
253
368
|
signup,
|
|
369
|
+
changeMe,
|
|
254
370
|
changePassword,
|
|
255
371
|
getMe,
|
|
256
372
|
logout,
|
|
257
373
|
getToken
|
|
258
374
|
}), [
|
|
375
|
+
changeMe,
|
|
259
376
|
changePassword,
|
|
260
377
|
error,
|
|
261
378
|
getMe,
|
|
@@ -366,7 +483,7 @@ var useInvites = () => {
|
|
|
366
483
|
var import_react3 = require("react");
|
|
367
484
|
|
|
368
485
|
// src/context/api/productApi.ts
|
|
369
|
-
var
|
|
486
|
+
var updateById2 = (items, updated) => {
|
|
370
487
|
const index = items.findIndex((item) => item.id === updated.id);
|
|
371
488
|
if (index === -1) {
|
|
372
489
|
return [updated, ...items];
|
|
@@ -418,7 +535,7 @@ var fetchProductRequest = async (config, id) => runWithRequestState(config, asyn
|
|
|
418
535
|
throw new Error("Product response missing data");
|
|
419
536
|
}
|
|
420
537
|
config.setProduct(payload);
|
|
421
|
-
config.setProducts((previous) =>
|
|
538
|
+
config.setProducts((previous) => updateById2(previous, payload));
|
|
422
539
|
return payload;
|
|
423
540
|
});
|
|
424
541
|
var createProductRequest = async (config, dto) => runWithRequestState(config, async () => {
|
|
@@ -459,7 +576,7 @@ var updateProductRequest = async (config, id, dto) => runWithRequestState(config
|
|
|
459
576
|
if (!payload) {
|
|
460
577
|
throw new Error("Update product response missing data");
|
|
461
578
|
}
|
|
462
|
-
config.setProducts((previous) =>
|
|
579
|
+
config.setProducts((previous) => updateById2(previous, payload));
|
|
463
580
|
config.setProduct(payload);
|
|
464
581
|
return payload;
|
|
465
582
|
});
|
|
@@ -578,7 +695,7 @@ var useProducts = () => {
|
|
|
578
695
|
var import_react4 = require("react");
|
|
579
696
|
|
|
580
697
|
// src/context/api/runningLowApi.ts
|
|
581
|
-
var
|
|
698
|
+
var updateById3 = (items, updated) => {
|
|
582
699
|
const index = items.findIndex((item) => item.id === updated.id);
|
|
583
700
|
if (index === -1) {
|
|
584
701
|
return [updated, ...items];
|
|
@@ -648,7 +765,7 @@ var editSettingRequest = async (config, storageId, id, dto) => runWithRequestSta
|
|
|
648
765
|
if (!payload) {
|
|
649
766
|
throw new Error("Edit running low response missing data");
|
|
650
767
|
}
|
|
651
|
-
config.setSettings((previous) =>
|
|
768
|
+
config.setSettings((previous) => updateById3(previous, payload));
|
|
652
769
|
return payload;
|
|
653
770
|
});
|
|
654
771
|
var deleteSettingRequest = async (config, storageId, id) => runWithRequestState(config, async () => {
|
|
@@ -732,7 +849,7 @@ var useRunningLow = () => {
|
|
|
732
849
|
var import_react5 = require("react");
|
|
733
850
|
|
|
734
851
|
// src/context/api/storageApi.ts
|
|
735
|
-
var
|
|
852
|
+
var updateById4 = (items, updated) => {
|
|
736
853
|
const index = items.findIndex((item) => item.id === updated.id);
|
|
737
854
|
if (index === -1) {
|
|
738
855
|
return [updated, ...items];
|
|
@@ -776,7 +893,7 @@ var fetchStorageRequest = async (config, id) => runWithRequestState(config, asyn
|
|
|
776
893
|
throw new Error("Storage response missing data");
|
|
777
894
|
}
|
|
778
895
|
config.setStorage(payload);
|
|
779
|
-
config.setStorages((previous) =>
|
|
896
|
+
config.setStorages((previous) => updateById4(previous, payload));
|
|
780
897
|
return payload;
|
|
781
898
|
});
|
|
782
899
|
var createStorageRequest = async (config, dto) => runWithRequestState(config, async () => {
|
|
@@ -817,7 +934,7 @@ var changeStorageNameRequest = async (config, id, dto) => runWithRequestState(co
|
|
|
817
934
|
if (!payload) {
|
|
818
935
|
throw new Error("Update storage response missing data");
|
|
819
936
|
}
|
|
820
|
-
config.setStorages((previous) =>
|
|
937
|
+
config.setStorages((previous) => updateById4(previous, payload));
|
|
821
938
|
config.setStorage(payload);
|
|
822
939
|
return payload;
|
|
823
940
|
});
|
|
@@ -901,7 +1018,7 @@ var useStorages = () => {
|
|
|
901
1018
|
var import_react6 = require("react");
|
|
902
1019
|
|
|
903
1020
|
// src/context/api/storageItemApi.ts
|
|
904
|
-
var
|
|
1021
|
+
var updateById5 = (items, updated) => {
|
|
905
1022
|
const index = items.findIndex((item) => item.id === updated.id);
|
|
906
1023
|
if (index === -1) {
|
|
907
1024
|
return [updated, ...items];
|
|
@@ -962,7 +1079,7 @@ var editItemRequest = async (config, storageId, itemId, dto) => runWithRequestSt
|
|
|
962
1079
|
if (!payload) {
|
|
963
1080
|
throw new Error("Edit item response missing data");
|
|
964
1081
|
}
|
|
965
|
-
config.setItems((previous) =>
|
|
1082
|
+
config.setItems((previous) => updateById5(previous, payload));
|
|
966
1083
|
return payload;
|
|
967
1084
|
});
|
|
968
1085
|
var deleteItemRequest = async (config, storageId, itemId) => runWithRequestState(config, async () => {
|
|
@@ -1212,113 +1329,6 @@ var useStorageMembers = () => {
|
|
|
1212
1329
|
|
|
1213
1330
|
// src/context/UserContext.tsx
|
|
1214
1331
|
var import_react8 = require("react");
|
|
1215
|
-
|
|
1216
|
-
// src/context/api/userApi.ts
|
|
1217
|
-
var updateById5 = (items, updated) => {
|
|
1218
|
-
const index = items.findIndex((item) => item.id === updated.id);
|
|
1219
|
-
if (index === -1) {
|
|
1220
|
-
return [updated, ...items];
|
|
1221
|
-
}
|
|
1222
|
-
const next = [...items];
|
|
1223
|
-
next[index] = updated;
|
|
1224
|
-
return next;
|
|
1225
|
-
};
|
|
1226
|
-
var fetchUsersRequest = async (config) => runWithRequestState(
|
|
1227
|
-
config,
|
|
1228
|
-
async () => {
|
|
1229
|
-
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1230
|
-
const response = await fetch(`${normalizedBaseUrl}/api/users`, {
|
|
1231
|
-
headers: buildAuthHeaders(config.token)
|
|
1232
|
-
});
|
|
1233
|
-
if (!response.ok) {
|
|
1234
|
-
throw new Error("Failed to fetch users");
|
|
1235
|
-
}
|
|
1236
|
-
const payload = await readJson(response);
|
|
1237
|
-
if (payload) {
|
|
1238
|
-
config.setUsers(payload);
|
|
1239
|
-
return payload;
|
|
1240
|
-
}
|
|
1241
|
-
return [];
|
|
1242
|
-
}
|
|
1243
|
-
);
|
|
1244
|
-
var fetchUserRequest = async (config, id) => runWithRequestState(config, async () => {
|
|
1245
|
-
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1246
|
-
const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
|
|
1247
|
-
headers: buildAuthHeaders(config.token)
|
|
1248
|
-
});
|
|
1249
|
-
if (response.status === 404) {
|
|
1250
|
-
config.setUser(null);
|
|
1251
|
-
return null;
|
|
1252
|
-
}
|
|
1253
|
-
if (!response.ok) {
|
|
1254
|
-
throw new Error("Failed to fetch user");
|
|
1255
|
-
}
|
|
1256
|
-
const payload = await readJson(response);
|
|
1257
|
-
if (!payload) {
|
|
1258
|
-
throw new Error("User response missing data");
|
|
1259
|
-
}
|
|
1260
|
-
config.setUser(payload);
|
|
1261
|
-
config.setUsers((previous) => updateById5(previous, payload));
|
|
1262
|
-
return payload;
|
|
1263
|
-
});
|
|
1264
|
-
var updateUserRequest = async (config, id, dto) => runWithRequestState(config, async () => {
|
|
1265
|
-
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1266
|
-
const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
|
|
1267
|
-
method: "PATCH",
|
|
1268
|
-
headers: {
|
|
1269
|
-
...buildAuthHeaders(config.token),
|
|
1270
|
-
"Content-Type": "application/json"
|
|
1271
|
-
},
|
|
1272
|
-
body: JSON.stringify(dto)
|
|
1273
|
-
});
|
|
1274
|
-
if (!response.ok) {
|
|
1275
|
-
throw new Error("Failed to update user");
|
|
1276
|
-
}
|
|
1277
|
-
const payload = await readJson(response);
|
|
1278
|
-
if (!payload) {
|
|
1279
|
-
throw new Error("Update user response missing data");
|
|
1280
|
-
}
|
|
1281
|
-
config.setUsers((previous) => updateById5(previous, payload));
|
|
1282
|
-
config.setUser(payload);
|
|
1283
|
-
return payload;
|
|
1284
|
-
});
|
|
1285
|
-
var deleteUserRequest = async (config, id) => runWithRequestState(config, async () => {
|
|
1286
|
-
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1287
|
-
const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
|
|
1288
|
-
method: "DELETE",
|
|
1289
|
-
headers: buildAuthHeaders(config.token)
|
|
1290
|
-
});
|
|
1291
|
-
if (!response.ok) {
|
|
1292
|
-
throw new Error("Failed to delete user");
|
|
1293
|
-
}
|
|
1294
|
-
config.setUsers((previous) => previous.filter((item) => item.id !== id));
|
|
1295
|
-
config.setUser((current) => current?.id === id ? null : current);
|
|
1296
|
-
});
|
|
1297
|
-
var getUserPfpRequest = async (config, id) => runWithRequestState(config, async () => {
|
|
1298
|
-
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1299
|
-
const response = await fetch(`${normalizedBaseUrl}/api/users/${id}/pfp`, {
|
|
1300
|
-
headers: buildAuthHeaders(config.token)
|
|
1301
|
-
});
|
|
1302
|
-
if (!response.ok) {
|
|
1303
|
-
throw new Error("Failed to fetch user pfp");
|
|
1304
|
-
}
|
|
1305
|
-
return response.blob();
|
|
1306
|
-
});
|
|
1307
|
-
var uploadUserPfpRequest = async (config, id, file) => runWithRequestState(config, async () => {
|
|
1308
|
-
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1309
|
-
const formData = new FormData();
|
|
1310
|
-
formData.append("pfp", file);
|
|
1311
|
-
const response = await fetch(`${normalizedBaseUrl}/api/users/${id}/pfp`, {
|
|
1312
|
-
method: "POST",
|
|
1313
|
-
headers: buildAuthHeaders(config.token),
|
|
1314
|
-
body: formData
|
|
1315
|
-
});
|
|
1316
|
-
if (!response.ok) {
|
|
1317
|
-
throw new Error("Failed to upload user pfp");
|
|
1318
|
-
}
|
|
1319
|
-
});
|
|
1320
|
-
|
|
1321
|
-
// src/context/UserContext.tsx
|
|
1322
1332
|
var import_jsx_runtime8 = require("react/jsx-runtime");
|
|
1323
1333
|
var UserContext = (0, import_react8.createContext)(void 0);
|
|
1324
1334
|
var UserProvider = ({ baseUrl, children }) => {
|