amplifyquery 1.0.8 β 1.0.10
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/service.js +28 -13
- package/package.json +1 -1
package/dist/service.js
CHANGED
|
@@ -11,9 +11,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.createAmplifyService = createAmplifyService;
|
|
13
13
|
const client_1 = require("./client");
|
|
14
|
+
const config_1 = require("./config");
|
|
14
15
|
const query_1 = require("./query");
|
|
15
16
|
const utils_1 = require("./utils");
|
|
16
|
-
const config_1 = require("./config");
|
|
17
17
|
const react_query_1 = require("@tanstack/react-query");
|
|
18
18
|
const auth_1 = require("aws-amplify/auth");
|
|
19
19
|
const expo_crypto_1 = require("expo-crypto");
|
|
@@ -487,7 +487,16 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
487
487
|
// Get parameters based on auth mode
|
|
488
488
|
const { authModeParams } = yield getOwnerByAuthMode(authMode);
|
|
489
489
|
// API call - apply auth mode
|
|
490
|
-
const { data:
|
|
490
|
+
const { data: apiResponse } = yield (0, client_1.getClient)().models[modelName].get({ id }, authModeParams);
|
|
491
|
+
// Handle case where API returns array instead of single item
|
|
492
|
+
let item = apiResponse;
|
|
493
|
+
if (Array.isArray(apiResponse)) {
|
|
494
|
+
console.warn(`π¬ ${modelName} get: API returned array instead of single item. Taking first item.`);
|
|
495
|
+
item =
|
|
496
|
+
apiResponse.find((i) => (i === null || i === void 0 ? void 0 : i.id) === id) ||
|
|
497
|
+
apiResponse[0] ||
|
|
498
|
+
null;
|
|
499
|
+
}
|
|
491
500
|
// Update cache
|
|
492
501
|
if (item) {
|
|
493
502
|
const itemId = item === null || item === void 0 ? void 0 : item.id;
|
|
@@ -1180,8 +1189,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1180
1189
|
console.error(`π¬ ${modelName} useHook create error:`, _error);
|
|
1181
1190
|
throw _error; // Re-throw error
|
|
1182
1191
|
}
|
|
1183
|
-
}), [service, refetch]
|
|
1184
|
-
);
|
|
1192
|
+
}), [service, refetch]);
|
|
1185
1193
|
const updateItem = (0, react_1.useCallback)((data) => __awaiter(this, void 0, void 0, function* () {
|
|
1186
1194
|
try {
|
|
1187
1195
|
const result = yield service.update(data);
|
|
@@ -1193,8 +1201,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1193
1201
|
console.error(`π¬ ${modelName} useHook update error:`, _error);
|
|
1194
1202
|
throw _error;
|
|
1195
1203
|
}
|
|
1196
|
-
}), [service, refetch]
|
|
1197
|
-
);
|
|
1204
|
+
}), [service, refetch]);
|
|
1198
1205
|
const deleteItem = (0, react_1.useCallback)((id) => __awaiter(this, void 0, void 0, function* () {
|
|
1199
1206
|
try {
|
|
1200
1207
|
const result = yield service.delete(id);
|
|
@@ -1206,8 +1213,7 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1206
1213
|
console.error(`π¬ ${modelName} useHook delete error:`, error);
|
|
1207
1214
|
throw error;
|
|
1208
1215
|
}
|
|
1209
|
-
}), [service, refetch]
|
|
1210
|
-
);
|
|
1216
|
+
}), [service, refetch]);
|
|
1211
1217
|
const refresh = (0, react_1.useCallback)((refreshOptions) => __awaiter(this, void 0, void 0, function* () {
|
|
1212
1218
|
console.log(`π¬ ${modelName} useHook refresh called`, queryKey);
|
|
1213
1219
|
const { data } = yield refetch({ throwOnError: true }); // Throw on error
|
|
@@ -1241,12 +1247,21 @@ function createAmplifyService(modelName, defaultAuthMode) {
|
|
|
1241
1247
|
const singleItemQueryKey = [modelName, id];
|
|
1242
1248
|
// First check data from cache
|
|
1243
1249
|
const rawCachedData = hookQueryClient.getQueryData(singleItemQueryKey);
|
|
1244
|
-
// π§ λ²κ·Έ μμ :
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
:
|
|
1248
|
-
|
|
1250
|
+
// π§ λ²κ·Έ μμ : λ°°μ΄μ΄ μΊμλμ΄ μλ κ²½μ° μ²λ¦¬
|
|
1251
|
+
let cachedData;
|
|
1252
|
+
if (Array.isArray(rawCachedData)) {
|
|
1253
|
+
console.warn(`π¬ ${modelName} useItemHook: Cache contains array instead of single item. Finding matching item.`);
|
|
1254
|
+
const matchingItem = rawCachedData.find((item) => (item === null || item === void 0 ? void 0 : item.id) === id);
|
|
1255
|
+
cachedData = matchingItem || undefined;
|
|
1256
|
+
// λ°°μ΄μ΄ μΊμλμ΄ μμΌλ©΄ μΊμλ₯Ό μ κ±°νμ¬ get λ©μλκ° λ€μ νΈμΆλλλ‘ ν¨
|
|
1257
|
+
hookQueryClient.removeQueries({ queryKey: singleItemQueryKey });
|
|
1258
|
+
}
|
|
1259
|
+
else if (rawCachedData && (rawCachedData === null || rawCachedData === void 0 ? void 0 : rawCachedData.id) === id) {
|
|
1260
|
+
cachedData = rawCachedData;
|
|
1261
|
+
}
|
|
1262
|
+
else if (rawCachedData) {
|
|
1249
1263
|
console.warn(`π¬ ${modelName} useItemHook: Cache ID mismatch! Requested: ${id}, Cached: ${rawCachedData === null || rawCachedData === void 0 ? void 0 : rawCachedData.id}. Ignoring cached data.`);
|
|
1264
|
+
cachedData = undefined;
|
|
1250
1265
|
}
|
|
1251
1266
|
// Single item query
|
|
1252
1267
|
const { data: item, isLoading, error, refetch, } = (0, react_query_1.useQuery)({
|