@opra/core 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/enums/http-headers.enum.js +3 -2
- package/cjs/implementation/adapter-utils/entity-resource-execute.util.js +36 -34
- package/cjs/implementation/adapter-utils/resource-prepare.util.js +1 -1
- package/cjs/implementation/adapter.js +14 -19
- package/cjs/implementation/express-adapter.js +3 -0
- package/cjs/implementation/headers-map.js +18 -0
- package/cjs/implementation/http-adapter.js +65 -79
- package/cjs/implementation/query-context.js +12 -19
- package/cjs/index.js +0 -2
- package/cjs/services/json-data-service.js +367 -131
- package/cjs/utils/path-to-tree.js +7 -5
- package/esm/enums/http-headers.enum.d.ts +3 -2
- package/esm/enums/http-headers.enum.js +3 -2
- package/esm/implementation/adapter-utils/entity-resource-execute.util.js +35 -33
- package/esm/implementation/adapter-utils/resource-execute.util.d.ts +2 -2
- package/esm/implementation/adapter-utils/resource-prepare.util.d.ts +2 -2
- package/esm/implementation/adapter-utils/resource-prepare.util.js +1 -1
- package/esm/implementation/adapter.d.ts +2 -2
- package/esm/implementation/adapter.js +12 -17
- package/esm/implementation/express-adapter.js +2 -0
- package/esm/implementation/headers-map.d.ts +5 -0
- package/esm/implementation/headers-map.js +14 -0
- package/esm/implementation/http-adapter.d.ts +6 -6
- package/esm/implementation/http-adapter.js +61 -75
- package/esm/implementation/query-context.d.ts +9 -15
- package/esm/implementation/query-context.js +11 -17
- package/esm/index.d.ts +0 -2
- package/esm/index.js +0 -2
- package/esm/interfaces/entity-service.interface.d.ts +8 -6
- package/esm/services/json-data-service.d.ts +56 -39
- package/esm/services/json-data-service.js +365 -129
- package/esm/types.d.ts +0 -3
- package/esm/utils/path-to-tree.d.ts +1 -1
- package/esm/utils/path-to-tree.js +7 -5
- package/i18n/en/error.json +5 -5
- package/package.json +11 -8
- package/cjs/exception/api-exception.js +0 -68
- package/cjs/exception/http-errors/bad-request.error.js +0 -26
- package/cjs/exception/http-errors/failed-dependency.error.js +0 -25
- package/cjs/exception/http-errors/forbidden.error.js +0 -27
- package/cjs/exception/http-errors/internal-server.error.js +0 -27
- package/cjs/exception/http-errors/method-not-allowed.error.js +0 -26
- package/cjs/exception/http-errors/not-acceptable.error.js +0 -26
- package/cjs/exception/http-errors/not-found.error.js +0 -29
- package/cjs/exception/http-errors/unauthorized.error.js +0 -26
- package/cjs/exception/http-errors/unprocessable-entity.error.js +0 -25
- package/cjs/exception/index.js +0 -15
- package/cjs/exception/resource-errors/resource-conflict.error.js +0 -19
- package/cjs/exception/resource-errors/resource-not-found.error.js +0 -19
- package/cjs/exception/wrap-error.js +0 -17
- package/cjs/interfaces/query.interface.js +0 -207
- package/esm/exception/api-exception.d.ts +0 -40
- package/esm/exception/api-exception.js +0 -64
- package/esm/exception/http-errors/bad-request.error.d.ts +0 -10
- package/esm/exception/http-errors/bad-request.error.js +0 -22
- package/esm/exception/http-errors/failed-dependency.error.d.ts +0 -9
- package/esm/exception/http-errors/failed-dependency.error.js +0 -21
- package/esm/exception/http-errors/forbidden.error.d.ts +0 -11
- package/esm/exception/http-errors/forbidden.error.js +0 -23
- package/esm/exception/http-errors/internal-server.error.d.ts +0 -9
- package/esm/exception/http-errors/internal-server.error.js +0 -22
- package/esm/exception/http-errors/method-not-allowed.error.d.ts +0 -10
- package/esm/exception/http-errors/method-not-allowed.error.js +0 -22
- package/esm/exception/http-errors/not-acceptable.error.d.ts +0 -10
- package/esm/exception/http-errors/not-acceptable.error.js +0 -22
- package/esm/exception/http-errors/not-found.error.d.ts +0 -13
- package/esm/exception/http-errors/not-found.error.js +0 -25
- package/esm/exception/http-errors/unauthorized.error.d.ts +0 -10
- package/esm/exception/http-errors/unauthorized.error.js +0 -22
- package/esm/exception/http-errors/unprocessable-entity.error.d.ts +0 -9
- package/esm/exception/http-errors/unprocessable-entity.error.js +0 -21
- package/esm/exception/index.d.ts +0 -12
- package/esm/exception/index.js +0 -12
- package/esm/exception/resource-errors/resource-conflict.error.d.ts +0 -4
- package/esm/exception/resource-errors/resource-conflict.error.js +0 -15
- package/esm/exception/resource-errors/resource-not-found.error.d.ts +0 -4
- package/esm/exception/resource-errors/resource-not-found.error.js +0 -15
- package/esm/exception/wrap-error.d.ts +0 -2
- package/esm/exception/wrap-error.js +0 -13
- package/esm/interfaces/query.interface.d.ts +0 -115
- package/esm/interfaces/query.interface.js +0 -203
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import { ComplexType } from '@opra/schema';
|
|
3
|
-
import { pathToTree } from '../utils/path-to-tree.js';
|
|
4
|
-
export var OpraQuery;
|
|
5
|
-
(function (OpraQuery) {
|
|
6
|
-
function forCreate(resource, values, options) {
|
|
7
|
-
if (options?.pick)
|
|
8
|
-
options.pick = normalizePick(resource, options.pick);
|
|
9
|
-
if (options?.omit)
|
|
10
|
-
options.omit = normalizePick(resource, options.omit);
|
|
11
|
-
if (options?.include)
|
|
12
|
-
options.include = normalizePick(resource, options.include);
|
|
13
|
-
const out = {
|
|
14
|
-
queryType: 'create',
|
|
15
|
-
scope: 'collection',
|
|
16
|
-
operation: 'create',
|
|
17
|
-
resource,
|
|
18
|
-
data: values
|
|
19
|
-
};
|
|
20
|
-
Object.assign(out, _.omit(options, Object.keys(out)));
|
|
21
|
-
return out;
|
|
22
|
-
}
|
|
23
|
-
OpraQuery.forCreate = forCreate;
|
|
24
|
-
function forGetSchema(resourcePath, options) {
|
|
25
|
-
// if (options?.pick)
|
|
26
|
-
// options.pick = normalizePick(resource, options.pick);
|
|
27
|
-
// if (options?.omit)
|
|
28
|
-
// options.omit = normalizePick(resource, options.omit);
|
|
29
|
-
// if (options?.include)
|
|
30
|
-
// options.include = normalizePick(resource, options.include);
|
|
31
|
-
const out = {
|
|
32
|
-
queryType: 'schema',
|
|
33
|
-
scope: 'instance',
|
|
34
|
-
resourcePath,
|
|
35
|
-
operation: 'read',
|
|
36
|
-
};
|
|
37
|
-
Object.assign(out, _.omit(options, Object.keys(out)));
|
|
38
|
-
return out;
|
|
39
|
-
}
|
|
40
|
-
OpraQuery.forGetSchema = forGetSchema;
|
|
41
|
-
function forGetEntity(resource, key, options) {
|
|
42
|
-
if (options?.pick)
|
|
43
|
-
options.pick = normalizePick(resource, options.pick);
|
|
44
|
-
if (options?.omit)
|
|
45
|
-
options.omit = normalizePick(resource, options.omit);
|
|
46
|
-
if (options?.include)
|
|
47
|
-
options.include = normalizePick(resource, options.include);
|
|
48
|
-
checkKeyFields(resource, key);
|
|
49
|
-
const out = {
|
|
50
|
-
queryType: 'get',
|
|
51
|
-
scope: 'instance',
|
|
52
|
-
operation: 'read',
|
|
53
|
-
resource,
|
|
54
|
-
keyValue: key
|
|
55
|
-
};
|
|
56
|
-
Object.assign(out, _.omit(options, Object.keys(out)));
|
|
57
|
-
return out;
|
|
58
|
-
}
|
|
59
|
-
OpraQuery.forGetEntity = forGetEntity;
|
|
60
|
-
function forSearch(resource, options) {
|
|
61
|
-
if (options?.pick)
|
|
62
|
-
options.pick = normalizePick(resource, options.pick);
|
|
63
|
-
if (options?.omit)
|
|
64
|
-
options.omit = normalizePick(resource, options.omit);
|
|
65
|
-
if (options?.include)
|
|
66
|
-
options.include = normalizePick(resource, options.include);
|
|
67
|
-
if (options?.sort)
|
|
68
|
-
options.sort = normalizePick(resource, options.sort); // todo check allowed sort fields
|
|
69
|
-
const out = {
|
|
70
|
-
queryType: 'search',
|
|
71
|
-
scope: 'collection',
|
|
72
|
-
operation: 'read',
|
|
73
|
-
resource
|
|
74
|
-
};
|
|
75
|
-
Object.assign(out, _.omit(options, Object.keys(out)));
|
|
76
|
-
return out;
|
|
77
|
-
}
|
|
78
|
-
OpraQuery.forSearch = forSearch;
|
|
79
|
-
function forGetProperty(property, options) {
|
|
80
|
-
const out = {
|
|
81
|
-
queryType: 'get',
|
|
82
|
-
scope: 'property',
|
|
83
|
-
operation: 'read',
|
|
84
|
-
property
|
|
85
|
-
};
|
|
86
|
-
Object.assign(out, _.omit(options, Object.keys(out)));
|
|
87
|
-
return out;
|
|
88
|
-
}
|
|
89
|
-
OpraQuery.forGetProperty = forGetProperty;
|
|
90
|
-
function forUpdate(resource, keyValue, values, options) {
|
|
91
|
-
if (options?.pick)
|
|
92
|
-
options.pick = normalizePick(resource, options.pick);
|
|
93
|
-
if (options?.omit)
|
|
94
|
-
options.omit = normalizePick(resource, options.omit);
|
|
95
|
-
if (options?.include)
|
|
96
|
-
options.include = normalizePick(resource, options.include);
|
|
97
|
-
checkKeyFields(resource, keyValue);
|
|
98
|
-
const out = {
|
|
99
|
-
queryType: 'update',
|
|
100
|
-
scope: 'instance',
|
|
101
|
-
operation: 'update',
|
|
102
|
-
resource,
|
|
103
|
-
keyValue,
|
|
104
|
-
data: values
|
|
105
|
-
};
|
|
106
|
-
Object.assign(out, _.omit(options, Object.keys(out)));
|
|
107
|
-
return out;
|
|
108
|
-
}
|
|
109
|
-
OpraQuery.forUpdate = forUpdate;
|
|
110
|
-
function forUpdateMany(resource, values, options) {
|
|
111
|
-
const out = {
|
|
112
|
-
queryType: 'updateMany',
|
|
113
|
-
scope: 'collection',
|
|
114
|
-
operation: 'update',
|
|
115
|
-
resource,
|
|
116
|
-
data: values
|
|
117
|
-
};
|
|
118
|
-
Object.assign(out, _.omit(options, Object.keys(out)));
|
|
119
|
-
return out;
|
|
120
|
-
}
|
|
121
|
-
OpraQuery.forUpdateMany = forUpdateMany;
|
|
122
|
-
function forDelete(resource, key) {
|
|
123
|
-
checkKeyFields(resource, key);
|
|
124
|
-
return {
|
|
125
|
-
queryType: 'delete',
|
|
126
|
-
scope: 'instance',
|
|
127
|
-
operation: 'delete',
|
|
128
|
-
resource,
|
|
129
|
-
keyValue: key
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
OpraQuery.forDelete = forDelete;
|
|
133
|
-
function forDeleteMany(resource, options) {
|
|
134
|
-
const out = {
|
|
135
|
-
queryType: 'deleteMany',
|
|
136
|
-
scope: 'collection',
|
|
137
|
-
operation: 'delete',
|
|
138
|
-
resource,
|
|
139
|
-
};
|
|
140
|
-
Object.assign(out, _.omit(options, Object.keys(out)));
|
|
141
|
-
return out;
|
|
142
|
-
}
|
|
143
|
-
OpraQuery.forDeleteMany = forDeleteMany;
|
|
144
|
-
function isCreateQuery(q) {
|
|
145
|
-
return q && typeof q === 'object' && q.scope === 'collection' && q.queryType === 'create';
|
|
146
|
-
}
|
|
147
|
-
OpraQuery.isCreateQuery = isCreateQuery;
|
|
148
|
-
function isSearchQuery(q) {
|
|
149
|
-
return q && typeof q === 'object' && q.scope === 'collection' && q.queryType === 'search';
|
|
150
|
-
}
|
|
151
|
-
OpraQuery.isSearchQuery = isSearchQuery;
|
|
152
|
-
function isReadQuery(q) {
|
|
153
|
-
return q && typeof q === 'object' && q.scope === 'instance' && q.queryType === 'read';
|
|
154
|
-
}
|
|
155
|
-
OpraQuery.isReadQuery = isReadQuery;
|
|
156
|
-
function isDeleteQuery(q) {
|
|
157
|
-
return q && typeof q === 'object' && q.scope === 'instance' && q.queryType === 'delete';
|
|
158
|
-
}
|
|
159
|
-
OpraQuery.isDeleteQuery = isDeleteQuery;
|
|
160
|
-
})(OpraQuery || (OpraQuery = {}));
|
|
161
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
162
|
-
function checkKeyFields(resource, key) {
|
|
163
|
-
if (!resource.dataType.primaryKey)
|
|
164
|
-
throw new Error(`"${resource.name}" has no primary key`);
|
|
165
|
-
}
|
|
166
|
-
function normalizePick(resource, fields) {
|
|
167
|
-
const fieldsTree = pathToTree(fields) || {};
|
|
168
|
-
return _normalizeFieldsList([], resource.service, resource, resource.dataType, fieldsTree, '', {
|
|
169
|
-
additionalFields: true
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
function _normalizeFieldsList(target, service, resource, dataType, node, parentPath = '', options) {
|
|
173
|
-
let curPath = '';
|
|
174
|
-
for (const k of Object.keys(node)) {
|
|
175
|
-
const nodeVal = node[k];
|
|
176
|
-
const prop = dataType?.fields.get(k);
|
|
177
|
-
if (!prop) {
|
|
178
|
-
curPath = parentPath ? parentPath + '.' + k : k;
|
|
179
|
-
if (!options?.additionalFields || (dataType && !dataType.additionalFields))
|
|
180
|
-
throw new TypeError(`Unknown field path "${resource.name + '.' + curPath}"`);
|
|
181
|
-
if (typeof nodeVal === 'object')
|
|
182
|
-
_normalizeFieldsList(target, service, resource, undefined, nodeVal, curPath, options);
|
|
183
|
-
else
|
|
184
|
-
target.push(curPath);
|
|
185
|
-
continue;
|
|
186
|
-
}
|
|
187
|
-
curPath = parentPath ? parentPath + '.' + prop.name : prop.name;
|
|
188
|
-
const propType = service.getDataType(prop.type || 'string');
|
|
189
|
-
if (typeof nodeVal === 'object') {
|
|
190
|
-
if (!(propType && propType instanceof ComplexType))
|
|
191
|
-
throw new TypeError(`"${curPath}" is not a complex type and has no sub fields`);
|
|
192
|
-
if (target.findIndex(x => x === parentPath) >= 0)
|
|
193
|
-
continue;
|
|
194
|
-
target = _normalizeFieldsList(target, service, resource, propType, nodeVal, curPath, options);
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
if (target.findIndex(x => x.startsWith(curPath + '.')) >= 0) {
|
|
198
|
-
target = target.filter(x => !x.startsWith(curPath + '.'));
|
|
199
|
-
}
|
|
200
|
-
target.push(curPath);
|
|
201
|
-
}
|
|
202
|
-
return target;
|
|
203
|
-
}
|