shelflife-react-hooks 1.0.5 → 1.0.7

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.esm.js CHANGED
@@ -35,10 +35,9 @@ var runWithRequestState = async (handlers, request) => {
35
35
  try {
36
36
  return await request();
37
37
  } catch (err) {
38
- const error = err instanceof Error ? err : new Error("Unknown error");
39
38
  handlers.setIsError(true);
40
- handlers.setError(error);
41
- throw error;
39
+ handlers.setError(err);
40
+ throw err;
42
41
  } finally {
43
42
  handlers.setIsLoading(false);
44
43
  }
@@ -56,7 +55,9 @@ var loginRequest = async (config, dto) => runWithRequestState(config, async () =
56
55
  });
57
56
  if (!response.ok) {
58
57
  const error = await readJson(response);
59
- throw new Error(error?.error ?? "Login failed");
58
+ if (error?.error)
59
+ throw error;
60
+ throw new Error("Login failed");
60
61
  }
61
62
  const payload = await readJson(response);
62
63
  if (!payload || !("token" in payload)) {
@@ -84,7 +85,9 @@ var signupRequest = async (config, dto) => runWithRequestState(config, async ()
84
85
  });
85
86
  if (!response.ok) {
86
87
  const error = await readJson(response);
87
- throw new Error(error?.error ?? "Signup failed");
88
+ if (error?.email || error?.password || error?.passwordRepeat || error?.username)
89
+ throw error;
90
+ throw new Error("Signup failed");
88
91
  }
89
92
  const payload = await readJson(response);
90
93
  if (!payload) {
@@ -105,7 +108,9 @@ var changePasswordRequest = async (config, dto) => runWithRequestState(config, a
105
108
  });
106
109
  if (!response.ok) {
107
110
  const error = await readJson(response);
108
- throw new Error(error?.error ?? "Change password failed");
111
+ if (error?.oldPassword || error?.newPasswordRepeat)
112
+ throw error;
113
+ throw new Error("Change password failed");
109
114
  }
110
115
  });
111
116
  var getMeRequest = async (config) => runWithRequestState(
@@ -143,6 +148,111 @@ var logoutRequest = async (config) => runWithRequestState(
143
148
  }
144
149
  );
145
150
 
