@diphyx/harlemify 1.0.4 → 3.0.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.
@@ -2,30 +2,95 @@ import { defu } from "defu";
2
2
  import { createStore as createHarlemStore } from "@harlem/core";
3
3
  import { createApi } from "./api.js";
4
4
  import { sharedConfig } from "../shared.js";
5
- import { resolveSchema } from "../utils/schema.js";
5
+ import { createCache } from "../utils/cache.js";
6
+ import { defineApiAdapter } from "../utils/adapter.js";
6
7
  import { pluralize } from "../utils/transform.js";
7
- import { Endpoint, EndpointStatus, getEndpoint, makeEndpointStatusName, resolveEndpointUrl } from "../utils/endpoint.js";
8
- export var StoreMemoryAction = /* @__PURE__ */ ((StoreMemoryAction2) => {
9
- StoreMemoryAction2["SET"] = "set";
10
- StoreMemoryAction2["EDIT"] = "edit";
11
- StoreMemoryAction2["DROP"] = "drop";
12
- return StoreMemoryAction2;
13
- })(StoreMemoryAction || {});
14
- export var StoreMemoryPosition = /* @__PURE__ */ ((StoreMemoryPosition2) => {
15
- StoreMemoryPosition2["FIRST"] = "first";
16
- StoreMemoryPosition2["LAST"] = "last";
17
- return StoreMemoryPosition2;
18
- })(StoreMemoryPosition || {});
19
- export function createStore(entity, schema, endpoints, options) {
20
- const { indicator } = resolveSchema(schema, {
8
+ import { resolveSchema } from "../utils/schema.js";
9
+ import { EndpointMethod, EndpointStatus, resolveEndpointUrl } from "../utils/endpoint.js";
10
+ function getDefaultMutation(method, target) {
11
+ const defaults = {
12
+ [EndpointMethod.GET]: {
13
+ unit: "set",
14
+ units: "set"
15
+ },
16
+ [EndpointMethod.POST]: {
17
+ unit: "set",
18
+ units: "add"
19
+ },
20
+ [EndpointMethod.PUT]: {
21
+ unit: "set",
22
+ units: "edit"
23
+ },
24
+ [EndpointMethod.PATCH]: {
25
+ unit: "edit",
26
+ units: "edit"
27
+ },
28
+ [EndpointMethod.DELETE]: {
29
+ unit: "drop",
30
+ units: "drop"
31
+ }
32
+ };
33
+ return defaults[method][target];
34
+ }
35
+ function setNestedValue(object, path, value) {
36
+ if (path.length === 0) {
37
+ return;
38
+ }
39
+ let current = object;
40
+ for (let index = 0; index < path.length - 1; index++) {
41
+ if (current[path[index]] === void 0) {
42
+ current[path[index]] = {};
43
+ }
44
+ current = current[path[index]];
45
+ }
46
+ current[path[path.length - 1]] = value;
47
+ }
48
+ function editNestedValue(object, path, value, deep) {
49
+ if (path.length === 0) {
50
+ return;
51
+ }
52
+ let current = object;
53
+ for (let index = 0; index < path.length - 1; index++) {
54
+ if (current[path[index]] === void 0) {
55
+ return;
56
+ }
57
+ current = current[path[index]];
58
+ }
59
+ const key = path[path.length - 1];
60
+ if (current[key] === void 0) {
61
+ current[key] = value;
62
+ return;
63
+ }
64
+ if (deep) {
65
+ current[key] = defu(value, current[key]);
66
+ return;
67
+ }
68
+ Object.assign(current[key], value);
69
+ }
70
+ export function createStore(entity, schema, actions, options) {
71
+ const resolvedSchema = resolveSchema(schema, {
21
72
  indicator: options?.indicator
22
73
  });
74
+ const indicator = resolvedSchema.indicator;
75
+ const indexCache = createCache();
76
+ const schemaCache = createCache();
77
+ for (const actionName in actions) {
78
+ const actionSchema = resolveSchema(schema, {
79
+ indicator,
80
+ action: actionName
81
+ });
82
+ schemaCache.set(actionName, {
83
+ keys: actionSchema.keys,
84
+ fields: Object.keys(actionSchema.keys)
85
+ });
86
+ }
23
87
  let apiClient;
24
88
  function api() {
25
89
  if (!apiClient) {
26
90
  apiClient = createApi({
27
- ...sharedConfig.api ?? {},
28
- ...options?.api
91
+ headers: sharedConfig.api?.headers,
92
+ query: sharedConfig.api?.query,
93
+ adapter: options?.adapter ?? defineApiAdapter(sharedConfig.api?.adapter)
29
94
  });
30
95
  }
31
96
  return apiClient;
@@ -35,8 +100,11 @@ export function createStore(entity, schema, endpoints, options) {
35
100
  unit: null,
36
101
  units: []
37
102
  },
38
- endpoints: {}
103
+ status: {}
39
104
  };
105
+ for (const actionName in actions) {
106
+ state.status[actionName] = EndpointStatus.IDLE;
107
+ }
40
108
  const store = createHarlemStore(entity, state, {
41
109
  extensions: options?.extensions ?? []
42
110
  });
@@ -55,246 +123,459 @@ export function createStore(entity, schema, endpoints, options) {
55
123
  });
56
124
  const setMemorizedUnits = store.mutation("setMemorizedUnits", (state2, units = []) => {
57
125
  state2.memory.units = units;
58
- });
59
- const editMemorizedUnit = store.mutation("editMemorizedUnit", (state2, unit) => {
60
- if (state2.memory.unit?.[indicator] === unit[indicator]) {
61
- state2.memory.unit = defu(unit, state2.memory.unit);
126
+ indexCache.clear();
127
+ if (units && Array.isArray(units)) {
128
+ for (let index = 0; index < units.length; index++) {
129
+ if (units[index]) {
130
+ indexCache.set(units[index][indicator], index);
131
+ }
132
+ }
62
133
  }
63
134
  });
64
- const editMemorizedUnits = store.mutation("editMemorizedUnits", (state2, units) => {
65
- for (const unit of units) {
66
- const index = state2.memory.units.findIndex((memorizedUnit2) => {
67
- return memorizedUnit2[indicator] === unit[indicator];
68
- });
69
- if (index !== -1) {
70
- state2.memory.units[index] = defu(unit, state2.memory.units[index]);
135
+ const editMemorizedUnit = store.mutation(
136
+ "editMemorizedUnit",
137
+ (state2, payload) => {
138
+ if (state2.memory.unit?.[indicator] !== payload.unit[indicator]) {
139
+ return;
140
+ }
141
+ if (payload.deep) {
142
+ state2.memory.unit = defu(payload.unit, state2.memory.unit);
143
+ return;
71
144
  }
145
+ Object.assign(state2.memory.unit, payload.unit);
72
146
  }
73
- });
147
+ );
148
+ const editMemorizedUnits = store.mutation(
149
+ "editMemorizedUnits",
150
+ (state2, payload) => {
151
+ const tempIndex = /* @__PURE__ */ new Map();
152
+ for (let index = 0; index < state2.memory.units.length; index++) {
153
+ tempIndex.set(state2.memory.units[index][indicator], index);
154
+ }
155
+ for (const unit of payload.units) {
156
+ let unitIndex = indexCache.get(unit[indicator]);
157
+ if (unitIndex === void 0 || unitIndex >= state2.memory.units.length || state2.memory.units[unitIndex]?.[indicator] !== unit[indicator]) {
158
+ const foundIndex = tempIndex.get(unit[indicator]);
159
+ if (foundIndex !== void 0) {
160
+ unitIndex = foundIndex;
161
+ indexCache.set(unit[indicator], foundIndex);
162
+ } else {
163
+ unitIndex = void 0;
164
+ }
165
+ }
166
+ if (unitIndex === void 0) {
167
+ continue;
168
+ }
169
+ if (payload.deep) {
170
+ state2.memory.units[unitIndex] = defu(unit, state2.memory.units[unitIndex]);
171
+ continue;
172
+ }
173
+ Object.assign(state2.memory.units[unitIndex], unit);
174
+ }
175
+ }
176
+ );
74
177
  const dropMemorizedUnit = store.mutation("dropMemorizedUnit", (state2, unit) => {
75
178
  if (state2.memory.unit?.[indicator] === unit[indicator]) {
76
179
  state2.memory.unit = null;
77
180
  }
78
181
  });
79
- const dropMemorizedUnits = store.mutation("dropMemorizedUnits", (state2, units) => {
80
- state2.memory.units = state2.memory.units.filter((memorizedUnit2) => {
182
+ const dropMemorizedUnits = store.mutation(
183
+ "dropMemorizedUnits",
184
+ (state2, units) => {
185
+ const dropSet = new Set(
186
+ units.map((unit) => {
187
+ return unit[indicator];
188
+ })
189
+ );
81
190
  for (const unit of units) {
82
- if (memorizedUnit2[indicator] === unit[indicator]) {
83
- return false;
84
- }
191
+ indexCache.delete(unit[indicator]);
85
192
  }
86
- return true;
87
- });
88
- });
89
- const monitor = {};
90
- for (const endpoint of Object.values(Endpoint)) {
91
- for (const endpointStatus of Object.values(EndpointStatus)) {
92
- const statusKey = makeEndpointStatusName(endpoint, endpointStatus);
93
- monitor[statusKey] = store.getter(statusKey, (state2) => {
94
- return state2.endpoints[endpoint] === endpointStatus;
193
+ state2.memory.units = state2.memory.units.filter((memorizedUnit2) => {
194
+ return !dropSet.has(memorizedUnit2[indicator]);
95
195
  });
96
196
  }
97
- }
98
- const patchMonitor = store.mutation(
99
- "patchMonitor",
197
+ );
198
+ const addMemorizedUnits = store.mutation(
199
+ "addMemorizedUnits",
100
200
  (state2, payload) => {
101
- state2.endpoints[payload.endpoint] = payload.status;
201
+ if (payload.prepend) {
202
+ indexCache.clear();
203
+ for (let index = 0; index < payload.units.length; index++) {
204
+ indexCache.set(payload.units[index][indicator], index);
205
+ }
206
+ for (let index = 0; index < state2.memory.units.length; index++) {
207
+ indexCache.set(state2.memory.units[index][indicator], index + payload.units.length);
208
+ }
209
+ state2.memory.units = [...payload.units, ...state2.memory.units];
210
+ return;
211
+ }
212
+ for (let index = 0; index < payload.units.length; index++) {
213
+ indexCache.set(payload.units[index][indicator], state2.memory.units.length + index);
214
+ }
215
+ state2.memory.units = [...state2.memory.units, ...payload.units];
102
216
  }
103
217
  );
104
- function patchMonitorTo(endpoint, status) {
105
- if (status === EndpointStatus.PENDING) {
106
- const statusKey = makeEndpointStatusName(endpoint, EndpointStatus.PENDING);
107
- if (monitor[statusKey].value) {
108
- throw new Error(`Endpoint "${endpoint}" is already pending`);
218
+ const setNestedUnit = store.mutation("setNestedUnit", (state2, payload) => {
219
+ if (!state2.memory.unit) {
220
+ return;
221
+ }
222
+ setNestedValue(state2.memory.unit, payload.path, payload.value);
223
+ });
224
+ const editNestedUnit = store.mutation(
225
+ "editNestedUnit",
226
+ (state2, payload) => {
227
+ if (!state2.memory.unit) {
228
+ return;
109
229
  }
230
+ editNestedValue(state2.memory.unit, payload.path, payload.value, payload.deep);
110
231
  }
111
- patchMonitor({
112
- endpoint,
113
- status
114
- });
232
+ );
233
+ const dropNestedUnit = store.mutation("dropNestedUnit", (state2, payload) => {
234
+ if (!state2.memory.unit || payload.path.length === 0) {
235
+ return;
236
+ }
237
+ setNestedValue(state2.memory.unit, payload.path, null);
238
+ });
239
+ const patchStatus = store.mutation(
240
+ "patchStatus",
241
+ (state2, payload) => {
242
+ state2.status[payload.action] = payload.status;
243
+ }
244
+ );
245
+ function createActionStatus(actionName) {
246
+ const current = store.getter(
247
+ `${actionName}:current`,
248
+ (state2) => state2.status[actionName] ?? EndpointStatus.IDLE
249
+ );
250
+ const pending = store.getter(
251
+ `${actionName}:pending`,
252
+ (state2) => state2.status[actionName] === EndpointStatus.PENDING
253
+ );
254
+ const success = store.getter(
255
+ `${actionName}:success`,
256
+ (state2) => state2.status[actionName] === EndpointStatus.SUCCESS
257
+ );
258
+ const failed = store.getter(
259
+ `${actionName}:failed`,
260
+ (state2) => state2.status[actionName] === EndpointStatus.FAILED
261
+ );
262
+ const idle = store.getter(
263
+ `${actionName}:idle`,
264
+ (state2) => state2.status[actionName] === EndpointStatus.IDLE
265
+ );
266
+ return {
267
+ current() {
268
+ return current.value;
269
+ },
270
+ pending() {
271
+ return pending.value;
272
+ },
273
+ success() {
274
+ return success.value;
275
+ },
276
+ failed() {
277
+ return failed.value;
278
+ },
279
+ idle() {
280
+ return idle.value;
281
+ }
282
+ };
283
+ }
284
+ const monitor = {};
285
+ for (const actionName in actions) {
286
+ monitor[actionName] = createActionStatus(actionName);
115
287
  }
116
- async function withStatus(key, operation) {
288
+ async function withStatus(actionName, operation) {
117
289
  await options?.hooks?.before?.();
118
- patchMonitorTo(key, EndpointStatus.PENDING);
290
+ if (store.state.status[actionName] === EndpointStatus.PENDING) {
291
+ throw new Error(`Action "${actionName}" is already pending`);
292
+ }
293
+ patchStatus({
294
+ action: actionName,
295
+ status: EndpointStatus.PENDING
296
+ });
119
297
  try {
120
298
  const result = await operation();
121
- patchMonitorTo(key, EndpointStatus.SUCCESS);
299
+ patchStatus({
300
+ action: actionName,
301
+ status: EndpointStatus.SUCCESS
302
+ });
122
303
  await options?.hooks?.after?.();
123
304
  return result;
124
305
  } catch (error) {
125
- patchMonitorTo(key, EndpointStatus.FAILED);
306
+ patchStatus({
307
+ action: actionName,
308
+ status: EndpointStatus.FAILED
309
+ });
126
310
  await options?.hooks?.after?.(error);
127
311
  throw error;
128
312
  }
129
313
  }
130
- async function getUnitEndpoint(unit, options2) {
131
- const endpoint = getEndpoint(endpoints, Endpoint.GET_UNIT);
132
- return withStatus(Endpoint.GET_UNIT, async () => {
133
- const response = await api().get(resolveEndpointUrl(endpoint, unit), options2);
134
- setMemorizedUnit(response);
135
- return response;
136
- });
137
- }
138
- async function getUnitsEndpoint(options2) {
139
- const endpoint = getEndpoint(endpoints, Endpoint.GET_UNITS);
140
- return withStatus(Endpoint.GET_UNITS, async () => {
141
- const response = await api().get(resolveEndpointUrl(endpoint), options2);
142
- setMemorizedUnits(response);
143
- return response;
144
- });
145
- }
146
- async function postUnitEndpoint(unit, actionOptions) {
147
- const endpoint = getEndpoint(endpoints, Endpoint.POST_UNIT);
148
- const resolvedSchema = resolveSchema(schema, { indicator, endpoint, unit });
149
- if (actionOptions?.validate) {
150
- schema.pick(resolvedSchema.keys).parse(unit);
314
+ function applyMemoryOperation(memoryDefinition, method, response, params) {
315
+ if (!memoryDefinition) {
316
+ return;
151
317
  }
152
- return withStatus(Endpoint.POST_UNIT, async () => {
153
- const response = await api().post(resolveEndpointUrl(endpoint, unit), {
154
- ...actionOptions,
155
- body: actionOptions?.body ?? resolvedSchema.values
156
- });
157
- setMemorizedUnit({ ...unit, ...response });
158
- return response;
159
- });
160
- }
161
- async function postUnitsEndpoint(units, options2) {
162
- const endpoint = getEndpoint(endpoints, Endpoint.POST_UNITS);
163
- return withStatus(Endpoint.POST_UNITS, async () => {
164
- const responses = [];
165
- for (const unit of units) {
166
- const resolvedSchema = resolveSchema(schema, { indicator, endpoint, unit });
167
- if (options2?.validate) {
168
- schema.pick(resolvedSchema.keys).parse(unit);
318
+ const mutation = memoryDefinition.mutation ?? getDefaultMutation(method, memoryDefinition.on);
319
+ if (memoryDefinition.on === "unit") {
320
+ if (memoryDefinition.path.length > 0) {
321
+ switch (mutation) {
322
+ case "set": {
323
+ setNestedUnit({
324
+ path: memoryDefinition.path,
325
+ value: response
326
+ });
327
+ break;
328
+ }
329
+ case "edit": {
330
+ editNestedUnit({
331
+ path: memoryDefinition.path,
332
+ value: response,
333
+ deep: memoryDefinition.deep
334
+ });
335
+ break;
336
+ }
337
+ case "drop": {
338
+ dropNestedUnit({
339
+ path: memoryDefinition.path
340
+ });
341
+ break;
342
+ }
169
343
  }
170
- const response = await api().post(resolveEndpointUrl(endpoint, unit), {
171
- ...options2,
172
- body: options2?.body ?? resolvedSchema.values
173
- });
174
- const clonedUnits = [...memorizedUnits.value];
175
- if (options2?.position === "last" /* LAST */) {
176
- clonedUnits.push({ ...unit, ...response });
177
- } else {
178
- clonedUnits.unshift({ ...unit, ...response });
344
+ return;
345
+ }
346
+ switch (mutation) {
347
+ case "set": {
348
+ setMemorizedUnit(response);
349
+ break;
350
+ }
351
+ case "edit": {
352
+ const editData = {
353
+ ...params,
354
+ ...response
355
+ };
356
+ editMemorizedUnit({
357
+ unit: editData,
358
+ deep: memoryDefinition.deep
359
+ });
360
+ editMemorizedUnits({
361
+ units: [editData],
362
+ deep: memoryDefinition.deep
363
+ });
364
+ break;
365
+ }
366
+ case "drop": {
367
+ dropMemorizedUnit(params);
368
+ break;
179
369
  }
180
- setMemorizedUnits(clonedUnits);
181
- responses.push(response);
182
370
  }
183
- return responses;
184
- });
185
- }
186
- async function putUnitEndpoint(unit, options2) {
187
- const endpoint = getEndpoint(endpoints, Endpoint.PUT_UNIT);
188
- const resolvedSchema = resolveSchema(schema, { indicator, endpoint, unit });
189
- if (options2?.validate) {
190
- schema.pick(resolvedSchema.keys).parse(unit);
371
+ return;
191
372
  }
192
- return withStatus(Endpoint.PUT_UNIT, async () => {
193
- const response = await api().put(resolveEndpointUrl(endpoint, unit), {
194
- ...options2,
195
- body: options2?.body ?? resolvedSchema.values
196
- });
197
- setMemorizedUnit({ ...unit, ...response });
198
- return response;
199
- });
200
- }
201
- async function putUnitsEndpoint(units, options2) {
202
- const endpoint = getEndpoint(endpoints, Endpoint.PUT_UNITS);
203
- return withStatus(Endpoint.PUT_UNITS, async () => {
204
- const responses = [];
205
- for (const unit of units) {
206
- const resolvedSchema = resolveSchema(schema, { indicator, endpoint, unit });
207
- if (options2?.validate) {
208
- schema.pick(resolvedSchema.keys).parse(unit);
373
+ if (Array.isArray(response)) {
374
+ switch (mutation) {
375
+ case "set": {
376
+ setMemorizedUnits(response);
377
+ break;
378
+ }
379
+ case "edit": {
380
+ editMemorizedUnits({
381
+ units: response.map((item) => {
382
+ return {
383
+ ...params,
384
+ ...item
385
+ };
386
+ }),
387
+ deep: memoryDefinition.deep
388
+ });
389
+ break;
390
+ }
391
+ case "drop": {
392
+ if (params) {
393
+ dropMemorizedUnits([params]);
394
+ break;
395
+ }
396
+ dropMemorizedUnits(response);
397
+ break;
209
398
  }
210
- const response = await api().put(resolveEndpointUrl(endpoint, unit), {
211
- ...options2,
212
- body: options2?.body ?? resolvedSchema.values
399
+ case "add": {
400
+ addMemorizedUnits({
401
+ units: response,
402
+ prepend: memoryDefinition.prepend
403
+ });
404
+ break;
405
+ }
406
+ }
407
+ return;
408
+ }
409
+ switch (mutation) {
410
+ case "set": {
411
+ setMemorizedUnits([response]);
412
+ break;
413
+ }
414
+ case "edit": {
415
+ editMemorizedUnits({
416
+ units: [
417
+ {
418
+ ...params,
419
+ ...response
420
+ }
421
+ ],
422
+ deep: memoryDefinition.deep
213
423
  });
214
- editMemorizedUnits([{ ...unit, ...response }]);
215
- responses.push(response);
424
+ break;
216
425
  }
217
- return responses;
218
- });
426
+ case "drop": {
427
+ if (params) {
428
+ dropMemorizedUnits([params]);
429
+ break;
430
+ }
431
+ dropMemorizedUnits([response]);
432
+ break;
433
+ }
434
+ case "add": {
435
+ addMemorizedUnits({
436
+ units: [response],
437
+ prepend: memoryDefinition.prepend
438
+ });
439
+ break;
440
+ }
441
+ }
219
442
  }
220
- async function patchUnitEndpoint(unit, options2) {
221
- const endpoint = getEndpoint(endpoints, Endpoint.PATCH_UNIT);
222
- const resolvedSchema = resolveSchema(schema, { indicator, endpoint, unit });
223
- if (options2?.validate) {
224
- schema.pick(resolvedSchema.keys).partial().parse(unit);
443
+ function resolveRequestBody(actionName, params, actionOptions) {
444
+ if (actionOptions?.body) {
445
+ return actionOptions.body;
225
446
  }
226
- return withStatus(Endpoint.PATCH_UNIT, async () => {
227
- const response = await api().patch(resolveEndpointUrl(endpoint, unit), {
228
- ...options2,
229
- body: options2?.body ?? resolvedSchema.values
230
- });
231
- editMemorizedUnit({ ...unit, ...response });
232
- return response;
233
- });
447
+ const cached = schemaCache.get(actionName);
448
+ if (cached && cached.fields.length > 0 && params) {
449
+ return cached.fields.reduce((accumulator, key) => {
450
+ if (key in params) {
451
+ accumulator[key] = params[key];
452
+ }
453
+ return accumulator;
454
+ }, {});
455
+ }
456
+ return params;
234
457
  }
235
- async function patchUnitsEndpoint(units, options2) {
236
- const endpoint = getEndpoint(endpoints, Endpoint.PATCH_UNITS);
237
- return withStatus(Endpoint.PATCH_UNITS, async () => {
238
- const responses = [];
239
- for (const unit of units) {
240
- const resolvedSchema = resolveSchema(schema, { indicator, endpoint, unit });
241
- if (options2?.validate) {
242
- schema.pick(resolvedSchema.keys).partial().parse(unit);
458
+ function validateRequestBody(actionName, params, partial) {
459
+ const cached = schemaCache.get(actionName);
460
+ if (!cached || cached.fields.length === 0) {
461
+ return;
462
+ }
463
+ if (partial) {
464
+ schema.pick(cached.keys).partial().parse(params);
465
+ return;
466
+ }
467
+ schema.pick(cached.keys).parse(params);
468
+ }
469
+ function createActionFunction(actionName, actionDefinition) {
470
+ return async (params, actionOptions) => {
471
+ return withStatus(actionName, async () => {
472
+ const url = resolveEndpointUrl(actionDefinition.endpoint, params);
473
+ const body = resolveRequestBody(actionName, params, actionOptions);
474
+ const adapter = actionOptions?.adapter ?? actionDefinition.endpoint.adapter;
475
+ const baseOptions = {
476
+ query: actionOptions?.query,
477
+ headers: actionOptions?.headers,
478
+ signal: actionOptions?.signal,
479
+ ...adapter && { adapter }
480
+ };
481
+ let response;
482
+ switch (actionDefinition.endpoint.method) {
483
+ case EndpointMethod.GET: {
484
+ response = await api().get(url, baseOptions);
485
+ break;
486
+ }
487
+ case EndpointMethod.POST: {
488
+ if (actionOptions?.validate) {
489
+ validateRequestBody(actionName, params);
490
+ }
491
+ response = await api().post(url, {
492
+ ...baseOptions,
493
+ body
494
+ });
495
+ break;
496
+ }
497
+ case EndpointMethod.PUT: {
498
+ if (actionOptions?.validate) {
499
+ validateRequestBody(actionName, params);
500
+ }
501
+ response = await api().put(url, {
502
+ ...baseOptions,
503
+ body
504
+ });
505
+ break;
506
+ }
507
+ case EndpointMethod.PATCH: {
508
+ if (actionOptions?.validate) {
509
+ validateRequestBody(actionName, params, true);
510
+ }
511
+ response = await api().patch(url, {
512
+ ...baseOptions,
513
+ body
514
+ });
515
+ break;
516
+ }
517
+ case EndpointMethod.DELETE: {
518
+ await api().del(url, baseOptions);
519
+ response = true;
520
+ break;
521
+ }
243
522
  }
244
- const response = await api().patch(resolveEndpointUrl(endpoint, unit), {
245
- ...options2,
246
- body: options2?.body ?? resolvedSchema.values
247
- });
248
- editMemorizedUnits([{ ...unit, ...response }]);
249
- responses.push(response);
250
- }
251
- return responses;
252
- });
523
+ applyMemoryOperation(actionDefinition.memory, actionDefinition.endpoint.method, response, params);
524
+ return response;
525
+ });
526
+ };
253
527
  }
254
- async function deleteUnitEndpoint(unit, options2) {
255
- const endpoint = getEndpoint(endpoints, Endpoint.DELETE_UNIT);
256
- return withStatus(Endpoint.DELETE_UNIT, async () => {
257
- await api().del(resolveEndpointUrl(endpoint, unit), options2);
258
- dropMemorizedUnit(unit);
259
- return true;
260
- });
528
+ const storeActions = {};
529
+ for (const actionName in actions) {
530
+ storeActions[actionName] = createActionFunction(actionName, actions[actionName]);
261
531
  }
262
- async function deleteUnitsEndpoint(units, options2) {
263
- const endpoint = getEndpoint(endpoints, Endpoint.DELETE_UNITS);
264
- return withStatus(Endpoint.DELETE_UNITS, async () => {
265
- for (const unit of units) {
266
- await api().del(resolveEndpointUrl(endpoint, unit), options2);
267
- dropMemorizedUnits([unit]);
532
+ const memory = {
533
+ set(data) {
534
+ if (Array.isArray(data)) {
535
+ setMemorizedUnits(data);
536
+ return;
268
537
  }
269
- return true;
270
- });
271
- }
538
+ setMemorizedUnit(data);
539
+ },
540
+ edit(data, editOptions) {
541
+ if (Array.isArray(data)) {
542
+ editMemorizedUnit({
543
+ unit: data[0],
544
+ deep: editOptions?.deep
545
+ });
546
+ editMemorizedUnits({
547
+ units: data,
548
+ deep: editOptions?.deep
549
+ });
550
+ return;
551
+ }
552
+ editMemorizedUnit({
553
+ unit: data,
554
+ deep: editOptions?.deep
555
+ });
556
+ editMemorizedUnits({
557
+ units: [data],
558
+ deep: editOptions?.deep
559
+ });
560
+ },
561
+ drop(data) {
562
+ if (Array.isArray(data)) {
563
+ dropMemorizedUnit(data[0]);
564
+ dropMemorizedUnits(data);
565
+ return;
566
+ }
567
+ dropMemorizedUnit(data);
568
+ dropMemorizedUnits([data]);
569
+ }
570
+ };
272
571
  return {
273
572
  store,
274
573
  alias,
275
574
  indicator,
276
575
  unit: memorizedUnit,
277
576
  units: memorizedUnits,
278
- memory: {
279
- setUnit: setMemorizedUnit,
280
- setUnits: setMemorizedUnits,
281
- editUnit: editMemorizedUnit,
282
- editUnits: editMemorizedUnits,
283
- dropUnit: dropMemorizedUnit,
284
- dropUnits: dropMemorizedUnits
285
- },
286
- endpoint: {
287
- getUnit: getUnitEndpoint,
288
- getUnits: getUnitsEndpoint,
289
- postUnit: postUnitEndpoint,
290
- postUnits: postUnitsEndpoint,
291
- putUnit: putUnitEndpoint,
292
- putUnits: putUnitsEndpoint,
293
- patchUnit: patchUnitEndpoint,
294
- patchUnits: patchUnitsEndpoint,
295
- deleteUnit: deleteUnitEndpoint,
296
- deleteUnits: deleteUnitsEndpoint
297
- },
577
+ action: storeActions,
578
+ memory,
298
579
  monitor
299
580
  };
300
581
  }