@opra/core 0.1.1 → 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.
Files changed (86) hide show
  1. package/cjs/enums/http-headers.enum.js +3 -2
  2. package/cjs/implementation/adapter-utils/entity-resource-execute.util.js +36 -34
  3. package/cjs/implementation/adapter-utils/resource-prepare.util.js +1 -1
  4. package/cjs/implementation/adapter.js +14 -19
  5. package/cjs/implementation/express-adapter.js +3 -0
  6. package/cjs/implementation/headers-map.js +18 -0
  7. package/cjs/implementation/http-adapter.js +65 -79
  8. package/cjs/implementation/query-context.js +12 -19
  9. package/cjs/index.js +1 -3
  10. package/cjs/services/json-collection-service.js +505 -0
  11. package/cjs/utils/path-to-tree.js +7 -5
  12. package/esm/enums/http-headers.enum.d.ts +3 -2
  13. package/esm/enums/http-headers.enum.js +3 -2
  14. package/esm/implementation/adapter-utils/entity-resource-execute.util.d.ts +2 -2
  15. package/esm/implementation/adapter-utils/entity-resource-execute.util.js +35 -33
  16. package/esm/implementation/adapter-utils/resource-execute.util.d.ts +2 -2
  17. package/esm/implementation/adapter-utils/resource-prepare.util.d.ts +2 -2
  18. package/esm/implementation/adapter-utils/resource-prepare.util.js +1 -1
  19. package/esm/implementation/adapter.d.ts +5 -5
  20. package/esm/implementation/adapter.js +12 -17
  21. package/esm/implementation/express-adapter.d.ts +2 -2
  22. package/esm/implementation/express-adapter.js +2 -0
  23. package/esm/implementation/headers-map.d.ts +5 -0
  24. package/esm/implementation/headers-map.js +14 -0
  25. package/esm/implementation/http-adapter.d.ts +6 -6
  26. package/esm/implementation/http-adapter.js +61 -75
  27. package/esm/implementation/query-context.d.ts +10 -16
  28. package/esm/implementation/query-context.js +11 -17
  29. package/esm/index.d.ts +1 -3
  30. package/esm/index.js +1 -3
  31. package/esm/interfaces/entity-service.interface.d.ts +8 -6
  32. package/esm/services/json-collection-service.d.ts +85 -0
  33. package/esm/services/json-collection-service.js +500 -0
  34. package/esm/types.d.ts +0 -3
  35. package/esm/utils/path-to-tree.d.ts +1 -1
  36. package/esm/utils/path-to-tree.js +7 -5
  37. package/i18n/en/error.json +5 -5
  38. package/package.json +10 -7
  39. package/cjs/exception/api-exception.js +0 -68
  40. package/cjs/exception/http-errors/bad-request.error.js +0 -26
  41. package/cjs/exception/http-errors/failed-dependency.error.js +0 -25
  42. package/cjs/exception/http-errors/forbidden.error.js +0 -27
  43. package/cjs/exception/http-errors/internal-server.error.js +0 -27
  44. package/cjs/exception/http-errors/method-not-allowed.error.js +0 -26
  45. package/cjs/exception/http-errors/not-acceptable.error.js +0 -26
  46. package/cjs/exception/http-errors/not-found.error.js +0 -29
  47. package/cjs/exception/http-errors/unauthorized.error.js +0 -26
  48. package/cjs/exception/http-errors/unprocessable-entity.error.js +0 -25
  49. package/cjs/exception/index.js +0 -15
  50. package/cjs/exception/resource-errors/resource-conflict.error.js +0 -19
  51. package/cjs/exception/resource-errors/resource-not-found.error.js +0 -19
  52. package/cjs/exception/wrap-error.js +0 -17
  53. package/cjs/interfaces/query.interface.js +0 -207
  54. package/cjs/services/json-data-service.js +0 -252
  55. package/esm/exception/api-exception.d.ts +0 -40
  56. package/esm/exception/api-exception.js +0 -64
  57. package/esm/exception/http-errors/bad-request.error.d.ts +0 -10
  58. package/esm/exception/http-errors/bad-request.error.js +0 -22
  59. package/esm/exception/http-errors/failed-dependency.error.d.ts +0 -9
  60. package/esm/exception/http-errors/failed-dependency.error.js +0 -21
  61. package/esm/exception/http-errors/forbidden.error.d.ts +0 -11
  62. package/esm/exception/http-errors/forbidden.error.js +0 -23
  63. package/esm/exception/http-errors/internal-server.error.d.ts +0 -9
  64. package/esm/exception/http-errors/internal-server.error.js +0 -22
  65. package/esm/exception/http-errors/method-not-allowed.error.d.ts +0 -10
  66. package/esm/exception/http-errors/method-not-allowed.error.js +0 -22
  67. package/esm/exception/http-errors/not-acceptable.error.d.ts +0 -10
  68. package/esm/exception/http-errors/not-acceptable.error.js +0 -22
  69. package/esm/exception/http-errors/not-found.error.d.ts +0 -13
  70. package/esm/exception/http-errors/not-found.error.js +0 -25
  71. package/esm/exception/http-errors/unauthorized.error.d.ts +0 -10
  72. package/esm/exception/http-errors/unauthorized.error.js +0 -22
  73. package/esm/exception/http-errors/unprocessable-entity.error.d.ts +0 -9
  74. package/esm/exception/http-errors/unprocessable-entity.error.js +0 -21
  75. package/esm/exception/index.d.ts +0 -12
  76. package/esm/exception/index.js +0 -12
  77. package/esm/exception/resource-errors/resource-conflict.error.d.ts +0 -4
  78. package/esm/exception/resource-errors/resource-conflict.error.js +0 -15
  79. package/esm/exception/resource-errors/resource-not-found.error.d.ts +0 -4
  80. package/esm/exception/resource-errors/resource-not-found.error.js +0 -15
  81. package/esm/exception/wrap-error.d.ts +0 -2
  82. package/esm/exception/wrap-error.js +0 -13
  83. package/esm/interfaces/query.interface.d.ts +0 -115
  84. package/esm/interfaces/query.interface.js +0 -203
  85. package/esm/services/json-data-service.d.ts +0 -68
  86. package/esm/services/json-data-service.js +0 -247