151
+ // src/context/api/userApi.ts
152
+ var updateById = (items, updated) => {
153
+ const index = items.findIndex((item) => item.id === updated.id);
154
+ if (index === -1) {
155
+ return [updated, ...items];
156
+ }
157
+ const next = [...items];
158
+ next[index] = updated;
159
+ return next;
160
+ };
161
+ var fetchUsersRequest = async (config) => runWithRequestState(
162
+ config,
163
+ async () => {
164
+ const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
165
+ const response = await fetch(`${normalizedBaseUrl}/api/users`, {
166
+ headers: buildAuthHeaders(config.token)
167
+ });
168
+ if (!response.ok) {
169
+ throw new Error("Failed to fetch users");
170
+ }
171
+ const payload = await readJson(response);
172
+ if (payload) {
173
+ config.setUsers(payload);
174
+ return payload;
175
+ }
176
+ return [];
177
+ }
178
+ );
179
+ var fetchUserRequest = async (config, id) => runWithRequestState(config, async () => {
180
+ const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
181
+ const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
182
+ headers: buildAuthHeaders(config.token)
183
+ });
184
+ if (response.status === 404) {
185
+ config.setUser(null);
186
+ return null;
187
+ }
188
+ if (!response.ok) {
189
+ throw new Error("Failed to fetch user");
190
+ }
191
+ const payload = await readJson(response);
192
+ if (!payload) {
193
+ throw new Error("User response missing data");
194
+ }
195
+ config.setUser(payload);
196
+ config.setUsers((previous) => updateById(previous, payload));
197
+ return payload;
198
+ });
199
+ var updateUserRequest = async (config, id, dto) => runWithRequestState(config, async () => {
200
+ const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
201
+ const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
202
+ method: "PATCH",
203
+ headers: {
204
+ ...buildAuthHeaders(config.token),
205
+ "Content-Type": "application/json"
206
+ },
207
+ body: JSON.stringify(dto)
208
+ });
209
+ if (!response.ok) {
210
+ throw new Error("Failed to update user");
211
+ }
212
+ const payload = await readJson(response);
213
+ if (!payload) {
214
+ throw new Error("Update user response missing data");
215
+ }
216
+ config.setUsers((previous) => updateById(previous, payload));
217
+ config.setUser(payload);
218
+ return payload;
219
+ });
220
+ var deleteUserRequest = async (config, id) => runWithRequestState(config, async () => {
221
+ const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
222
+ const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
223
+ method: "DELETE",
224
+ headers: buildAuthHeaders(config.token)
225
+ });
226
+ if (!response.ok) {
227
+ throw new Error("Failed to delete user");
228
+ }
229
+ config.setUsers((previous) => previous.filter((item) => item.id !== id));
230
+ config.setUser((current) => current?.id === id ? null : current);
231
+ });
232
+ var getUserPfpRequest = async (config, id) => runWithRequestState(config, async () => {
233
+ const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
234
+ const response = await fetch(`${normalizedBaseUrl}/api/users/${id}/pfp`, {
235
+ headers: buildAuthHeaders(config.token)
236
+ });
237
+ if (!response.ok) {
238
+ throw new Error("Failed to fetch user pfp");
239
+ }
240
+ return response.blob();
241
+ });
242
+ var uploadUserPfpRequest = async (config, id, file) => runWithRequestState(config, async () => {
243
+ const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
244
+ const formData = new FormData();
245
+ formData.append("pfp", file);
246
+ const response = await fetch(`${normalizedBaseUrl}/api/users/${id}/pfp`, {
247
+ method: "POST",
248
+ headers: buildAuthHeaders(config.token),
249
+ body: formData
250
+ });
251
+ if (!response.ok) {
252
+ throw new Error("Failed to upload user pfp");
253
+ }
254
+ });
255
+
146
256
  // src/context/AuthContext.tsx
147
257
  import { jsx } from "react/jsx-runtime";
148
258
  var AuthContext = createContext(void 0);
@@ -199,6 +309,11 @@ var AuthProvider = ({
199
309
  }), [baseUrl, token]);
200
310
  const login = useCallback((dto) => loginRequest(apiConfig, dto), [apiConfig]);
201
311
  const signup = useCallback((dto) => signupRequest(apiConfig, dto), [apiConfig]);
