@web-ts-toolkit/access-router-client 0.3.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/LICENSE +201 -0
- package/README.md +45 -0
- package/index.d.mts +552 -0
- package/index.d.ts +552 -0
- package/index.js +2016 -0
- package/index.mjs +1972 -0
- package/package.json +40 -0
package/index.mjs
ADDED
|
@@ -0,0 +1,1972 @@
|
|
|
1
|
+
// src/adapter.ts
|
|
2
|
+
import axios, { AxiosHeaders as AxiosHeaders3, mergeConfig as mergeConfig4 } from "axios";
|
|
3
|
+
import { castArray, isEmpty, set as set4 } from "@web-ts-toolkit/utils";
|
|
4
|
+
|
|
5
|
+
// src/services/model-service.ts
|
|
6
|
+
import { mergeConfig as mergeConfig2 } from "axios";
|
|
7
|
+
import { get as get2, set as set3 } from "@web-ts-toolkit/utils";
|
|
8
|
+
|
|
9
|
+
// src/types.ts
|
|
10
|
+
var wrapLazyPromise = (promiseFn, meta) => {
|
|
11
|
+
let promise;
|
|
12
|
+
const exec = () => {
|
|
13
|
+
if (!promise) {
|
|
14
|
+
promise = promiseFn();
|
|
15
|
+
}
|
|
16
|
+
return promise;
|
|
17
|
+
};
|
|
18
|
+
const prom = {
|
|
19
|
+
exec,
|
|
20
|
+
then(onFulfilled, onRejected) {
|
|
21
|
+
return exec().then(onFulfilled, onRejected);
|
|
22
|
+
},
|
|
23
|
+
catch(onRejected) {
|
|
24
|
+
return exec().catch(onRejected);
|
|
25
|
+
},
|
|
26
|
+
finally(onFinally) {
|
|
27
|
+
return exec().finally(onFinally);
|
|
28
|
+
},
|
|
29
|
+
[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
|
|
30
|
+
return "LazyPromise";
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
Object.defineProperty(prom, Symbol.toStringTag, {
|
|
34
|
+
value: "Promise",
|
|
35
|
+
writable: false,
|
|
36
|
+
enumerable: false,
|
|
37
|
+
configurable: true
|
|
38
|
+
});
|
|
39
|
+
Object.assign(prom, meta);
|
|
40
|
+
return prom;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/model.ts
|
|
44
|
+
import { assign as assignObject, cloneDeep, get as getValue, omit, pick, set as setValue } from "@web-ts-toolkit/utils";
|
|
45
|
+
var Model = class _Model {
|
|
46
|
+
constructor(data, adapter) {
|
|
47
|
+
this.modifiedPaths = /* @__PURE__ */ new Set();
|
|
48
|
+
this._snapshot = cloneDeep(data);
|
|
49
|
+
this.defineHiddenDataProp(cloneDeep(data));
|
|
50
|
+
this.defineHiddenAdapterProp(adapter);
|
|
51
|
+
this.definePublicDataProps();
|
|
52
|
+
this.initializeDirtyState();
|
|
53
|
+
}
|
|
54
|
+
static create(data, adapter) {
|
|
55
|
+
return new _Model(data, adapter);
|
|
56
|
+
}
|
|
57
|
+
async save(reqConfig) {
|
|
58
|
+
let result;
|
|
59
|
+
if (this._data._id) {
|
|
60
|
+
result = await this._service.update(this._data._id, this.prepareData(), { returningAll: false }, reqConfig);
|
|
61
|
+
} else {
|
|
62
|
+
result = await this._service.create(this.prepareData(), null, reqConfig);
|
|
63
|
+
}
|
|
64
|
+
if (result.success) {
|
|
65
|
+
this.updateModel(result.raw);
|
|
66
|
+
this._snapshot = cloneDeep(this._data);
|
|
67
|
+
this.modifiedPaths.clear();
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
...result,
|
|
71
|
+
data: result.success ? _Model.create(this._data, this._service) : null
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
isDirty(path) {
|
|
75
|
+
return path ? this.modifiedPaths.has(this.normalizePath(String(path))) : this.modifiedPaths.size > 0;
|
|
76
|
+
}
|
|
77
|
+
markModified(path) {
|
|
78
|
+
this.trackModified(String(path));
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
get(path) {
|
|
82
|
+
return getValue(this._data, path);
|
|
83
|
+
}
|
|
84
|
+
set(path, value) {
|
|
85
|
+
const currentValue = getValue(this._data, path);
|
|
86
|
+
if (Object.is(currentValue, value)) {
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
setValue(this._data, path, value);
|
|
90
|
+
this.trackModified(path);
|
|
91
|
+
this.definePublicDataProps();
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
assign(partial) {
|
|
95
|
+
const keys = Object.keys(partial);
|
|
96
|
+
for (let x = 0; x < keys.length; x++) {
|
|
97
|
+
const key = keys[x];
|
|
98
|
+
const nextValue = partial[key];
|
|
99
|
+
if (!Object.is(this._data[key], nextValue)) {
|
|
100
|
+
this._data[key] = nextValue;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
this.definePublicDataProps();
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
reset() {
|
|
107
|
+
this.replaceData(this._snapshot);
|
|
108
|
+
this.modifiedPaths.clear();
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
toObject() {
|
|
112
|
+
return cloneDeep(this._data);
|
|
113
|
+
}
|
|
114
|
+
toJSON() {
|
|
115
|
+
return this.toObject();
|
|
116
|
+
}
|
|
117
|
+
// async validate() {}
|
|
118
|
+
updateModel(data) {
|
|
119
|
+
assignObject(this._data, data);
|
|
120
|
+
this.definePublicDataProps();
|
|
121
|
+
}
|
|
122
|
+
replaceData(data) {
|
|
123
|
+
const nextData = cloneDeep(data);
|
|
124
|
+
const currentKeys = Object.keys(this._data);
|
|
125
|
+
for (let x = 0; x < currentKeys.length; x++) {
|
|
126
|
+
const key = currentKeys[x];
|
|
127
|
+
if (!Object.prototype.hasOwnProperty.call(nextData, key)) {
|
|
128
|
+
delete this._data[key];
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
assignObject(this._data, nextData);
|
|
132
|
+
this.definePublicDataProps();
|
|
133
|
+
}
|
|
134
|
+
initializeDirtyState() {
|
|
135
|
+
if (this._data._id) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const keys = Object.keys(this._data);
|
|
139
|
+
for (let x = 0; x < keys.length; x++) {
|
|
140
|
+
const key = keys[x];
|
|
141
|
+
if (key !== "_id") {
|
|
142
|
+
this.trackModified(key);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
prepareData() {
|
|
147
|
+
return omit(pick(this._data, Array.from(this.modifiedPaths).map(String)), ["_id"]);
|
|
148
|
+
}
|
|
149
|
+
defineHiddenDataProp(initialValue) {
|
|
150
|
+
Object.defineProperty(this, "_data", {
|
|
151
|
+
value: new Proxy(initialValue, {
|
|
152
|
+
set: (target, key, value) => {
|
|
153
|
+
const keystr = String(key);
|
|
154
|
+
if (Object.is(target[key], value)) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
this.trackModified(keystr);
|
|
158
|
+
target[key] = value;
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
}),
|
|
162
|
+
enumerable: false,
|
|
163
|
+
writable: true,
|
|
164
|
+
configurable: false
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
defineHiddenAdapterProp(initialValue) {
|
|
168
|
+
Object.defineProperty(this, "_service", {
|
|
169
|
+
value: initialValue,
|
|
170
|
+
enumerable: false,
|
|
171
|
+
writable: false,
|
|
172
|
+
configurable: false
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
definePublicDataProps() {
|
|
176
|
+
const keys = Object.keys(this._data);
|
|
177
|
+
const keycnt = keys.length;
|
|
178
|
+
for (let x = 0; x < keycnt; x++) {
|
|
179
|
+
const key = keys[x];
|
|
180
|
+
if (key in this) continue;
|
|
181
|
+
Object.defineProperty(this, key, {
|
|
182
|
+
enumerable: true,
|
|
183
|
+
get: () => Object.prototype.hasOwnProperty.call(this._data, key) ? this._data[key] : null,
|
|
184
|
+
set: (value) => this._data[key] = value
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
trackModified(path) {
|
|
189
|
+
this.modifiedPaths.add(this.normalizePath(path));
|
|
190
|
+
}
|
|
191
|
+
normalizePath(path) {
|
|
192
|
+
return path.split(".")[0];
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// src/services/service.ts
|
|
197
|
+
import { AxiosHeaders, mergeConfig } from "axios";
|
|
198
|
+
import { set } from "@web-ts-toolkit/utils";
|
|
199
|
+
|
|
200
|
+
// src/constants.ts
|
|
201
|
+
var CACHE_HEADER = "x-axios-cache";
|
|
202
|
+
|
|
203
|
+
// src/helpers.ts
|
|
204
|
+
import { isPlainObject, mapValues } from "@web-ts-toolkit/utils";
|
|
205
|
+
function replaceSubQuery(filter) {
|
|
206
|
+
if (!isPlainObject(filter)) return filter;
|
|
207
|
+
const ret = mapValues(filter, (val) => {
|
|
208
|
+
if (val && val.__op && val.__query) {
|
|
209
|
+
return {
|
|
210
|
+
$$sq: val.__query
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
if (isPlainObject(val)) {
|
|
214
|
+
return replaceSubQuery(val);
|
|
215
|
+
}
|
|
216
|
+
if (Array.isArray(val)) {
|
|
217
|
+
return val.map((v) => replaceSubQuery(v));
|
|
218
|
+
}
|
|
219
|
+
return val;
|
|
220
|
+
});
|
|
221
|
+
return ret;
|
|
222
|
+
}
|
|
223
|
+
function template(templateString, data) {
|
|
224
|
+
return templateString.replace(/\{\{(\w+)\}\}/g, (match, key) => {
|
|
225
|
+
return data[key] !== void 0 ? data[key] : match;
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
function getWrapContext(url, options, config) {
|
|
229
|
+
const { queryParams, pathParams } = options ?? {};
|
|
230
|
+
const finalUrl = pathParams ? template(url, pathParams) : url;
|
|
231
|
+
if (queryParams && config) config.params = queryParams;
|
|
232
|
+
return { finalUrl, finalConfig: config };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// src/services/service.ts
|
|
236
|
+
var removeTrailingSlash = (inputString) => inputString.replace(/\/$/, "");
|
|
237
|
+
var removeLeadingSlash = (inputString) => inputString.replace(/^\/+/g, "");
|
|
238
|
+
var readProblemDetail = (value) => {
|
|
239
|
+
if (!value || typeof value !== "object") {
|
|
240
|
+
return void 0;
|
|
241
|
+
}
|
|
242
|
+
if ("detail" in value && typeof value.detail === "string" && value.detail) {
|
|
243
|
+
return value.detail;
|
|
244
|
+
}
|
|
245
|
+
if ("message" in value && typeof value.message === "string" && value.message) {
|
|
246
|
+
return value.message;
|
|
247
|
+
}
|
|
248
|
+
if ("title" in value && typeof value.title === "string" && value.title) {
|
|
249
|
+
return value.title;
|
|
250
|
+
}
|
|
251
|
+
if ("errors" in value && Array.isArray(value.errors)) {
|
|
252
|
+
for (const item of value.errors) {
|
|
253
|
+
if (typeof item === "string" && item) {
|
|
254
|
+
return item;
|
|
255
|
+
}
|
|
256
|
+
const nested = readProblemDetail(item);
|
|
257
|
+
if (nested) {
|
|
258
|
+
return nested;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return void 0;
|
|
263
|
+
};
|
|
264
|
+
var stringifyErrorPayload = (value) => {
|
|
265
|
+
if (typeof value === "string") {
|
|
266
|
+
return value;
|
|
267
|
+
}
|
|
268
|
+
const detail = readProblemDetail(value);
|
|
269
|
+
if (detail) {
|
|
270
|
+
return detail;
|
|
271
|
+
}
|
|
272
|
+
if (value == null) {
|
|
273
|
+
return "";
|
|
274
|
+
}
|
|
275
|
+
try {
|
|
276
|
+
return JSON.stringify(value);
|
|
277
|
+
} catch {
|
|
278
|
+
return String(value);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
var Service = class {
|
|
282
|
+
constructor(axios2, basePath) {
|
|
283
|
+
this._axios = axios2;
|
|
284
|
+
this._basePath = basePath;
|
|
285
|
+
}
|
|
286
|
+
handleSuccess(res, extra = {}) {
|
|
287
|
+
return { success: true, raw: res.data, status: res.status, headers: res.headers, ...extra };
|
|
288
|
+
}
|
|
289
|
+
// See https://axios-http.com/docs/handling_errors
|
|
290
|
+
handleError(error) {
|
|
291
|
+
const result = {
|
|
292
|
+
success: false,
|
|
293
|
+
raw: null,
|
|
294
|
+
data: null,
|
|
295
|
+
message: "",
|
|
296
|
+
status: 0,
|
|
297
|
+
headers: {}
|
|
298
|
+
};
|
|
299
|
+
if (error.response) {
|
|
300
|
+
result.status = error.response.status;
|
|
301
|
+
result.headers = error.response.headers;
|
|
302
|
+
const responseData = error.response.data;
|
|
303
|
+
result.raw = responseData;
|
|
304
|
+
result.data = responseData;
|
|
305
|
+
result.message = stringifyErrorPayload(responseData);
|
|
306
|
+
} else if (error.request) {
|
|
307
|
+
result.message = "The server is not responding";
|
|
308
|
+
} else {
|
|
309
|
+
result.message = error.message;
|
|
310
|
+
}
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
wrapGet(url, defaultAxiosRequestConfig = {}) {
|
|
314
|
+
const _url = `${removeTrailingSlash(this._basePath)}/${removeLeadingSlash(url)}`;
|
|
315
|
+
set(defaultAxiosRequestConfig, `headers.${CACHE_HEADER}`, "true");
|
|
316
|
+
return (options, axiosRequestConfig) => {
|
|
317
|
+
const { finalUrl, finalConfig } = getWrapContext(
|
|
318
|
+
_url,
|
|
319
|
+
options,
|
|
320
|
+
mergeConfig(defaultAxiosRequestConfig, axiosRequestConfig)
|
|
321
|
+
);
|
|
322
|
+
return this._axios.get(finalUrl, finalConfig);
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
wrapPost(url, defaultAxiosRequestConfig = {}) {
|
|
326
|
+
const _url = `${removeTrailingSlash(this._basePath)}/${removeLeadingSlash(url)}`;
|
|
327
|
+
set(defaultAxiosRequestConfig, `headers.${CACHE_HEADER}`, "false");
|
|
328
|
+
return (data, options, axiosRequestConfig) => {
|
|
329
|
+
const { finalUrl, finalConfig } = getWrapContext(
|
|
330
|
+
_url,
|
|
331
|
+
options,
|
|
332
|
+
mergeConfig(defaultAxiosRequestConfig, axiosRequestConfig)
|
|
333
|
+
);
|
|
334
|
+
return this._axios.post(finalUrl, data, finalConfig);
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
wrapPut(url, defaultAxiosRequestConfig = {}) {
|
|
338
|
+
const _url = `${removeTrailingSlash(this._basePath)}/${removeLeadingSlash(url)}`;
|
|
339
|
+
set(defaultAxiosRequestConfig, `headers.${CACHE_HEADER}`, "false");
|
|
340
|
+
return (data, options, axiosRequestConfig) => {
|
|
341
|
+
const { finalUrl, finalConfig } = getWrapContext(
|
|
342
|
+
_url,
|
|
343
|
+
options,
|
|
344
|
+
mergeConfig(defaultAxiosRequestConfig, axiosRequestConfig)
|
|
345
|
+
);
|
|
346
|
+
return this._axios.put(finalUrl, data, finalConfig);
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
wrapPatch(url, defaultAxiosRequestConfig = {}) {
|
|
350
|
+
const _url = `${removeTrailingSlash(this._basePath)}/${removeLeadingSlash(url)}`;
|
|
351
|
+
set(defaultAxiosRequestConfig, `headers.${CACHE_HEADER}`, "false");
|
|
352
|
+
return (data, options, axiosRequestConfig) => {
|
|
353
|
+
const { finalUrl, finalConfig } = getWrapContext(
|
|
354
|
+
_url,
|
|
355
|
+
options,
|
|
356
|
+
mergeConfig(defaultAxiosRequestConfig, axiosRequestConfig)
|
|
357
|
+
);
|
|
358
|
+
return this._axios.patch(finalUrl, data, finalConfig);
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
wrapDelete(url, defaultAxiosRequestConfig = {}) {
|
|
362
|
+
const _url = `${removeTrailingSlash(this._basePath)}/${removeLeadingSlash(url)}`;
|
|
363
|
+
set(defaultAxiosRequestConfig, `headers.${CACHE_HEADER}`, "false");
|
|
364
|
+
return (options, axiosRequestConfig) => {
|
|
365
|
+
const { finalUrl, finalConfig } = getWrapContext(
|
|
366
|
+
_url,
|
|
367
|
+
options,
|
|
368
|
+
mergeConfig(defaultAxiosRequestConfig, axiosRequestConfig)
|
|
369
|
+
);
|
|
370
|
+
return this._axios.delete(finalUrl, finalConfig);
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
updateHeaders(headers, { ignoreCache }) {
|
|
374
|
+
const cacheValue = ignoreCache ? "false" : "true";
|
|
375
|
+
if (!headers) {
|
|
376
|
+
return { [CACHE_HEADER]: cacheValue };
|
|
377
|
+
}
|
|
378
|
+
if (headers instanceof AxiosHeaders) {
|
|
379
|
+
if (headers.has(CACHE_HEADER)) return headers;
|
|
380
|
+
headers.set(CACHE_HEADER, cacheValue);
|
|
381
|
+
return headers;
|
|
382
|
+
}
|
|
383
|
+
if (CACHE_HEADER in headers) return headers;
|
|
384
|
+
return {
|
|
385
|
+
...headers,
|
|
386
|
+
[CACHE_HEADER]: cacheValue
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
var ServiceError = class extends Error {
|
|
391
|
+
constructor(result) {
|
|
392
|
+
super(result.message);
|
|
393
|
+
this.name = "ServiceError";
|
|
394
|
+
this.success = result.success;
|
|
395
|
+
this.raw = result.raw;
|
|
396
|
+
this.data = result.data;
|
|
397
|
+
this.status = result.status;
|
|
398
|
+
this.headers = result.headers;
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
// src/services/shared.ts
|
|
403
|
+
import { get, noop, set as set2 } from "@web-ts-toolkit/utils";
|
|
404
|
+
var setDefaultObjectProp = (obj, key, value) => {
|
|
405
|
+
if (!get(obj, key)) {
|
|
406
|
+
set2(obj, key, value);
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
var createResponseHandler = (onSuccess, onFailure, throwOnError) => {
|
|
410
|
+
const successHandler = onSuccess ?? noop;
|
|
411
|
+
const failureHandler = onFailure ?? noop;
|
|
412
|
+
return (res, shouldThrowOnError = throwOnError) => {
|
|
413
|
+
if (res.success) {
|
|
414
|
+
successHandler(res);
|
|
415
|
+
return res;
|
|
416
|
+
}
|
|
417
|
+
failureHandler(res);
|
|
418
|
+
if (shouldThrowOnError) {
|
|
419
|
+
throw new ServiceError(res);
|
|
420
|
+
}
|
|
421
|
+
return res;
|
|
422
|
+
};
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
// src/services/model-service.ts
|
|
426
|
+
var ModelService = class extends Service {
|
|
427
|
+
constructor({ axios: axios2, modelName, basePath, queryPath, mutationPath, onSuccess, onFailure, throwOnError }, defaults) {
|
|
428
|
+
super(axios2, basePath);
|
|
429
|
+
this._modelName = modelName;
|
|
430
|
+
this._queryPath = queryPath;
|
|
431
|
+
this._mutationPath = mutationPath;
|
|
432
|
+
this._defaults = defaults ?? {};
|
|
433
|
+
this._handleCallbacks = createResponseHandler(onSuccess, onFailure, throwOnError);
|
|
434
|
+
[
|
|
435
|
+
"listArgs",
|
|
436
|
+
"listOptions",
|
|
437
|
+
"listAdvancedArgs",
|
|
438
|
+
"listAdvancedOptions",
|
|
439
|
+
"readOptions",
|
|
440
|
+
"readAdvancedArgs",
|
|
441
|
+
"readAdvancedOptions",
|
|
442
|
+
"createOptions",
|
|
443
|
+
"createAdvancedArgs",
|
|
444
|
+
"createAdvancedOptions",
|
|
445
|
+
"updateOptions",
|
|
446
|
+
"updateAdvancedArgs",
|
|
447
|
+
"updateAdvancedOptions",
|
|
448
|
+
"upsertOptions",
|
|
449
|
+
"upsertAdvancedArgs",
|
|
450
|
+
"upsertAdvancedOptions"
|
|
451
|
+
].forEach((key) => setDefaultObjectProp(this._defaults, key, {}));
|
|
452
|
+
}
|
|
453
|
+
list(args, options, axiosRequestConfig) {
|
|
454
|
+
const {
|
|
455
|
+
skip = this._defaults.listArgs.skip,
|
|
456
|
+
limit = this._defaults.listArgs.limit,
|
|
457
|
+
page = this._defaults.listArgs.page,
|
|
458
|
+
pageSize = this._defaults.listArgs.pageSize
|
|
459
|
+
} = args ?? {};
|
|
460
|
+
const {
|
|
461
|
+
skim = this._defaults.listOptions.skim ?? true,
|
|
462
|
+
includePermissions = this._defaults.listOptions.includePermissions ?? false,
|
|
463
|
+
includeCount = this._defaults.listOptions.includeCount ?? false,
|
|
464
|
+
includeExtraHeaders = this._defaults.listOptions.includeExtraHeaders ?? false,
|
|
465
|
+
ignoreCache = this._defaults.listOptions.ignoreCache ?? false,
|
|
466
|
+
sq
|
|
467
|
+
} = options ?? {};
|
|
468
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
469
|
+
reqConfig.headers = this.updateHeaders(reqConfig.headers, { ignoreCache });
|
|
470
|
+
const result = wrapLazyPromise(
|
|
471
|
+
() => this._axios.get(
|
|
472
|
+
this._basePath,
|
|
473
|
+
mergeConfig2(reqConfig, {
|
|
474
|
+
params: {
|
|
475
|
+
skip,
|
|
476
|
+
limit,
|
|
477
|
+
page,
|
|
478
|
+
page_size: pageSize,
|
|
479
|
+
skim,
|
|
480
|
+
include_permissions: includePermissions,
|
|
481
|
+
include_count: includeCount,
|
|
482
|
+
include_extra_headers: includeExtraHeaders
|
|
483
|
+
}
|
|
484
|
+
})
|
|
485
|
+
).then(this.handleSuccess).then((result2) => {
|
|
486
|
+
return this.processListResult(this, result2, { includeCount, includeExtraHeaders });
|
|
487
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
488
|
+
{
|
|
489
|
+
__op: "list",
|
|
490
|
+
__query: {
|
|
491
|
+
target: "model",
|
|
492
|
+
name: this._modelName,
|
|
493
|
+
model: this._modelName,
|
|
494
|
+
op: "list",
|
|
495
|
+
filter: {},
|
|
496
|
+
args: { skip, limit, page, pageSize },
|
|
497
|
+
options: {
|
|
498
|
+
skim,
|
|
499
|
+
includePermissions,
|
|
500
|
+
includeCount,
|
|
501
|
+
includeExtraHeaders
|
|
502
|
+
},
|
|
503
|
+
sqOptions: sq
|
|
504
|
+
},
|
|
505
|
+
__requestConfig: reqConfig,
|
|
506
|
+
__service: this
|
|
507
|
+
}
|
|
508
|
+
);
|
|
509
|
+
return result;
|
|
510
|
+
}
|
|
511
|
+
listAdvanced(filter, args, options, axiosRequestConfig) {
|
|
512
|
+
const {
|
|
513
|
+
populate = this._defaults.listAdvancedArgs.populate,
|
|
514
|
+
include = this._defaults.listAdvancedArgs.include,
|
|
515
|
+
sort = this._defaults.listAdvancedArgs.sort,
|
|
516
|
+
skip = this._defaults.listAdvancedArgs.skip,
|
|
517
|
+
limit = this._defaults.listAdvancedArgs.limit,
|
|
518
|
+
page = this._defaults.listAdvancedArgs.page,
|
|
519
|
+
pageSize = this._defaults.listAdvancedArgs.pageSize,
|
|
520
|
+
tasks = this._defaults.listAdvancedArgs.tasks
|
|
521
|
+
} = args ?? {};
|
|
522
|
+
const select = args?.select ?? this._defaults.listAdvancedArgs.select;
|
|
523
|
+
const {
|
|
524
|
+
skim = this._defaults.listAdvancedOptions.skim ?? true,
|
|
525
|
+
includePermissions = this._defaults.listAdvancedOptions.includePermissions ?? false,
|
|
526
|
+
includeCount = this._defaults.listAdvancedOptions.includeCount ?? false,
|
|
527
|
+
includeExtraHeaders = this._defaults.listAdvancedOptions.includeExtraHeaders ?? false,
|
|
528
|
+
populateAccess = this._defaults.listAdvancedOptions.populateAccess,
|
|
529
|
+
ignoreCache = this._defaults.listAdvancedOptions.ignoreCache ?? false,
|
|
530
|
+
sq
|
|
531
|
+
} = options ?? {};
|
|
532
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
533
|
+
reqConfig.headers = this.updateHeaders(reqConfig.headers, { ignoreCache });
|
|
534
|
+
const _filter = replaceSubQuery(filter);
|
|
535
|
+
const result = wrapLazyPromise(
|
|
536
|
+
() => this._axios.post(
|
|
537
|
+
`${this._basePath}/${this._queryPath}`,
|
|
538
|
+
{
|
|
539
|
+
filter: _filter,
|
|
540
|
+
select,
|
|
541
|
+
sort,
|
|
542
|
+
populate,
|
|
543
|
+
include,
|
|
544
|
+
skip,
|
|
545
|
+
limit,
|
|
546
|
+
page,
|
|
547
|
+
pageSize,
|
|
548
|
+
tasks,
|
|
549
|
+
options: {
|
|
550
|
+
skim,
|
|
551
|
+
includePermissions,
|
|
552
|
+
includeCount,
|
|
553
|
+
includeExtraHeaders,
|
|
554
|
+
populateAccess
|
|
555
|
+
}
|
|
556
|
+
},
|
|
557
|
+
reqConfig
|
|
558
|
+
).then(this.handleSuccess).then((result2) => {
|
|
559
|
+
return this.processListResult(this, result2, { includeCount, includeExtraHeaders });
|
|
560
|
+
}).catch(this.handleError).then(
|
|
561
|
+
(res) => this._handleCallbacks(res, throwOnError)
|
|
562
|
+
),
|
|
563
|
+
{
|
|
564
|
+
__op: "listAdvanced",
|
|
565
|
+
__query: {
|
|
566
|
+
target: "model",
|
|
567
|
+
name: this._modelName,
|
|
568
|
+
model: this._modelName,
|
|
569
|
+
op: "list",
|
|
570
|
+
filter: _filter,
|
|
571
|
+
args: { select, sort, populate, include, skip, limit, page, pageSize, tasks },
|
|
572
|
+
options: {
|
|
573
|
+
skim,
|
|
574
|
+
includePermissions,
|
|
575
|
+
includeCount,
|
|
576
|
+
includeExtraHeaders,
|
|
577
|
+
populateAccess
|
|
578
|
+
},
|
|
579
|
+
sqOptions: sq
|
|
580
|
+
},
|
|
581
|
+
__requestConfig: reqConfig,
|
|
582
|
+
__service: this
|
|
583
|
+
}
|
|
584
|
+
);
|
|
585
|
+
return result;
|
|
586
|
+
}
|
|
587
|
+
read(identifier, options, axiosRequestConfig) {
|
|
588
|
+
const {
|
|
589
|
+
includePermissions = this._defaults.readOptions.includePermissions ?? true,
|
|
590
|
+
tryList = this._defaults.readOptions.tryList ?? true,
|
|
591
|
+
ignoreCache = this._defaults.readOptions.ignoreCache ?? false,
|
|
592
|
+
sq
|
|
593
|
+
} = options ?? {};
|
|
594
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
595
|
+
reqConfig.headers = this.updateHeaders(reqConfig.headers, { ignoreCache });
|
|
596
|
+
const result = wrapLazyPromise(
|
|
597
|
+
() => this._axios.get(
|
|
598
|
+
`${this._basePath}/${identifier}`,
|
|
599
|
+
mergeConfig2(reqConfig, {
|
|
600
|
+
params: {
|
|
601
|
+
include_permissions: includePermissions,
|
|
602
|
+
try_list: tryList
|
|
603
|
+
}
|
|
604
|
+
})
|
|
605
|
+
).then(this.handleSuccess).then((result2) => {
|
|
606
|
+
result2.data = result2.success ? Model.create(result2.raw, this) : null;
|
|
607
|
+
return result2;
|
|
608
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
609
|
+
{
|
|
610
|
+
__op: "read",
|
|
611
|
+
__query: {
|
|
612
|
+
target: "model",
|
|
613
|
+
name: this._modelName,
|
|
614
|
+
model: this._modelName,
|
|
615
|
+
op: "read",
|
|
616
|
+
id: identifier,
|
|
617
|
+
args: {},
|
|
618
|
+
options: {
|
|
619
|
+
includePermissions,
|
|
620
|
+
tryList
|
|
621
|
+
},
|
|
622
|
+
sqOptions: sq
|
|
623
|
+
},
|
|
624
|
+
__requestConfig: reqConfig,
|
|
625
|
+
__service: this
|
|
626
|
+
}
|
|
627
|
+
);
|
|
628
|
+
return result;
|
|
629
|
+
}
|
|
630
|
+
readAdvanced(identifier, args, options, axiosRequestConfig) {
|
|
631
|
+
const {
|
|
632
|
+
populate = this._defaults.readAdvancedArgs.populate,
|
|
633
|
+
include = this._defaults.readAdvancedArgs.include,
|
|
634
|
+
tasks = this._defaults.readAdvancedArgs.tasks
|
|
635
|
+
} = args ?? {};
|
|
636
|
+
const select = args?.select ?? this._defaults.readAdvancedArgs.select;
|
|
637
|
+
const {
|
|
638
|
+
skim = this._defaults.readAdvancedOptions.skim ?? true,
|
|
639
|
+
includePermissions = this._defaults.readAdvancedOptions.includePermissions ?? true,
|
|
640
|
+
tryList = this._defaults.readAdvancedOptions.tryList ?? true,
|
|
641
|
+
populateAccess = this._defaults.readAdvancedOptions.populateAccess,
|
|
642
|
+
ignoreCache = this._defaults.readAdvancedOptions.ignoreCache ?? false,
|
|
643
|
+
sq
|
|
644
|
+
} = options ?? {};
|
|
645
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
646
|
+
reqConfig.headers = this.updateHeaders(reqConfig.headers, { ignoreCache });
|
|
647
|
+
const result = wrapLazyPromise(
|
|
648
|
+
() => this._axios.post(
|
|
649
|
+
`${this._basePath}/${this._queryPath}/${identifier}`,
|
|
650
|
+
{
|
|
651
|
+
select,
|
|
652
|
+
populate,
|
|
653
|
+
include,
|
|
654
|
+
tasks,
|
|
655
|
+
options: {
|
|
656
|
+
skim,
|
|
657
|
+
includePermissions,
|
|
658
|
+
tryList,
|
|
659
|
+
populateAccess
|
|
660
|
+
}
|
|
661
|
+
},
|
|
662
|
+
reqConfig
|
|
663
|
+
).then(this.handleSuccess).then((result2) => {
|
|
664
|
+
result2.data = result2.success ? Model.create(result2.raw, this) : null;
|
|
665
|
+
return result2;
|
|
666
|
+
}).catch(this.handleError).then(
|
|
667
|
+
(res) => this._handleCallbacks(res, throwOnError)
|
|
668
|
+
),
|
|
669
|
+
{
|
|
670
|
+
__op: "readAdvanced",
|
|
671
|
+
__query: {
|
|
672
|
+
target: "model",
|
|
673
|
+
name: this._modelName,
|
|
674
|
+
model: this._modelName,
|
|
675
|
+
op: "read",
|
|
676
|
+
id: identifier,
|
|
677
|
+
args: { select, populate, include, tasks },
|
|
678
|
+
options: {
|
|
679
|
+
skim,
|
|
680
|
+
includePermissions,
|
|
681
|
+
tryList,
|
|
682
|
+
populateAccess
|
|
683
|
+
},
|
|
684
|
+
sqOptions: sq
|
|
685
|
+
},
|
|
686
|
+
__requestConfig: reqConfig,
|
|
687
|
+
__service: this
|
|
688
|
+
}
|
|
689
|
+
);
|
|
690
|
+
return result;
|
|
691
|
+
}
|
|
692
|
+
readAdvancedFilter(filter, args, options, axiosRequestConfig) {
|
|
693
|
+
const {
|
|
694
|
+
sort = this._defaults.readAdvancedArgs.sort,
|
|
695
|
+
populate = this._defaults.readAdvancedArgs.populate,
|
|
696
|
+
include = this._defaults.readAdvancedArgs.include,
|
|
697
|
+
tasks = this._defaults.readAdvancedArgs.tasks
|
|
698
|
+
} = args ?? {};
|
|
699
|
+
const select = args?.select ?? this._defaults.readAdvancedArgs.select;
|
|
700
|
+
const {
|
|
701
|
+
skim = this._defaults.readAdvancedOptions.skim ?? true,
|
|
702
|
+
includePermissions = this._defaults.readAdvancedOptions.includePermissions ?? true,
|
|
703
|
+
tryList = this._defaults.readAdvancedOptions.tryList ?? true,
|
|
704
|
+
populateAccess = this._defaults.readAdvancedOptions.populateAccess,
|
|
705
|
+
ignoreCache = this._defaults.readAdvancedOptions.ignoreCache ?? false,
|
|
706
|
+
sq
|
|
707
|
+
} = options ?? {};
|
|
708
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
709
|
+
reqConfig.headers = this.updateHeaders(reqConfig.headers, { ignoreCache });
|
|
710
|
+
const _filter = replaceSubQuery(filter);
|
|
711
|
+
const result = wrapLazyPromise(
|
|
712
|
+
() => this._axios.post(
|
|
713
|
+
`${this._basePath}/${this._queryPath}/__filter`,
|
|
714
|
+
{
|
|
715
|
+
filter: _filter,
|
|
716
|
+
select,
|
|
717
|
+
sort,
|
|
718
|
+
populate,
|
|
719
|
+
include,
|
|
720
|
+
tasks,
|
|
721
|
+
options: {
|
|
722
|
+
skim,
|
|
723
|
+
includePermissions,
|
|
724
|
+
tryList,
|
|
725
|
+
populateAccess
|
|
726
|
+
}
|
|
727
|
+
},
|
|
728
|
+
reqConfig
|
|
729
|
+
).then(this.handleSuccess).then((result2) => {
|
|
730
|
+
result2.data = result2.success ? Model.create(result2.raw, this) : null;
|
|
731
|
+
return result2;
|
|
732
|
+
}).catch(this.handleError).then(
|
|
733
|
+
(res) => this._handleCallbacks(res, throwOnError)
|
|
734
|
+
),
|
|
735
|
+
{
|
|
736
|
+
__op: "readAdvancedFilter",
|
|
737
|
+
__query: {
|
|
738
|
+
target: "model",
|
|
739
|
+
name: this._modelName,
|
|
740
|
+
model: this._modelName,
|
|
741
|
+
op: "read",
|
|
742
|
+
filter: _filter,
|
|
743
|
+
args: { select, sort, populate, include, tasks },
|
|
744
|
+
options: {
|
|
745
|
+
skim,
|
|
746
|
+
includePermissions,
|
|
747
|
+
tryList,
|
|
748
|
+
populateAccess
|
|
749
|
+
},
|
|
750
|
+
sqOptions: sq
|
|
751
|
+
},
|
|
752
|
+
__requestConfig: reqConfig,
|
|
753
|
+
__service: this
|
|
754
|
+
}
|
|
755
|
+
);
|
|
756
|
+
return result;
|
|
757
|
+
}
|
|
758
|
+
new(axiosRequestConfig) {
|
|
759
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
760
|
+
set3(reqConfig, `headers.${CACHE_HEADER}`, "false");
|
|
761
|
+
const result = wrapLazyPromise(
|
|
762
|
+
() => this._axios.get(`${this._basePath}/new`, reqConfig).then(this.handleSuccess).then((result2) => {
|
|
763
|
+
delete result2.raw._id;
|
|
764
|
+
result2.data = result2.success ? Model.create(result2.raw, this) : null;
|
|
765
|
+
return result2;
|
|
766
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
767
|
+
{
|
|
768
|
+
__op: "new",
|
|
769
|
+
__query: {
|
|
770
|
+
target: "model",
|
|
771
|
+
name: this._modelName,
|
|
772
|
+
model: this._modelName,
|
|
773
|
+
op: "new"
|
|
774
|
+
},
|
|
775
|
+
__requestConfig: reqConfig,
|
|
776
|
+
__service: this
|
|
777
|
+
}
|
|
778
|
+
);
|
|
779
|
+
return result;
|
|
780
|
+
}
|
|
781
|
+
create(data, options, axiosRequestConfig) {
|
|
782
|
+
const { includePermissions = this._defaults.createOptions.includePermissions ?? true } = options ?? {};
|
|
783
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
784
|
+
set3(reqConfig, `headers.${CACHE_HEADER}`, "false");
|
|
785
|
+
const result = wrapLazyPromise(
|
|
786
|
+
() => this._axios.post(this._basePath, data, mergeConfig2(reqConfig, { params: { include_permissions: includePermissions } })).then(this.handleSuccess).then((result2) => {
|
|
787
|
+
result2.data = result2.success ? Model.create(result2.raw, this) : null;
|
|
788
|
+
return result2;
|
|
789
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
790
|
+
{
|
|
791
|
+
__op: "create",
|
|
792
|
+
__query: {
|
|
793
|
+
target: "model",
|
|
794
|
+
name: this._modelName,
|
|
795
|
+
model: this._modelName,
|
|
796
|
+
op: "create",
|
|
797
|
+
data,
|
|
798
|
+
options: {
|
|
799
|
+
includePermissions
|
|
800
|
+
}
|
|
801
|
+
},
|
|
802
|
+
__requestConfig: reqConfig,
|
|
803
|
+
__service: this
|
|
804
|
+
}
|
|
805
|
+
);
|
|
806
|
+
return result;
|
|
807
|
+
}
|
|
808
|
+
createAdvanced(data, args, options, axiosRequestConfig) {
|
|
809
|
+
const { populate = this._defaults.createAdvancedArgs.populate, tasks = this._defaults.createAdvancedArgs.tasks } = args ?? {};
|
|
810
|
+
const select = args?.select ?? this._defaults.createAdvancedArgs.select;
|
|
811
|
+
const {
|
|
812
|
+
includePermissions = this._defaults.createAdvancedOptions.includePermissions ?? true,
|
|
813
|
+
populateAccess = this._defaults.createAdvancedOptions.populateAccess
|
|
814
|
+
} = options ?? {};
|
|
815
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
816
|
+
set3(reqConfig, `headers.${CACHE_HEADER}`, "false");
|
|
817
|
+
const result = wrapLazyPromise(
|
|
818
|
+
() => this._axios.post(
|
|
819
|
+
`${this._basePath}/${this._mutationPath}`,
|
|
820
|
+
{ data, select, populate, tasks, options: { includePermissions, populateAccess } },
|
|
821
|
+
reqConfig
|
|
822
|
+
).then(this.handleSuccess).then((result2) => {
|
|
823
|
+
result2.data = result2.success ? Model.create(result2.raw, this) : null;
|
|
824
|
+
return result2;
|
|
825
|
+
}).catch(this.handleError).then(
|
|
826
|
+
(res) => this._handleCallbacks(res, throwOnError)
|
|
827
|
+
),
|
|
828
|
+
{
|
|
829
|
+
__op: "createAdvanced",
|
|
830
|
+
__query: {
|
|
831
|
+
target: "model",
|
|
832
|
+
name: this._modelName,
|
|
833
|
+
model: this._modelName,
|
|
834
|
+
op: "create",
|
|
835
|
+
data,
|
|
836
|
+
args: { select, populate, tasks },
|
|
837
|
+
options: {
|
|
838
|
+
includePermissions,
|
|
839
|
+
populateAccess
|
|
840
|
+
}
|
|
841
|
+
},
|
|
842
|
+
__requestConfig: reqConfig,
|
|
843
|
+
__service: this
|
|
844
|
+
}
|
|
845
|
+
);
|
|
846
|
+
return result;
|
|
847
|
+
}
|
|
848
|
+
update(identifier, data, options, axiosRequestConfig) {
|
|
849
|
+
const { returningAll = this._defaults.updateOptions.returningAll ?? true } = options ?? {};
|
|
850
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
851
|
+
set3(reqConfig, `headers.${CACHE_HEADER}`, "false");
|
|
852
|
+
const result = wrapLazyPromise(
|
|
853
|
+
() => this._axios.patch(
|
|
854
|
+
`${this._basePath}/${identifier}`,
|
|
855
|
+
data,
|
|
856
|
+
mergeConfig2(reqConfig, { params: { returning_all: returningAll } })
|
|
857
|
+
).then(this.handleSuccess).then((result2) => {
|
|
858
|
+
result2.data = result2.success ? Model.create(result2.raw, this) : null;
|
|
859
|
+
return result2;
|
|
860
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
861
|
+
{
|
|
862
|
+
__op: "update",
|
|
863
|
+
__query: {
|
|
864
|
+
target: "model",
|
|
865
|
+
name: this._modelName,
|
|
866
|
+
model: this._modelName,
|
|
867
|
+
op: "update",
|
|
868
|
+
id: identifier,
|
|
869
|
+
data,
|
|
870
|
+
options: {
|
|
871
|
+
returningAll
|
|
872
|
+
}
|
|
873
|
+
},
|
|
874
|
+
__requestConfig: reqConfig,
|
|
875
|
+
__service: this
|
|
876
|
+
}
|
|
877
|
+
);
|
|
878
|
+
return result;
|
|
879
|
+
}
|
|
880
|
+
updateAdvanced(identifier, data, args, options, axiosRequestConfig) {
|
|
881
|
+
const { populate = this._defaults.updateAdvancedArgs.populate, tasks = this._defaults.updateAdvancedArgs.tasks } = args ?? {};
|
|
882
|
+
const select = args?.select ?? this._defaults.updateAdvancedArgs.select;
|
|
883
|
+
const {
|
|
884
|
+
returningAll = this._defaults.updateAdvancedOptions.returningAll ?? true,
|
|
885
|
+
includePermissions = this._defaults.updateAdvancedOptions.includePermissions ?? true,
|
|
886
|
+
populateAccess = this._defaults.updateAdvancedOptions.populateAccess
|
|
887
|
+
} = options ?? {};
|
|
888
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
889
|
+
set3(reqConfig, `headers.${CACHE_HEADER}`, "false");
|
|
890
|
+
const result = wrapLazyPromise(
|
|
891
|
+
() => this._axios.patch(
|
|
892
|
+
`${this._basePath}/${this._mutationPath}/${identifier}`,
|
|
893
|
+
{
|
|
894
|
+
data,
|
|
895
|
+
select,
|
|
896
|
+
populate,
|
|
897
|
+
tasks,
|
|
898
|
+
options: { returningAll, includePermissions, populateAccess }
|
|
899
|
+
},
|
|
900
|
+
reqConfig
|
|
901
|
+
).then(this.handleSuccess).then((result2) => {
|
|
902
|
+
result2.data = result2.success ? Model.create(result2.raw, this) : null;
|
|
903
|
+
return result2;
|
|
904
|
+
}).catch(this.handleError).then(
|
|
905
|
+
(res) => this._handleCallbacks(res, throwOnError)
|
|
906
|
+
),
|
|
907
|
+
{
|
|
908
|
+
__op: "updateAdvanced",
|
|
909
|
+
__query: {
|
|
910
|
+
target: "model",
|
|
911
|
+
name: this._modelName,
|
|
912
|
+
model: this._modelName,
|
|
913
|
+
op: "update",
|
|
914
|
+
id: identifier,
|
|
915
|
+
data,
|
|
916
|
+
args: { select, populate, tasks },
|
|
917
|
+
options: {
|
|
918
|
+
returningAll,
|
|
919
|
+
includePermissions,
|
|
920
|
+
populateAccess
|
|
921
|
+
}
|
|
922
|
+
},
|
|
923
|
+
__requestConfig: reqConfig,
|
|
924
|
+
__service: this
|
|
925
|
+
}
|
|
926
|
+
);
|
|
927
|
+
return result;
|
|
928
|
+
}
|
|
929
|
+
upsert(data, options, axiosRequestConfig) {
|
|
930
|
+
const { returningAll = this._defaults.upsertOptions.returningAll ?? true } = options ?? {};
|
|
931
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
932
|
+
set3(reqConfig, `headers.${CACHE_HEADER}`, "false");
|
|
933
|
+
const result = wrapLazyPromise(
|
|
934
|
+
() => this._axios.put(this._basePath, data, mergeConfig2(reqConfig, { params: { returning_all: returningAll } })).then(this.handleSuccess).then((result2) => {
|
|
935
|
+
result2.data = result2.success ? Model.create(result2.raw, this) : null;
|
|
936
|
+
return result2;
|
|
937
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
938
|
+
{
|
|
939
|
+
__op: "upsert",
|
|
940
|
+
__query: {
|
|
941
|
+
target: "model",
|
|
942
|
+
name: this._modelName,
|
|
943
|
+
model: this._modelName,
|
|
944
|
+
op: "upsert",
|
|
945
|
+
data,
|
|
946
|
+
options: {
|
|
947
|
+
returningAll
|
|
948
|
+
}
|
|
949
|
+
},
|
|
950
|
+
__requestConfig: reqConfig,
|
|
951
|
+
__service: this
|
|
952
|
+
}
|
|
953
|
+
);
|
|
954
|
+
return result;
|
|
955
|
+
}
|
|
956
|
+
upsertAdvanced(data, args, options, axiosRequestConfig) {
|
|
957
|
+
const { populate = this._defaults.upsertAdvancedArgs.populate, tasks = this._defaults.upsertAdvancedArgs.tasks } = args ?? {};
|
|
958
|
+
const select = args?.select ?? this._defaults.upsertAdvancedArgs.select;
|
|
959
|
+
const {
|
|
960
|
+
returningAll = this._defaults.upsertAdvancedOptions.returningAll ?? true,
|
|
961
|
+
includePermissions = this._defaults.upsertAdvancedOptions.includePermissions ?? true,
|
|
962
|
+
populateAccess = this._defaults.upsertAdvancedOptions.populateAccess
|
|
963
|
+
} = options ?? {};
|
|
964
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
965
|
+
set3(reqConfig, `headers.${CACHE_HEADER}`, "false");
|
|
966
|
+
const result = wrapLazyPromise(
|
|
967
|
+
() => this._axios.put(
|
|
968
|
+
`${this._basePath}/${this._mutationPath}`,
|
|
969
|
+
{
|
|
970
|
+
data,
|
|
971
|
+
select,
|
|
972
|
+
populate,
|
|
973
|
+
tasks,
|
|
974
|
+
options: { returningAll, includePermissions, populateAccess }
|
|
975
|
+
},
|
|
976
|
+
reqConfig
|
|
977
|
+
).then(this.handleSuccess).then((result2) => {
|
|
978
|
+
result2.data = result2.success ? Model.create(result2.raw, this) : null;
|
|
979
|
+
return result2;
|
|
980
|
+
}).catch(this.handleError).then(
|
|
981
|
+
(res) => this._handleCallbacks(res, throwOnError)
|
|
982
|
+
),
|
|
983
|
+
{
|
|
984
|
+
__op: "upsertAdvanced",
|
|
985
|
+
__query: {
|
|
986
|
+
target: "model",
|
|
987
|
+
name: this._modelName,
|
|
988
|
+
model: this._modelName,
|
|
989
|
+
op: "upsert",
|
|
990
|
+
data,
|
|
991
|
+
args: { select, populate, tasks },
|
|
992
|
+
options: {
|
|
993
|
+
returningAll,
|
|
994
|
+
includePermissions,
|
|
995
|
+
populateAccess
|
|
996
|
+
}
|
|
997
|
+
},
|
|
998
|
+
__requestConfig: reqConfig,
|
|
999
|
+
__service: this
|
|
1000
|
+
}
|
|
1001
|
+
);
|
|
1002
|
+
return result;
|
|
1003
|
+
}
|
|
1004
|
+
delete(identifier, axiosRequestConfig) {
|
|
1005
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1006
|
+
set3(reqConfig, `headers.${CACHE_HEADER}`, "false");
|
|
1007
|
+
const result = wrapLazyPromise(
|
|
1008
|
+
() => this._axios.delete(`${this._basePath}/${identifier}`, reqConfig).then(this.handleSuccess).then((result2) => {
|
|
1009
|
+
result2.data = result2.raw;
|
|
1010
|
+
return result2;
|
|
1011
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1012
|
+
{
|
|
1013
|
+
__op: "delete",
|
|
1014
|
+
__query: {
|
|
1015
|
+
target: "model",
|
|
1016
|
+
name: this._modelName,
|
|
1017
|
+
model: this._modelName,
|
|
1018
|
+
op: "delete",
|
|
1019
|
+
id: identifier
|
|
1020
|
+
},
|
|
1021
|
+
__requestConfig: reqConfig,
|
|
1022
|
+
__service: this
|
|
1023
|
+
}
|
|
1024
|
+
);
|
|
1025
|
+
return result;
|
|
1026
|
+
}
|
|
1027
|
+
distinct(field, axiosRequestConfig) {
|
|
1028
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1029
|
+
const result = wrapLazyPromise(
|
|
1030
|
+
() => this._axios.get(`${this._basePath}/distinct/${field}`, reqConfig).then(this.handleSuccess).then((result2) => {
|
|
1031
|
+
result2.data = result2.raw;
|
|
1032
|
+
return result2;
|
|
1033
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1034
|
+
{
|
|
1035
|
+
__op: "distinct",
|
|
1036
|
+
__query: {
|
|
1037
|
+
target: "model",
|
|
1038
|
+
name: this._modelName,
|
|
1039
|
+
model: this._modelName,
|
|
1040
|
+
op: "distinct",
|
|
1041
|
+
field
|
|
1042
|
+
},
|
|
1043
|
+
__requestConfig: reqConfig,
|
|
1044
|
+
__service: this
|
|
1045
|
+
}
|
|
1046
|
+
);
|
|
1047
|
+
return result;
|
|
1048
|
+
}
|
|
1049
|
+
distinctAdvanced(field, conditions, axiosRequestConfig) {
|
|
1050
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1051
|
+
const result = wrapLazyPromise(
|
|
1052
|
+
() => this._axios.post(`${this._basePath}/distinct/${field}`, conditions, reqConfig).then(this.handleSuccess).then((result2) => {
|
|
1053
|
+
result2.data = result2.raw;
|
|
1054
|
+
return result2;
|
|
1055
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1056
|
+
{
|
|
1057
|
+
__op: "distinctAdvanced",
|
|
1058
|
+
__query: {
|
|
1059
|
+
target: "model",
|
|
1060
|
+
name: this._modelName,
|
|
1061
|
+
model: this._modelName,
|
|
1062
|
+
op: "distinct",
|
|
1063
|
+
field,
|
|
1064
|
+
filter: conditions
|
|
1065
|
+
},
|
|
1066
|
+
__requestConfig: reqConfig,
|
|
1067
|
+
__service: this
|
|
1068
|
+
}
|
|
1069
|
+
);
|
|
1070
|
+
return result;
|
|
1071
|
+
}
|
|
1072
|
+
count(axiosRequestConfig) {
|
|
1073
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1074
|
+
const result = wrapLazyPromise(
|
|
1075
|
+
() => this._axios.get(`${this._basePath}/count`, reqConfig).then(this.handleSuccess).then((result2) => {
|
|
1076
|
+
result2.data = result2.raw;
|
|
1077
|
+
return result2;
|
|
1078
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1079
|
+
{
|
|
1080
|
+
__op: "count",
|
|
1081
|
+
__query: {
|
|
1082
|
+
target: "model",
|
|
1083
|
+
name: this._modelName,
|
|
1084
|
+
model: this._modelName,
|
|
1085
|
+
op: "count"
|
|
1086
|
+
},
|
|
1087
|
+
__requestConfig: reqConfig,
|
|
1088
|
+
__service: this
|
|
1089
|
+
}
|
|
1090
|
+
);
|
|
1091
|
+
return result;
|
|
1092
|
+
}
|
|
1093
|
+
countAdvanced(filter, args, axiosRequestConfig) {
|
|
1094
|
+
const { access } = args ?? {};
|
|
1095
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1096
|
+
const result = wrapLazyPromise(
|
|
1097
|
+
() => this._axios.post(`${this._basePath}/count`, { filter, options: { access } }, reqConfig).then(this.handleSuccess).then((result2) => {
|
|
1098
|
+
result2.data = result2.raw;
|
|
1099
|
+
return result2;
|
|
1100
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1101
|
+
{
|
|
1102
|
+
__op: "countAdvanced",
|
|
1103
|
+
__query: {
|
|
1104
|
+
target: "model",
|
|
1105
|
+
name: this._modelName,
|
|
1106
|
+
model: this._modelName,
|
|
1107
|
+
op: "count",
|
|
1108
|
+
filter,
|
|
1109
|
+
options: { access }
|
|
1110
|
+
},
|
|
1111
|
+
__requestConfig: reqConfig,
|
|
1112
|
+
__service: this
|
|
1113
|
+
}
|
|
1114
|
+
);
|
|
1115
|
+
return result;
|
|
1116
|
+
}
|
|
1117
|
+
id(id) {
|
|
1118
|
+
return {
|
|
1119
|
+
subs: (field) => {
|
|
1120
|
+
const sub = String(field);
|
|
1121
|
+
return {
|
|
1122
|
+
list: (axiosRequestConfig) => {
|
|
1123
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1124
|
+
const result = wrapLazyPromise(
|
|
1125
|
+
() => this._axios.get(
|
|
1126
|
+
`${this._basePath}/${id}/${sub}`,
|
|
1127
|
+
mergeConfig2(reqConfig, {
|
|
1128
|
+
params: {}
|
|
1129
|
+
})
|
|
1130
|
+
).then(this.handleSuccess).then((result2) => {
|
|
1131
|
+
result2.totalCount = Array.isArray(result2.raw) ? result2.raw.length : 0;
|
|
1132
|
+
result2.data = [];
|
|
1133
|
+
return result2;
|
|
1134
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1135
|
+
{
|
|
1136
|
+
__op: "listSub",
|
|
1137
|
+
__query: {
|
|
1138
|
+
target: "model",
|
|
1139
|
+
name: this._modelName,
|
|
1140
|
+
model: this._modelName,
|
|
1141
|
+
op: "subList",
|
|
1142
|
+
id,
|
|
1143
|
+
sub,
|
|
1144
|
+
filter: {},
|
|
1145
|
+
args: {},
|
|
1146
|
+
options: {}
|
|
1147
|
+
},
|
|
1148
|
+
__requestConfig: reqConfig,
|
|
1149
|
+
__service: this
|
|
1150
|
+
}
|
|
1151
|
+
);
|
|
1152
|
+
return result;
|
|
1153
|
+
},
|
|
1154
|
+
listAdvanced: (filter, args, axiosRequestConfig) => {
|
|
1155
|
+
const select = args?.select;
|
|
1156
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1157
|
+
const result = wrapLazyPromise(
|
|
1158
|
+
() => this._axios.post(`${this._basePath}/${id}/${sub}/${this._queryPath}`, { filter, select }, reqConfig).then(this.handleSuccess).then((result2) => {
|
|
1159
|
+
result2.totalCount = result2.raw.length;
|
|
1160
|
+
result2.data = [];
|
|
1161
|
+
return result2;
|
|
1162
|
+
}).catch(this.handleError).then(
|
|
1163
|
+
(res) => this._handleCallbacks(
|
|
1164
|
+
res,
|
|
1165
|
+
throwOnError
|
|
1166
|
+
)
|
|
1167
|
+
),
|
|
1168
|
+
{
|
|
1169
|
+
__op: "listAdvancedSub",
|
|
1170
|
+
__query: {
|
|
1171
|
+
target: "model",
|
|
1172
|
+
name: this._modelName,
|
|
1173
|
+
model: this._modelName,
|
|
1174
|
+
op: "subList",
|
|
1175
|
+
id,
|
|
1176
|
+
sub,
|
|
1177
|
+
filter,
|
|
1178
|
+
args: { select },
|
|
1179
|
+
options: {}
|
|
1180
|
+
},
|
|
1181
|
+
__requestConfig: reqConfig,
|
|
1182
|
+
__service: this
|
|
1183
|
+
}
|
|
1184
|
+
);
|
|
1185
|
+
return result;
|
|
1186
|
+
},
|
|
1187
|
+
read: (subId, axiosRequestConfig) => {
|
|
1188
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1189
|
+
const result = wrapLazyPromise(
|
|
1190
|
+
() => this._axios.get(
|
|
1191
|
+
`${this._basePath}/${id}/${sub}/${subId}`,
|
|
1192
|
+
mergeConfig2(reqConfig, {
|
|
1193
|
+
params: {}
|
|
1194
|
+
})
|
|
1195
|
+
).then(this.handleSuccess).then((result2) => {
|
|
1196
|
+
result2.data = null;
|
|
1197
|
+
return result2;
|
|
1198
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1199
|
+
{
|
|
1200
|
+
__op: "readSub",
|
|
1201
|
+
__query: {
|
|
1202
|
+
target: "model",
|
|
1203
|
+
name: this._modelName,
|
|
1204
|
+
model: this._modelName,
|
|
1205
|
+
op: "subRead",
|
|
1206
|
+
id,
|
|
1207
|
+
sub,
|
|
1208
|
+
subId,
|
|
1209
|
+
args: {},
|
|
1210
|
+
options: {}
|
|
1211
|
+
},
|
|
1212
|
+
__requestConfig: reqConfig,
|
|
1213
|
+
__service: this
|
|
1214
|
+
}
|
|
1215
|
+
);
|
|
1216
|
+
return result;
|
|
1217
|
+
},
|
|
1218
|
+
readAdvanced: (subId, args, axiosRequestConfig) => {
|
|
1219
|
+
const { select, populate } = args ?? {};
|
|
1220
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1221
|
+
const result = wrapLazyPromise(
|
|
1222
|
+
() => this._axios.post(
|
|
1223
|
+
`${this._basePath}/${id}/${sub}/${subId}/${this._queryPath}`,
|
|
1224
|
+
{
|
|
1225
|
+
select,
|
|
1226
|
+
populate
|
|
1227
|
+
},
|
|
1228
|
+
reqConfig
|
|
1229
|
+
).then(this.handleSuccess).then((result2) => {
|
|
1230
|
+
result2.data = null;
|
|
1231
|
+
return result2;
|
|
1232
|
+
}).catch(this.handleError).then(
|
|
1233
|
+
(res) => this._handleCallbacks(
|
|
1234
|
+
res,
|
|
1235
|
+
throwOnError
|
|
1236
|
+
)
|
|
1237
|
+
),
|
|
1238
|
+
{
|
|
1239
|
+
__op: "readAdvancedSub",
|
|
1240
|
+
__query: {
|
|
1241
|
+
target: "model",
|
|
1242
|
+
name: this._modelName,
|
|
1243
|
+
model: this._modelName,
|
|
1244
|
+
op: "subRead",
|
|
1245
|
+
id,
|
|
1246
|
+
sub,
|
|
1247
|
+
subId,
|
|
1248
|
+
args: { select, populate },
|
|
1249
|
+
options: {}
|
|
1250
|
+
},
|
|
1251
|
+
__requestConfig: reqConfig,
|
|
1252
|
+
__service: this
|
|
1253
|
+
}
|
|
1254
|
+
);
|
|
1255
|
+
return result;
|
|
1256
|
+
},
|
|
1257
|
+
update: (subId, data, axiosRequestConfig) => {
|
|
1258
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1259
|
+
const result = wrapLazyPromise(
|
|
1260
|
+
() => this._axios.patch(`${this._basePath}/${id}/${sub}/${subId}`, data, mergeConfig2(reqConfig, { params: {} })).then(this.handleSuccess).then((result2) => {
|
|
1261
|
+
result2.data = null;
|
|
1262
|
+
return result2;
|
|
1263
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1264
|
+
{
|
|
1265
|
+
__op: "updateSub",
|
|
1266
|
+
__query: {
|
|
1267
|
+
target: "model",
|
|
1268
|
+
name: this._modelName,
|
|
1269
|
+
model: this._modelName,
|
|
1270
|
+
op: "subUpdate",
|
|
1271
|
+
id,
|
|
1272
|
+
sub,
|
|
1273
|
+
subId,
|
|
1274
|
+
data,
|
|
1275
|
+
options: {}
|
|
1276
|
+
},
|
|
1277
|
+
__requestConfig: reqConfig,
|
|
1278
|
+
__service: this
|
|
1279
|
+
}
|
|
1280
|
+
);
|
|
1281
|
+
return result;
|
|
1282
|
+
},
|
|
1283
|
+
bulkUpdate: (data, _options, axiosRequestConfig) => {
|
|
1284
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1285
|
+
const result = wrapLazyPromise(
|
|
1286
|
+
() => this._axios.patch(`${this._basePath}/${id}/${sub}`, data, mergeConfig2(reqConfig, { params: {} })).then(this.handleSuccess).then((result2) => {
|
|
1287
|
+
result2.data = [];
|
|
1288
|
+
return result2;
|
|
1289
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1290
|
+
{
|
|
1291
|
+
__op: "bulkUpdateSub",
|
|
1292
|
+
__query: {
|
|
1293
|
+
target: "model",
|
|
1294
|
+
name: this._modelName,
|
|
1295
|
+
model: this._modelName,
|
|
1296
|
+
op: "subBulkUpdate",
|
|
1297
|
+
id,
|
|
1298
|
+
sub,
|
|
1299
|
+
data,
|
|
1300
|
+
options: {}
|
|
1301
|
+
},
|
|
1302
|
+
__requestConfig: reqConfig,
|
|
1303
|
+
__service: this
|
|
1304
|
+
}
|
|
1305
|
+
);
|
|
1306
|
+
return result;
|
|
1307
|
+
},
|
|
1308
|
+
create: (data, axiosRequestConfig) => {
|
|
1309
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1310
|
+
const result = wrapLazyPromise(
|
|
1311
|
+
() => this._axios.post(`${this._basePath}/${id}/${sub}`, data, mergeConfig2(reqConfig, { params: {} })).then(this.handleSuccess).then((result2) => {
|
|
1312
|
+
result2.data = null;
|
|
1313
|
+
return result2;
|
|
1314
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1315
|
+
{
|
|
1316
|
+
__op: "createSub",
|
|
1317
|
+
__query: {
|
|
1318
|
+
target: "model",
|
|
1319
|
+
name: this._modelName,
|
|
1320
|
+
model: this._modelName,
|
|
1321
|
+
op: "subCreate",
|
|
1322
|
+
id,
|
|
1323
|
+
sub,
|
|
1324
|
+
data,
|
|
1325
|
+
options: {}
|
|
1326
|
+
},
|
|
1327
|
+
__requestConfig: reqConfig,
|
|
1328
|
+
__service: this
|
|
1329
|
+
}
|
|
1330
|
+
);
|
|
1331
|
+
return result;
|
|
1332
|
+
},
|
|
1333
|
+
delete: (subId, axiosRequestConfig) => {
|
|
1334
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1335
|
+
const result = wrapLazyPromise(
|
|
1336
|
+
() => this._axios.delete(`${this._basePath}/${id}/${sub}/${subId}`, reqConfig).then(this.handleSuccess).then((result2) => {
|
|
1337
|
+
result2.data = null;
|
|
1338
|
+
return result2;
|
|
1339
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1340
|
+
{
|
|
1341
|
+
__op: "deleteSub",
|
|
1342
|
+
__query: {
|
|
1343
|
+
target: "model",
|
|
1344
|
+
name: this._modelName,
|
|
1345
|
+
model: this._modelName,
|
|
1346
|
+
op: "subDelete",
|
|
1347
|
+
id,
|
|
1348
|
+
sub,
|
|
1349
|
+
subId
|
|
1350
|
+
},
|
|
1351
|
+
__requestConfig: reqConfig,
|
|
1352
|
+
__service: this
|
|
1353
|
+
}
|
|
1354
|
+
);
|
|
1355
|
+
return result;
|
|
1356
|
+
}
|
|
1357
|
+
};
|
|
1358
|
+
},
|
|
1359
|
+
fetch: (args, options, axiosRequestConfig) => {
|
|
1360
|
+
return this.readAdvanced(id, args, options, axiosRequestConfig);
|
|
1361
|
+
}
|
|
1362
|
+
};
|
|
1363
|
+
}
|
|
1364
|
+
processListResult(_this, result, { includeCount, includeExtraHeaders }) {
|
|
1365
|
+
const wrappedRows = get2(result, "raw.data");
|
|
1366
|
+
const wrappedTotalCount = get2(result, "raw.meta.totalCount");
|
|
1367
|
+
if (Array.isArray(wrappedRows)) {
|
|
1368
|
+
const rows = wrappedRows;
|
|
1369
|
+
result.raw = wrappedRows;
|
|
1370
|
+
if (includeCount) {
|
|
1371
|
+
if (includeExtraHeaders) {
|
|
1372
|
+
const totalCount = get2(result, `headers.${"wtt-total-count" /* TotalCount */}`, 0);
|
|
1373
|
+
result.totalCount = Number(totalCount);
|
|
1374
|
+
} else {
|
|
1375
|
+
result.totalCount = Number(wrappedTotalCount ?? rows.length);
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
} else if (includeCount) {
|
|
1379
|
+
if (includeExtraHeaders) {
|
|
1380
|
+
const totalCount = get2(result, `headers.${"wtt-total-count" /* TotalCount */}`, 0);
|
|
1381
|
+
result.totalCount = Number(totalCount);
|
|
1382
|
+
} else {
|
|
1383
|
+
result.totalCount = result.raw.count;
|
|
1384
|
+
result.raw = result.raw.rows;
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
result.data = result.success ? result.raw.map((item) => Model.create(item, _this)) : [];
|
|
1388
|
+
return result;
|
|
1389
|
+
}
|
|
1390
|
+
};
|
|
1391
|
+
|
|
1392
|
+
// src/services/data-service.ts
|
|
1393
|
+
import { mergeConfig as mergeConfig3 } from "axios";
|
|
1394
|
+
import { get as get3 } from "@web-ts-toolkit/utils";
|
|
1395
|
+
var DataService = class extends Service {
|
|
1396
|
+
constructor({ axios: axios2, dataName, basePath, queryPath, onSuccess, onFailure, throwOnError }, defaults) {
|
|
1397
|
+
super(axios2, basePath);
|
|
1398
|
+
this._dataName = dataName;
|
|
1399
|
+
this._queryPath = queryPath;
|
|
1400
|
+
this._defaults = defaults ?? {};
|
|
1401
|
+
this._handleCallbacks = createResponseHandler(onSuccess, onFailure, throwOnError);
|
|
1402
|
+
[
|
|
1403
|
+
"listArgs",
|
|
1404
|
+
"listOptions",
|
|
1405
|
+
"listAdvancedArgs",
|
|
1406
|
+
"listAdvancedOptions",
|
|
1407
|
+
"readOptions",
|
|
1408
|
+
"readAdvancedArgs",
|
|
1409
|
+
"readAdvancedOptions"
|
|
1410
|
+
].forEach((key) => setDefaultObjectProp(this._defaults, key, {}));
|
|
1411
|
+
}
|
|
1412
|
+
list(args, options, axiosRequestConfig) {
|
|
1413
|
+
const {
|
|
1414
|
+
skip = this._defaults.listArgs.skip,
|
|
1415
|
+
limit = this._defaults.listArgs.limit,
|
|
1416
|
+
page = this._defaults.listArgs.page,
|
|
1417
|
+
pageSize = this._defaults.listArgs.pageSize
|
|
1418
|
+
} = args ?? {};
|
|
1419
|
+
const {
|
|
1420
|
+
includePermissions = this._defaults.listOptions.includePermissions ?? false,
|
|
1421
|
+
includeCount = this._defaults.listOptions.includeCount ?? false,
|
|
1422
|
+
includeExtraHeaders = this._defaults.listOptions.includeExtraHeaders ?? false,
|
|
1423
|
+
ignoreCache = this._defaults.listOptions.ignoreCache ?? false
|
|
1424
|
+
} = options ?? {};
|
|
1425
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1426
|
+
reqConfig.headers = this.updateHeaders(reqConfig.headers, { ignoreCache });
|
|
1427
|
+
const result = wrapLazyPromise(
|
|
1428
|
+
() => this._axios.get(
|
|
1429
|
+
this._basePath,
|
|
1430
|
+
mergeConfig3(reqConfig, {
|
|
1431
|
+
params: {
|
|
1432
|
+
skip,
|
|
1433
|
+
limit,
|
|
1434
|
+
page,
|
|
1435
|
+
page_size: pageSize,
|
|
1436
|
+
include_permissions: includePermissions,
|
|
1437
|
+
include_count: includeCount,
|
|
1438
|
+
include_extra_headers: includeExtraHeaders
|
|
1439
|
+
}
|
|
1440
|
+
})
|
|
1441
|
+
).then(this.handleSuccess).then((result2) => {
|
|
1442
|
+
return this.processListResult(result2, { includeCount, includeExtraHeaders });
|
|
1443
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1444
|
+
{
|
|
1445
|
+
__op: "list",
|
|
1446
|
+
__query: {
|
|
1447
|
+
target: "data",
|
|
1448
|
+
name: this._dataName,
|
|
1449
|
+
op: "list",
|
|
1450
|
+
filter: {},
|
|
1451
|
+
args: { skip, limit, page, pageSize },
|
|
1452
|
+
options: {
|
|
1453
|
+
includePermissions,
|
|
1454
|
+
includeCount,
|
|
1455
|
+
includeExtraHeaders
|
|
1456
|
+
}
|
|
1457
|
+
},
|
|
1458
|
+
__requestConfig: reqConfig,
|
|
1459
|
+
__service: this
|
|
1460
|
+
}
|
|
1461
|
+
);
|
|
1462
|
+
return result;
|
|
1463
|
+
}
|
|
1464
|
+
listAdvanced(filter, args, options, axiosRequestConfig) {
|
|
1465
|
+
const {
|
|
1466
|
+
sort = this._defaults.listAdvancedArgs.sort,
|
|
1467
|
+
skip = this._defaults.listAdvancedArgs.skip,
|
|
1468
|
+
limit = this._defaults.listAdvancedArgs.limit,
|
|
1469
|
+
page = this._defaults.listAdvancedArgs.page,
|
|
1470
|
+
pageSize = this._defaults.listAdvancedArgs.pageSize
|
|
1471
|
+
} = args ?? {};
|
|
1472
|
+
const select = args?.select ?? this._defaults.listAdvancedArgs.select;
|
|
1473
|
+
const {
|
|
1474
|
+
includePermissions = this._defaults.listAdvancedOptions.includePermissions ?? false,
|
|
1475
|
+
includeCount = this._defaults.listAdvancedOptions.includeCount ?? false,
|
|
1476
|
+
includeExtraHeaders = this._defaults.listAdvancedOptions.includeExtraHeaders ?? false,
|
|
1477
|
+
ignoreCache = this._defaults.listAdvancedOptions.ignoreCache ?? false
|
|
1478
|
+
} = options ?? {};
|
|
1479
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1480
|
+
reqConfig.headers = this.updateHeaders(reqConfig.headers, { ignoreCache });
|
|
1481
|
+
const _filter = replaceSubQuery(filter);
|
|
1482
|
+
const result = wrapLazyPromise(
|
|
1483
|
+
() => this._axios.post(
|
|
1484
|
+
`${this._basePath}/${this._queryPath}`,
|
|
1485
|
+
{
|
|
1486
|
+
filter: _filter,
|
|
1487
|
+
select,
|
|
1488
|
+
sort,
|
|
1489
|
+
skip,
|
|
1490
|
+
limit,
|
|
1491
|
+
page,
|
|
1492
|
+
pageSize,
|
|
1493
|
+
options: {
|
|
1494
|
+
includePermissions,
|
|
1495
|
+
includeCount,
|
|
1496
|
+
includeExtraHeaders
|
|
1497
|
+
}
|
|
1498
|
+
},
|
|
1499
|
+
reqConfig
|
|
1500
|
+
).then(this.handleSuccess).then((result2) => {
|
|
1501
|
+
return this.processListResult(result2, { includeCount, includeExtraHeaders });
|
|
1502
|
+
}).catch(this.handleError).then(
|
|
1503
|
+
(res) => this._handleCallbacks(res, throwOnError)
|
|
1504
|
+
),
|
|
1505
|
+
{
|
|
1506
|
+
__op: "listAdvanced",
|
|
1507
|
+
__query: {
|
|
1508
|
+
target: "data",
|
|
1509
|
+
name: this._dataName,
|
|
1510
|
+
op: "list",
|
|
1511
|
+
filter: _filter,
|
|
1512
|
+
args: { select, sort, skip, limit, page, pageSize },
|
|
1513
|
+
options: {
|
|
1514
|
+
includePermissions,
|
|
1515
|
+
includeCount,
|
|
1516
|
+
includeExtraHeaders
|
|
1517
|
+
}
|
|
1518
|
+
},
|
|
1519
|
+
__requestConfig: reqConfig,
|
|
1520
|
+
__service: this
|
|
1521
|
+
}
|
|
1522
|
+
);
|
|
1523
|
+
return result;
|
|
1524
|
+
}
|
|
1525
|
+
read(identifier, options, axiosRequestConfig) {
|
|
1526
|
+
const {
|
|
1527
|
+
includePermissions = this._defaults.readOptions.includePermissions ?? true,
|
|
1528
|
+
ignoreCache = this._defaults.readOptions.ignoreCache ?? false
|
|
1529
|
+
} = options ?? {};
|
|
1530
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1531
|
+
reqConfig.headers = this.updateHeaders(reqConfig.headers, { ignoreCache });
|
|
1532
|
+
const result = wrapLazyPromise(
|
|
1533
|
+
() => this._axios.get(
|
|
1534
|
+
`${this._basePath}/${identifier}`,
|
|
1535
|
+
mergeConfig3(reqConfig, {
|
|
1536
|
+
params: {
|
|
1537
|
+
include_permissions: includePermissions
|
|
1538
|
+
}
|
|
1539
|
+
})
|
|
1540
|
+
).then(this.handleSuccess).then((result2) => {
|
|
1541
|
+
result2.data = result2.raw;
|
|
1542
|
+
return result2;
|
|
1543
|
+
}).catch(this.handleError).then((res) => this._handleCallbacks(res, throwOnError)),
|
|
1544
|
+
{
|
|
1545
|
+
__op: "read",
|
|
1546
|
+
__query: {
|
|
1547
|
+
target: "data",
|
|
1548
|
+
name: this._dataName,
|
|
1549
|
+
op: "read",
|
|
1550
|
+
id: identifier,
|
|
1551
|
+
args: {},
|
|
1552
|
+
options: {
|
|
1553
|
+
includePermissions
|
|
1554
|
+
}
|
|
1555
|
+
},
|
|
1556
|
+
__requestConfig: reqConfig,
|
|
1557
|
+
__service: this
|
|
1558
|
+
}
|
|
1559
|
+
);
|
|
1560
|
+
return result;
|
|
1561
|
+
}
|
|
1562
|
+
readAdvanced(identifier, args, options, axiosRequestConfig) {
|
|
1563
|
+
const { ignoreCache = this._defaults.readAdvancedArgs.ignoreCache ?? false } = args ?? {};
|
|
1564
|
+
const select = args?.select ?? this._defaults.readAdvancedArgs.select;
|
|
1565
|
+
const { includePermissions = this._defaults.readAdvancedOptions.includePermissions ?? true } = options ?? {};
|
|
1566
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1567
|
+
reqConfig.headers = this.updateHeaders(reqConfig.headers, { ignoreCache });
|
|
1568
|
+
const result = wrapLazyPromise(
|
|
1569
|
+
() => this._axios.post(`${this._basePath}/${this._queryPath}/${identifier}`, { select }, reqConfig).then(this.handleSuccess).then((result2) => {
|
|
1570
|
+
result2.data = result2.raw;
|
|
1571
|
+
return result2;
|
|
1572
|
+
}).catch(this.handleError).then(
|
|
1573
|
+
(res) => this._handleCallbacks(res, throwOnError)
|
|
1574
|
+
),
|
|
1575
|
+
{
|
|
1576
|
+
__op: "readAdvanced",
|
|
1577
|
+
__query: {
|
|
1578
|
+
target: "data",
|
|
1579
|
+
name: this._dataName,
|
|
1580
|
+
op: "read",
|
|
1581
|
+
id: identifier,
|
|
1582
|
+
args: { select },
|
|
1583
|
+
options: {
|
|
1584
|
+
includePermissions
|
|
1585
|
+
}
|
|
1586
|
+
},
|
|
1587
|
+
__requestConfig: reqConfig,
|
|
1588
|
+
__service: this
|
|
1589
|
+
}
|
|
1590
|
+
);
|
|
1591
|
+
return result;
|
|
1592
|
+
}
|
|
1593
|
+
readAdvancedFilter(filter, args, options, axiosRequestConfig) {
|
|
1594
|
+
const select = args?.select ?? this._defaults.readAdvancedArgs.select;
|
|
1595
|
+
const {
|
|
1596
|
+
includePermissions = this._defaults.readAdvancedOptions.includePermissions ?? true,
|
|
1597
|
+
ignoreCache = this._defaults.readAdvancedOptions.ignoreCache ?? false
|
|
1598
|
+
} = options ?? {};
|
|
1599
|
+
const { throwOnError, ...reqConfig } = axiosRequestConfig ?? {};
|
|
1600
|
+
reqConfig.headers = this.updateHeaders(reqConfig.headers, { ignoreCache });
|
|
1601
|
+
const _filter = replaceSubQuery(filter);
|
|
1602
|
+
const result = wrapLazyPromise(
|
|
1603
|
+
() => this._axios.post(`${this._basePath}/${this._queryPath}/__filter`, { filter: _filter, select }, reqConfig).then(this.handleSuccess).then((result2) => {
|
|
1604
|
+
result2.data = result2.raw;
|
|
1605
|
+
return result2;
|
|
1606
|
+
}).catch(this.handleError).then(
|
|
1607
|
+
(res) => this._handleCallbacks(res, throwOnError)
|
|
1608
|
+
),
|
|
1609
|
+
{
|
|
1610
|
+
__op: "readAdvancedFilter",
|
|
1611
|
+
__query: {
|
|
1612
|
+
target: "data",
|
|
1613
|
+
name: this._dataName,
|
|
1614
|
+
op: "read",
|
|
1615
|
+
filter: _filter,
|
|
1616
|
+
args: { select },
|
|
1617
|
+
options: {
|
|
1618
|
+
includePermissions
|
|
1619
|
+
}
|
|
1620
|
+
},
|
|
1621
|
+
__requestConfig: reqConfig,
|
|
1622
|
+
__service: this
|
|
1623
|
+
}
|
|
1624
|
+
);
|
|
1625
|
+
return result;
|
|
1626
|
+
}
|
|
1627
|
+
processListResult(result, { includeCount, includeExtraHeaders }) {
|
|
1628
|
+
const wrappedRows = get3(result, "raw.data");
|
|
1629
|
+
const wrappedTotalCount = get3(result, "raw.meta.totalCount");
|
|
1630
|
+
if (Array.isArray(wrappedRows)) {
|
|
1631
|
+
const rows = wrappedRows;
|
|
1632
|
+
result.raw = wrappedRows;
|
|
1633
|
+
if (includeCount) {
|
|
1634
|
+
if (includeExtraHeaders) {
|
|
1635
|
+
const totalCount = get3(result, `headers.${"wtt-total-count" /* TotalCount */}`, 0);
|
|
1636
|
+
result.totalCount = Number(totalCount);
|
|
1637
|
+
} else {
|
|
1638
|
+
result.totalCount = Number(wrappedTotalCount ?? rows.length);
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
} else if (includeCount) {
|
|
1642
|
+
if (includeExtraHeaders) {
|
|
1643
|
+
const totalCount = get3(result, `headers.${"wtt-total-count" /* TotalCount */}`, 0);
|
|
1644
|
+
result.totalCount = Number(totalCount);
|
|
1645
|
+
} else {
|
|
1646
|
+
result.totalCount = result.raw.count;
|
|
1647
|
+
result.raw = result.raw.rows;
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
result.data = result.raw;
|
|
1651
|
+
return result;
|
|
1652
|
+
}
|
|
1653
|
+
};
|
|
1654
|
+
|
|
1655
|
+
// src/services/interceptors.ts
|
|
1656
|
+
import { AxiosHeaders as AxiosHeaders2 } from "axios";
|
|
1657
|
+
var SimpleCache = class {
|
|
1658
|
+
constructor() {
|
|
1659
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
1660
|
+
}
|
|
1661
|
+
set(key, value) {
|
|
1662
|
+
this.cache.set(key, value);
|
|
1663
|
+
}
|
|
1664
|
+
get(key) {
|
|
1665
|
+
return this.cache.get(key);
|
|
1666
|
+
}
|
|
1667
|
+
has(key) {
|
|
1668
|
+
return this.cache.has(key);
|
|
1669
|
+
}
|
|
1670
|
+
delete(key) {
|
|
1671
|
+
return this.cache.delete(key);
|
|
1672
|
+
}
|
|
1673
|
+
};
|
|
1674
|
+
var normalizeCacheValue = (value) => {
|
|
1675
|
+
if (value == null) return value;
|
|
1676
|
+
if (Array.isArray(value)) {
|
|
1677
|
+
return value.map((item) => normalizeCacheValue(item));
|
|
1678
|
+
}
|
|
1679
|
+
if (typeof value === "object") {
|
|
1680
|
+
return Object.entries(value).filter(([, item]) => item !== void 0).sort(([left], [right]) => left.localeCompare(right)).reduce((acc, [key, item]) => {
|
|
1681
|
+
acc[key] = normalizeCacheValue(item);
|
|
1682
|
+
return acc;
|
|
1683
|
+
}, {});
|
|
1684
|
+
}
|
|
1685
|
+
return value;
|
|
1686
|
+
};
|
|
1687
|
+
var IGNORED_CACHE_HEADERS = /* @__PURE__ */ new Set([
|
|
1688
|
+
"accept",
|
|
1689
|
+
"accept-encoding",
|
|
1690
|
+
"cache-control",
|
|
1691
|
+
"connection",
|
|
1692
|
+
"content-length",
|
|
1693
|
+
"content-type",
|
|
1694
|
+
"expires",
|
|
1695
|
+
"host",
|
|
1696
|
+
"pragma",
|
|
1697
|
+
"user-agent"
|
|
1698
|
+
]);
|
|
1699
|
+
var serializeHeaders = (headers) => {
|
|
1700
|
+
const resolvedHeaders = headers instanceof AxiosHeaders2 ? headers.toJSON() : headers;
|
|
1701
|
+
const normalizedHeaders = Object.entries(resolvedHeaders ?? {}).filter(([key, value]) => {
|
|
1702
|
+
const normalizedKey = key.toLowerCase();
|
|
1703
|
+
return normalizedKey !== CACHE_HEADER.toLowerCase() && !IGNORED_CACHE_HEADERS.has(normalizedKey) && value !== void 0;
|
|
1704
|
+
}).reduce((acc, [key, value]) => {
|
|
1705
|
+
acc[key.toLowerCase()] = value;
|
|
1706
|
+
return acc;
|
|
1707
|
+
}, {});
|
|
1708
|
+
return JSON.stringify(normalizeCacheValue(normalizedHeaders));
|
|
1709
|
+
};
|
|
1710
|
+
function generateCacheKey(config) {
|
|
1711
|
+
const key = `${config.baseURL}/${config.url}_${config.method}_${generateParamKey(config.params)}_${generateDataKey(
|
|
1712
|
+
config.data
|
|
1713
|
+
)}_${serializeHeaders(config.headers)}`;
|
|
1714
|
+
return encodeURI(key);
|
|
1715
|
+
}
|
|
1716
|
+
function generateParamKey(params) {
|
|
1717
|
+
if (!params) return "";
|
|
1718
|
+
return JSON.stringify(normalizeCacheValue(params));
|
|
1719
|
+
}
|
|
1720
|
+
function generateDataKey(data) {
|
|
1721
|
+
if (!data) return "";
|
|
1722
|
+
return typeof data === "string" ? data : JSON.stringify(normalizeCacheValue(data));
|
|
1723
|
+
}
|
|
1724
|
+
function useCacheInterceptors(instance, cacheTTL) {
|
|
1725
|
+
const store = new SimpleCache();
|
|
1726
|
+
instance.interceptors.request.use(
|
|
1727
|
+
async (config) => {
|
|
1728
|
+
if (config.headers[CACHE_HEADER] === "false") return config;
|
|
1729
|
+
const key = generateCacheKey(config);
|
|
1730
|
+
const cachedResponse = store.get(key);
|
|
1731
|
+
if (!cachedResponse) {
|
|
1732
|
+
return config;
|
|
1733
|
+
}
|
|
1734
|
+
config.adapter = async (_config) => {
|
|
1735
|
+
return {
|
|
1736
|
+
...cachedResponse,
|
|
1737
|
+
config: _config,
|
|
1738
|
+
headers: { ...cachedResponse.headers, [CACHE_HEADER]: "true" }
|
|
1739
|
+
};
|
|
1740
|
+
};
|
|
1741
|
+
return config;
|
|
1742
|
+
},
|
|
1743
|
+
(error) => Promise.reject(error)
|
|
1744
|
+
);
|
|
1745
|
+
instance.interceptors.response.use(
|
|
1746
|
+
(response) => {
|
|
1747
|
+
if (response.config.headers[CACHE_HEADER] === "false") return response;
|
|
1748
|
+
const key = generateCacheKey(response.config);
|
|
1749
|
+
store.set(key, response);
|
|
1750
|
+
setTimeout(() => store.delete(key), cacheTTL);
|
|
1751
|
+
return response;
|
|
1752
|
+
},
|
|
1753
|
+
(error) => Promise.reject(error)
|
|
1754
|
+
);
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
// src/adapter.ts
|
|
1758
|
+
var defaultAxiosConfig = Object.freeze({
|
|
1759
|
+
baseURL: "/api",
|
|
1760
|
+
timeout: 0,
|
|
1761
|
+
withCredentials: true,
|
|
1762
|
+
headers: {
|
|
1763
|
+
"Cache-Control": "no-cache",
|
|
1764
|
+
Pragma: "no-cache",
|
|
1765
|
+
Expires: "0"
|
|
1766
|
+
}
|
|
1767
|
+
});
|
|
1768
|
+
var isModelQuery = (query) => query.target === "model";
|
|
1769
|
+
var normalizeConfigValue = (value) => {
|
|
1770
|
+
if (value == null) return value;
|
|
1771
|
+
if (value instanceof AxiosHeaders3) {
|
|
1772
|
+
return normalizeConfigValue(value.toJSON());
|
|
1773
|
+
}
|
|
1774
|
+
if (Array.isArray(value)) {
|
|
1775
|
+
return value.map((item) => normalizeConfigValue(item));
|
|
1776
|
+
}
|
|
1777
|
+
if (typeof value === "object") {
|
|
1778
|
+
return Object.entries(value).filter(([, item]) => item !== void 0).sort(([left], [right]) => left.localeCompare(right)).reduce((acc, [key, item]) => {
|
|
1779
|
+
acc[key] = normalizeConfigValue(item);
|
|
1780
|
+
return acc;
|
|
1781
|
+
}, {});
|
|
1782
|
+
}
|
|
1783
|
+
return value;
|
|
1784
|
+
};
|
|
1785
|
+
var serializeRequestConfig = (config) => JSON.stringify(normalizeConfigValue(config ?? {}));
|
|
1786
|
+
function createAdapter(axiosConfig, adapterOptions) {
|
|
1787
|
+
const merged = mergeConfig4(defaultAxiosConfig, axiosConfig ?? {});
|
|
1788
|
+
const instance = axios.create(merged);
|
|
1789
|
+
const {
|
|
1790
|
+
rootRouterPath = "root",
|
|
1791
|
+
onSuccess: onSuccessRoot,
|
|
1792
|
+
onFailure: onFailureRoot,
|
|
1793
|
+
throwOnError: throwOnErrorRoot,
|
|
1794
|
+
cacheTTL = 0
|
|
1795
|
+
} = adapterOptions ?? {};
|
|
1796
|
+
if (cacheTTL > 0) useCacheInterceptors(instance, cacheTTL);
|
|
1797
|
+
return Object.freeze({
|
|
1798
|
+
axios: instance,
|
|
1799
|
+
createModelService: ({
|
|
1800
|
+
modelName,
|
|
1801
|
+
basePath,
|
|
1802
|
+
queryPath = "__query",
|
|
1803
|
+
mutationPath = "__mutation",
|
|
1804
|
+
onSuccess,
|
|
1805
|
+
onFailure,
|
|
1806
|
+
throwOnError
|
|
1807
|
+
}, defaults) => {
|
|
1808
|
+
return new ModelService(
|
|
1809
|
+
{
|
|
1810
|
+
axios: instance,
|
|
1811
|
+
modelName,
|
|
1812
|
+
basePath,
|
|
1813
|
+
queryPath,
|
|
1814
|
+
mutationPath,
|
|
1815
|
+
onSuccess: onSuccess ?? onSuccessRoot,
|
|
1816
|
+
onFailure: onFailure ?? onFailureRoot,
|
|
1817
|
+
throwOnError: throwOnError ?? throwOnErrorRoot ?? false
|
|
1818
|
+
},
|
|
1819
|
+
defaults
|
|
1820
|
+
);
|
|
1821
|
+
},
|
|
1822
|
+
createDataService: ({ dataName, basePath, queryPath = "__query", onSuccess, onFailure, throwOnError }, defaults) => {
|
|
1823
|
+
return new DataService(
|
|
1824
|
+
{
|
|
1825
|
+
axios: instance,
|
|
1826
|
+
dataName,
|
|
1827
|
+
basePath,
|
|
1828
|
+
queryPath,
|
|
1829
|
+
onSuccess: onSuccess ?? onSuccessRoot,
|
|
1830
|
+
onFailure: onFailure ?? onFailureRoot,
|
|
1831
|
+
throwOnError: throwOnError ?? throwOnErrorRoot ?? false
|
|
1832
|
+
},
|
|
1833
|
+
defaults
|
|
1834
|
+
);
|
|
1835
|
+
},
|
|
1836
|
+
wrapGet: (url, defaultAxiosRequestConfig = {}) => {
|
|
1837
|
+
set4(defaultAxiosRequestConfig, `headers.${CACHE_HEADER}`, "true");
|
|
1838
|
+
return (options, axiosRequestConfig) => {
|
|
1839
|
+
const { finalUrl, finalConfig } = getWrapContext(
|
|
1840
|
+
url,
|
|
1841
|
+
options,
|
|
1842
|
+
mergeConfig4(defaultAxiosRequestConfig, axiosRequestConfig)
|
|
1843
|
+
);
|
|
1844
|
+
return instance.get(finalUrl, finalConfig);
|
|
1845
|
+
};
|
|
1846
|
+
},
|
|
1847
|
+
wrapPost: (url, defaultAxiosRequestConfig = {}) => {
|
|
1848
|
+
set4(defaultAxiosRequestConfig, `headers.${CACHE_HEADER}`, "false");
|
|
1849
|
+
return (data, options, axiosRequestConfig) => {
|
|
1850
|
+
const { finalUrl, finalConfig } = getWrapContext(
|
|
1851
|
+
url,
|
|
1852
|
+
options,
|
|
1853
|
+
mergeConfig4(defaultAxiosRequestConfig, axiosRequestConfig)
|
|
1854
|
+
);
|
|
1855
|
+
return instance.post(finalUrl, data, finalConfig);
|
|
1856
|
+
};
|
|
1857
|
+
},
|
|
1858
|
+
wrapPut: (url, defaultAxiosRequestConfig = {}) => {
|
|
1859
|
+
set4(defaultAxiosRequestConfig, `headers.${CACHE_HEADER}`, "false");
|
|
1860
|
+
return (data, options, axiosRequestConfig) => {
|
|
1861
|
+
const { finalUrl, finalConfig } = getWrapContext(
|
|
1862
|
+
url,
|
|
1863
|
+
options,
|
|
1864
|
+
mergeConfig4(defaultAxiosRequestConfig, axiosRequestConfig)
|
|
1865
|
+
);
|
|
1866
|
+
return instance.put(finalUrl, data, finalConfig);
|
|
1867
|
+
};
|
|
1868
|
+
},
|
|
1869
|
+
wrapPatch: (url, defaultAxiosRequestConfig = {}) => {
|
|
1870
|
+
set4(defaultAxiosRequestConfig, `headers.${CACHE_HEADER}`, "false");
|
|
1871
|
+
return (data, options, axiosRequestConfig) => {
|
|
1872
|
+
const { finalUrl, finalConfig } = getWrapContext(
|
|
1873
|
+
url,
|
|
1874
|
+
options,
|
|
1875
|
+
mergeConfig4(defaultAxiosRequestConfig, axiosRequestConfig)
|
|
1876
|
+
);
|
|
1877
|
+
return instance.patch(finalUrl, data, finalConfig);
|
|
1878
|
+
};
|
|
1879
|
+
},
|
|
1880
|
+
wrapDelete: (url, defaultAxiosRequestConfig = {}) => {
|
|
1881
|
+
set4(defaultAxiosRequestConfig, `headers.${CACHE_HEADER}`, "false");
|
|
1882
|
+
return (options, axiosRequestConfig) => {
|
|
1883
|
+
const { finalUrl, finalConfig } = getWrapContext(
|
|
1884
|
+
url,
|
|
1885
|
+
options,
|
|
1886
|
+
mergeConfig4(defaultAxiosRequestConfig, axiosRequestConfig)
|
|
1887
|
+
);
|
|
1888
|
+
return instance.delete(finalUrl, finalConfig);
|
|
1889
|
+
};
|
|
1890
|
+
},
|
|
1891
|
+
group: async (...proms) => {
|
|
1892
|
+
let sharedConfig;
|
|
1893
|
+
let sharedConfigKey;
|
|
1894
|
+
const defs = proms.map((prom) => {
|
|
1895
|
+
if (!isEmpty(prom.__requestConfig)) {
|
|
1896
|
+
const configKey = serializeRequestConfig(prom.__requestConfig);
|
|
1897
|
+
if (sharedConfigKey && sharedConfigKey !== configKey) {
|
|
1898
|
+
throw new Error("Grouped requests must share the same axios request config");
|
|
1899
|
+
}
|
|
1900
|
+
sharedConfig = prom.__requestConfig;
|
|
1901
|
+
sharedConfigKey = configKey;
|
|
1902
|
+
}
|
|
1903
|
+
return prom.__query;
|
|
1904
|
+
});
|
|
1905
|
+
const result = await instance.post(rootRouterPath, defs, sharedConfig ?? {}).then((res) => {
|
|
1906
|
+
return res.data.map(({ result: result2, message, statusCode, op }, index) => {
|
|
1907
|
+
const service = proms[index].__service;
|
|
1908
|
+
const query = proms[index].__query;
|
|
1909
|
+
const success = result2.success;
|
|
1910
|
+
let _raw = success ? result2.data : null;
|
|
1911
|
+
let _data = _raw;
|
|
1912
|
+
if (!success) {
|
|
1913
|
+
_data = null;
|
|
1914
|
+
} else if (isModelQuery(query)) {
|
|
1915
|
+
const modelService = service;
|
|
1916
|
+
if (result2.kind === "list" && Array.isArray(result2.data)) {
|
|
1917
|
+
if (op === "create" && result2.data.length === 1) {
|
|
1918
|
+
_raw = result2.data[0];
|
|
1919
|
+
_data = Model.create(result2.data[0], modelService);
|
|
1920
|
+
} else if (!["distinct", "subList"].includes(op)) {
|
|
1921
|
+
_data = castArray(result2.data).map((item) => Model.create(item, modelService));
|
|
1922
|
+
}
|
|
1923
|
+
} else if (result2.kind === "single" && ["new", "read", "update", "upsert"].includes(op)) {
|
|
1924
|
+
_data = Model.create(result2.data, modelService);
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
return {
|
|
1928
|
+
success,
|
|
1929
|
+
raw: _raw,
|
|
1930
|
+
data: _data,
|
|
1931
|
+
message,
|
|
1932
|
+
status: statusCode,
|
|
1933
|
+
totalCount: result2.success && result2.kind === "list" ? result2.totalCount ?? result2.count ?? 0 : 0,
|
|
1934
|
+
headers: {}
|
|
1935
|
+
};
|
|
1936
|
+
});
|
|
1937
|
+
});
|
|
1938
|
+
return result;
|
|
1939
|
+
}
|
|
1940
|
+
});
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
// src/utils.ts
|
|
1944
|
+
function replaceItemById(items, newItem, options) {
|
|
1945
|
+
const { merge = true } = options ?? {};
|
|
1946
|
+
return items.map((item) => {
|
|
1947
|
+
if (item._id === newItem._id) {
|
|
1948
|
+
return merge ? { ...item, ...newItem } : newItem;
|
|
1949
|
+
}
|
|
1950
|
+
return item;
|
|
1951
|
+
});
|
|
1952
|
+
}
|
|
1953
|
+
function removeItemById(items, newItem) {
|
|
1954
|
+
return items.filter((item) => {
|
|
1955
|
+
return item._id !== newItem._id;
|
|
1956
|
+
});
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
// src/index.ts
|
|
1960
|
+
var index_default = { createAdapter };
|
|
1961
|
+
export {
|
|
1962
|
+
DataService,
|
|
1963
|
+
Model,
|
|
1964
|
+
ModelService,
|
|
1965
|
+
Service,
|
|
1966
|
+
ServiceError,
|
|
1967
|
+
createAdapter,
|
|
1968
|
+
index_default as default,
|
|
1969
|
+
removeItemById,
|
|
1970
|
+
replaceItemById,
|
|
1971
|
+
wrapLazyPromise
|
|
1972
|
+
};
|