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