@aeriajs/core 0.0.259 → 0.0.261
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.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Description, Property, ValidationError, RouteContext, TempId } from '@aeriajs/types';
|
|
2
|
+
import { Result } from '@aeriajs/types';
|
|
2
3
|
import { ObjectId } from 'mongodb';
|
|
3
4
|
export type TraverseOptionsBase = {
|
|
4
5
|
autoCast?: boolean;
|
|
@@ -24,7 +25,7 @@ export type TraverseOptions = (TraverseOptionsBase & {
|
|
|
24
25
|
});
|
|
25
26
|
export type TraverseNormalized = {
|
|
26
27
|
description: Description;
|
|
27
|
-
pipe: (value: unknown, phaseContext: PhaseContext) => unknown
|
|
28
|
+
pipe: (value: unknown, phaseContext: PhaseContext) => Promise<Result.Either<unknown, unknown>>;
|
|
28
29
|
};
|
|
29
30
|
export type ValidTempFile = null | undefined | ObjectId | TempId;
|
|
30
31
|
type PhaseContext = {
|
|
@@ -70,7 +70,7 @@ const cleanupReferences = async (value, ctx) => {
|
|
|
70
70
|
const refProperty = (0, common_1.getReferenceProperty)(ctx.property);
|
|
71
71
|
if (refProperty && (refProperty.$ref === 'file' || refProperty.inline)) {
|
|
72
72
|
if (ctx.isArray && !Array.isArray(value)) {
|
|
73
|
-
return value;
|
|
73
|
+
return types_1.Result.result(value);
|
|
74
74
|
}
|
|
75
75
|
const context = ctx.options.context;
|
|
76
76
|
const doc = await context.collections[ctx.options.description.$id].model.findOne({
|
|
@@ -85,7 +85,7 @@ const cleanupReferences = async (value, ctx) => {
|
|
|
85
85
|
}
|
|
86
86
|
let referenceIds = (0, common_1.getValueFromPath)(doc, ctx.propPath);
|
|
87
87
|
if (!referenceIds) {
|
|
88
|
-
return value;
|
|
88
|
+
return types_1.Result.result(value);
|
|
89
89
|
}
|
|
90
90
|
if (Array.isArray(referenceIds)) {
|
|
91
91
|
if (!Array.isArray(value)) {
|
|
@@ -95,7 +95,7 @@ const cleanupReferences = async (value, ctx) => {
|
|
|
95
95
|
}
|
|
96
96
|
else {
|
|
97
97
|
if (referenceIds.equals(value)) {
|
|
98
|
-
return value;
|
|
98
|
+
return types_1.Result.result(value);
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
const refMap = await (0, reference_js_1.getReferences)({
|
|
@@ -107,47 +107,47 @@ const cleanupReferences = async (value, ctx) => {
|
|
|
107
107
|
}));
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
return value;
|
|
110
|
+
return types_1.Result.result(value);
|
|
111
111
|
};
|
|
112
112
|
const autoCast = (value, ctx) => {
|
|
113
113
|
switch (typeof value) {
|
|
114
114
|
case 'boolean': {
|
|
115
|
-
return !!value;
|
|
115
|
+
return types_1.Result.result(!!value);
|
|
116
116
|
}
|
|
117
117
|
case 'string': {
|
|
118
118
|
if ((0, common_1.isReference)(ctx.property)) {
|
|
119
|
-
return mongodb_1.ObjectId.isValid(value)
|
|
119
|
+
return types_1.Result.result(mongodb_1.ObjectId.isValid(value)
|
|
120
120
|
? new mongodb_1.ObjectId(value)
|
|
121
|
-
: value;
|
|
121
|
+
: value);
|
|
122
122
|
}
|
|
123
123
|
if ('format' in ctx.property) {
|
|
124
124
|
if (ctx.property.format === 'date' || ctx.property.format === 'date-time') {
|
|
125
125
|
const timestamp = Date.parse(value);
|
|
126
|
-
return !Number.isNaN(timestamp)
|
|
126
|
+
return types_1.Result.result(!Number.isNaN(timestamp)
|
|
127
127
|
? new Date(timestamp)
|
|
128
|
-
: null;
|
|
128
|
+
: null);
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
break;
|
|
132
132
|
}
|
|
133
133
|
case 'number': {
|
|
134
134
|
if ('type' in ctx.property && ctx.property.type === 'integer') {
|
|
135
|
-
return parseInt(value.toString());
|
|
135
|
+
return types_1.Result.result(parseInt(value.toString()));
|
|
136
136
|
}
|
|
137
137
|
if ('format' in ctx.property) {
|
|
138
138
|
if (ctx.property.format === 'date' || ctx.property.format === 'date-time') {
|
|
139
|
-
return new Date(value);
|
|
139
|
+
return types_1.Result.result(new Date(value));
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
break;
|
|
143
143
|
}
|
|
144
144
|
case 'object': {
|
|
145
145
|
if (!value || value instanceof mongodb_1.ObjectId) {
|
|
146
|
-
return value;
|
|
146
|
+
return types_1.Result.result(value);
|
|
147
147
|
}
|
|
148
148
|
if (!('description' in ctx.options) || !ctx.options.recurseDeep) {
|
|
149
149
|
if (Array.isArray(value)) {
|
|
150
|
-
return value.map((v) => autoCast(v, ctx));
|
|
150
|
+
return types_1.Result.result(value.map((v) => (0, common_1.throwIfError)(autoCast(v, ctx))));
|
|
151
151
|
}
|
|
152
152
|
if (Object.keys(value).length > 0) {
|
|
153
153
|
const entries = {};
|
|
@@ -158,40 +158,44 @@ const autoCast = (value, ctx) => {
|
|
|
158
158
|
if (!subProperty) {
|
|
159
159
|
continue;
|
|
160
160
|
}
|
|
161
|
-
entries[k] = autoCast(v, {
|
|
161
|
+
entries[k] = (0, common_1.throwIfError)(autoCast(v, {
|
|
162
162
|
...ctx,
|
|
163
163
|
property: subProperty,
|
|
164
|
-
});
|
|
164
|
+
}));
|
|
165
165
|
}
|
|
166
|
-
return entries;
|
|
166
|
+
return types_1.Result.result(entries);
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
|
-
return value;
|
|
171
|
+
return types_1.Result.result(value);
|
|
172
172
|
};
|
|
173
|
-
const getters = (value, ctx) => {
|
|
173
|
+
const getters = async (value, ctx) => {
|
|
174
174
|
if ('getter' in ctx.property) {
|
|
175
175
|
if (!ctx.options.context) {
|
|
176
176
|
throw new Error;
|
|
177
177
|
}
|
|
178
|
-
return ctx.property.getter(ctx.target, ctx.options.context);
|
|
178
|
+
return types_1.Result.result(await ctx.property.getter(ctx.target, ctx.options.context));
|
|
179
179
|
}
|
|
180
|
-
return value;
|
|
180
|
+
return types_1.Result.result(value);
|
|
181
181
|
};
|
|
182
|
-
const validate = (value, ctx) => {
|
|
182
|
+
const validate = async (value, ctx) => {
|
|
183
183
|
if (ctx.options.recurseDeep) {
|
|
184
184
|
if ('properties' in ctx.property) {
|
|
185
|
-
return value;
|
|
185
|
+
return types_1.Result.result(value);
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
|
-
const { error } = (0, validation_1.
|
|
188
|
+
const { error } = await (0, validation_1.validatePropertyWithRefs)(value, ctx.property, {
|
|
189
|
+
checkObjectIds: true,
|
|
190
|
+
context: ctx.options.context,
|
|
191
|
+
objectIdConstructor: mongodb_1.ObjectId,
|
|
192
|
+
});
|
|
189
193
|
if (error) {
|
|
190
194
|
return types_1.Result.error({
|
|
191
195
|
[ctx.propName]: error,
|
|
192
196
|
});
|
|
193
197
|
}
|
|
194
|
-
return value;
|
|
198
|
+
return types_1.Result.result(value);
|
|
195
199
|
};
|
|
196
200
|
const isValidTempFile = (value) => {
|
|
197
201
|
if (value && typeof value === 'object') {
|
|
@@ -208,7 +212,7 @@ const isMissingPropertyError = (error) => {
|
|
|
208
212
|
};
|
|
209
213
|
const moveFiles = async (value, ctx) => {
|
|
210
214
|
if (!('$ref' in ctx.property) || ctx.property.$ref !== 'file') {
|
|
211
|
-
return value;
|
|
215
|
+
return types_1.Result.result(value);
|
|
212
216
|
}
|
|
213
217
|
const tempFileCollection = await (0, entrypoint_1.getCollection)('tempFile');
|
|
214
218
|
if (!tempFileCollection) {
|
|
@@ -218,10 +222,10 @@ const moveFiles = async (value, ctx) => {
|
|
|
218
222
|
return types_1.Result.error(types_1.TraverseError.InvalidTempfile);
|
|
219
223
|
}
|
|
220
224
|
if (!value) {
|
|
221
|
-
return null;
|
|
225
|
+
return types_1.Result.result(null);
|
|
222
226
|
}
|
|
223
227
|
if (value instanceof mongodb_1.ObjectId) {
|
|
224
|
-
return value;
|
|
228
|
+
return types_1.Result.result(value);
|
|
225
229
|
}
|
|
226
230
|
if (!ctx.options.context) {
|
|
227
231
|
throw new Error();
|
|
@@ -237,11 +241,11 @@ const moveFiles = async (value, ctx) => {
|
|
|
237
241
|
newFile.owner = ctx.options.context.token.sub;
|
|
238
242
|
await fs.rename(tempFile.absolute_path, newFile.absolute_path);
|
|
239
243
|
const file = await ctx.options.context.collections.file.model.insertOne(newFile);
|
|
240
|
-
return file.insertedId;
|
|
244
|
+
return types_1.Result.result(file.insertedId);
|
|
241
245
|
};
|
|
242
246
|
const recurseDeep = async (value, ctx) => {
|
|
243
247
|
if (!value) {
|
|
244
|
-
return value;
|
|
248
|
+
return types_1.Result.result(value);
|
|
245
249
|
}
|
|
246
250
|
if ('properties' in ctx.property) {
|
|
247
251
|
if (ctx.options.validateWholeness) {
|
|
@@ -250,28 +254,26 @@ const recurseDeep = async (value, ctx) => {
|
|
|
250
254
|
return types_1.Result.error(wholenessError);
|
|
251
255
|
}
|
|
252
256
|
}
|
|
253
|
-
|
|
254
|
-
if (error) {
|
|
255
|
-
return types_1.Result.error(error);
|
|
256
|
-
}
|
|
257
|
-
return result;
|
|
257
|
+
return recurse(value, ctx);
|
|
258
258
|
}
|
|
259
259
|
if ('items' in ctx.property) {
|
|
260
260
|
if (!Array.isArray(value)) {
|
|
261
|
-
return value;
|
|
261
|
+
return types_1.Result.result(value);
|
|
262
262
|
}
|
|
263
263
|
const items = [];
|
|
264
264
|
for (const item of value) {
|
|
265
|
-
const result = await ctx.options.pipe(item, {
|
|
265
|
+
const { error, result } = await ctx.options.pipe(item, {
|
|
266
266
|
...ctx,
|
|
267
267
|
property: ctx.property.items,
|
|
268
268
|
isArray: true,
|
|
269
269
|
});
|
|
270
|
-
|
|
270
|
+
if (!error) {
|
|
271
|
+
items.push(result);
|
|
272
|
+
}
|
|
271
273
|
}
|
|
272
|
-
return items;
|
|
274
|
+
return types_1.Result.result(items);
|
|
273
275
|
}
|
|
274
|
-
return value;
|
|
276
|
+
return types_1.Result.result(value);
|
|
275
277
|
};
|
|
276
278
|
const recurse = async (target, ctx) => {
|
|
277
279
|
const entries = {};
|
|
@@ -287,14 +289,14 @@ const recurse = async (target, ctx) => {
|
|
|
287
289
|
if (propName === '_id') {
|
|
288
290
|
if (value) {
|
|
289
291
|
if (ctx.options.autoCast) {
|
|
290
|
-
entries[propName] = autoCast(value, {
|
|
292
|
+
entries[propName] = (0, common_1.throwIfError)(autoCast(value, {
|
|
291
293
|
...ctx,
|
|
292
294
|
target,
|
|
293
295
|
propName,
|
|
294
296
|
property: {
|
|
295
297
|
$ref: '',
|
|
296
298
|
},
|
|
297
|
-
});
|
|
299
|
+
}));
|
|
298
300
|
}
|
|
299
301
|
else {
|
|
300
302
|
entries[propName] = value;
|
|
@@ -395,7 +397,7 @@ const recurse = async (target, ctx) => {
|
|
|
395
397
|
continue;
|
|
396
398
|
}
|
|
397
399
|
}
|
|
398
|
-
|
|
400
|
+
const { error, result } = await ctx.options.pipe(value, {
|
|
399
401
|
...ctx,
|
|
400
402
|
target,
|
|
401
403
|
propName,
|
|
@@ -404,6 +406,9 @@ const recurse = async (target, ctx) => {
|
|
|
404
406
|
: propName,
|
|
405
407
|
property,
|
|
406
408
|
});
|
|
409
|
+
if (!error) {
|
|
410
|
+
entries[propName] = result;
|
|
411
|
+
}
|
|
407
412
|
}
|
|
408
413
|
}
|
|
409
414
|
return types_1.Result.result(entries);
|
|
@@ -445,30 +450,27 @@ const traverseDocument = async (what, description, _options) => {
|
|
|
445
450
|
functions.push(moveFiles);
|
|
446
451
|
}
|
|
447
452
|
let traverseError, validationError;
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
options.pipe = (0, common_1.pipe)(functions.map(mutateTarget), {
|
|
456
|
-
returnFirst: (value) => {
|
|
457
|
-
if ((0, common_1.isError)(value)) {
|
|
458
|
-
const error = value.error;
|
|
459
|
-
switch (error) {
|
|
453
|
+
options.pipe = async (initialValue, ctx) => {
|
|
454
|
+
let value = initialValue;
|
|
455
|
+
for (const fn of functions) {
|
|
456
|
+
const { error, result } = await fn(value, ctx);
|
|
457
|
+
if (error) {
|
|
458
|
+
const narrowedError = error;
|
|
459
|
+
switch (narrowedError) {
|
|
460
460
|
case types_1.TraverseError.InvalidDocumentId:
|
|
461
461
|
case types_1.TraverseError.InvalidTempfile:
|
|
462
|
-
traverseError =
|
|
462
|
+
traverseError = narrowedError;
|
|
463
463
|
break;
|
|
464
464
|
default: {
|
|
465
|
-
validationError =
|
|
465
|
+
validationError = narrowedError;
|
|
466
466
|
}
|
|
467
467
|
}
|
|
468
|
-
return
|
|
468
|
+
return types_1.Result.error(error);
|
|
469
469
|
}
|
|
470
|
-
|
|
471
|
-
|
|
470
|
+
value = ctx.target[ctx.propName] = result;
|
|
471
|
+
}
|
|
472
|
+
return types_1.Result.result(value);
|
|
473
|
+
};
|
|
472
474
|
const { error, result } = await recurse(whatCopy, {
|
|
473
475
|
root: whatCopy,
|
|
474
476
|
property: description,
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
import * as fs from "node:fs/promises";
|
|
4
4
|
import { Result, ACError, ValidationErrorCode, TraverseError } from "@aeriajs/types";
|
|
5
|
-
import { throwIfError,
|
|
6
|
-
import { makeValidationError,
|
|
5
|
+
import { throwIfError, isReference, getReferenceProperty, getValueFromPath } from "@aeriajs/common";
|
|
6
|
+
import { makeValidationError, validatePropertyWithRefs, validateWholeness } from "@aeriajs/validation";
|
|
7
7
|
import { getCollection } from "@aeriajs/entrypoint";
|
|
8
8
|
import { ObjectId } from "mongodb";
|
|
9
9
|
import { getCollectionAsset } from "../assets.mjs";
|
|
@@ -35,7 +35,7 @@ const cleanupReferences = async (value, ctx) => {
|
|
|
35
35
|
const refProperty = getReferenceProperty(ctx.property);
|
|
36
36
|
if (refProperty && (refProperty.$ref === "file" || refProperty.inline)) {
|
|
37
37
|
if (ctx.isArray && !Array.isArray(value)) {
|
|
38
|
-
return value;
|
|
38
|
+
return Result.result(value);
|
|
39
39
|
}
|
|
40
40
|
const context = ctx.options.context;
|
|
41
41
|
const doc = await context.collections[ctx.options.description.$id].model.findOne({
|
|
@@ -50,7 +50,7 @@ const cleanupReferences = async (value, ctx) => {
|
|
|
50
50
|
}
|
|
51
51
|
let referenceIds = getValueFromPath(doc, ctx.propPath);
|
|
52
52
|
if (!referenceIds) {
|
|
53
|
-
return value;
|
|
53
|
+
return Result.result(value);
|
|
54
54
|
}
|
|
55
55
|
if (Array.isArray(referenceIds)) {
|
|
56
56
|
if (!Array.isArray(value)) {
|
|
@@ -59,7 +59,7 @@ const cleanupReferences = async (value, ctx) => {
|
|
|
59
59
|
referenceIds = referenceIds.filter((oldId) => !value.some((valueId) => valueId.equals(oldId)));
|
|
60
60
|
} else {
|
|
61
61
|
if (referenceIds.equals(value)) {
|
|
62
|
-
return value;
|
|
62
|
+
return Result.result(value);
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
const refMap = await getReferences({
|
|
@@ -71,43 +71,43 @@ const cleanupReferences = async (value, ctx) => {
|
|
|
71
71
|
}));
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
return value;
|
|
74
|
+
return Result.result(value);
|
|
75
75
|
};
|
|
76
76
|
const autoCast = (value, ctx) => {
|
|
77
77
|
switch (typeof value) {
|
|
78
78
|
case "boolean": {
|
|
79
|
-
return !!value;
|
|
79
|
+
return Result.result(!!value);
|
|
80
80
|
}
|
|
81
81
|
case "string": {
|
|
82
82
|
if (isReference(ctx.property)) {
|
|
83
|
-
return ObjectId.isValid(value) ? new ObjectId(value) : value;
|
|
83
|
+
return Result.result(ObjectId.isValid(value) ? new ObjectId(value) : value);
|
|
84
84
|
}
|
|
85
85
|
if ("format" in ctx.property) {
|
|
86
86
|
if (ctx.property.format === "date" || ctx.property.format === "date-time") {
|
|
87
87
|
const timestamp = Date.parse(value);
|
|
88
|
-
return !Number.isNaN(timestamp) ? new Date(timestamp) : null;
|
|
88
|
+
return Result.result(!Number.isNaN(timestamp) ? new Date(timestamp) : null);
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
break;
|
|
92
92
|
}
|
|
93
93
|
case "number": {
|
|
94
94
|
if ("type" in ctx.property && ctx.property.type === "integer") {
|
|
95
|
-
return parseInt(value.toString());
|
|
95
|
+
return Result.result(parseInt(value.toString()));
|
|
96
96
|
}
|
|
97
97
|
if ("format" in ctx.property) {
|
|
98
98
|
if (ctx.property.format === "date" || ctx.property.format === "date-time") {
|
|
99
|
-
return new Date(value);
|
|
99
|
+
return Result.result(new Date(value));
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
break;
|
|
103
103
|
}
|
|
104
104
|
case "object": {
|
|
105
105
|
if (!value || value instanceof ObjectId) {
|
|
106
|
-
return value;
|
|
106
|
+
return Result.result(value);
|
|
107
107
|
}
|
|
108
108
|
if (!("description" in ctx.options) || !ctx.options.recurseDeep) {
|
|
109
109
|
if (Array.isArray(value)) {
|
|
110
|
-
return value.map((v) => autoCast(v, ctx));
|
|
110
|
+
return Result.result(value.map((v) => throwIfError(autoCast(v, ctx))));
|
|
111
111
|
}
|
|
112
112
|
if (Object.keys(value).length > 0) {
|
|
113
113
|
const entries = {};
|
|
@@ -116,40 +116,44 @@ const autoCast = (value, ctx) => {
|
|
|
116
116
|
if (!subProperty) {
|
|
117
117
|
continue;
|
|
118
118
|
}
|
|
119
|
-
entries[k] = autoCast(v, {
|
|
119
|
+
entries[k] = throwIfError(autoCast(v, {
|
|
120
120
|
...ctx,
|
|
121
121
|
property: subProperty
|
|
122
|
-
});
|
|
122
|
+
}));
|
|
123
123
|
}
|
|
124
|
-
return entries;
|
|
124
|
+
return Result.result(entries);
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
|
-
return value;
|
|
129
|
+
return Result.result(value);
|
|
130
130
|
};
|
|
131
|
-
const getters = (value, ctx) => {
|
|
131
|
+
const getters = async (value, ctx) => {
|
|
132
132
|
if ("getter" in ctx.property) {
|
|
133
133
|
if (!ctx.options.context) {
|
|
134
134
|
throw new Error();
|
|
135
135
|
}
|
|
136
|
-
return ctx.property.getter(ctx.target, ctx.options.context);
|
|
136
|
+
return Result.result(await ctx.property.getter(ctx.target, ctx.options.context));
|
|
137
137
|
}
|
|
138
|
-
return value;
|
|
138
|
+
return Result.result(value);
|
|
139
139
|
};
|
|
140
|
-
const validate = (value, ctx) => {
|
|
140
|
+
const validate = async (value, ctx) => {
|
|
141
141
|
if (ctx.options.recurseDeep) {
|
|
142
142
|
if ("properties" in ctx.property) {
|
|
143
|
-
return value;
|
|
143
|
+
return Result.result(value);
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
-
const { error } =
|
|
146
|
+
const { error } = await validatePropertyWithRefs(value, ctx.property, {
|
|
147
|
+
checkObjectIds: true,
|
|
148
|
+
context: ctx.options.context,
|
|
149
|
+
objectIdConstructor: ObjectId
|
|
150
|
+
});
|
|
147
151
|
if (error) {
|
|
148
152
|
return Result.error({
|
|
149
153
|
[ctx.propName]: error
|
|
150
154
|
});
|
|
151
155
|
}
|
|
152
|
-
return value;
|
|
156
|
+
return Result.result(value);
|
|
153
157
|
};
|
|
154
158
|
const isValidTempFile = (value) => {
|
|
155
159
|
if (value && typeof value === "object") {
|
|
@@ -165,7 +169,7 @@ const isMissingPropertyError = (error) => {
|
|
|
165
169
|
};
|
|
166
170
|
const moveFiles = async (value, ctx) => {
|
|
167
171
|
if (!("$ref" in ctx.property) || ctx.property.$ref !== "file") {
|
|
168
|
-
return value;
|
|
172
|
+
return Result.result(value);
|
|
169
173
|
}
|
|
170
174
|
const tempFileCollection = await getCollection("tempFile");
|
|
171
175
|
if (!tempFileCollection) {
|
|
@@ -175,10 +179,10 @@ const moveFiles = async (value, ctx) => {
|
|
|
175
179
|
return Result.error(TraverseError.InvalidTempfile);
|
|
176
180
|
}
|
|
177
181
|
if (!value) {
|
|
178
|
-
return null;
|
|
182
|
+
return Result.result(null);
|
|
179
183
|
}
|
|
180
184
|
if (value instanceof ObjectId) {
|
|
181
|
-
return value;
|
|
185
|
+
return Result.result(value);
|
|
182
186
|
}
|
|
183
187
|
if (!ctx.options.context) {
|
|
184
188
|
throw new Error();
|
|
@@ -194,11 +198,11 @@ const moveFiles = async (value, ctx) => {
|
|
|
194
198
|
newFile.owner = ctx.options.context.token.sub;
|
|
195
199
|
await fs.rename(tempFile.absolute_path, newFile.absolute_path);
|
|
196
200
|
const file = await ctx.options.context.collections.file.model.insertOne(newFile);
|
|
197
|
-
return file.insertedId;
|
|
201
|
+
return Result.result(file.insertedId);
|
|
198
202
|
};
|
|
199
203
|
const recurseDeep = async (value, ctx) => {
|
|
200
204
|
if (!value) {
|
|
201
|
-
return value;
|
|
205
|
+
return Result.result(value);
|
|
202
206
|
}
|
|
203
207
|
if ("properties" in ctx.property) {
|
|
204
208
|
if (ctx.options.validateWholeness) {
|
|
@@ -207,28 +211,26 @@ const recurseDeep = async (value, ctx) => {
|
|
|
207
211
|
return Result.error(wholenessError);
|
|
208
212
|
}
|
|
209
213
|
}
|
|
210
|
-
|
|
211
|
-
if (error) {
|
|
212
|
-
return Result.error(error);
|
|
213
|
-
}
|
|
214
|
-
return result;
|
|
214
|
+
return recurse(value, ctx);
|
|
215
215
|
}
|
|
216
216
|
if ("items" in ctx.property) {
|
|
217
217
|
if (!Array.isArray(value)) {
|
|
218
|
-
return value;
|
|
218
|
+
return Result.result(value);
|
|
219
219
|
}
|
|
220
220
|
const items = [];
|
|
221
221
|
for (const item of value) {
|
|
222
|
-
const result = await ctx.options.pipe(item, {
|
|
222
|
+
const { error, result } = await ctx.options.pipe(item, {
|
|
223
223
|
...ctx,
|
|
224
224
|
property: ctx.property.items,
|
|
225
225
|
isArray: true
|
|
226
226
|
});
|
|
227
|
-
|
|
227
|
+
if (!error) {
|
|
228
|
+
items.push(result);
|
|
229
|
+
}
|
|
228
230
|
}
|
|
229
|
-
return items;
|
|
231
|
+
return Result.result(items);
|
|
230
232
|
}
|
|
231
|
-
return value;
|
|
233
|
+
return Result.result(value);
|
|
232
234
|
};
|
|
233
235
|
const recurse = async (target, ctx) => {
|
|
234
236
|
const entries = {};
|
|
@@ -242,14 +244,14 @@ const recurse = async (target, ctx) => {
|
|
|
242
244
|
if (propName === "_id") {
|
|
243
245
|
if (value) {
|
|
244
246
|
if (ctx.options.autoCast) {
|
|
245
|
-
entries[propName] = autoCast(value, {
|
|
247
|
+
entries[propName] = throwIfError(autoCast(value, {
|
|
246
248
|
...ctx,
|
|
247
249
|
target,
|
|
248
250
|
propName,
|
|
249
251
|
property: {
|
|
250
252
|
$ref: ""
|
|
251
253
|
}
|
|
252
|
-
});
|
|
254
|
+
}));
|
|
253
255
|
} else {
|
|
254
256
|
entries[propName] = value;
|
|
255
257
|
}
|
|
@@ -329,30 +331,33 @@ const recurse = async (target, ctx) => {
|
|
|
329
331
|
documents.push(new ObjectId(elem));
|
|
330
332
|
continue;
|
|
331
333
|
}
|
|
332
|
-
const { error:
|
|
333
|
-
if (
|
|
334
|
-
return Result.error(
|
|
334
|
+
const { error: error3, result: result2 } = await traverseDocument(elem, targetDescription, ctx.options);
|
|
335
|
+
if (error3) {
|
|
336
|
+
return Result.error(error3);
|
|
335
337
|
}
|
|
336
|
-
documents.push(
|
|
338
|
+
documents.push(result2);
|
|
337
339
|
}
|
|
338
340
|
entries[propName] = documents;
|
|
339
341
|
continue;
|
|
340
342
|
}
|
|
341
|
-
const { error, result: doc } = await traverseDocument(value, targetDescription, ctx.options);
|
|
342
|
-
if (
|
|
343
|
-
return Result.error(
|
|
343
|
+
const { error: error2, result: doc } = await traverseDocument(value, targetDescription, ctx.options);
|
|
344
|
+
if (error2) {
|
|
345
|
+
return Result.error(error2);
|
|
344
346
|
}
|
|
345
347
|
entries[propName] = doc;
|
|
346
348
|
continue;
|
|
347
349
|
}
|
|
348
350
|
}
|
|
349
|
-
|
|
351
|
+
const { error, result } = await ctx.options.pipe(value, {
|
|
350
352
|
...ctx,
|
|
351
353
|
target,
|
|
352
354
|
propName,
|
|
353
355
|
propPath: ctx.propPath ? `${ctx.propPath}.${propName}` : propName,
|
|
354
356
|
property
|
|
355
357
|
});
|
|
358
|
+
if (!error) {
|
|
359
|
+
entries[propName] = result;
|
|
360
|
+
}
|
|
356
361
|
}
|
|
357
362
|
}
|
|
358
363
|
return Result.result(entries);
|
|
@@ -394,30 +399,27 @@ export const traverseDocument = async (what, description, _options) => {
|
|
|
394
399
|
functions.push(moveFiles);
|
|
395
400
|
}
|
|
396
401
|
let traverseError, validationError;
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
options.pipe = pipe(functions.map(mutateTarget), {
|
|
405
|
-
returnFirst: (value) => {
|
|
406
|
-
if (isError(value)) {
|
|
407
|
-
const error2 = value.error;
|
|
408
|
-
switch (error2) {
|
|
402
|
+
options.pipe = async (initialValue, ctx) => {
|
|
403
|
+
let value = initialValue;
|
|
404
|
+
for (const fn of functions) {
|
|
405
|
+
const { error: error2, result: result2 } = await fn(value, ctx);
|
|
406
|
+
if (error2) {
|
|
407
|
+
const narrowedError = error2;
|
|
408
|
+
switch (narrowedError) {
|
|
409
409
|
case TraverseError.InvalidDocumentId:
|
|
410
410
|
case TraverseError.InvalidTempfile:
|
|
411
|
-
traverseError =
|
|
411
|
+
traverseError = narrowedError;
|
|
412
412
|
break;
|
|
413
413
|
default: {
|
|
414
|
-
validationError =
|
|
414
|
+
validationError = narrowedError;
|
|
415
415
|
}
|
|
416
416
|
}
|
|
417
|
-
return
|
|
417
|
+
return Result.error(error2);
|
|
418
418
|
}
|
|
419
|
+
value = ctx.target[ctx.propName] = result2;
|
|
419
420
|
}
|
|
420
|
-
|
|
421
|
+
return Result.result(value);
|
|
422
|
+
};
|
|
421
423
|
const { error, result } = await recurse(whatCopy, {
|
|
422
424
|
root: whatCopy,
|
|
423
425
|
property: description,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.261",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"aeriaMain": "tests/fixtures/aeriaMain.js",
|
|
@@ -42,13 +42,13 @@
|
|
|
42
42
|
"mongodb-memory-server": "^10.1.4"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@aeriajs/builtins": "^0.0.
|
|
46
|
-
"@aeriajs/common": "^0.0.
|
|
47
|
-
"@aeriajs/entrypoint": "^0.0.
|
|
48
|
-
"@aeriajs/http": "^0.0.
|
|
49
|
-
"@aeriajs/security": "^0.0.
|
|
50
|
-
"@aeriajs/types": "^0.0.
|
|
51
|
-
"@aeriajs/validation": "^0.0.
|
|
45
|
+
"@aeriajs/builtins": "^0.0.261",
|
|
46
|
+
"@aeriajs/common": "^0.0.146",
|
|
47
|
+
"@aeriajs/entrypoint": "^0.0.151",
|
|
48
|
+
"@aeriajs/http": "^0.0.177",
|
|
49
|
+
"@aeriajs/security": "^0.0.261",
|
|
50
|
+
"@aeriajs/types": "^0.0.127",
|
|
51
|
+
"@aeriajs/validation": "^0.0.164"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"mongodb": "^6.16.0",
|