@zenstackhq/swr 1.4.1 → 1.6.0
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/generator.js +2 -1
- package/generator.js.map +1 -1
- package/package.json +20 -13
- package/runtime/index.d.mts +156 -0
- package/runtime/index.d.ts +42 -22
- package/runtime/index.js +330 -312
- package/runtime/index.js.map +1 -1
- package/runtime/index.mjs +335 -0
- package/runtime/index.mjs.map +1 -0
- package/runtime/prisma-types.d.ts +0 -17
- package/runtime/prisma-types.js +0 -5
- package/runtime/prisma-types.js.map +0 -1
package/runtime/index.js
CHANGED
|
@@ -1,352 +1,370 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
7
21
|
}
|
|
8
|
-
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
22
|
+
return a;
|
|
24
23
|
};
|
|
25
|
-
var
|
|
26
|
-
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var __objRest = (source, exclude) => {
|
|
26
|
+
var target = {};
|
|
27
|
+
for (var prop in source)
|
|
28
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
+
target[prop] = source[prop];
|
|
30
|
+
if (source != null && __getOwnPropSymbols)
|
|
31
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
+
target[prop] = source[prop];
|
|
34
|
+
}
|
|
35
|
+
return target;
|
|
27
36
|
};
|
|
28
|
-
var
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
-
});
|
|
37
|
+
var __export = (target, all) => {
|
|
38
|
+
for (var name in all)
|
|
39
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
36
40
|
};
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
-
for (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
t[p[i]] = s[p[i]];
|
|
45
|
-
}
|
|
46
|
-
return t;
|
|
41
|
+
var __copyProps = (to, from, except, desc) => {
|
|
42
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
43
|
+
for (let key of __getOwnPropNames(from))
|
|
44
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
45
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
46
|
+
}
|
|
47
|
+
return to;
|
|
47
48
|
};
|
|
48
|
-
var
|
|
49
|
-
|
|
49
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
50
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
51
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
52
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
53
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
54
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
55
|
+
mod
|
|
56
|
+
));
|
|
57
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
58
|
+
var __async = (__this, __arguments, generator) => {
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
var fulfilled = (value) => {
|
|
61
|
+
try {
|
|
62
|
+
step(generator.next(value));
|
|
63
|
+
} catch (e) {
|
|
64
|
+
reject(e);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var rejected = (value) => {
|
|
68
|
+
try {
|
|
69
|
+
step(generator.throw(value));
|
|
70
|
+
} catch (e) {
|
|
71
|
+
reject(e);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
75
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
76
|
+
});
|
|
50
77
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
* Context for configuring react hooks.
|
|
66
|
-
*/
|
|
67
|
-
exports.RequestHandlerContext = (0, react_1.createContext)({
|
|
68
|
-
endpoint: DEFAULT_QUERY_ENDPOINT,
|
|
69
|
-
fetch: undefined,
|
|
78
|
+
|
|
79
|
+
// src/runtime/index.ts
|
|
80
|
+
var runtime_exports = {};
|
|
81
|
+
__export(runtime_exports, {
|
|
82
|
+
Provider: () => Provider,
|
|
83
|
+
RequestHandlerContext: () => RequestHandlerContext,
|
|
84
|
+
fetcher: () => fetcher,
|
|
85
|
+
getQueryKey: () => getQueryKey,
|
|
86
|
+
mutationRequest: () => mutationRequest,
|
|
87
|
+
useHooksContext: () => useHooksContext,
|
|
88
|
+
useInfiniteModelQuery: () => useInfiniteModelQuery,
|
|
89
|
+
useInvalidation: () => useInvalidation,
|
|
90
|
+
useModelMutation: () => useModelMutation,
|
|
91
|
+
useModelQuery: () => useModelQuery
|
|
70
92
|
});
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
93
|
+
module.exports = __toCommonJS(runtime_exports);
|
|
94
|
+
var import_browser = require("@zenstackhq/runtime/browser");
|
|
95
|
+
var import_cross = require("@zenstackhq/runtime/cross");
|
|
96
|
+
var crossFetch = __toESM(require("cross-fetch"));
|
|
97
|
+
var import_lower_case_first = require("lower-case-first");
|
|
98
|
+
var import_react = require("react");
|
|
99
|
+
var import_swr = __toESM(require("swr"));
|
|
100
|
+
var import_infinite = __toESM(require("swr/infinite"));
|
|
101
|
+
var import_mutation = __toESM(require("swr/mutation"));
|
|
102
|
+
var DEFAULT_QUERY_ENDPOINT = "/api/model";
|
|
103
|
+
var RequestHandlerContext = (0, import_react.createContext)({
|
|
104
|
+
endpoint: DEFAULT_QUERY_ENDPOINT,
|
|
105
|
+
fetch: void 0
|
|
106
|
+
});
|
|
107
|
+
var Provider = RequestHandlerContext.Provider;
|
|
78
108
|
function useHooksContext() {
|
|
79
|
-
|
|
80
|
-
|
|
109
|
+
const _a = (0, import_react.useContext)(RequestHandlerContext), { endpoint } = _a, rest = __objRest(_a, ["endpoint"]);
|
|
110
|
+
return __spreadValues({ endpoint: endpoint != null ? endpoint : DEFAULT_QUERY_ENDPOINT }, rest);
|
|
81
111
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
args,
|
|
94
|
-
infinite: infinite === true,
|
|
95
|
-
optimisticUpdate: optimisticUpdate !== false,
|
|
96
|
-
});
|
|
112
|
+
var QUERY_KEY_PREFIX = "zenstack:query";
|
|
113
|
+
var MUTATION_KEY_PREFIX = "zenstack:mutation";
|
|
114
|
+
function getQueryKey(model, operation, args, infinite, optimisticUpdate2) {
|
|
115
|
+
return JSON.stringify({
|
|
116
|
+
prefix: QUERY_KEY_PREFIX,
|
|
117
|
+
model,
|
|
118
|
+
operation,
|
|
119
|
+
args,
|
|
120
|
+
infinite: infinite === true,
|
|
121
|
+
optimisticUpdate: optimisticUpdate2 !== false
|
|
122
|
+
});
|
|
97
123
|
}
|
|
98
|
-
exports.getQueryKey = getQueryKey;
|
|
99
124
|
function getMutationKey(model, operation) {
|
|
100
|
-
|
|
101
|
-
// https://github.com/vercel/swr/discussions/2461#discussioncomment-5281784
|
|
102
|
-
return JSON.stringify({ prefix: MUTATION_KEY_PREFIX, model, operation, r: Date.now() });
|
|
125
|
+
return JSON.stringify({ prefix: MUTATION_KEY_PREFIX, model, operation, r: Date.now() });
|
|
103
126
|
}
|
|
104
127
|
function parseQueryKey(key) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return undefined;
|
|
112
|
-
}
|
|
128
|
+
let keyValue = key;
|
|
129
|
+
if (typeof key === "string") {
|
|
130
|
+
try {
|
|
131
|
+
keyValue = JSON.parse(key);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
return void 0;
|
|
113
134
|
}
|
|
114
|
-
|
|
135
|
+
}
|
|
136
|
+
return (keyValue == null ? void 0 : keyValue.prefix) === QUERY_KEY_PREFIX ? keyValue : void 0;
|
|
115
137
|
}
|
|
116
|
-
/**
|
|
117
|
-
* Makes a model query with SWR.
|
|
118
|
-
*
|
|
119
|
-
* @param model Model name
|
|
120
|
-
* @param operation Prisma operation (e.g, `findMany`)
|
|
121
|
-
* @param args The request args object, which will be superjson-stringified and appended as "?q=" parameter
|
|
122
|
-
* @param options Query options
|
|
123
|
-
* @returns SWR response
|
|
124
|
-
*/
|
|
125
138
|
function useModelQuery(model, operation, args, options) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
139
|
+
var _a;
|
|
140
|
+
const { endpoint, fetch: fetch2 } = useHooksContext();
|
|
141
|
+
const key = (options == null ? void 0 : options.disabled) ? null : getQueryKey(model, operation, args, false, (options == null ? void 0 : options.optimisticUpdate) !== false);
|
|
142
|
+
const url = makeUrl(`${endpoint}/${(0, import_lower_case_first.lowerCaseFirst)(model)}/${operation}`, args);
|
|
143
|
+
return (0, import_swr.default)(key, () => fetcher(url, void 0, fetch2, false), __spreadProps(__spreadValues({}, options), {
|
|
144
|
+
fallbackData: (_a = options == null ? void 0 : options.initialData) != null ? _a : options == null ? void 0 : options.fallbackData
|
|
145
|
+
}));
|
|
133
146
|
}
|
|
134
|
-
exports.useModelQuery = useModelQuery;
|
|
135
|
-
/**
|
|
136
|
-
* Makes an infinite GET request with SWR.
|
|
137
|
-
*
|
|
138
|
-
* @param model Model name
|
|
139
|
-
* @param operation Prisma operation (e.g, `findMany`)
|
|
140
|
-
* @param getNextArgs Function for computing the query args for a page
|
|
141
|
-
* @param options Query options
|
|
142
|
-
* @returns SWR infinite query response
|
|
143
|
-
*/
|
|
144
147
|
function useInfiniteModelQuery(model, operation, getNextArgs, options) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
148
|
+
var _a;
|
|
149
|
+
const { endpoint, fetch: fetch2 } = useHooksContext();
|
|
150
|
+
const getKey = (pageIndex, previousPageData) => {
|
|
151
|
+
if (options == null ? void 0 : options.disabled) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
const nextArgs = getNextArgs(pageIndex, previousPageData);
|
|
155
|
+
return nextArgs !== null ? getQueryKey(model, operation, nextArgs, true, false) : null;
|
|
156
|
+
};
|
|
157
|
+
return (0, import_infinite.default)(
|
|
158
|
+
getKey,
|
|
159
|
+
(key) => {
|
|
160
|
+
const parsedKey = parseQueryKey(key);
|
|
161
|
+
if (parsedKey) {
|
|
162
|
+
const { model: model2, operation: operation2, args } = parsedKey;
|
|
163
|
+
const url = makeUrl(`${endpoint}/${(0, import_lower_case_first.lowerCaseFirst)(model2)}/${operation2}`, args);
|
|
164
|
+
return fetcher(url, void 0, fetch2, false);
|
|
165
|
+
} else {
|
|
166
|
+
throw new Error("Invalid query key: " + key);
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
__spreadProps(__spreadValues({}, options), {
|
|
170
|
+
fallbackData: (_a = options == null ? void 0 : options.initialData) != null ? _a : options == null ? void 0 : options.fallbackData
|
|
171
|
+
})
|
|
172
|
+
);
|
|
167
173
|
}
|
|
168
|
-
exports.useInfiniteModelQuery = useInfiniteModelQuery;
|
|
169
174
|
function useModelMutation(model, method, operation, modelMeta, options, checkReadBack) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
175
|
+
const { endpoint, fetch: fetch2, logging } = useHooksContext();
|
|
176
|
+
const invalidate = (options == null ? void 0 : options.revalidate) !== false ? useInvalidation(model, modelMeta) : void 0;
|
|
177
|
+
const { cache, mutate } = (0, import_swr.useSWRConfig)();
|
|
178
|
+
return (0, import_mutation.default)(
|
|
179
|
+
getMutationKey(model, operation),
|
|
180
|
+
(_key, { arg }) => {
|
|
181
|
+
if (options == null ? void 0 : options.optimisticUpdate) {
|
|
182
|
+
optimisticUpdate(model, operation, arg, modelMeta, cache, mutate, logging);
|
|
183
|
+
}
|
|
184
|
+
const url = `${endpoint}/${(0, import_lower_case_first.lowerCaseFirst)(model)}/${operation}`;
|
|
185
|
+
return mutationRequest(method, url, arg, invalidate, fetch2, checkReadBack);
|
|
186
|
+
},
|
|
187
|
+
options
|
|
188
|
+
);
|
|
180
189
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
return r;
|
|
203
|
-
});
|
|
190
|
+
function mutationRequest(method, url, data, invalidate, fetch2, checkReadBack) {
|
|
191
|
+
return __async(this, null, function* () {
|
|
192
|
+
const reqUrl = method === "DELETE" ? makeUrl(url, data) : url;
|
|
193
|
+
const r = yield fetcher(
|
|
194
|
+
reqUrl,
|
|
195
|
+
{
|
|
196
|
+
method,
|
|
197
|
+
headers: {
|
|
198
|
+
"content-type": "application/json"
|
|
199
|
+
},
|
|
200
|
+
body: data ? marshal(data) : void 0
|
|
201
|
+
},
|
|
202
|
+
fetch2,
|
|
203
|
+
checkReadBack
|
|
204
|
+
);
|
|
205
|
+
if (invalidate) {
|
|
206
|
+
yield invalidate(getOperationFromUrl(url), data);
|
|
207
|
+
}
|
|
208
|
+
return r;
|
|
209
|
+
});
|
|
204
210
|
}
|
|
205
|
-
exports.mutationRequest = mutationRequest;
|
|
206
211
|
function useInvalidation(model, modelMeta) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return modelsRead.some((m) => mutatedModels.includes(m));
|
|
222
|
-
});
|
|
223
|
-
if (logging) {
|
|
224
|
-
keys.forEach((key) => {
|
|
225
|
-
console.log(`Invalidating query ${key} due to mutation "${model}.${operation}"`);
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
const mutations = keys.map((key) => {
|
|
229
|
-
const parsedKey = parseQueryKey(key);
|
|
230
|
-
// FIX: special handling for infinite query keys, but still not working
|
|
231
|
-
// https://github.com/vercel/swr/discussions/2843
|
|
232
|
-
return mutate((parsedKey === null || parsedKey === void 0 ? void 0 : parsedKey.infinite) ? (0, infinite_1.unstable_serialize)(() => key) : key);
|
|
233
|
-
});
|
|
234
|
-
return Promise.all(mutations);
|
|
212
|
+
const { logging } = useHooksContext();
|
|
213
|
+
const { cache, mutate } = (0, import_swr.useSWRConfig)();
|
|
214
|
+
return (operation, args) => __async(this, null, function* () {
|
|
215
|
+
if (!(cache instanceof Map)) {
|
|
216
|
+
throw new Error("mutate requires the cache provider to be a Map instance");
|
|
217
|
+
}
|
|
218
|
+
const mutatedModels = yield (0, import_cross.getMutatedModels)(model, operation, args, modelMeta);
|
|
219
|
+
const keys = Array.from(cache.keys()).filter((key) => {
|
|
220
|
+
const parsedKey = parseQueryKey(key);
|
|
221
|
+
if (!parsedKey) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
const modelsRead = (0, import_cross.getReadModels)(parsedKey.model, modelMeta, parsedKey.args);
|
|
225
|
+
return modelsRead.some((m) => mutatedModels.includes(m));
|
|
235
226
|
});
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const _fetch = fetch !== null && fetch !== void 0 ? fetch : crossFetch.fetch;
|
|
245
|
-
const res = yield _fetch(url, options);
|
|
246
|
-
if (!res.ok) {
|
|
247
|
-
const errData = unmarshal(yield res.text());
|
|
248
|
-
if (checkReadBack !== false &&
|
|
249
|
-
((_a = errData.error) === null || _a === void 0 ? void 0 : _a.prisma) &&
|
|
250
|
-
((_b = errData.error) === null || _b === void 0 ? void 0 : _b.code) === 'P2004' &&
|
|
251
|
-
((_c = errData.error) === null || _c === void 0 ? void 0 : _c.reason) === 'RESULT_NOT_READABLE') {
|
|
252
|
-
// policy doesn't allow mutation result to be read back, just return undefined
|
|
253
|
-
return undefined;
|
|
254
|
-
}
|
|
255
|
-
const error = new Error('An error occurred while fetching the data.');
|
|
256
|
-
error.info = errData.error;
|
|
257
|
-
error.status = res.status;
|
|
258
|
-
throw error;
|
|
259
|
-
}
|
|
260
|
-
const textResult = yield res.text();
|
|
261
|
-
try {
|
|
262
|
-
return unmarshal(textResult).data;
|
|
263
|
-
}
|
|
264
|
-
catch (err) {
|
|
265
|
-
console.error(`Unable to deserialize data:`, textResult);
|
|
266
|
-
throw err;
|
|
267
|
-
}
|
|
227
|
+
if (logging) {
|
|
228
|
+
keys.forEach((key) => {
|
|
229
|
+
console.log(`Invalidating query ${key} due to mutation "${model}.${operation}"`);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
const mutations = keys.map((key) => {
|
|
233
|
+
const parsedKey = parseQueryKey(key);
|
|
234
|
+
return mutate((parsedKey == null ? void 0 : parsedKey.infinite) ? (0, import_infinite.unstable_serialize)(() => key) : key);
|
|
268
235
|
});
|
|
236
|
+
return Promise.all(mutations);
|
|
237
|
+
});
|
|
269
238
|
}
|
|
270
|
-
|
|
271
|
-
function
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
239
|
+
function fetcher(url, options, fetch2, checkReadBack) {
|
|
240
|
+
return __async(this, null, function* () {
|
|
241
|
+
var _a, _b, _c;
|
|
242
|
+
const _fetch = fetch2 != null ? fetch2 : crossFetch.fetch;
|
|
243
|
+
const res = yield _fetch(url, options);
|
|
244
|
+
if (!res.ok) {
|
|
245
|
+
const errData = unmarshal(yield res.text());
|
|
246
|
+
if (checkReadBack !== false && ((_a = errData.error) == null ? void 0 : _a.prisma) && ((_b = errData.error) == null ? void 0 : _b.code) === "P2004" && ((_c = errData.error) == null ? void 0 : _c.reason) === "RESULT_NOT_READABLE") {
|
|
247
|
+
return void 0;
|
|
248
|
+
}
|
|
249
|
+
const error = new Error(
|
|
250
|
+
"An error occurred while fetching the data."
|
|
251
|
+
);
|
|
252
|
+
error.info = errData.error;
|
|
253
|
+
error.status = res.status;
|
|
254
|
+
throw error;
|
|
275
255
|
}
|
|
276
|
-
|
|
277
|
-
|
|
256
|
+
const textResult = yield res.text();
|
|
257
|
+
try {
|
|
258
|
+
return unmarshal(textResult).data;
|
|
259
|
+
} catch (err) {
|
|
260
|
+
console.error(`Unable to deserialize data:`, textResult);
|
|
261
|
+
throw err;
|
|
278
262
|
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
function marshal(value) {
|
|
266
|
+
const { data, meta } = (0, import_browser.serialize)(value);
|
|
267
|
+
if (meta) {
|
|
268
|
+
return JSON.stringify(__spreadProps(__spreadValues({}, data), { meta: { serialization: meta } }));
|
|
269
|
+
} else {
|
|
270
|
+
return JSON.stringify(data);
|
|
271
|
+
}
|
|
279
272
|
}
|
|
280
273
|
function unmarshal(value) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
}
|
|
274
|
+
var _a;
|
|
275
|
+
const parsed = JSON.parse(value);
|
|
276
|
+
if (parsed.data && ((_a = parsed.meta) == null ? void 0 : _a.serialization)) {
|
|
277
|
+
const deserializedData = (0, import_browser.deserialize)(parsed.data, parsed.meta.serialization);
|
|
278
|
+
return __spreadProps(__spreadValues({}, parsed), { data: deserializedData });
|
|
279
|
+
} else {
|
|
280
|
+
return parsed;
|
|
281
|
+
}
|
|
290
282
|
}
|
|
291
283
|
function makeUrl(url, args) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
284
|
+
if (!args) {
|
|
285
|
+
return url;
|
|
286
|
+
}
|
|
287
|
+
const { data, meta } = (0, import_browser.serialize)(args);
|
|
288
|
+
let result = `${url}?q=${encodeURIComponent(JSON.stringify(data))}`;
|
|
289
|
+
if (meta) {
|
|
290
|
+
result += `&meta=${encodeURIComponent(JSON.stringify({ serialization: meta }))}`;
|
|
291
|
+
}
|
|
292
|
+
return result;
|
|
301
293
|
}
|
|
302
294
|
function getOperationFromUrl(url) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
295
|
+
const parts = url.split("/");
|
|
296
|
+
const r = parts.pop();
|
|
297
|
+
if (!r) {
|
|
298
|
+
throw new Error(`Invalid URL: ${url}`);
|
|
299
|
+
} else {
|
|
300
|
+
return r;
|
|
301
|
+
}
|
|
311
302
|
}
|
|
312
303
|
function optimisticUpdate(mutationModel, mutationOp, mutationArgs, modelMeta, cache, mutator, logging = false) {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
continue;
|
|
325
|
-
}
|
|
326
|
-
const cacheValue = cache.get(key);
|
|
327
|
-
if (!cacheValue) {
|
|
328
|
-
continue;
|
|
329
|
-
}
|
|
330
|
-
if (cacheValue.error) {
|
|
331
|
-
if (logging) {
|
|
332
|
-
console.warn(`Skipping optimistic update for ${key} due to error:`, cacheValue.error);
|
|
333
|
-
}
|
|
334
|
-
continue;
|
|
335
|
-
}
|
|
336
|
-
const mutatedData = yield (0, cross_1.applyMutation)(parsedKey.model, parsedKey.operation, cacheValue.data, mutationModel, mutationOp, mutationArgs, modelMeta, logging);
|
|
337
|
-
if (mutatedData !== undefined) {
|
|
338
|
-
// mutation applicable to this query, update cache
|
|
339
|
-
if (logging) {
|
|
340
|
-
console.log(`Optimistically updating query ${JSON.stringify(key)} due to mutation "${mutationModel}.${mutationOp}"`);
|
|
341
|
-
}
|
|
342
|
-
optimisticPromises.push(mutator(key, mutatedData, {
|
|
343
|
-
// don't trigger revalidation here since we will do it
|
|
344
|
-
// when the remote mutation succeeds
|
|
345
|
-
revalidate: false,
|
|
346
|
-
}));
|
|
347
|
-
}
|
|
304
|
+
return __async(this, null, function* () {
|
|
305
|
+
const optimisticPromises = [];
|
|
306
|
+
for (const key of cache.keys()) {
|
|
307
|
+
const parsedKey = parseQueryKey(key);
|
|
308
|
+
if (!parsedKey) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (!parsedKey.optimisticUpdate) {
|
|
312
|
+
if (logging) {
|
|
313
|
+
console.log(`Skipping optimistic update for ${key} due to opt-out`);
|
|
348
314
|
}
|
|
349
|
-
|
|
350
|
-
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
const cacheValue = cache.get(key);
|
|
318
|
+
if (!cacheValue) {
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (cacheValue.error) {
|
|
322
|
+
if (logging) {
|
|
323
|
+
console.warn(`Skipping optimistic update for ${key} due to error:`, cacheValue.error);
|
|
324
|
+
}
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
const mutatedData = yield (0, import_cross.applyMutation)(
|
|
328
|
+
parsedKey.model,
|
|
329
|
+
parsedKey.operation,
|
|
330
|
+
cacheValue.data,
|
|
331
|
+
mutationModel,
|
|
332
|
+
mutationOp,
|
|
333
|
+
mutationArgs,
|
|
334
|
+
modelMeta,
|
|
335
|
+
logging
|
|
336
|
+
);
|
|
337
|
+
if (mutatedData !== void 0) {
|
|
338
|
+
if (logging) {
|
|
339
|
+
console.log(
|
|
340
|
+
`Optimistically updating query ${JSON.stringify(
|
|
341
|
+
key
|
|
342
|
+
)} due to mutation "${mutationModel}.${mutationOp}"`
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
optimisticPromises.push(
|
|
346
|
+
mutator(key, mutatedData, {
|
|
347
|
+
// don't trigger revalidation here since we will do it
|
|
348
|
+
// when the remote mutation succeeds
|
|
349
|
+
revalidate: false
|
|
350
|
+
})
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return Promise.all(optimisticPromises);
|
|
355
|
+
});
|
|
351
356
|
}
|
|
357
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
358
|
+
0 && (module.exports = {
|
|
359
|
+
Provider,
|
|
360
|
+
RequestHandlerContext,
|
|
361
|
+
fetcher,
|
|
362
|
+
getQueryKey,
|
|
363
|
+
mutationRequest,
|
|
364
|
+
useHooksContext,
|
|
365
|
+
useInfiniteModelQuery,
|
|
366
|
+
useInvalidation,
|
|
367
|
+
useModelMutation,
|
|
368
|
+
useModelQuery
|
|
369
|
+
});
|
|
352
370
|
//# sourceMappingURL=index.js.map
|