@shopware-ag/app-server-sdk 1.1.0 → 1.1.2

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.
Files changed (117) hide show
  1. package/dist/commonjs/helper/admin-api.d.ts +56 -0
  2. package/dist/commonjs/helper/admin-api.d.ts.map +1 -0
  3. package/dist/commonjs/helper/admin-api.js +126 -0
  4. package/dist/commonjs/helper/admin-api.js.map +1 -0
  5. package/dist/commonjs/helper/criteria.d.ts +377 -0
  6. package/dist/commonjs/helper/criteria.d.ts.map +1 -0
  7. package/dist/commonjs/helper/criteria.js +456 -0
  8. package/dist/commonjs/helper/criteria.js.map +1 -0
  9. package/dist/commonjs/http-client.js +2 -1
  10. package/dist/commonjs/http-client.js.map +1 -1
  11. package/dist/commonjs/integration/better-sqlite3.d.ts +11 -0
  12. package/dist/commonjs/integration/better-sqlite3.d.ts.map +1 -0
  13. package/dist/commonjs/integration/better-sqlite3.js +58 -0
  14. package/dist/commonjs/integration/better-sqlite3.js.map +1 -0
  15. package/dist/esm/helper/admin-api.d.ts +56 -0
  16. package/dist/esm/helper/admin-api.d.ts.map +1 -0
  17. package/dist/esm/helper/admin-api.js +117 -0
  18. package/dist/esm/helper/admin-api.js.map +1 -0
  19. package/dist/esm/helper/criteria.d.ts +377 -0
  20. package/dist/esm/helper/criteria.d.ts.map +1 -0
  21. package/dist/esm/helper/criteria.js +452 -0
  22. package/dist/esm/helper/criteria.js.map +1 -0
  23. package/dist/esm/http-client.js +2 -1
  24. package/dist/esm/http-client.js.map +1 -1
  25. package/dist/esm/integration/better-sqlite3.d.ts +11 -0
  26. package/dist/esm/integration/better-sqlite3.d.ts.map +1 -0
  27. package/dist/esm/integration/better-sqlite3.js +51 -0
  28. package/dist/esm/integration/better-sqlite3.js.map +1 -0
  29. package/package.json +206 -141
  30. package/dist/commonjs/context-resolver.test.d.ts +0 -2
  31. package/dist/commonjs/context-resolver.test.d.ts.map +0 -1
  32. package/dist/commonjs/context-resolver.test.js +0 -81
  33. package/dist/commonjs/context-resolver.test.js.map +0 -1
  34. package/dist/commonjs/helper/app-actions.test.d.ts +0 -2
  35. package/dist/commonjs/helper/app-actions.test.d.ts.map +0 -1
  36. package/dist/commonjs/helper/app-actions.test.js +0 -31
  37. package/dist/commonjs/helper/app-actions.test.js.map +0 -1
  38. package/dist/commonjs/http-client.test.d.ts +0 -2
  39. package/dist/commonjs/http-client.test.d.ts.map +0 -1
  40. package/dist/commonjs/http-client.test.js +0 -71
  41. package/dist/commonjs/http-client.test.js.map +0 -1
  42. package/dist/commonjs/integration/bun-sqlite.test.d.ts +0 -2
  43. package/dist/commonjs/integration/bun-sqlite.test.d.ts.map +0 -1
  44. package/dist/commonjs/integration/bun-sqlite.test.js +0 -24
  45. package/dist/commonjs/integration/bun-sqlite.test.js.map +0 -1
  46. package/dist/commonjs/integration/cloudflare.test.d.ts +0 -2
  47. package/dist/commonjs/integration/cloudflare.test.d.ts.map +0 -1
  48. package/dist/commonjs/integration/cloudflare.test.js +0 -39
  49. package/dist/commonjs/integration/cloudflare.test.js.map +0 -1
  50. package/dist/commonjs/integration/deno.test.d.ts +0 -2
  51. package/dist/commonjs/integration/deno.test.d.ts.map +0 -1
  52. package/dist/commonjs/integration/deno.test.js +0 -48
  53. package/dist/commonjs/integration/deno.test.js.map +0 -1
  54. package/dist/commonjs/integration/dynamodb.test.d.ts +0 -2
  55. package/dist/commonjs/integration/dynamodb.test.d.ts.map +0 -1
  56. package/dist/commonjs/integration/dynamodb.test.js +0 -132
  57. package/dist/commonjs/integration/dynamodb.test.js.map +0 -1
  58. package/dist/commonjs/integration/hono.test.d.ts +0 -2
  59. package/dist/commonjs/integration/hono.test.d.ts.map +0 -1
  60. package/dist/commonjs/integration/hono.test.js +0 -86
  61. package/dist/commonjs/integration/hono.test.js.map +0 -1
  62. package/dist/commonjs/registration.test.d.ts +0 -2
  63. package/dist/commonjs/registration.test.d.ts.map +0 -1
  64. package/dist/commonjs/registration.test.js +0 -59
  65. package/dist/commonjs/registration.test.js.map +0 -1
  66. package/dist/commonjs/repository.test.d.ts +0 -2
  67. package/dist/commonjs/repository.test.d.ts.map +0 -1
  68. package/dist/commonjs/repository.test.js +0 -32
  69. package/dist/commonjs/repository.test.js.map +0 -1
  70. package/dist/commonjs/signer.test.d.ts +0 -2
  71. package/dist/commonjs/signer.test.d.ts.map +0 -1
  72. package/dist/commonjs/signer.test.js +0 -25
  73. package/dist/commonjs/signer.test.js.map +0 -1
  74. package/dist/esm/context-resolver.test.d.ts +0 -2
  75. package/dist/esm/context-resolver.test.d.ts.map +0 -1
  76. package/dist/esm/context-resolver.test.js +0 -79
  77. package/dist/esm/context-resolver.test.js.map +0 -1
  78. package/dist/esm/helper/app-actions.test.d.ts +0 -2
  79. package/dist/esm/helper/app-actions.test.d.ts.map +0 -1
  80. package/dist/esm/helper/app-actions.test.js +0 -29
  81. package/dist/esm/helper/app-actions.test.js.map +0 -1
  82. package/dist/esm/http-client.test.d.ts +0 -2
  83. package/dist/esm/http-client.test.d.ts.map +0 -1
  84. package/dist/esm/http-client.test.js +0 -69
  85. package/dist/esm/http-client.test.js.map +0 -1
  86. package/dist/esm/integration/bun-sqlite.test.d.ts +0 -2
  87. package/dist/esm/integration/bun-sqlite.test.d.ts.map +0 -1
  88. package/dist/esm/integration/bun-sqlite.test.js +0 -22
  89. package/dist/esm/integration/bun-sqlite.test.js.map +0 -1
  90. package/dist/esm/integration/cloudflare.test.d.ts +0 -2
  91. package/dist/esm/integration/cloudflare.test.d.ts.map +0 -1
  92. package/dist/esm/integration/cloudflare.test.js +0 -37
  93. package/dist/esm/integration/cloudflare.test.js.map +0 -1
  94. package/dist/esm/integration/deno.test.d.ts +0 -2
  95. package/dist/esm/integration/deno.test.d.ts.map +0 -1
  96. package/dist/esm/integration/deno.test.js +0 -46
  97. package/dist/esm/integration/deno.test.js.map +0 -1
  98. package/dist/esm/integration/dynamodb.test.d.ts +0 -2
  99. package/dist/esm/integration/dynamodb.test.d.ts.map +0 -1
  100. package/dist/esm/integration/dynamodb.test.js +0 -130
  101. package/dist/esm/integration/dynamodb.test.js.map +0 -1
  102. package/dist/esm/integration/hono.test.d.ts +0 -2
  103. package/dist/esm/integration/hono.test.d.ts.map +0 -1
  104. package/dist/esm/integration/hono.test.js +0 -84
  105. package/dist/esm/integration/hono.test.js.map +0 -1
  106. package/dist/esm/registration.test.d.ts +0 -2
  107. package/dist/esm/registration.test.d.ts.map +0 -1
  108. package/dist/esm/registration.test.js +0 -57
  109. package/dist/esm/registration.test.js.map +0 -1
  110. package/dist/esm/repository.test.d.ts +0 -2
  111. package/dist/esm/repository.test.d.ts.map +0 -1
  112. package/dist/esm/repository.test.js +0 -30
  113. package/dist/esm/repository.test.js.map +0 -1
  114. package/dist/esm/signer.test.d.ts +0 -2
  115. package/dist/esm/signer.test.d.ts.map +0 -1
  116. package/dist/esm/signer.test.js +0 -23
  117. package/dist/esm/signer.test.js.map +0 -1