@@ -1,247 +0,0 @@
1
- import _ from 'lodash';
2
- import ruleJudgment from 'rule-judgment';
3
- import { $parse, ArrayExpression, BooleanLiteral, ComparisonExpression, DateLiteral, Expression, LogicalExpression, NullLiteral, NumberLiteral, ParenthesesExpression, QualifiedIdentifier, StringLiteral, TimeLiteral } from '@opra/url';
4
- import { ResourceConflictError } from '../exception/index.js';
5
- // Fix invalid importing (with ESM) of rule-judgment package
6
- const createFilterFn = typeof ruleJudgment === 'function'
7
- ? ruleJudgment
8
- : ruleJudgment.default;
9
- export class JsonDataService {
10
- resourceName;
11
- data;
12
- primaryKey;
13
- constructor(args) {
14
- this.resourceName = args.resourceName;
15
- this.data = args.data;
16
- this.primaryKey = args.primaryKey;
17
- }
18
- processRequest(ctx) {
19
- const prepared = JsonDataService.prepare(ctx.query);
20
- const fn = this[prepared.method];
21
- if (!fn)
22
- throw new TypeError(`Unimplemented method (${prepared.method})`);
23
- return fn.apply(this, prepared.args);
24
- }
25
- get(keyValue, options) {
26
- const primaryKey = this.primaryKey;
27
- let v = this.data.find(x => '' + x[primaryKey] === '' + keyValue);
28
- v = JsonDataService.filterFields(v, options?.pick, options?.omit, options?.include);
29
- return v;
30
- }
31
- count(options) {
32
- return this.search(options).length;
33
- }
34
- search(options) {
35
- let out;
36
- if (options?.filter) {
37
- const filter = JsonDataService.convertFilter(options.filter);
38
- const filterFn = createFilterFn(filter);
39
- out = this.data.filter(filterFn);
40
- }
41
- else
42
- out = this.data;
43
- return out.map(v => JsonDataService.filterFields(v, options?.pick, options?.omit, options?.include));
44
- }
45
- create(data, options) {
46
- if (this.get(data[this.primaryKey]))
47
- throw new ResourceConflictError(this.resourceName, this.primaryKey);
48
- this.data.push(data);
49
- return JsonDataService.filterFields(data, options?.pick, options?.omit, options?.include);
50
- }
51
- update(keyValue, data, options) {
52
- const primaryKey = this.primaryKey;
53
- const i = this.data.findIndex(x => '' + x[primaryKey] === '' + keyValue);
54
- if (i >= 0) {
55
- data = Object.assign(this.data[i], data);
56
- return JsonDataService.filterFields(data, options?.pick, options?.omit, options?.include);
57
- }
58
- }
59
- updateMany(data, options) {
60
- const items = this.search({ filter: options?.filter });
61
- for (let i = 0; i < items.length; i++) {
62
- Object.assign(items[i], data);
63
- }
64
- return items.length;
65
- }
66
- delete(keyValue) {
67
- const primaryKey = this.primaryKey;
68
- const i = this.data.findIndex(x => '' + x[primaryKey] === '' + keyValue);
69
- if (i >= 0) {
70
- this.data = this.data.slice(i, 1);
71
- return true;
72
- }
73
- return false;
74
- }
75
- deleteMany(options) {
76
- const items = this.search({ filter: options?.filter });
77
- this.data = this.data.filter(x => !items.includes(x));
78
- return items.length;
79
- }
80
- static filterFields(obj,
81
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
82
- pick,
83
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
84
- omit,
85
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
86
- include) {
87
- if (!obj)
88
- return;
89
- return obj;
90
- }
91
- static prepare(query) {
92
- switch (query.queryType) {
93
- case 'create': {
94
- const options = _.omitBy({
95
- pick: query.pick?.length ? query.pick : undefined,
96
- omit: query.omit?.length ? query.omit : undefined,
97
- include: query.include?.length ? query.include : undefined,
98
- }, _.isNil);
99
- const { data } = query;
100
- return {
101
- method: query.queryType,
102
- values: data,
103
- options,
104
- args: [data, options]
105
- };
106
- }
107
- case 'get': {
108
- const options = _.omitBy({
109
- pick: query.pick?.length ? query.pick : undefined,
110
- omit: query.omit?.length ? query.omit : undefined,
111
- include: query.include?.length ? query.include : undefined,
112
- }, _.isNil);
113
- const keyValue = query.keyValue;
114
- return {
115
- method: query.queryType,
116
- keyValue,
117
- options,
118
- args: [keyValue, options]
119
- };
120
- }
121
- case 'search': {
122
- const options = _.omitBy({
123
- pick: query.pick?.length ? query.pick : undefined,
124
- omit: query.omit?.length ? query.omit : undefined,
125
- include: query.include?.length ? query.include : undefined,
126
- sort: query.sort?.length ? query.sort : undefined,
127
- limit: query.limit,
128
- offset: query.skip,
129
- distinct: query.distinct,
130
- total: query.count,
131
- filter: JsonDataService.convertFilter(query.filter)
132
- }, _.isNil);
133
- return {
134
- method: query.queryType,
135
- options,
136
- args: [options]
137
- };
138
- }
139
- case 'update': {
140
- const options = _.omitBy({
141
- pick: query.pick?.length ? query.pick : undefined,
142
- omit: query.omit?.length ? query.omit : undefined,
143
- include: query.include?.length ? query.include : undefined,
144
- }, _.isNil);
145
- const { data } = query;
146
- const keyValue = query.keyValue;
147
- return {
148
- method: query.queryType,
149
- keyValue: query.keyValue,
150
- values: data,
151
- options,
152
- args: [keyValue, data, options]
153
- };
154
- }
155
- case 'updateMany': {
156
- const options = _.omitBy({
157
- filter: JsonDataService.convertFilter(query.filter)
158
- }, _.isNil);
159
- const { data } = query;
160
- return {
161
- method: query.queryType,
162
- options,
163
- args: [data, options]
164
- };
165
- }
166
- case 'delete': {
167
- const options = {};
168
- const keyValue = query.keyValue;
169
- return {
170
- method: query.queryType,
171
- keyValue,
172
- options,
173
- args: [keyValue, options]
174
- };
175
- }
176
- case 'deleteMany': {
177
- const options = _.omitBy({
178
- filter: JsonDataService.convertFilter(query.filter)
179
- }, _.isNil);
180
- return {
181
- method: query.queryType,
182
- options,
183
- args: [options]
184
- };
185
- }
186
- default:
187
- throw new Error(`Unimplemented query type "${query.queryType}"`);
188
- }
189
- }
190
- static convertFilter(str) {
191
- const ast = typeof str === 'string'
192
- ? $parse(str)
193
- : str;
194
- if (!ast || !(ast instanceof Expression))
195
- return ast;
196
- if (ast instanceof ComparisonExpression) {
197
- const left = JsonDataService.convertFilter(ast.left);
198
- const right = JsonDataService.convertFilter(ast.right);
199
- switch (ast.op) {
200
- case '=':
201
- return { $eq: { [left]: right } };
202
- case '!=':
203
- return { $ne: { [left]: right } };
204
- case '>':
205
- return { $gt: { [left]: right } };
206
- case '>=':
207
- return { $gte: { [left]: right } };
208
- case '<':
209
- return { $lt: { [left]: right } };
210
- case '<=':
211
- return { $lte: { [left]: right } };
212
- case 'in':
213
- return { $in: { [left]: right } };
214
- case '!in':
215
- return { $nin: { [left]: right } };
216
- default:
217
- throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
218
- }
219
- }
220
- if (ast instanceof QualifiedIdentifier) {
221
- return ast.value;
222
- }
223
- if (ast instanceof NumberLiteral ||
224
- ast instanceof StringLiteral ||
225
- ast instanceof BooleanLiteral ||
226
- ast instanceof NullLiteral ||
227
- ast instanceof DateLiteral ||
228
- ast instanceof TimeLiteral) {
229
- return ast.value;
230
- }
231
- if (ast instanceof ArrayExpression) {
232
- return ast.items.map(JsonDataService.convertFilter);
233
- }
234
- if (ast instanceof LogicalExpression) {
235
- if (ast.op === 'or')
236
- return { $or: ast.items.map(JsonDataService.convertFilter) };
237
- return { $and: ast.items.map(JsonDataService.convertFilter) };
238
- }
239
- if (ast instanceof ArrayExpression) {
240
- return ast.items.map(JsonDataService.convertFilter);
241
- }
242
- if (ast instanceof ParenthesesExpression) {
243
- return JsonDataService.convertFilter(ast.expression);
244
- }
245
- throw new Error(`${ast.type} is not implemented yet`);
246
- }
247
- }