312
+ const changeMe = useCallback(
313
+ (dto) => updateUserRequest({ ...apiConfig, setUsers: () => {
314
+ }, setUser }, user.id, dto),
315
+ [apiConfig, user]
316
+ );
202
317
  const changePassword = useCallback(
203
318
  (dto) => changePasswordRequest(apiConfig, dto),
204
319
  [apiConfig]
@@ -213,11 +328,13 @@ var AuthProvider = ({
213
328
  error,
214
329
  login,
215
330
  signup,
331
+ changeMe,
216
332
  changePassword,
217
333
  getMe,
218
334
  logout,
219
335
  getToken
220
336
  }), [
337
+ changeMe,
221
338
  changePassword,
222
339
  error,
223
340
  getMe,
@@ -340,7 +457,7 @@ import {
340
457
  } from "react";
341
458
 
342
459
  // src/context/api/productApi.ts
343
- var updateById = (items, updated) => {
460
+ var updateById2 = (items, updated) => {
344
461
  const index = items.findIndex((item) => item.id === updated.id);
345
462
  if (index === -1) {
346
463
  return [updated, ...items];
@@ -392,7 +509,7 @@ var fetchProductRequest = async (config, id) => runWithRequestState(config, asyn
392
509
  throw new Error("Product response missing data");
393
510
  }
394
511
  config.setProduct(payload);
395
- config.setProducts((previous) => updateById(previous, payload));
512
+ config.setProducts((previous) => updateById2(previous, payload));
396
513
  return payload;
397
514
  });
398
515
  var createProductRequest = async (config, dto) => runWithRequestState(config, async () => {
@@ -433,7 +550,7 @@ var updateProductRequest = async (config, id, dto) => runWithRequestState(config
433
550
  if (!payload) {
434
551
  throw new Error("Update product response missing data");
435
552
  }
436
- config.setProducts((previous) => updateById(previous, payload));
553
+ config.setProducts((previous) => updateById2(previous, payload));
437
554
  config.setProduct(payload);
438
555
  return payload;
439
556
  });
@@ -558,7 +675,7 @@ import {
558
675
  } from "react";
559
676
 
560
677
  // src/context/api/runningLowApi.ts
561
- var updateById2 = (items, updated) => {
678
+ var updateById3 = (items, updated) => {
562
679
  const index = items.findIndex((item) => item.id === updated.id);
563
680
  if (index === -1) {
564
681
  return [updated, ...items];
@@ -628,7 +745,7 @@ var editSettingRequest = async (config, storageId, id, dto) => runWithRequestSta
628
745
  if (!payload) {
629
746
  throw new Error("Edit running low response missing data");
630
747
  }
631
- config.setSettings((previous) => updateById2(previous, payload));
748
+ config.setSettings((previous) => updateById3(previous, payload));
632
749
  return payload;
633
750
  });
634
751
  var deleteSettingRequest = async (config, storageId, id) => runWithRequestState(config, async () => {
@@ -718,7 +835,7 @@ import {
718
835
  } from "react";
719
836
 
720
837
  // src/context/api/storageApi.ts
721
- var updateById3 = (items, updated) => {
838
+ var updateById4 = (items, updated) => {
722
839
  const index = items.findIndex((item) => item.id === updated.id);
723
840
  if (index === -1) {
724
841
  return [updated, ...items];
@@ -762,7 +879,7 @@ var fetchStorageRequest = async (config, id) => runWithRequestState(config, asyn
762
879
  throw new Error("Storage response missing data");
763
880
  }
764
881
  config.setStorage(payload);
765
- config.setStorages((previous) => updateById3(previous, payload));
882
+ config.setStorages((previous) => updateById4(previous, payload));
766
883
  return payload;
767
884
  });
768
885
  var createStorageRequest = async (config, dto) => runWithRequestState(config, async () => {
@@ -803,7 +920,7 @@ var changeStorageNameRequest = async (config, id, dto) => runWithRequestState(co
803
920
  if (!payload) {
804
921
  throw new Error("Update storage response missing data");
805
922
  }
806
- config.setStorages((previous) => updateById3(previous, payload));
923
+ config.setStorages((previous) => updateById4(previous, payload));
807
924
  config.setStorage(payload);
808
925
  return payload;
809
926
  });
@@ -893,7 +1010,7 @@ import {
893
1010
  } from "react";
894
1011
 
895
1012
  // src/context/api/storageItemApi.ts
896
- var updateById4 = (items, updated) => {
1013
+ var updateById5 = (items, updated) => {
897
1014
  const index = items.findIndex((item) => item.id === updated.id);
898
1015
  if (index === -1) {
899
1016
  return [updated, ...items];
@@ -954,7 +1071,7 @@ var editItemRequest = async (config, storageId, itemId, dto) => runWithRequestSt
954
1071
  if (!payload) {
955
1072
  throw new Error("Edit item response missing data");
956
1073
  }
957
- config.setItems((previous) => updateById4(previous, payload));
1074
+ config.setItems((previous) => updateById5(previous, payload));
958
1075
  return payload;
959
1076
  });
960
1077
  var deleteItemRequest = async (config, storageId, itemId) => runWithRequestState(config, async () => {
@@ -1216,113 +1333,6 @@ import {
1216
1333
  useMemo as useMemo8,
1217
1334
  useState as useState8
1218
1335
  } from "react";
1219
-
1220
- // src/context/api/userApi.ts
1221
- var updateById5 = (items, updated) => {
1222
- const index = items.findIndex((item) => item.id === updated.id);
1223
- if (index === -1) {
1224
- return [updated, ...items];
1225
- }
1226
- const next = [...items];
1227
- next[index] = updated;
1228
- return next;
1229
- };
1230
- var fetchUsersRequest = async (config) => runWithRequestState(
1231
- config,
1232
- async () => {
1233
- const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
1234
- const response = await fetch(`${normalizedBaseUrl}/api/users`, {
1235
- headers: buildAuthHeaders(config.token)
1236
- });
1237
- if (!response.ok) {
1238
- throw new Error("Failed to fetch users");
1239
- }
1240
- const payload = await readJson(response);
1241
- if (payload) {
1242
- config.setUsers(payload);
1243
- return payload;
1244
- }
1245
- return [];
1246
- }
1247
- );
1248
- var fetchUserRequest = async (config, id) => runWithRequestState(config, async () => {
1249
- const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
1250
- const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
1251
- headers: buildAuthHeaders(config.token)
1252
- });
1253
- if (response.status === 404) {
1254
- config.setUser(null);
1255
- return null;
1256
- }
1257
- if (!response.ok) {
1258
- throw new Error("Failed to fetch user");
1259
- }
1260
- const payload = await readJson(response);
1261
- if (!payload) {
1262
- throw new Error("User response missing data");
1263
- }
1264
- config.setUser(payload);
1265
- config.setUsers((previous) => updateById5(previous, payload));
1266
- return payload;
1267
- });
1268
- var updateUserRequest = async (config, id, dto) => runWithRequestState(config, async () => {
1269
- const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
1270
- const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
1271
- method: "PATCH",
1272
- headers: {
1273
- ...buildAuthHeaders(config.token),
1274
- "Content-Type": "application/json"
1275
- },
1276
- body: JSON.stringify(dto)
1277
- });
1278
- if (!response.ok) {
1279
- throw new Error("Failed to update user");
1280
- }
1281
- const payload = await readJson(response);
1282
- if (!payload) {
1283
- throw new Error("Update user response missing data");
1284
- }
1285
- config.setUsers((previous) => updateById5(previous, payload));
1286
- config.setUser(payload);
1287
- return payload;
1288
- });
1289
- var deleteUserRequest = async (config, id) => runWithRequestState(config, async () => {
1290
- const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
1291
- const response = await fetch(`${normalizedBaseUrl}/api/users/${id}`, {
1292
- method: "DELETE",
1293
- headers: buildAuthHeaders(config.token)
1294
- });
1295
- if (!response.ok) {
1296
- throw new Error("Failed to delete user");
1297
- }
1298
- config.setUsers((previous) => previous.filter((item) => item.id !== id));
1299
- config.setUser((current) => current?.id === id ? null : current);
1300
- });
1301
- var getUserPfpRequest = async (config, id) => runWithRequestState(config, async () => {
1302
- const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
1303
- const response = await fetch(`${normalizedBaseUrl}/api/users/${id}/pfp`, {
1304
- headers: buildAuthHeaders(config.token)
1305
- });
1306
- if (!response.ok) {
1307
- throw new Error("Failed to fetch user pfp");
1308
- }
1309
- return response.blob();
1310
- });
1311
- var uploadUserPfpRequest = async (config, id, file) => runWithRequestState(config, async () => {
1312
- const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
1313
- const formData = new FormData();
1314
- formData.append("pfp", file);
1315
- const response = await fetch(`${normalizedBaseUrl}/api/users/${id}/pfp`, {
1316
- method: "POST",
1317
- headers: buildAuthHeaders(config.token),
1318
- body: formData
1319
- });
1320
- if (!response.ok) {
1321
- throw new Error("Failed to upload user pfp");
1322
- }
1323
- });
1324
-
1325
- // src/context/UserContext.tsx
1326
1336
  import { jsx as jsx8 } from "react/jsx-runtime";
1327
1337
  var UserContext = createContext8(void 0);
1328
1338
  var UserProvider = ({ baseUrl, children }) => {