@@ -0,0 +1,456 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Criteria = exports.TotalCountMode = void 0;
4
+ var TotalCountMode;
5
+ (function (TotalCountMode) {
6
+ /* No total count will be selected. Should be used if no pagination required (fastest) */
7
+ TotalCountMode[TotalCountMode["NO_TOTAL_COUNT"] = 0] = "NO_TOTAL_COUNT";
8
+ /* Exact total count will be selected. Should be used if an exact pagination is required (slow) */
9
+ TotalCountMode[TotalCountMode["EXACT_TOTAL_COUNT"] = 1] = "EXACT_TOTAL_COUNT";
10
+ /* Fetches limit * 5 + 1. Should be used if pagination can work with "next page exists" (fast) */
11
+ TotalCountMode[TotalCountMode["PAGINATION_TOTAL_COUNT"] = 2] = "PAGINATION_TOTAL_COUNT";
12
+ })(TotalCountMode || (exports.TotalCountMode = TotalCountMode = {}));
13
+ class Criteria {
14
+ title;
15
+ page;
16
+ limit;
17
+ term;
18
+ filters;
19
+ ids;
20
+ queries;
21
+ associations;
22
+ postFilter;
23
+ sortings;
24
+ aggregations;
25
+ grouping;
26
+ fields;
27
+ totalCountMode;
28
+ includes;
29
+ constructor(ids = []) {
30
+ this.page = null;
31
+ this.limit = null;
32
+ this.term = null;
33
+ this.title = null;
34
+ this.filters = [];
35
+ this.includes = null;
36
+ this.ids = ids;
37
+ this.queries = [];
38
+ this.associations = [];
39
+ this.postFilter = [];
40
+ this.sortings = [];
41
+ this.aggregations = [];
42
+ this.grouping = [];
43
+ this.fields = [];
44
+ this.totalCountMode = null;
45
+ }
46
+ /**
47
+ * Parses the current criteria and generates an object which can be provided to the api
48
+ */
49
+ toPayload() {
50
+ const params = {};
51
+ if (this.ids.length > 0) {
52
+ params.ids = this.ids;
53
+ }
54
+ if (this.page !== null) {
55
+ params.page = this.page;
56
+ }
57
+ if (this.limit !== null) {
58
+ params.limit = this.limit;
59
+ }
60
+ if (this.term !== null) {
61
+ params.term = this.term;
62
+ }
63
+ if (this.queries.length > 0) {
64
+ params.query = this.queries;
65
+ }
66
+ if (this.filters.length > 0) {
67
+ params.filter = this.filters;
68
+ }
69
+ if (this.postFilter.length > 0) {
70
+ params["post-filter"] = this.postFilter;
71
+ }
72
+ if (this.sortings.length > 0) {
73
+ params.sort = this.sortings;
74
+ }
75
+ if (this.aggregations.length > 0) {
76
+ params.aggregations = this.aggregations;
77
+ }
78
+ if (this.grouping.length > 0) {
79
+ params.grouping = this.grouping;
80
+ }
81
+ if (this.fields.length > 0) {
82
+ params.fields = this.fields;
83
+ }
84
+ if (this.associations.length > 0) {
85
+ params.associations = {};
86
+ for (const item of this.associations) {
87
+ if (!params.associations) {
88
+ continue;
89
+ }
90
+ params.associations[item.association] = item.criteria.toPayload();
91
+ }
92
+ }
93
+ if (this.includes !== null) {
94
+ params.includes = this.includes;
95
+ }
96
+ if (this.totalCountMode !== null) {
97
+ params["total-count-mode"] = this.totalCountMode;
98
+ }
99
+ return params;
100
+ }
101
+ /**
102
+ * Allows to provide a title for the criteria. This title will be shown in the `repository.search` request url so it can be used for debugging in network's tab
103
+ */
104
+ setTitle(title) {
105
+ this.title = title;
106
+ return this;
107
+ }
108
+ getTitle() {
109
+ return this.title;
110
+ }
111
+ /**
112
+ * Allows to provide a list of ids which are used as a filter
113
+ */
114
+ setIds(ids) {
115
+ this.ids = ids;
116
+ return this;
117
+ }
118
+ /**
119
+ * Allows to configure the total value of a search result.
120
+ * 0 - no total count will be selected. Should be used if no pagination required (fastest)
121
+ * 1 - exact total count will be selected. Should be used if an exact pagination is required (slow)
122
+ * 2 - fetches limit * 5 + 1. Should be used if pagination can work with "next page exists" (fast)
123
+ */
124
+ setTotalCountMode(mode) {
125
+ if (typeof mode !== "number") {
126
+ this.totalCountMode = null;
127
+ }
128
+ this.totalCountMode = mode < 0 || mode > 2 ? null : mode;
129
+ return this;
130
+ }
131
+ setPage(page) {
132
+ this.page = page;
133
+ return this;
134
+ }
135
+ setLimit(limit) {
136
+ this.limit = limit;
137
+ return this;
138
+ }
139
+ setTerm(term) {
140
+ this.term = term;
141
+ return this;
142
+ }
143
+ addFilter(filter) {
144
+ this.filters.push(filter);
145
+ return this;
146
+ }
147
+ addIncludes(include) {
148
+ for (const [entityName, includeValues] of Object.entries(include)) {
149
+ if (this.includes === null) {
150
+ this.includes = {};
151
+ }
152
+ if (!this.includes[entityName]) {
153
+ this.includes[entityName] = [];
154
+ }
155
+ this.includes[entityName].push(...includeValues);
156
+ }
157
+ return this;
158
+ }
159
+ /**
160
+ * Adds the provided filter as post filter.
161
+ * Post filter will be considered for the documents query but not for the aggregations.
162
+ */
163
+ addPostFilter(filter) {
164
+ this.postFilter.push(filter);
165
+ return this;
166
+ }
167
+ /**
168
+ * Allows to add different sortings for the criteria, to sort the entity result.
169
+ */
170
+ addSorting(sorting) {
171
+ this.sortings.push(sorting);
172
+ return this;
173
+ }
174
+ /**
175
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Query\ScoreQuery.
176
+ * These queries are used to search for documents and score them with a ranking
177
+ */
178
+ addQuery(filter, score, scoreField = null) {
179
+ const query = { score: score, query: filter };
180
+ if (scoreField) {
181
+ query.scoreField = scoreField;
182
+ }
183
+ this.queries.push(query);
184
+ return this;
185
+ }
186
+ /**
187
+ * Allows grouping the result by a specific field
188
+ */
189
+ addGrouping(field) {
190
+ this.grouping.push(field);
191
+ return this;
192
+ }
193
+ /**
194
+ * Allows loading partial fields for the result.
195
+ */
196
+ addFields(...field) {
197
+ this.fields.push(...field);
198
+ return this;
199
+ }
200
+ /**
201
+ * @param {Object} aggregation
202
+ */
203
+ addAggregation(aggregation) {
204
+ this.aggregations.push(aggregation);
205
+ return this;
206
+ }
207
+ /**
208
+ * Ensures that a criterion is created for each segment of the passed path.
209
+ * Existing Criteria objects are not overwritten.
210
+ * Returns the own instance
211
+ */
212
+ addAssociation(path) {
213
+ this.getAssociation(path);
214
+ return this;
215
+ }
216
+ /**
217
+ * Ensures that a criterion is created for each segment of the passed path.
218
+ * Returns the criteria instance of the last path segment
219
+ */
220
+ getAssociation(path) {
221
+ const parts = path.split(".");
222
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
223
+ let criteria = this;
224
+ for (const part of parts) {
225
+ if (!criteria.hasAssociation(part)) {
226
+ criteria.associations.push({
227
+ association: part,
228
+ criteria: new Criteria(),
229
+ });
230
+ }
231
+ // @ts-expect-error - another this instance
232
+ criteria = criteria.getAssociationCriteria(part);
233
+ }
234
+ return criteria;
235
+ }
236
+ getAssociationCriteria(part) {
237
+ let criteria = null;
238
+ for (const association of this.associations) {
239
+ if (association.association === part) {
240
+ criteria = association.criteria;
241
+ }
242
+ }
243
+ if (!criteria) {
244
+ criteria = new Criteria();
245
+ this.associations.push({
246
+ association: part,
247
+ criteria,
248
+ });
249
+ }
250
+ return criteria;
251
+ }
252
+ getLimit() {
253
+ return this.limit ?? 0;
254
+ }
255
+ getPage() {
256
+ return this.page ?? 0;
257
+ }
258
+ hasAssociation(property) {
259
+ return this.associations.some((assocation) => {
260
+ return assocation.association === property;
261
+ });
262
+ }
263
+ /**
264
+ * Resets the sorting parameter
265
+ */
266
+ resetSorting() {
267
+ this.sortings = [];
268
+ }
269
+ /**
270
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\AvgAggregation
271
+ * Allows to calculate the avg value for the provided field
272
+ */
273
+ static avg(name, field) {
274
+ return { type: "avg", name, field };
275
+ }
276
+ /**
277
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\CountAggregation
278
+ * Allows to calculate the count value for the provided field
279
+ */
280
+ static count(name, field) {
281
+ return { type: "count", name, field };
282
+ }
283
+ /**
284
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MaxAggregation
285
+ * Allows to calculate the max value for the provided field
286
+ */
287
+ static max(name, field) {
288
+ return { type: "max", name, field };
289
+ }
290
+ /**
291
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MinAggregation
292
+ * Allows to calculate the min value for the provided field
293
+ */
294
+ static min(name, field) {
295
+ return { type: "min", name, field };
296
+ }
297
+ /**
298
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\StatsAggregation
299
+ * Allows to calculate the sum, max, min, avg, count values for the provided field
300
+ */
301
+ static stats(name, field) {
302
+ return { type: "stats", name, field };
303
+ }
304
+ /**
305
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\SumAggregation
306
+ * Allows to calculate the sum value for the provided field
307
+ */
308
+ static sum(name, field) {
309
+ return { type: "sum", name, field };
310
+ }
311
+ /**
312
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\TermsAggregation
313
+ * Allows to fetch term buckets for the provided field
314
+ */
315
+ static terms(name, field, limit = null, sort = null, aggregation = null) {
316
+ return { type: "terms", name, field, limit, sort, aggregation };
317
+ }
318
+ /**
319
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\EntityAggregation
320
+ * Allows to filter an aggregation result
321
+ */
322
+ static entityAggregation(name, field, definition) {
323
+ return { type: "entity", name, field, definition };
324
+ }
325
+ /**
326
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\FilterAggregation
327
+ * Allows to filter an aggregation result
328
+ */
329
+ static filter(name, filter, aggregation) {
330
+ return { type: "filter", name, filter, aggregation };
331
+ }
332
+ /**
333
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\DateHistogramAggregation
334
+ * Allows to fetch date buckets for the provided date interval
335
+ */
336
+ static histogram(name, field, interval = null, format = null, aggregation = null, timeZone = null) {
337
+ return {
338
+ type: "histogram",
339
+ name,
340
+ field,
341
+ interval,
342
+ format,
343
+ aggregation,
344
+ timeZone,
345
+ };
346
+ }
347
+ /**
348
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting.
349
+ * Allows to sort the documents by the provided field
350
+ */
351
+ static sort(field, order = "ASC", naturalSorting = false) {
352
+ return { field, order, naturalSorting };
353
+ }
354
+ /**
355
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting.
356
+ * Allows to sort the documents by the provided field naturally
357
+ */
358
+ static naturalSorting(field, order = "ASC") {
359
+ return { field, order, naturalSorting: true };
360
+ }
361
+ /**
362
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\CountSorting.
363
+ * Allows to sort the documents by counting associations via the provided field
364
+ *
365
+ * Sql representation: `ORDER BY COUNT({field}) {order}`
366
+ */
367
+ static countSorting(field, order = "ASC") {
368
+ return { field, order, naturalSorting: false, type: "count" };
369
+ }
370
+ /**
371
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter.
372
+ * This allows to filter documents where the value are contained in the provided field.
373
+ *
374
+ * Sql representation: `{field} LIKE %{value}%`
375
+ */
376
+ static contains(field, value) {
377
+ return { type: "contains", field, value };
378
+ }
379
+ /**
380
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\PrefixFilter.
381
+ * This allows to filter documents where the value marks the beginning of the provided field.
382
+ *
383
+ * Sql representation: `{field} LIKE {value}%`
384
+ */
385
+ static prefix(field, value) {
386
+ return { type: "prefix", field, value };
387
+ }
388
+ /**
389
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\SuffixFilter.
390
+ * This allows to filter documents where the value marks the end of the provided field.
391
+ *
392
+ * Sql representation: `{field} LIKE %{value}`
393
+ */
394
+ static suffix(field, value) {
395
+ return { type: "suffix", field, value };
396
+ }
397
+ /**
398
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter.
399
+ * This allows to filter documents where the field matches one of the provided values
400
+ *
401
+ * Sql representation: `{field} IN ({value}, {value})`
402
+ */
403
+ static equalsAny(field, value) {
404
+ return { type: "equalsAny", field, value: value };
405
+ }
406
+ /**
407
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter.
408
+ * This allows to filter documents where the field matches a defined range
409
+ *
410
+ * Sql representation: `{field} >= {value}`, `{field} <= {value}`, ...
411
+ */
412
+ static range(field, range) {
413
+ return { type: "range", field, parameters: range };
414
+ }
415
+ /**
416
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter.
417
+ * This allows to filter documents where the field matches a defined range
418
+ *
419
+ * Sql representation: `{field} = {value}`
420
+ */
421
+ static equals(field, value) {
422
+ return { type: "equals", field, value };
423
+ }
424
+ /**
425
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter.
426
+ * This allows to filter documents which not matches for the provided filters
427
+ * All above listed queries can be provided (equals, equalsAny, range, contains)
428
+ *
429
+ * Sql representation: `NOT({query} {operator} {query} {operator} {query})`
430
+ *
431
+ * @param {string} operator - and/or
432
+ * @param {array} queries
433
+ *
434
+ * @returns {Object}
435
+ */
436
+ static not(operator, queries = []) {
437
+ return { type: "not", operator: operator, queries: queries };
438
+ }
439
+ /**
440
+ * @see \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter.
441
+ * This allows to filter documents which matches for the provided filters
442
+ * All above listed queries can be provided (equals, equalsAny, range, contains)
443
+ *
444
+ * Sql representation: `({query} {operator} {query} {operator} {query})`
445
+ *
446
+ * @param {string} operator - and/or
447
+ * @param {array} queries
448
+ *
449
+ * @returns {Object}
450
+ */
451
+ static multi(operator, queries = []) {
452
+ return { type: "multi", operator, queries };
453
+ }
454
+ }
455
+ exports.Criteria = Criteria;
456
+ //# sourceMappingURL=criteria.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"criteria.js","sourceRoot":"","sources":["../../../src/helper/criteria.ts"],"names":[],"mappings":";;;AAAA,IAAY,cAOX;AAPD,WAAY,cAAc;IACzB,yFAAyF;IACzF,uEAAkB,CAAA;IAClB,kGAAkG;IAClG,6EAAqB,CAAA;IACrB,iGAAiG;IACjG,uFAA0B,CAAA;AAC3B,CAAC,EAPW,cAAc,8BAAd,cAAc,QAOzB;AA0JD,MAAa,QAAQ;IACpB,KAAK,CAAgB;IAErB,IAAI,CAAgB;IAEpB,KAAK,CAAgB;IAErB,IAAI,CAAgB;IAEpB,OAAO,CAAiB;IAExB,GAAG,CAAW;IAEd,OAAO,CAAU;IAEjB,YAAY,CAAgB;IAE5B,UAAU,CAAiB;IAE3B,QAAQ,CAAY;IAEpB,YAAY,CAAgB;IAE5B,QAAQ,CAAW;IAEnB,MAAM,CAAW;IAEjB,cAAc,CAAwB;IAEtC,QAAQ,CAAiB;IAEzB,YAAY,MAAgB,EAAE;QAC7B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,IAAI,CAAC,GAAG,GAAG,GAAG,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OAAO,EAAE,OAAO,EAAE,CAAC;IAC9D,CAAC;IAED;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,KAAK,CACX,QAAsC,EACtC,UAA0B,EAAE;QAE5B,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;IAC7C,CAAC;CACD;AAvjBD,4BAujBC","sourcesContent":["export enum TotalCountMode {\n\t/* No total count will be selected. Should be used if no pagination required (fastest) */\n\tNO_TOTAL_COUNT = 0,\n\t/* Exact total count will be selected. Should be used if an exact pagination is required (slow) */\n\tEXACT_TOTAL_COUNT = 1,\n\t/* Fetches limit * 5 + 1. Should be used if pagination can work with \"next page exists\" (fast) */\n\tPAGINATION_TOTAL_COUNT = 2,\n}\n\ninterface Filters {\n\tcontains: {\n\t\ttype: \"contains\";\n\t\tfield: string;\n\t\tvalue: string;\n\t};\n\tprefix: {\n\t\ttype: \"prefix\";\n\t\tfield: string;\n\t\tvalue: string;\n\t};\n\tsuffix: {\n\t\ttype: \"suffix\";\n\t\tfield: string;\n\t\tvalue: string;\n\t};\n\tequalsAny: {\n\t\ttype: \"equalsAny\";\n\t\tfield: string;\n\t\tvalue: (string | number | boolean | null)[];\n\t};\n\tequals: {\n\t\ttype: \"equals\";\n\t\tfield: string;\n\t\tvalue: string | number | boolean | null;\n\t};\n\trange: {\n\t\ttype: \"range\";\n\t\tfield: string;\n\t\tparameters: {\n\t\t\tlte?: string | number;\n\t\t\tlt?: string | number;\n\t\t\tgte?: string | number;\n\t\t\tgt?: string | number;\n\t\t};\n\t};\n\tnot: {\n\t\ttype: \"not\";\n\t\toperator: \"and\" | \"AND\" | \"or\" | \"OR\";\n\t\tqueries: SingleFilter[];\n\t};\n\tmulti: {\n\t\ttype: \"multi\";\n\t\toperator: \"and\" | \"AND\" | \"or\" | \"OR\";\n\t\tqueries: SingleFilter[];\n\t};\n}\n\ninterface Aggregations {\n\thistogram: {\n\t\ttype: \"histogram\";\n\t\tname: string;\n\t\tfield: string;\n\t\tinterval: string | null;\n\t\tformat: string | null;\n\t\taggregation: Aggregation | null;\n\t\ttimeZone: string | null;\n\t};\n\tterms: {\n\t\ttype: \"terms\";\n\t\tname: string;\n\t\tfield: string;\n\t\tlimit?: number | null;\n\t\tsort?: Sorting | null;\n\t\taggregation?: Aggregation | null;\n\t};\n\tsum: {\n\t\ttype: \"sum\";\n\t\tname: string;\n\t\tfield: string;\n\t};\n\tstats: {\n\t\ttype: \"stats\";\n\t\tname: string;\n\t\tfield: string;\n\t};\n\tmin: {\n\t\ttype: \"min\";\n\t\tname: string;\n\t\tfield: string;\n\t};\n\tmax: {\n\t\ttype: \"max\";\n\t\tname: string;\n\t\tfield: string;\n\t};\n\tcount: {\n\t\ttype: \"count\";\n\t\tname: string;\n\t\tfield: string;\n\t};\n\tavg: {\n\t\ttype: \"avg\";\n\t\tname: string;\n\t\tfield: string;\n\t};\n\tentity: {\n\t\ttype: \"entity\";\n\t\tname: string;\n\t\tfield: string;\n\t\tdefinition: string;\n\t};\n\tfilter: {\n\t\ttype: \"filter\";\n\t\tname: string;\n\t\tfilter: SingleFilter[];\n\t\taggregation: Aggregation;\n\t};\n}\n\ntype ValueOf<T> = T[keyof T];\nexport type SingleFilter = ValueOf<Filters>;\ntype Aggregation = ValueOf<Aggregations>;\n\ninterface Include {\n\t[entityName: string]: string[];\n}\ninterface Association {\n\tassociation: string;\n\tcriteria: Criteria;\n}\ninterface Query {\n\tscore: number;\n\tquery: SingleFilter;\n\tscoreField?: string;\n}\ninterface Sorting {\n\tfield: string;\n\torder: \"ASC\" | \"DESC\";\n\tnaturalSorting: boolean;\n\ttype?: string;\n}\ntype GroupField = string;\ninterface RequestParams {\n\tids?: string[];\n\tpage?: number;\n\tlimit?: number;\n\tterm?: string;\n\tquery?: Query[];\n\tfilter?: SingleFilter[];\n\t\"post-filter\"?: SingleFilter[];\n\tsort?: Sorting[];\n\taggregations?: Aggregation[];\n\tgrouping?: string[];\n\tfields?: string[];\n\tassociations?: {\n\t\t[association: string]: RequestParams;\n\t};\n\tincludes?: Include;\n\t\"total-count-mode\"?: TotalCountMode;\n}\n\nexport class Criteria {\n\ttitle: string | null;\n\n\tpage: number | null;\n\n\tlimit: number | null;\n\n\tterm: string | null;\n\n\tfilters: SingleFilter[];\n\n\tids: string[];\n\n\tqueries: Query[];\n\n\tassociations: Association[];\n\n\tpostFilter: SingleFilter[];\n\n\tsortings: Sorting[];\n\n\taggregations: Aggregation[];\n\n\tgrouping: string[];\n\n\tfields: string[];\n\n\ttotalCountMode: TotalCountMode | null;\n\n\tincludes: Include | null;\n\n\tconstructor(ids: string[] = []) {\n\t\tthis.page = null;\n\t\tthis.limit = null;\n\t\tthis.term = null;\n\t\tthis.title = null;\n\t\tthis.filters = [];\n\t\tthis.includes = null;\n\t\tthis.ids = ids;\n\t\tthis.queries = [];\n\t\tthis.associations = [];\n\t\tthis.postFilter = [];\n\t\tthis.sortings = [];\n\t\tthis.aggregations = [];\n\t\tthis.grouping = [];\n\t\tthis.fields = [];\n\t\tthis.totalCountMode = null;\n\t}\n\n\t/**\n\t * Parses the current criteria and generates an object which can be provided to the api\n\t */\n\ttoPayload(): RequestParams {\n\t\tconst params: RequestParams = {};\n\n\t\tif (this.ids.length > 0) {\n\t\t\tparams.ids = this.ids;\n\t\t}\n\t\tif (this.page !== null) {\n\t\t\tparams.page = this.page;\n\t\t}\n\t\tif (this.limit !== null) {\n\t\t\tparams.limit = this.limit;\n\t\t}\n\t\tif (this.term !== null) {\n\t\t\tparams.term = this.term;\n\t\t}\n\t\tif (this.queries.length > 0) {\n\t\t\tparams.query = this.queries;\n\t\t}\n\t\tif (this.filters.length > 0) {\n\t\t\tparams.filter = this.filters;\n\t\t}\n\t\tif (this.postFilter.length > 0) {\n\t\t\tparams[\"post-filter\"] = this.postFilter;\n\t\t}\n\t\tif (this.sortings.length > 0) {\n\t\t\tparams.sort = this.sortings;\n\t\t}\n\t\tif (this.aggregations.length > 0) {\n\t\t\tparams.aggregations = this.aggregations;\n\t\t}\n\t\tif (this.grouping.length > 0) {\n\t\t\tparams.grouping = this.grouping;\n\t\t}\n\t\tif (this.fields.length > 0) {\n\t\t\tparams.fields = this.fields;\n\t\t}\n\t\tif (this.associations.length > 0) {\n\t\t\tparams.associations = {};\n\n\t\t\tfor (const item of this.associations) {\n\t\t\t\tif (!params.associations) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tparams.associations[item.association] = item.criteria.toPayload();\n\t\t\t}\n\t\t}\n\t\tif (this.includes !== null) {\n\t\t\tparams.includes = this.includes;\n\t\t}\n\n\t\tif (this.totalCountMode !== null) {\n\t\t\tparams[\"total-count-mode\"] = this.totalCountMode;\n\t\t}\n\n\t\treturn params;\n\t}\n\n\t/**\n\t * Allows to provide a title for the criteria. This title will be shown in the `repository.search` request url so it can be used for debugging in network's tab\n\t */\n\tsetTitle(title: string): this {\n\t\tthis.title = title;\n\t\treturn this;\n\t}\n\n\tgetTitle(): string | null {\n\t\treturn this.title;\n\t}\n\t/**\n\t * Allows to provide a list of ids which are used as a filter\n\t */\n\tsetIds(ids: string[]): this {\n\t\tthis.ids = ids;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Allows to configure the total value of a search result.\n\t * 0 - no total count will be selected. Should be used if no pagination required (fastest)\n\t * 1 - exact total count will be selected. Should be used if an exact pagination is required (slow)\n\t * 2 - fetches limit * 5 + 1. Should be used if pagination can work with \"next page exists\" (fast)\n\t */\n\tsetTotalCountMode(mode: TotalCountMode): this {\n\t\tif (typeof mode !== \"number\") {\n\t\t\tthis.totalCountMode = null;\n\t\t}\n\n\t\tthis.totalCountMode = mode < 0 || mode > 2 ? null : mode;\n\t\treturn this;\n\t}\n\n\tsetPage(page: number): this {\n\t\tthis.page = page;\n\t\treturn this;\n\t}\n\n\tsetLimit(limit: number): this {\n\t\tthis.limit = limit;\n\t\treturn this;\n\t}\n\n\tsetTerm(term: string): this {\n\t\tthis.term = term;\n\t\treturn this;\n\t}\n\n\taddFilter(filter: SingleFilter): this {\n\t\tthis.filters.push(filter);\n\n\t\treturn this;\n\t}\n\n\taddIncludes(include: Include): this {\n\t\tfor (const [entityName, includeValues] of Object.entries(include)) {\n\t\t\tif (this.includes === null) {\n\t\t\t\tthis.includes = {};\n\t\t\t}\n\t\t\tif (!this.includes[entityName]) {\n\t\t\t\tthis.includes[entityName] = [];\n\t\t\t}\n\n\t\t\tthis.includes[entityName].push(...includeValues);\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Adds the provided filter as post filter.\n\t * Post filter will be considered for the documents query but not for the aggregations.\n\t */\n\taddPostFilter(filter: SingleFilter): this {\n\t\tthis.postFilter.push(filter);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Allows to add different sortings for the criteria, to sort the entity result.\n\t */\n\taddSorting(sorting: Sorting): this {\n\t\tthis.sortings.push(sorting);\n\t\treturn this;\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Query\\ScoreQuery.\n\t * These queries are used to search for documents and score them with a ranking\n\t */\n\taddQuery(\n\t\tfilter: SingleFilter,\n\t\tscore: number,\n\t\tscoreField: string | null = null,\n\t): this {\n\t\tconst query: Query = { score: score, query: filter };\n\n\t\tif (scoreField) {\n\t\t\tquery.scoreField = scoreField;\n\t\t}\n\n\t\tthis.queries.push(query);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Allows grouping the result by a specific field\n\t */\n\taddGrouping(field: string): this {\n\t\tthis.grouping.push(field);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Allows loading partial fields for the result.\n\t */\n\taddFields(...field: string[]): this {\n\t\tthis.fields.push(...field);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * @param {Object} aggregation\n\t */\n\taddAggregation(aggregation: Aggregation): this {\n\t\tthis.aggregations.push(aggregation);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Ensures that a criterion is created for each segment of the passed path.\n\t * Existing Criteria objects are not overwritten.\n\t * Returns the own instance\n\t */\n\taddAssociation(path: string): this {\n\t\tthis.getAssociation(path);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Ensures that a criterion is created for each segment of the passed path.\n\t * Returns the criteria instance of the last path segment\n\t */\n\tgetAssociation(path: string): Criteria {\n\t\tconst parts = path.split(\".\");\n\n\t\t// eslint-disable-next-line @typescript-eslint/no-this-alias\n\t\tlet criteria = this;\n\n\t\tfor (const part of parts) {\n\t\t\tif (!criteria.hasAssociation(part)) {\n\t\t\t\tcriteria.associations.push({\n\t\t\t\t\tassociation: part,\n\t\t\t\t\tcriteria: new Criteria(),\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// @ts-expect-error - another this instance\n\t\t\tcriteria = criteria.getAssociationCriteria(part);\n\t\t}\n\n\t\treturn criteria;\n\t}\n\n\tprivate getAssociationCriteria(part: string): Criteria {\n\t\tlet criteria = null;\n\n\t\tfor (const association of this.associations) {\n\t\t\tif (association.association === part) {\n\t\t\t\tcriteria = association.criteria;\n\t\t\t}\n\t\t}\n\n\t\tif (!criteria) {\n\t\t\tcriteria = new Criteria();\n\t\t\tthis.associations.push({\n\t\t\t\tassociation: part,\n\t\t\t\tcriteria,\n\t\t\t});\n\t\t}\n\n\t\treturn criteria;\n\t}\n\n\tgetLimit(): number {\n\t\treturn this.limit ?? 0;\n\t}\n\n\tgetPage(): number {\n\t\treturn this.page ?? 0;\n\t}\n\n\thasAssociation(property: string): boolean {\n\t\treturn this.associations.some((assocation) => {\n\t\t\treturn assocation.association === property;\n\t\t});\n\t}\n\n\t/**\n\t * Resets the sorting parameter\n\t */\n\tresetSorting(): void {\n\t\tthis.sortings = [];\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Aggregation\\Metric\\AvgAggregation\n\t * Allows to calculate the avg value for the provided field\n\t */\n\tstatic avg(name: string, field: string): Aggregations[\"avg\"] {\n\t\treturn { type: \"avg\", name, field };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Aggregation\\Metric\\CountAggregation\n\t * Allows to calculate the count value for the provided field\n\t */\n\tstatic count(name: string, field: string): Aggregations[\"count\"] {\n\t\treturn { type: \"count\", name, field };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Aggregation\\Metric\\MaxAggregation\n\t * Allows to calculate the max value for the provided field\n\t */\n\tstatic max(name: string, field: string): Aggregations[\"max\"] {\n\t\treturn { type: \"max\", name, field };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Aggregation\\Metric\\MinAggregation\n\t * Allows to calculate the min value for the provided field\n\t */\n\tstatic min(name: string, field: string): Aggregations[\"min\"] {\n\t\treturn { type: \"min\", name, field };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Aggregation\\Metric\\StatsAggregation\n\t * Allows to calculate the sum, max, min, avg, count values for the provided field\n\t */\n\tstatic stats(name: string, field: string): Aggregations[\"stats\"] {\n\t\treturn { type: \"stats\", name, field };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Aggregation\\Metric\\SumAggregation\n\t * Allows to calculate the sum value for the provided field\n\t */\n\tstatic sum(name: string, field: string): Aggregations[\"sum\"] {\n\t\treturn { type: \"sum\", name, field };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Aggregation\\Bucket\\TermsAggregation\n\t * Allows to fetch term buckets for the provided field\n\t */\n\tstatic terms(\n\t\tname: string,\n\t\tfield: string,\n\t\tlimit: number | null = null,\n\t\tsort: Sorting | null = null,\n\t\taggregation: Aggregation | null = null,\n\t): Aggregations[\"terms\"] {\n\t\treturn { type: \"terms\", name, field, limit, sort, aggregation };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Aggregation\\Metric\\EntityAggregation\n\t * Allows to filter an aggregation result\n\t */\n\tstatic entityAggregation(\n\t\tname: string,\n\t\tfield: string,\n\t\tdefinition: string,\n\t): Aggregations[\"entity\"] {\n\t\treturn { type: \"entity\", name, field, definition };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Aggregation\\Bucket\\FilterAggregation\n\t * Allows to filter an aggregation result\n\t */\n\tstatic filter(\n\t\tname: string,\n\t\tfilter: SingleFilter[],\n\t\taggregation: Aggregation,\n\t): Aggregations[\"filter\"] {\n\t\treturn { type: \"filter\", name, filter, aggregation };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Aggregation\\Bucket\\DateHistogramAggregation\n\t * Allows to fetch date buckets for the provided date interval\n\t */\n\tstatic histogram(\n\t\tname: string,\n\t\tfield: string,\n\t\tinterval: string | null = null,\n\t\tformat: string | null = null,\n\t\taggregation: Aggregation | null = null,\n\t\ttimeZone: string | null = null,\n\t): Aggregations[\"histogram\"] {\n\t\treturn {\n\t\t\ttype: \"histogram\",\n\t\t\tname,\n\t\t\tfield,\n\t\t\tinterval,\n\t\t\tformat,\n\t\t\taggregation,\n\t\t\ttimeZone,\n\t\t};\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Sorting\\FieldSorting.\n\t * Allows to sort the documents by the provided field\n\t */\n\tstatic sort(\n\t\tfield: string,\n\t\torder: Sorting[\"order\"] = \"ASC\",\n\t\tnaturalSorting = false,\n\t): Sorting {\n\t\treturn { field, order, naturalSorting };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Sorting\\FieldSorting.\n\t * Allows to sort the documents by the provided field naturally\n\t */\n\tstatic naturalSorting(\n\t\tfield: string,\n\t\torder: Sorting[\"order\"] = \"ASC\",\n\t): Sorting {\n\t\treturn { field, order, naturalSorting: true };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Sorting\\CountSorting.\n\t * Allows to sort the documents by counting associations via the provided field\n\t *\n\t * Sql representation: `ORDER BY COUNT({field}) {order}`\n\t */\n\tstatic countSorting(field: string, order: Sorting[\"order\"] = \"ASC\"): Sorting {\n\t\treturn { field, order, naturalSorting: false, type: \"count\" };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Filter\\ContainsFilter.\n\t * This allows to filter documents where the value are contained in the provided field.\n\t *\n\t * Sql representation: `{field} LIKE %{value}%`\n\t */\n\tstatic contains(field: string, value: string): Filters[\"contains\"] {\n\t\treturn { type: \"contains\", field, value };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Filter\\PrefixFilter.\n\t * This allows to filter documents where the value marks the beginning of the provided field.\n\t *\n\t * Sql representation: `{field} LIKE {value}%`\n\t */\n\tstatic prefix(field: string, value: string): Filters[\"prefix\"] {\n\t\treturn { type: \"prefix\", field, value };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Filter\\SuffixFilter.\n\t * This allows to filter documents where the value marks the end of the provided field.\n\t *\n\t * Sql representation: `{field} LIKE %{value}`\n\t */\n\tstatic suffix(field: string, value: string): Filters[\"suffix\"] {\n\t\treturn { type: \"suffix\", field, value };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Filter\\EqualsAnyFilter.\n\t * This allows to filter documents where the field matches one of the provided values\n\t *\n\t * Sql representation: `{field} IN ({value}, {value})`\n\t */\n\tstatic equalsAny(\n\t\tfield: string,\n\t\tvalue: (string | number | boolean | null)[],\n\t): Filters[\"equalsAny\"] {\n\t\treturn { type: \"equalsAny\", field, value: value };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Filter\\RangeFilter.\n\t * This allows to filter documents where the field matches a defined range\n\t *\n\t * Sql representation: `{field} >= {value}`, `{field} <= {value}`, ...\n\t */\n\tstatic range(\n\t\tfield: string,\n\t\trange: Filters[\"range\"][\"parameters\"],\n\t): Filters[\"range\"] {\n\t\treturn { type: \"range\", field, parameters: range };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Filter\\EqualsFilter.\n\t * This allows to filter documents where the field matches a defined range\n\t *\n\t * Sql representation: `{field} = {value}`\n\t */\n\tstatic equals(\n\t\tfield: string,\n\t\tvalue: string | number | boolean | null,\n\t): Filters[\"equals\"] {\n\t\treturn { type: \"equals\", field, value };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Filter\\NotFilter.\n\t * This allows to filter documents which not matches for the provided filters\n\t * All above listed queries can be provided (equals, equalsAny, range, contains)\n\t *\n\t * Sql representation: `NOT({query} {operator} {query} {operator} {query})`\n\t *\n\t * @param {string} operator - and/or\n\t * @param {array} queries\n\t *\n\t * @returns {Object}\n\t */\n\tstatic not(\n\t\toperator: Filters[\"not\"][\"operator\"],\n\t\tqueries: SingleFilter[] = [],\n\t): Filters[\"not\"] {\n\t\treturn { type: \"not\", operator: operator, queries: queries };\n\t}\n\n\t/**\n\t * @see \\Shopware\\Core\\Framework\\DataAbstractionLayer\\Search\\Filter\\NotFilter.\n\t * This allows to filter documents which matches for the provided filters\n\t * All above listed queries can be provided (equals, equalsAny, range, contains)\n\t *\n\t * Sql representation: `({query} {operator} {query} {operator} {query})`\n\t *\n\t * @param {string} operator - and/or\n\t * @param {array} queries\n\t *\n\t * @returns {Object}\n\t */\n\tstatic multi(\n\t\toperator: Filters[\"multi\"][\"operator\"],\n\t\tqueries: SingleFilter[] = [],\n\t): Filters[\"multi\"] {\n\t\treturn { type: \"multi\", operator, queries };\n\t}\n}\n"]}
@@ -147,7 +147,8 @@ exports.ApiClientAuthenticationFailed = ApiClientAuthenticationFailed;
147
147
  class ApiClientRequestFailed extends Error {
148
148
  response;
149
149
  constructor(shopId, response) {
150
- super(`The api request failed with status code: ${response.statusCode} for shop with id: ${shopId}`);
150
+ const message = response.body.errors.map((e) => e.detail).join(", ");
151
+ super(`Request failed with error: ${message} for shop with id: ${shopId}`);
151
152
  this.response = response;
152
153
  }
153
154
  }
