@aeriajs/core 0.0.95 → 0.0.97
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/dist/assets.d.ts +39 -3
- package/dist/assets.js +18 -20
- package/dist/assets.mjs +19 -21
- package/dist/collection/cascadingRemove.js +6 -7
- package/dist/collection/cascadingRemove.mjs +6 -7
- package/dist/collection/preload.js +5 -4
- package/dist/collection/preload.mjs +6 -5
- package/dist/collection/prepareInsert.js +2 -1
- package/dist/collection/prepareInsert.mjs +2 -1
- package/dist/collection/reference.js +4 -4
- package/dist/collection/reference.mjs +5 -5
- package/dist/collection/traverseDocument.d.ts +9 -1
- package/dist/collection/traverseDocument.js +37 -34
- package/dist/collection/traverseDocument.mjs +41 -38
- package/dist/context.js +4 -4
- package/dist/context.mjs +5 -5
- package/dist/functions/builtin/count.d.ts +5 -1
- package/dist/functions/builtin/count.js +5 -5
- package/dist/functions/builtin/count.mjs +5 -5
- package/dist/functions/builtin/get.js +9 -6
- package/dist/functions/builtin/get.mjs +10 -7
- package/dist/functions/builtin/getAll.d.ts +5 -1
- package/dist/functions/builtin/getAll.js +4 -4
- package/dist/functions/builtin/getAll.mjs +5 -5
- package/dist/functions/builtin/insert.d.ts +1 -20
- package/dist/functions/builtin/insert.js +6 -8
- package/dist/functions/builtin/insert.mjs +7 -9
- package/dist/functions/builtin/remove.d.ts +10 -1
- package/dist/functions/builtin/remove.js +2 -2
- package/dist/functions/builtin/remove.mjs +3 -3
- package/dist/functions/builtin/removeAll.d.ts +5 -1
- package/dist/functions/builtin/removeAll.js +5 -5
- package/dist/functions/builtin/removeAll.mjs +6 -6
- package/dist/functions/builtin/removeFile.d.ts +5 -1
- package/dist/functions/builtin/removeFile.js +3 -1
- package/dist/functions/builtin/removeFile.mjs +3 -1
- package/dist/functions/builtin/upload.d.ts +6 -2
- package/dist/functions/builtin/upload.js +9 -10
- package/dist/functions/builtin/upload.mjs +9 -10
- package/package.json +8 -8
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { ACError, ValidationErrorCode } from "@aeriajs/types";
|
|
3
|
-
import {
|
|
3
|
+
import { Result, throwIfError, pipe, isReference, getValueFromPath, isObjectId } from "@aeriajs/common";
|
|
4
4
|
import { makeValidationError, validateProperty, validateWholeness } from "@aeriajs/validation";
|
|
5
5
|
import { ObjectId } from "mongodb";
|
|
6
6
|
import { getCollectionAsset } from "../assets.mjs";
|
|
@@ -40,7 +40,7 @@ const disposeOldFiles = async (ctx, options = {}) => {
|
|
|
40
40
|
}
|
|
41
41
|
});
|
|
42
42
|
if (!doc) {
|
|
43
|
-
return
|
|
43
|
+
return Result.error("INVALID_DOCUMENT_ID" /* InvalidDocumentId */);
|
|
44
44
|
}
|
|
45
45
|
let fileIds = getValueFromPath(doc, ctx.propPath);
|
|
46
46
|
if (options.fromIds) {
|
|
@@ -137,10 +137,10 @@ const validate = (value, ctx) => {
|
|
|
137
137
|
return value;
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
|
-
const
|
|
141
|
-
if (
|
|
142
|
-
return
|
|
143
|
-
[ctx.propName]:
|
|
140
|
+
const { error } = validateProperty(ctx.propName, value, ctx.property);
|
|
141
|
+
if (error) {
|
|
142
|
+
return Result.error({
|
|
143
|
+
[ctx.propName]: error
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
146
|
return value;
|
|
@@ -162,7 +162,7 @@ const moveFiles = async (value, ctx) => {
|
|
|
162
162
|
_id: new ObjectId(value.tempId)
|
|
163
163
|
});
|
|
164
164
|
if (!tempFile) {
|
|
165
|
-
return
|
|
165
|
+
return Result.error("INVALID_TEMPFILE" /* InvalidTempfile */);
|
|
166
166
|
}
|
|
167
167
|
if (ctx.root._id) {
|
|
168
168
|
await disposeOldFiles(ctx);
|
|
@@ -178,8 +178,11 @@ const recurseDeep = async (value, ctx) => {
|
|
|
178
178
|
return value;
|
|
179
179
|
}
|
|
180
180
|
if ("properties" in ctx.property) {
|
|
181
|
-
const
|
|
182
|
-
|
|
181
|
+
const { error, result } = await recurse(value, ctx);
|
|
182
|
+
if (error) {
|
|
183
|
+
return Result.error(error);
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
183
186
|
}
|
|
184
187
|
if ("items" in ctx.property) {
|
|
185
188
|
if (!Array.isArray(value)) {
|
|
@@ -231,7 +234,7 @@ const recurse = async (target, ctx) => {
|
|
|
231
234
|
if (!property && value && (value.constructor === Object || value.constructor === Array)) {
|
|
232
235
|
if (Object.keys(value)[0]?.startsWith("$")) {
|
|
233
236
|
if (!ctx.options.allowOperators) {
|
|
234
|
-
return
|
|
237
|
+
return Result.error(ACError.InsecureOperator);
|
|
235
238
|
}
|
|
236
239
|
entries.push([
|
|
237
240
|
propName,
|
|
@@ -242,11 +245,11 @@ const recurse = async (target, ctx) => {
|
|
|
242
245
|
if (Array.isArray(value)) {
|
|
243
246
|
const operations = [];
|
|
244
247
|
for (const operation of value) {
|
|
245
|
-
const
|
|
246
|
-
if (
|
|
247
|
-
return
|
|
248
|
+
const { error: error2, result } = await recurse(operation, ctx);
|
|
249
|
+
if (error2) {
|
|
250
|
+
return Result.error(error2);
|
|
248
251
|
}
|
|
249
|
-
operations.push(
|
|
252
|
+
operations.push(result);
|
|
250
253
|
}
|
|
251
254
|
entries.push([
|
|
252
255
|
propName,
|
|
@@ -254,20 +257,20 @@ const recurse = async (target, ctx) => {
|
|
|
254
257
|
]);
|
|
255
258
|
continue;
|
|
256
259
|
}
|
|
257
|
-
const
|
|
258
|
-
if (
|
|
259
|
-
return
|
|
260
|
+
const { error, result: operator } = await recurse(value, ctx);
|
|
261
|
+
if (error) {
|
|
262
|
+
return Result.error(error);
|
|
260
263
|
}
|
|
261
264
|
entries.push([
|
|
262
265
|
propName,
|
|
263
|
-
|
|
266
|
+
operator
|
|
264
267
|
]);
|
|
265
268
|
}
|
|
266
269
|
if (property) {
|
|
267
270
|
if (ctx.options.recurseReferences) {
|
|
268
271
|
const propCast = "items" in property ? property.items : property;
|
|
269
272
|
if ("$ref" in propCast && value && !(value instanceof ObjectId)) {
|
|
270
|
-
const targetDescription = await preloadDescription(
|
|
273
|
+
const targetDescription = await preloadDescription(throwIfError(await getCollectionAsset(propCast.$ref, "description")));
|
|
271
274
|
if (Array.isArray(value)) {
|
|
272
275
|
const documents = [];
|
|
273
276
|
for (const elem of value) {
|
|
@@ -275,11 +278,11 @@ const recurse = async (target, ctx) => {
|
|
|
275
278
|
documents.push(elem);
|
|
276
279
|
continue;
|
|
277
280
|
}
|
|
278
|
-
const
|
|
279
|
-
if (
|
|
280
|
-
return
|
|
281
|
+
const { error: error2, result } = await traverseDocument(elem, targetDescription, ctx.options);
|
|
282
|
+
if (error2) {
|
|
283
|
+
return Result.error(error2);
|
|
281
284
|
}
|
|
282
|
-
documents.push(
|
|
285
|
+
documents.push(result);
|
|
283
286
|
}
|
|
284
287
|
entries.push([
|
|
285
288
|
propName,
|
|
@@ -287,13 +290,13 @@ const recurse = async (target, ctx) => {
|
|
|
287
290
|
]);
|
|
288
291
|
continue;
|
|
289
292
|
}
|
|
290
|
-
const
|
|
291
|
-
if (
|
|
292
|
-
return
|
|
293
|
+
const { error, result: document } = await traverseDocument(value, targetDescription, ctx.options);
|
|
294
|
+
if (error) {
|
|
295
|
+
return Result.error(error);
|
|
293
296
|
}
|
|
294
297
|
entries.push([
|
|
295
298
|
propName,
|
|
296
|
-
|
|
299
|
+
document
|
|
297
300
|
]);
|
|
298
301
|
continue;
|
|
299
302
|
}
|
|
@@ -310,13 +313,13 @@ const recurse = async (target, ctx) => {
|
|
|
310
313
|
]);
|
|
311
314
|
}
|
|
312
315
|
}
|
|
313
|
-
return
|
|
316
|
+
return Result.result(Object.fromEntries(entries));
|
|
314
317
|
};
|
|
315
318
|
export const traverseDocument = async (what, description, _options) => {
|
|
316
319
|
const options = Object.assign({}, _options);
|
|
317
320
|
const functions = [];
|
|
318
321
|
if (!options.validate && Object.keys(what).length === 0) {
|
|
319
|
-
return
|
|
322
|
+
return Result.result({});
|
|
320
323
|
}
|
|
321
324
|
if (options.recurseDeep) {
|
|
322
325
|
functions.push(recurseDeep);
|
|
@@ -334,7 +337,7 @@ export const traverseDocument = async (what, description, _options) => {
|
|
|
334
337
|
}
|
|
335
338
|
const wholenessError = validateWholeness(what, descriptionCopy);
|
|
336
339
|
if (wholenessError) {
|
|
337
|
-
return
|
|
340
|
+
return Result.error(wholenessError);
|
|
338
341
|
}
|
|
339
342
|
functions.push(validate);
|
|
340
343
|
}
|
|
@@ -344,9 +347,9 @@ export const traverseDocument = async (what, description, _options) => {
|
|
|
344
347
|
let validationError;
|
|
345
348
|
const mutateTarget = (fn) => {
|
|
346
349
|
return async (value, ctx) => {
|
|
347
|
-
const
|
|
348
|
-
ctx.target[ctx.propName] =
|
|
349
|
-
return
|
|
350
|
+
const result2 = await fn(value, ctx);
|
|
351
|
+
ctx.target[ctx.propName] = result2;
|
|
352
|
+
return result2;
|
|
350
353
|
};
|
|
351
354
|
};
|
|
352
355
|
options.description = description;
|
|
@@ -360,20 +363,20 @@ export const traverseDocument = async (what, description, _options) => {
|
|
|
360
363
|
}
|
|
361
364
|
})
|
|
362
365
|
});
|
|
363
|
-
const
|
|
366
|
+
const { error, result } = await recurse(what, {
|
|
364
367
|
root: what,
|
|
365
368
|
property: description,
|
|
366
369
|
propPath: "",
|
|
367
370
|
options
|
|
368
371
|
});
|
|
369
|
-
if (
|
|
370
|
-
return
|
|
372
|
+
if (error) {
|
|
373
|
+
return Result.error(error);
|
|
371
374
|
}
|
|
372
375
|
if (validationError) {
|
|
373
|
-
return
|
|
376
|
+
return Result.error(makeValidationError({
|
|
374
377
|
code: ValidationErrorCode.InvalidProperties,
|
|
375
378
|
errors: validationError
|
|
376
379
|
}));
|
|
377
380
|
}
|
|
378
|
-
return
|
|
381
|
+
return Result.result(result);
|
|
379
382
|
};
|
package/dist/context.js
CHANGED
|
@@ -76,16 +76,16 @@ const createContext = async (options = {}) => {
|
|
|
76
76
|
created_at: new Date(),
|
|
77
77
|
});
|
|
78
78
|
};
|
|
79
|
-
context.error = (httpStatus,
|
|
80
|
-
return (0, common_1.
|
|
79
|
+
context.error = (httpStatus, error) => {
|
|
80
|
+
return (0, common_1.endpointError)(Object.assign({
|
|
81
81
|
httpStatus,
|
|
82
|
-
},
|
|
82
|
+
}, error));
|
|
83
83
|
};
|
|
84
84
|
context.limitRate = (params) => {
|
|
85
85
|
return (0, security_1.limitRate)(params, context);
|
|
86
86
|
};
|
|
87
87
|
if (collectionName) {
|
|
88
|
-
const description = (0, common_1.
|
|
88
|
+
const description = (0, common_1.throwIfError)(await getCollectionAsset(collectionName, 'description'));
|
|
89
89
|
context.description = await (0, preload_js_1.preloadDescription)(description);
|
|
90
90
|
context.collectionName = collectionName;
|
|
91
91
|
context.collection = indepthCollection(collectionName, collections, context);
|
package/dist/context.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
import {
|
|
2
|
+
import { throwIfError, endpointError } from "@aeriajs/common";
|
|
3
3
|
import { getCollections } from "@aeriajs/entrypoint";
|
|
4
4
|
import { limitRate } from "@aeriajs/security";
|
|
5
5
|
import { getDatabaseCollection } from "./database.mjs";
|
|
@@ -51,16 +51,16 @@ export const createContext = async (options = {}) => {
|
|
|
51
51
|
created_at: /* @__PURE__ */ new Date()
|
|
52
52
|
});
|
|
53
53
|
};
|
|
54
|
-
context.error = (httpStatus,
|
|
55
|
-
return
|
|
54
|
+
context.error = (httpStatus, error) => {
|
|
55
|
+
return endpointError(Object.assign({
|
|
56
56
|
httpStatus
|
|
57
|
-
},
|
|
57
|
+
}, error));
|
|
58
58
|
};
|
|
59
59
|
context.limitRate = (params) => {
|
|
60
60
|
return limitRate(params, context);
|
|
61
61
|
};
|
|
62
62
|
if (collectionName) {
|
|
63
|
-
const description =
|
|
63
|
+
const description = throwIfError(await getCollectionAsset(collectionName, "description"));
|
|
64
64
|
context.description = await preloadDescription(description);
|
|
65
65
|
context.collectionName = collectionName;
|
|
66
66
|
context.collection = indepthCollection(collectionName, collections, context);
|
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
import type { Context, SchemaWithId, CountPayload } from '@aeriajs/types';
|
|
2
|
-
export declare const count: <TContext extends Context>(payload: CountPayload<SchemaWithId<Context['description']>>, context: TContext extends Context<any> ? TContext : never) => Promise<
|
|
2
|
+
export declare const count: <TContext extends Context>(payload: CountPayload<SchemaWithId<Context['description']>>, context: TContext extends Context<any> ? TContext : never) => Promise<{
|
|
3
|
+
readonly _tag: "Result";
|
|
4
|
+
readonly error: undefined;
|
|
5
|
+
readonly result: any;
|
|
6
|
+
}>;
|
|
@@ -6,9 +6,9 @@ const common_1 = require("@aeriajs/common");
|
|
|
6
6
|
const index_js_1 = require("../../collection/index.js");
|
|
7
7
|
const count = async (payload, context) => {
|
|
8
8
|
const security = (0, security_1.useSecurity)(context);
|
|
9
|
-
const { filters } = (0, common_1.
|
|
9
|
+
const { filters } = (0, common_1.throwIfError)(await security.beforeRead(payload));
|
|
10
10
|
const { $text, ...filtersRest } = filters;
|
|
11
|
-
const traversedFilters = (0, common_1.
|
|
11
|
+
const traversedFilters = (0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(filtersRest, context.description, {
|
|
12
12
|
autoCast: true,
|
|
13
13
|
allowOperators: true,
|
|
14
14
|
}));
|
|
@@ -28,10 +28,10 @@ const count = async (payload, context) => {
|
|
|
28
28
|
$count: 'total',
|
|
29
29
|
});
|
|
30
30
|
const result = await context.collection.model.aggregate(pipeline).next();
|
|
31
|
-
return result
|
|
31
|
+
return common_1.Result.result(result
|
|
32
32
|
? result.total
|
|
33
|
-
: 0;
|
|
33
|
+
: 0);
|
|
34
34
|
}
|
|
35
|
-
return context.collection.model.countDocuments(traversedFilters);
|
|
35
|
+
return common_1.Result.result(await context.collection.model.countDocuments(traversedFilters));
|
|
36
36
|
};
|
|
37
37
|
exports.count = count;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { useSecurity } from "@aeriajs/security";
|
|
3
|
-
import {
|
|
3
|
+
import { Result, throwIfError } from "@aeriajs/common";
|
|
4
4
|
import { traverseDocument } from "../../collection/index.mjs";
|
|
5
5
|
export const count = async (payload, context) => {
|
|
6
6
|
const security = useSecurity(context);
|
|
7
|
-
const { filters } =
|
|
7
|
+
const { filters } = throwIfError(await security.beforeRead(payload));
|
|
8
8
|
const { $text, ...filtersRest } = filters;
|
|
9
|
-
const traversedFilters =
|
|
9
|
+
const traversedFilters = throwIfError(await traverseDocument(filtersRest, context.description, {
|
|
10
10
|
autoCast: true,
|
|
11
11
|
allowOperators: true
|
|
12
12
|
}));
|
|
@@ -26,7 +26,7 @@ export const count = async (payload, context) => {
|
|
|
26
26
|
$count: "total"
|
|
27
27
|
});
|
|
28
28
|
const result = await context.collection.model.aggregate(pipeline).next();
|
|
29
|
-
return result ? result.total : 0;
|
|
29
|
+
return Result.result(result ? result.total : 0);
|
|
30
30
|
}
|
|
31
|
-
return context.collection.model.countDocuments(traversedFilters);
|
|
31
|
+
return Result.result(await context.collection.model.countDocuments(traversedFilters));
|
|
32
32
|
};
|
|
@@ -8,17 +8,19 @@ const index_js_1 = require("../../collection/index.js");
|
|
|
8
8
|
const get = async (payload, context, options) => {
|
|
9
9
|
const security = (0, security_1.useSecurity)(context);
|
|
10
10
|
const { filters = {}, project = [], } = !options?.bypassSecurity
|
|
11
|
-
? (0, common_1.
|
|
11
|
+
? (0, common_1.throwIfError)(await security.beforeRead(payload))
|
|
12
12
|
: payload;
|
|
13
13
|
if (Object.keys(filters).length === 0) {
|
|
14
|
-
|
|
14
|
+
return context.error(types_1.HTTPStatus.BadRequest, {
|
|
15
|
+
code: types_1.ACError.MalformedInput,
|
|
16
|
+
});
|
|
15
17
|
}
|
|
16
18
|
const pipeline = [];
|
|
17
19
|
const references = await (0, index_js_1.getReferences)(context.description.properties, {
|
|
18
20
|
memoize: context.description.$id,
|
|
19
21
|
});
|
|
20
22
|
pipeline.push({
|
|
21
|
-
$match: (0, common_1.
|
|
23
|
+
$match: (0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(filters, context.description, {
|
|
22
24
|
autoCast: true,
|
|
23
25
|
allowOperators: true,
|
|
24
26
|
})),
|
|
@@ -34,17 +36,18 @@ const get = async (payload, context, options) => {
|
|
|
34
36
|
project: payload.populate || project,
|
|
35
37
|
properties: context.description.properties,
|
|
36
38
|
}));
|
|
37
|
-
const
|
|
38
|
-
if (!
|
|
39
|
+
const doc = await context.collection.model.aggregate(pipeline).next();
|
|
40
|
+
if (!doc) {
|
|
39
41
|
return context.error(types_1.HTTPStatus.NotFound, {
|
|
40
42
|
code: types_1.ACError.ResourceNotFound,
|
|
41
43
|
});
|
|
42
44
|
}
|
|
43
|
-
|
|
45
|
+
const result = (0, index_js_1.fill)((0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(doc, context.description, {
|
|
44
46
|
getters: true,
|
|
45
47
|
fromProperties: true,
|
|
46
48
|
recurseReferences: true,
|
|
47
49
|
recurseDeep: true,
|
|
48
50
|
})), context.description);
|
|
51
|
+
return common_1.Result.result(result);
|
|
49
52
|
};
|
|
50
53
|
exports.get = get;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { HTTPStatus, ACError } from "@aeriajs/types";
|
|
3
3
|
import { useSecurity } from "@aeriajs/security";
|
|
4
|
-
import {
|
|
4
|
+
import { Result, throwIfError } from "@aeriajs/common";
|
|
5
5
|
import {
|
|
6
6
|
traverseDocument,
|
|
7
7
|
normalizeProjection,
|
|
@@ -14,16 +14,18 @@ export const get = async (payload, context, options) => {
|
|
|
14
14
|
const {
|
|
15
15
|
filters = {},
|
|
16
16
|
project = []
|
|
17
|
-
} = !options?.bypassSecurity ?
|
|
17
|
+
} = !options?.bypassSecurity ? throwIfError(await security.beforeRead(payload)) : payload;
|
|
18
18
|
if (Object.keys(filters).length === 0) {
|
|
19
|
-
|
|
19
|
+
return context.error(HTTPStatus.BadRequest, {
|
|
20
|
+
code: ACError.MalformedInput
|
|
21
|
+
});
|
|
20
22
|
}
|
|
21
23
|
const pipeline = [];
|
|
22
24
|
const references = await getReferences(context.description.properties, {
|
|
23
25
|
memoize: context.description.$id
|
|
24
26
|
});
|
|
25
27
|
pipeline.push({
|
|
26
|
-
$match:
|
|
28
|
+
$match: throwIfError(await traverseDocument(filters, context.description, {
|
|
27
29
|
autoCast: true,
|
|
28
30
|
allowOperators: true
|
|
29
31
|
}))
|
|
@@ -39,16 +41,17 @@ export const get = async (payload, context, options) => {
|
|
|
39
41
|
project: payload.populate || project,
|
|
40
42
|
properties: context.description.properties
|
|
41
43
|
}));
|
|
42
|
-
const
|
|
43
|
-
if (!
|
|
44
|
+
const doc = await context.collection.model.aggregate(pipeline).next();
|
|
45
|
+
if (!doc) {
|
|
44
46
|
return context.error(HTTPStatus.NotFound, {
|
|
45
47
|
code: ACError.ResourceNotFound
|
|
46
48
|
});
|
|
47
49
|
}
|
|
48
|
-
|
|
50
|
+
const result = fill(throwIfError(await traverseDocument(doc, context.description, {
|
|
49
51
|
getters: true,
|
|
50
52
|
fromProperties: true,
|
|
51
53
|
recurseReferences: true,
|
|
52
54
|
recurseDeep: true
|
|
53
55
|
})), context.description);
|
|
56
|
+
return Result.result(result);
|
|
54
57
|
};
|
|
@@ -2,4 +2,8 @@ import type { Context, SchemaWithId, GetAllPayload } from '@aeriajs/types';
|
|
|
2
2
|
export type GetAllOptions = {
|
|
3
3
|
bypassSecurity?: boolean;
|
|
4
4
|
};
|
|
5
|
-
export declare const getAll: <TContext extends Context>(_payload: GetAllPayload<SchemaWithId<Context['description']>> | undefined, context: TContext, options?: GetAllOptions) => Promise<
|
|
5
|
+
export declare const getAll: <TContext extends Context>(_payload: GetAllPayload<SchemaWithId<Context['description']>> | undefined, context: TContext, options?: GetAllOptions) => Promise<{
|
|
6
|
+
readonly _tag: "Result";
|
|
7
|
+
readonly error: undefined;
|
|
8
|
+
readonly result: SchemaWithId<TContext["description"]>[];
|
|
9
|
+
}>;
|
|
@@ -8,7 +8,7 @@ const getAll = async (_payload, context, options = {}) => {
|
|
|
8
8
|
const security = (0, security_1.useSecurity)(context);
|
|
9
9
|
const payload = _payload || {};
|
|
10
10
|
const { filters = {}, limit = context.config.paginationLimit, sort, project = [], offset = 0, } = !options.bypassSecurity
|
|
11
|
-
? (0, common_1.
|
|
11
|
+
? (0, common_1.throwIfError)(await security.beforeRead(payload))
|
|
12
12
|
: payload;
|
|
13
13
|
const { $text, ...filtersRest } = filters;
|
|
14
14
|
const pipeline = [];
|
|
@@ -36,7 +36,7 @@ const getAll = async (_payload, context, options = {}) => {
|
|
|
36
36
|
}
|
|
37
37
|
if (Object.keys(filtersRest).length > 0) {
|
|
38
38
|
pipeline.push({
|
|
39
|
-
$match: (0, common_1.
|
|
39
|
+
$match: (0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(filtersRest, context.description, {
|
|
40
40
|
autoCast: true,
|
|
41
41
|
allowOperators: true,
|
|
42
42
|
})),
|
|
@@ -69,13 +69,13 @@ const getAll = async (_payload, context, options = {}) => {
|
|
|
69
69
|
const result = await context.collection.model.aggregate(pipeline).toArray();
|
|
70
70
|
const documents = [];
|
|
71
71
|
for (const document of result) {
|
|
72
|
-
documents.push((0, common_1.
|
|
72
|
+
documents.push((0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)((0, index_js_1.fill)(document, context.description), context.description, {
|
|
73
73
|
getters: true,
|
|
74
74
|
fromProperties: true,
|
|
75
75
|
recurseReferences: true,
|
|
76
76
|
recurseDeep: true,
|
|
77
77
|
})));
|
|
78
78
|
}
|
|
79
|
-
return documents;
|
|
79
|
+
return common_1.Result.result(documents);
|
|
80
80
|
};
|
|
81
81
|
exports.getAll = getAll;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { useSecurity } from "@aeriajs/security";
|
|
3
|
-
import {
|
|
3
|
+
import { Result, throwIfError } from "@aeriajs/common";
|
|
4
4
|
import {
|
|
5
5
|
traverseDocument,
|
|
6
6
|
normalizeProjection,
|
|
@@ -17,7 +17,7 @@ export const getAll = async (_payload, context, options = {}) => {
|
|
|
17
17
|
sort,
|
|
18
18
|
project = [],
|
|
19
19
|
offset = 0
|
|
20
|
-
} = !options.bypassSecurity ?
|
|
20
|
+
} = !options.bypassSecurity ? throwIfError(await security.beforeRead(payload)) : payload;
|
|
21
21
|
const { $text, ...filtersRest } = filters;
|
|
22
22
|
const pipeline = [];
|
|
23
23
|
const references = await getReferences(context.description.properties, {
|
|
@@ -40,7 +40,7 @@ export const getAll = async (_payload, context, options = {}) => {
|
|
|
40
40
|
}
|
|
41
41
|
if (Object.keys(filtersRest).length > 0) {
|
|
42
42
|
pipeline.push({
|
|
43
|
-
$match:
|
|
43
|
+
$match: throwIfError(await traverseDocument(filtersRest, context.description, {
|
|
44
44
|
autoCast: true,
|
|
45
45
|
allowOperators: true
|
|
46
46
|
}))
|
|
@@ -73,12 +73,12 @@ export const getAll = async (_payload, context, options = {}) => {
|
|
|
73
73
|
const result = await context.collection.model.aggregate(pipeline).toArray();
|
|
74
74
|
const documents = [];
|
|
75
75
|
for (const document of result) {
|
|
76
|
-
documents.push(
|
|
76
|
+
documents.push(throwIfError(await traverseDocument(fill(document, context.description), context.description, {
|
|
77
77
|
getters: true,
|
|
78
78
|
fromProperties: true,
|
|
79
79
|
recurseReferences: true,
|
|
80
80
|
recurseDeep: true
|
|
81
81
|
})));
|
|
82
82
|
}
|
|
83
|
-
return documents;
|
|
83
|
+
return Result.result(documents);
|
|
84
84
|
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { Context, SchemaWithId, InsertPayload, InsertReturnType } from '@aeriajs/types';
|
|
2
|
-
import { HTTPStatus, ACError, ValidationErrorCode } from '@aeriajs/types';
|
|
3
2
|
export type InsertOptions = {
|
|
4
3
|
bypassSecurity?: boolean;
|
|
5
4
|
};
|
|
@@ -9,25 +8,7 @@ export declare const insertErrorSchema: () => {
|
|
|
9
8
|
readonly _tag: {
|
|
10
9
|
readonly const: "Error";
|
|
11
10
|
};
|
|
12
|
-
readonly
|
|
13
|
-
readonly type: "object";
|
|
14
|
-
readonly required: readonly ["httpStatus", "code"];
|
|
15
|
-
readonly properties: {
|
|
16
|
-
readonly httpStatus: {
|
|
17
|
-
readonly enum: [HTTPStatus.UnprocessableContent, HTTPStatus.NotFound];
|
|
18
|
-
};
|
|
19
|
-
readonly code: {
|
|
20
|
-
readonly enum: [ACError.InsecureOperator, ACError.OwnershipError, ACError.ResourceNotFound, ACError.TargetImmutable, ValidationErrorCode.EmptyTarget, ValidationErrorCode.InvalidProperties, ValidationErrorCode.MissingProperties];
|
|
21
|
-
};
|
|
22
|
-
readonly message: {
|
|
23
|
-
readonly type: "string";
|
|
24
|
-
};
|
|
25
|
-
readonly details: {
|
|
26
|
-
readonly type: "object";
|
|
27
|
-
readonly variable: true;
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
};
|
|
11
|
+
readonly error: import("@aeriajs/types").Property;
|
|
31
12
|
};
|
|
32
13
|
};
|
|
33
14
|
export declare const insert: <TContext extends Context>(payload: InsertPayload<SchemaWithId<TContext['description']>>, context: TContext, options?: InsertOptions) => Promise<InsertReturnType<SchemaWithId<TContext['description']>>>;
|
|
@@ -5,7 +5,7 @@ const types_1 = require("@aeriajs/types");
|
|
|
5
5
|
const security_1 = require("@aeriajs/security");
|
|
6
6
|
const common_1 = require("@aeriajs/common");
|
|
7
7
|
const index_js_1 = require("../../collection/index.js");
|
|
8
|
-
const insertErrorSchema = () => (0, common_1.
|
|
8
|
+
const insertErrorSchema = () => (0, common_1.endpointErrorSchema)({
|
|
9
9
|
httpStatus: [
|
|
10
10
|
types_1.HTTPStatus.UnprocessableContent,
|
|
11
11
|
types_1.HTTPStatus.NotFound,
|
|
@@ -24,9 +24,9 @@ exports.insertErrorSchema = insertErrorSchema;
|
|
|
24
24
|
const insert = async (payload, context, options) => {
|
|
25
25
|
const security = (0, security_1.useSecurity)(context);
|
|
26
26
|
const query = !options?.bypassSecurity
|
|
27
|
-
? (0, common_1.
|
|
27
|
+
? (0, common_1.throwIfError)(await security.beforeWrite(payload))
|
|
28
28
|
: payload;
|
|
29
|
-
const
|
|
29
|
+
const { error, result: what } = await (0, index_js_1.traverseDocument)(query.what, context.description, {
|
|
30
30
|
recurseDeep: true,
|
|
31
31
|
autoCast: true,
|
|
32
32
|
validate: true,
|
|
@@ -36,8 +36,7 @@ const insert = async (payload, context, options) => {
|
|
|
36
36
|
moveFiles: true,
|
|
37
37
|
context,
|
|
38
38
|
});
|
|
39
|
-
if (
|
|
40
|
-
const error = (0, common_1.unwrapEither)(whatEither);
|
|
39
|
+
if (error) {
|
|
41
40
|
if (typeof error === 'string') {
|
|
42
41
|
return context.error(types_1.HTTPStatus.UnprocessableContent, {
|
|
43
42
|
code: error,
|
|
@@ -48,7 +47,6 @@ const insert = async (payload, context, options) => {
|
|
|
48
47
|
details: error.errors,
|
|
49
48
|
});
|
|
50
49
|
}
|
|
51
|
-
const what = (0, common_1.unwrapEither)(whatEither);
|
|
52
50
|
const docId = '_id' in what
|
|
53
51
|
? what._id
|
|
54
52
|
: null;
|
|
@@ -96,11 +94,11 @@ const insert = async (payload, context, options) => {
|
|
|
96
94
|
code: types_1.ACError.ResourceNotFound,
|
|
97
95
|
});
|
|
98
96
|
}
|
|
99
|
-
const result = (0, common_1.
|
|
97
|
+
const result = (0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(doc, context.description, {
|
|
100
98
|
getters: true,
|
|
101
99
|
fromProperties: true,
|
|
102
100
|
recurseReferences: true,
|
|
103
101
|
}));
|
|
104
|
-
return result;
|
|
102
|
+
return common_1.Result.result(result);
|
|
105
103
|
};
|
|
106
104
|
exports.insert = insert;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { HTTPStatus, ACError, ValidationErrorCode } from "@aeriajs/types";
|
|
3
3
|
import { useSecurity } from "@aeriajs/security";
|
|
4
|
-
import {
|
|
4
|
+
import { Result, throwIfError, endpointErrorSchema } from "@aeriajs/common";
|
|
5
5
|
import { traverseDocument, normalizeProjection, prepareInsert } from "../../collection/index.mjs";
|
|
6
|
-
export const insertErrorSchema = () =>
|
|
6
|
+
export const insertErrorSchema = () => endpointErrorSchema({
|
|
7
7
|
httpStatus: [
|
|
8
8
|
HTTPStatus.UnprocessableContent,
|
|
9
9
|
HTTPStatus.NotFound
|
|
@@ -20,8 +20,8 @@ export const insertErrorSchema = () => errorSchema({
|
|
|
20
20
|
});
|
|
21
21
|
export const insert = async (payload, context, options) => {
|
|
22
22
|
const security = useSecurity(context);
|
|
23
|
-
const query = !options?.bypassSecurity ?
|
|
24
|
-
const
|
|
23
|
+
const query = !options?.bypassSecurity ? throwIfError(await security.beforeWrite(payload)) : payload;
|
|
24
|
+
const { error, result: what } = await traverseDocument(query.what, context.description, {
|
|
25
25
|
recurseDeep: true,
|
|
26
26
|
autoCast: true,
|
|
27
27
|
validate: true,
|
|
@@ -29,8 +29,7 @@ export const insert = async (payload, context, options) => {
|
|
|
29
29
|
moveFiles: true,
|
|
30
30
|
context
|
|
31
31
|
});
|
|
32
|
-
if (
|
|
33
|
-
const error = unwrapEither(whatEither);
|
|
32
|
+
if (error) {
|
|
34
33
|
if (typeof error === "string") {
|
|
35
34
|
return context.error(HTTPStatus.UnprocessableContent, {
|
|
36
35
|
code: error
|
|
@@ -41,7 +40,6 @@ export const insert = async (payload, context, options) => {
|
|
|
41
40
|
details: error.errors
|
|
42
41
|
});
|
|
43
42
|
}
|
|
44
|
-
const what = unwrapEither(whatEither);
|
|
45
43
|
const docId = "_id" in what ? what._id : null;
|
|
46
44
|
const content = prepareInsert(what, context.description);
|
|
47
45
|
const projection = payload.project ? normalizeProjection(payload.project, context.description) : {};
|
|
@@ -83,10 +81,10 @@ export const insert = async (payload, context, options) => {
|
|
|
83
81
|
code: ACError.ResourceNotFound
|
|
84
82
|
});
|
|
85
83
|
}
|
|
86
|
-
const result =
|
|
84
|
+
const result = throwIfError(await traverseDocument(doc, context.description, {
|
|
87
85
|
getters: true,
|
|
88
86
|
fromProperties: true,
|
|
89
87
|
recurseReferences: true
|
|
90
88
|
}));
|
|
91
|
-
return result;
|
|
89
|
+
return Result.result(result);
|
|
92
90
|
};
|
|
@@ -1,2 +1,11 @@
|
|
|
1
1
|
import type { Context, SchemaWithId, RemovePayload } from '@aeriajs/types';
|
|
2
|
-
|
|
2
|
+
import { HTTPStatus, ACError } from '@aeriajs/types';
|
|
3
|
+
export declare const remove: <TContext extends Context>(payload: RemovePayload<SchemaWithId<TContext['description']>>, context: TContext) => Promise<{
|
|
4
|
+
readonly _tag: "Result";
|
|
5
|
+
readonly error: undefined;
|
|
6
|
+
readonly result: any;
|
|
7
|
+
} | import("@aeriajs/types/result.js").Result.Error<{
|
|
8
|
+
readonly code: ACError.ResourceNotFound;
|
|
9
|
+
} & {
|
|
10
|
+
httpStatus: HTTPStatus.NotFound;
|
|
11
|
+
}>>;
|