@@ -1 +1 @@
1
- {"version":3,"file":"http-client.js","sourceRoot":"","sources":["../../src/http-client.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACH,MAAa,UAAU;IAGF;IAFZ,OAAO,CAAmD;IAElE,YAAoB,IAAmB;QAAnB,SAAI,GAAJ,IAAI,CAAe;QACtC,IAAI,CAAC,OAAO,GAAG;YACd,KAAK,EAAE,IAAI;YACX,SAAS,EAAE,IAAI;SACf,CAAC;IACH,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAG,CACR,GAAW,EACX,UAAkC,EAAE;QAEpC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACtD,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI,CACT,GAAW,EACX,OAAe,EAAE,EACjB,UAAkC,EAAE;QAEpC,OAAO,CAAC,cAAc,CAAC,GAAG,kBAAkB,CAAC;QAC7C,OAAO,CAAC,MAAM,GAAG,kBAAkB,CAAC;QAEpC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC;IACvE,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAG,CACR,GAAW,EACX,OAAe,EAAE,EACjB,UAAkC,EAAE;QAEpC,OAAO,CAAC,cAAc,CAAC,GAAG,kBAAkB,CAAC;QAC7C,OAAO,CAAC,MAAM,GAAG,kBAAkB,CAAC;QAEpC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,CAAC,SAAS,C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ACH,MAAa,kBAAkB;IAEtB;IACA;IACA;IAHR,YACQ,UAAkB,EAClB,IAAkB,EAClB,OAAgB;QAFhB,eAAU,GAAV,UAAU,CAAQ;QAClB,SAAI,GAAJ,IAAI,CAAc;QAClB,YAAO,GAAP,OAAO,CAAS;IACrB,CAAC;CACJ;AAND,gDAMC;AAWD;;GAEG;AACH,MAAa,6BAA8B,SAAQ,KAAK;IAG/C;IAFR,YACC,MAAc,EACP,QAAoC;QAE3C,KAAK,CACJ,kDAAkD,MAAM,mBAAmB,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAC1G,CAAC;QAJK,aAAQ,GAAR,QAAQ,CAA4B;IAK5C,CAAC;CACD;AATD,sEASC;AAED;;GAEG;AACH,MAAa,sBAAuB,SAAQ,KAAK;IAGxC;IAFR,YACC,MAAc,EACP,QAAmD;QAE1D,KAAK,CACJ,4CAA4C,QAAQ,CAAC,UAAU,sBAAsB,MAAM,EAAE,CAC7F,CAAC;QAJK,aAAQ,GAAR,QAAQ,CAA2C;IAK3D,CAAC;CACD;AATD,wDASC","sourcesContent":["import type { ShopInterface } from \"./repository.js\";\n\n/**\n * HttpClient is a simple wrapper around the fetch API, pre-configured with the shop's URL and access token\n */\nexport class HttpClient {\n\tprivate storage: { expiresIn: Date | null; token: string | null };\n\n\tconstructor(private shop: ShopInterface) {\n\t\tthis.storage = {\n\t\t\ttoken: null,\n\t\t\texpiresIn: null,\n\t\t};\n\t}\n\n\t/**\n\t * Permform a GET request\n\t */\n\tasync get<ResponseType>(\n\t\turl: string,\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\treturn await this.request(\"GET\", url, null, headers);\n\t}\n\n\t/**\n\t * Permform a POST request\n\t */\n\tasync post<ResponseType>(\n\t\turl: string,\n\t\tjson: object = {},\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\theaders[\"content-type\"] = \"application/json\";\n\t\theaders.accept = \"application/json\";\n\n\t\treturn await this.request(\"POST\", url, JSON.stringify(json), headers);\n\t}\n\n\t/**\n\t * Permform a PUT request\n\t */\n\tasync put<ResponseType>(\n\t\turl: string,\n\t\tjson: object = {},\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\theaders[\"content-type\"] = \"application/json\";\n\t\theaders.accept = \"application/json\";\n\n\t\treturn await this.request(\"PUT\", url, JSON.stringify(json), headers);\n\t}\n\n\t/**\n\t * Permform a PATCH request\n\t */\n\tasync patch<ResponseType>(\n\t\turl: string,\n\t\tjson: object = {},\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\theaders[\"content-type\"] = \"application/json\";\n\t\theaders.accept = \"application/json\";\n\n\t\treturn await this.request(\"PATCH\", url, JSON.stringify(json), headers);\n\t}\n\n\t/**\n\t * Permform a DELETE request\n\t */\n\tasync delete<ResponseType>(\n\t\turl: string,\n\t\tjson: object = {},\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\theaders[\"content-type\"] = \"application/json\";\n\t\theaders.accept = \"application/json\";\n\n\t\treturn await this.request(\"DELETE\", url, JSON.stringify(json), headers);\n\t}\n\n\tprivate async request<ResponseType>(\n\t\tmethod: string,\n\t\turl: string,\n\t\tbody: string | null = \"\",\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\tconst f = await globalThis.fetch(`${this.shop.getShopUrl()}/api${url}`, {\n\t\t\tbody,\n\t\t\theaders: Object.assign(\n\t\t\t\t{\n\t\t\t\t\tAuthorization: `Bearer ${await this.getToken()}`,\n\t\t\t\t},\n\t\t\t\theaders,\n\t\t\t),\n\t\t\tmethod,\n\t\t});\n\n\t\t// Obtain new token\n\t\tif (!f.ok && f.status === 401) {\n\t\t\tthis.storage.expiresIn = null;\n\n\t\t\treturn await this.request(method, url, body, headers);\n\t\t}\n\t\tif (!f.ok) {\n\t\t\tthrow new ApiClientRequestFailed(\n\t\t\t\tthis.shop.getShopId(),\n\t\t\t\tnew HttpClientResponse(f.status, await f.json(), f.headers),\n\t\t\t);\n\t\t}\n\n\t\tif (f.status === 204) {\n\t\t\treturn new HttpClientResponse<ResponseType>(\n\t\t\t\tf.status,\n\t\t\t\t{} as ResponseType,\n\t\t\t\tf.headers,\n\t\t\t);\n\t\t}\n\n\t\treturn new HttpClientResponse(f.status, await f.json(), f.headers);\n\t}\n\n\t/**\n\t * Obtain a valid bearer token\n\t */\n\tasync getToken(): Promise<string> {\n\t\tif (this.storage.expiresIn === null) {\n\t\t\tconst auth = await globalThis.fetch(\n\t\t\t\t`${this.shop.getShopUrl()}/api/oauth/token`,\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t\t\t},\n\t\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\t\tgrant_type: \"client_credentials\",\n\t\t\t\t\t\tclient_id: this.shop.getShopClientId(),\n\t\t\t\t\t\tclient_secret: this.shop.getShopClientSecret(),\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t);\n\n\t\t\tif (!auth.ok) {\n\t\t\t\tconst contentType = auth.headers.get(\"content-type\") || \"text/plain\";\n\t\t\t\tlet body = \"\";\n\n\t\t\t\tif (contentType.indexOf(\"application/json\") !== -1) {\n\t\t\t\t\tbody = await auth.json();\n\t\t\t\t} else {\n\t\t\t\t\tbody = await auth.text();\n\t\t\t\t}\n\n\t\t\t\tthrow new ApiClientAuthenticationFailed(\n\t\t\t\t\tthis.shop.getShopId(),\n\t\t\t\t\tnew HttpClientResponse<string>(auth.status, body, auth.headers),\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst expireDate = new Date();\n\t\t\tconst authBody = (await auth.json()) as {\n\t\t\t\taccess_token: string;\n\t\t\t\texpires_in: number;\n\t\t\t};\n\t\t\tthis.storage.token = authBody.access_token;\n\t\t\texpireDate.setSeconds(expireDate.getSeconds() + authBody.expires_in);\n\t\t\tthis.storage.expiresIn = expireDate;\n\n\t\t\treturn this.storage.token as string;\n\t\t}\n\n\t\tif (this.storage.expiresIn.getTime() < new Date().getTime()) {\n\t\t\t// Expired\n\n\t\t\tthis.storage.expiresIn = null;\n\n\t\t\treturn await this.getToken();\n\t\t}\n\n\t\treturn this.storage.token as string;\n\t}\n}\n\n/**\n * HttpClientResponse is the response object of the HttpClient\n */\nexport class HttpClientResponse<ResponseType> {\n\tconstructor(\n\t\tpublic statusCode: number,\n\t\tpublic body: ResponseType,\n\t\tpublic headers: Headers,\n\t) {}\n}\n\ntype ShopwareErrorResponse = {\n\terrors: {\n\t\tcode: string;\n\t\tstatus: string;\n\t\ttitle: string;\n\t\tdetail: string;\n\t}[];\n};\n\n/**\n * ApiClientAuthenticationFailed is thrown when the authentication to the shop's API fails\n */\nexport class ApiClientAuthenticationFailed extends Error {\n\tconstructor(\n\t\tshopId: string,\n\t\tpublic response: HttpClientResponse<string>,\n\t) {\n\t\tsuper(\n\t\t\t`The api client authentication to shop with id: ${shopId} with response: ${JSON.stringify(response.body)}`,\n\t\t);\n\t}\n}\n\n/**\n * ApiClientRequestFailed is thrown when the request to the shop's API fails\n */\nexport class ApiClientRequestFailed extends Error {\n\tconstructor(\n\t\tshopId: string,\n\t\tpublic response: HttpClientResponse<ShopwareErrorResponse>,\n\t) {\n\t\tsuper(\n\t\t\t`The api request failed with status code: ${response.statusCode} for shop with id: ${shopId}`,\n\t\t);\n\t}\n}\n"]}
1
+ {"version":3,"file":"http-client.js","sourceRoot":"","sources":["../../src/http-client.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACH,MAAa,UAAU;IAGF;IAFZ,OAAO,CAAmD;IAElE,YAAoB,IAAmB;QAAnB,SAAI,GAAJ,IAAI,CAAe;QACtC,IAAI,CAAC,OAAO,GAAG;YACd,KAAK,EAAE,IAAI;YACX,SAAS,EAAE,IAAI;SACf,CAAC;I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;GAEG;AACH,MAAa,sBAAuB,SAAQ,KAAK;IAGxC;IAFR,YACC,MAAc,EACP,QAAmD;QAE1D,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAErE,KAAK,CAAC,8BAA8B,OAAO,sBAAsB,MAAM,EAAE,CAAC,CAAC;QAJpE,aAAQ,GAAR,QAAQ,CAA2C;IAK3D,CAAC;CACD;AATD,wDASC","sourcesContent":["import type { ShopInterface } from \"./repository.js\";\n\n/**\n * HttpClient is a simple wrapper around the fetch API, pre-configured with the shop's URL and access token\n */\nexport class HttpClient {\n\tprivate storage: { expiresIn: Date | null; token: string | null };\n\n\tconstructor(private shop: ShopInterface) {\n\t\tthis.storage = {\n\t\t\ttoken: null,\n\t\t\texpiresIn: null,\n\t\t};\n\t}\n\n\t/**\n\t * Permform a GET request\n\t */\n\tasync get<ResponseType>(\n\t\turl: string,\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\treturn await this.request(\"GET\", url, null, headers);\n\t}\n\n\t/**\n\t * Permform a POST request\n\t */\n\tasync post<ResponseType>(\n\t\turl: string,\n\t\tjson: object = {},\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\theaders[\"content-type\"] = \"application/json\";\n\t\theaders.accept = \"application/json\";\n\n\t\treturn await this.request(\"POST\", url, JSON.stringify(json), headers);\n\t}\n\n\t/**\n\t * Permform a PUT request\n\t */\n\tasync put<ResponseType>(\n\t\turl: string,\n\t\tjson: object = {},\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\theaders[\"content-type\"] = \"application/json\";\n\t\theaders.accept = \"application/json\";\n\n\t\treturn await this.request(\"PUT\", url, JSON.stringify(json), headers);\n\t}\n\n\t/**\n\t * Permform a PATCH request\n\t */\n\tasync patch<ResponseType>(\n\t\turl: string,\n\t\tjson: object = {},\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\theaders[\"content-type\"] = \"application/json\";\n\t\theaders.accept = \"application/json\";\n\n\t\treturn await this.request(\"PATCH\", url, JSON.stringify(json), headers);\n\t}\n\n\t/**\n\t * Permform a DELETE request\n\t */\n\tasync delete<ResponseType>(\n\t\turl: string,\n\t\tjson: object = {},\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\theaders[\"content-type\"] = \"application/json\";\n\t\theaders.accept = \"application/json\";\n\n\t\treturn await this.request(\"DELETE\", url, JSON.stringify(json), headers);\n\t}\n\n\tprivate async request<ResponseType>(\n\t\tmethod: string,\n\t\turl: string,\n\t\tbody: string | null = \"\",\n\t\theaders: Record<string, string> = {},\n\t): Promise<HttpClientResponse<ResponseType>> {\n\t\tconst f = await globalThis.fetch(`${this.shop.getShopUrl()}/api${url}`, {\n\t\t\tbody,\n\t\t\theaders: Object.assign(\n\t\t\t\t{\n\t\t\t\t\tAuthorization: `Bearer ${await this.getToken()}`,\n\t\t\t\t},\n\t\t\t\theaders,\n\t\t\t),\n\t\t\tmethod,\n\t\t});\n\n\t\t// Obtain new token\n\t\tif (!f.ok && f.status === 401) {\n\t\t\tthis.storage.expiresIn = null;\n\n\t\t\treturn await this.request(method, url, body, headers);\n\t\t}\n\t\tif (!f.ok) {\n\t\t\tthrow new ApiClientRequestFailed(\n\t\t\t\tthis.shop.getShopId(),\n\t\t\t\tnew HttpClientResponse(f.status, await f.json(), f.headers),\n\t\t\t);\n\t\t}\n\n\t\tif (f.status === 204) {\n\t\t\treturn new HttpClientResponse<ResponseType>(\n\t\t\t\tf.status,\n\t\t\t\t{} as ResponseType,\n\t\t\t\tf.headers,\n\t\t\t);\n\t\t}\n\n\t\treturn new HttpClientResponse(f.status, await f.json(), f.headers);\n\t}\n\n\t/**\n\t * Obtain a valid bearer token\n\t */\n\tasync getToken(): Promise<string> {\n\t\tif (this.storage.expiresIn === null) {\n\t\t\tconst auth = await globalThis.fetch(\n\t\t\t\t`${this.shop.getShopUrl()}/api/oauth/token`,\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t\t\t},\n\t\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\t\tgrant_type: \"client_credentials\",\n\t\t\t\t\t\tclient_id: this.shop.getShopClientId(),\n\t\t\t\t\t\tclient_secret: this.shop.getShopClientSecret(),\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t);\n\n\t\t\tif (!auth.ok) {\n\t\t\t\tconst contentType = auth.headers.get(\"content-type\") || \"text/plain\";\n\t\t\t\tlet body = \"\";\n\n\t\t\t\tif (contentType.indexOf(\"application/json\") !== -1) {\n\t\t\t\t\tbody = await auth.json();\n\t\t\t\t} else {\n\t\t\t\t\tbody = await auth.text();\n\t\t\t\t}\n\n\t\t\t\tthrow new ApiClientAuthenticationFailed(\n\t\t\t\t\tthis.shop.getShopId(),\n\t\t\t\t\tnew HttpClientResponse<string>(auth.status, body, auth.headers),\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst expireDate = new Date();\n\t\t\tconst authBody = (await auth.json()) as {\n\t\t\t\taccess_token: string;\n\t\t\t\texpires_in: number;\n\t\t\t};\n\t\t\tthis.storage.token = authBody.access_token;\n\t\t\texpireDate.setSeconds(expireDate.getSeconds() + authBody.expires_in);\n\t\t\tthis.storage.expiresIn = expireDate;\n\n\t\t\treturn this.storage.token as string;\n\t\t}\n\n\t\tif (this.storage.expiresIn.getTime() < new Date().getTime()) {\n\t\t\t// Expired\n\n\t\t\tthis.storage.expiresIn = null;\n\n\t\t\treturn await this.getToken();\n\t\t}\n\n\t\treturn this.storage.token as string;\n\t}\n}\n\n/**\n * HttpClientResponse is the response object of the HttpClient\n */\nexport class HttpClientResponse<ResponseType> {\n\tconstructor(\n\t\tpublic statusCode: number,\n\t\tpublic body: ResponseType,\n\t\tpublic headers: Headers,\n\t) {}\n}\n\ntype ShopwareErrorResponse = {\n\terrors: {\n\t\tcode: string;\n\t\tstatus: string;\n\t\ttitle: string;\n\t\tdetail: string;\n\t}[];\n};\n\n/**\n * ApiClientAuthenticationFailed is thrown when the authentication to the shop's API fails\n */\nexport class ApiClientAuthenticationFailed extends Error {\n\tconstructor(\n\t\tshopId: string,\n\t\tpublic response: HttpClientResponse<string>,\n\t) {\n\t\tsuper(\n\t\t\t`The api client authentication to shop with id: ${shopId} with response: ${JSON.stringify(response.body)}`,\n\t\t);\n\t}\n}\n\n/**\n * ApiClientRequestFailed is thrown when the request to the shop's API fails\n */\nexport class ApiClientRequestFailed extends Error {\n\tconstructor(\n\t\tshopId: string,\n\t\tpublic response: HttpClientResponse<ShopwareErrorResponse>,\n\t) {\n\t\tconst message = response.body.errors.map((e) => e.detail).join(\", \");\n\n\t\tsuper(`Request failed with error: ${message} for shop with id: ${shopId}`);\n\t}\n}\n"]}
@@ -0,0 +1,11 @@
1
+ import { type ShopRepositoryInterface, SimpleShop } from "../repository.js";
2
+ import Database from "better-sqlite3";
3
+ export declare class BetterSqlite3Repository implements ShopRepositoryInterface<SimpleShop> {
4
+ db: Database.Database;
5
+ constructor(fileName: string);
6
+ createShop(id: string, url: string, secret: string): Promise<void>;
7
+ getShopById(id: string): Promise<SimpleShop | null>;
8
+ updateShop(shop: SimpleShop): Promise<void>;
9
+ deleteShop(id: string): Promise<void>;
10
+ }
11
+ //# sourceMappingURL=better-sqlite3.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"better-sqlite3.d.ts","sourceRoot":"","sources":["../../../src/integration/better-sqlite3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,uBAAuB,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE5E,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAEtC,qBAAa,uBACZ,YAAW,uBAAuB,CAAC,UAAU,CAAC;IAE9C,EAAE,EAAE,QAAQ,CAAC,QAAQ,CAAC;gBACV,QAAQ,EAAE,MAAM;IAetB,UAAU,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAYlE,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;IA0BnD,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAc3C,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAG3C"}
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BetterSqlite3Repository = void 0;
7
+ const repository_js_1 = require("../repository.js");
8
+ const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
9
+ class BetterSqlite3Repository {
10
+ db;
11
+ constructor(fileName) {
12
+ this.db = new better_sqlite3_1.default(fileName);
13
+ this.db.pragma("journal_mode = WAL");
14
+ this.db.exec(`
15
+ CREATE TABLE IF NOT EXISTS shop (
16
+ id TEXT PRIMARY KEY,
17
+ active BOOLEAN DEFAULT 1,
18
+ url TEXT NOT NULL,
19
+ secret TEXT NOT NULL,
20
+ client_id TEXT NULL,
21
+ client_secret TEXT NULL
22
+ );
23
+ `);
24
+ }
25
+ async createShop(id, url, secret) {
26
+ const shop = await this.getShopById(id);
27
+ if (shop) {
28
+ return await this.updateShop(shop);
29
+ }
30
+ this.db
31
+ .prepare("INSERT INTO shop (id, url, secret) VALUES (?, ?, ?)")
32
+ .run(id, url, secret);
33
+ }
34
+ async getShopById(id) {
35
+ const result = this.db
36
+ .prepare("SELECT * FROM shop WHERE id = ?")
37
+ .get(id);
38
+ if (!result) {
39
+ return null;
40
+ }
41
+ const shop = new repository_js_1.SimpleShop(result.id, result.url, result.secret);
42
+ if (result.client_id && result.client_secret) {
43
+ shop.setShopCredentials(result.client_id, result.client_secret);
44
+ }
45
+ shop.setShopActive(result.active === 1);
46
+ return shop;
47
+ }
48
+ async updateShop(shop) {
49
+ this.db
50
+ .prepare("UPDATE shop SET url = ?, secret = ?, client_id = ?, client_secret = ?, active = ? WHERE id = ?")
51
+ .run(shop.getShopUrl(), shop.getShopSecret(), shop.getShopClientId(), shop.getShopClientSecret(), +shop.getShopActive(), shop.getShopId());
52
+ }
53
+ async deleteShop(id) {
54
+ this.db.prepare("DELETE FROM shop where id = ?").run(id);
55
+ }
56
+ }
57
+ exports.BetterSqlite3Repository = BetterSqlite3Repository;
58
+ //# sourceMappingURL=better-sqlite3.js.map