@aeriajs/core 0.0.94 → 0.0.96

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 (42) hide show
  1. package/dist/accessControl.js +4 -4
  2. package/dist/accessControl.mjs +4 -4
  3. package/dist/assets.d.ts +39 -3
  4. package/dist/assets.js +18 -20
  5. package/dist/assets.mjs +19 -21
  6. package/dist/collection/cascadingRemove.js +6 -7
  7. package/dist/collection/cascadingRemove.mjs +6 -7
  8. package/dist/collection/preload.js +5 -4
  9. package/dist/collection/preload.mjs +6 -5
  10. package/dist/collection/prepareInsert.js +2 -1
  11. package/dist/collection/prepareInsert.mjs +2 -1
  12. package/dist/collection/reference.js +4 -4
  13. package/dist/collection/reference.mjs +5 -5
  14. package/dist/collection/traverseDocument.d.ts +9 -1
  15. package/dist/collection/traverseDocument.js +37 -34
  16. package/dist/collection/traverseDocument.mjs +41 -38
  17. package/dist/context.js +4 -4
  18. package/dist/context.mjs +5 -5
  19. package/dist/functions/builtin/count.d.ts +5 -1
  20. package/dist/functions/builtin/count.js +5 -5
  21. package/dist/functions/builtin/count.mjs +5 -5
  22. package/dist/functions/builtin/get.js +9 -6
  23. package/dist/functions/builtin/get.mjs +10 -7
  24. package/dist/functions/builtin/getAll.d.ts +5 -1
  25. package/dist/functions/builtin/getAll.js +4 -4
  26. package/dist/functions/builtin/getAll.mjs +5 -5
  27. package/dist/functions/builtin/insert.d.ts +11 -19
  28. package/dist/functions/builtin/insert.js +6 -8
  29. package/dist/functions/builtin/insert.mjs +7 -9
  30. package/dist/functions/builtin/remove.d.ts +10 -1
  31. package/dist/functions/builtin/remove.js +2 -2
  32. package/dist/functions/builtin/remove.mjs +3 -3
  33. package/dist/functions/builtin/removeAll.d.ts +5 -1
  34. package/dist/functions/builtin/removeAll.js +5 -5
  35. package/dist/functions/builtin/removeAll.mjs +6 -6
  36. package/dist/functions/builtin/removeFile.d.ts +5 -1
  37. package/dist/functions/builtin/removeFile.js +3 -1
  38. package/dist/functions/builtin/removeFile.mjs +3 -1
  39. package/dist/functions/builtin/upload.d.ts +6 -2
  40. package/dist/functions/builtin/upload.js +9 -10
  41. package/dist/functions/builtin/upload.mjs +9 -10
  42. package/package.json +8 -8
@@ -65,7 +65,7 @@ const disposeOldFiles = async (ctx, options = {}) => {
65
65
  },
66
66
  });
67
67
  if (!doc) {
68
- return (0, common_1.left)(TraverseError.InvalidDocumentId);
68
+ return common_1.Result.error(TraverseError.InvalidDocumentId);
69
69
  }
70
70
  let fileIds = (0, common_1.getValueFromPath)(doc, ctx.propPath);
71
71
  if (options.fromIds) {
@@ -171,10 +171,10 @@ const validate = (value, ctx) => {
171
171
  return value;
172
172
  }
173
173
  }
174
- const validationEither = (0, validation_1.validateProperty)(ctx.propName, value, ctx.property);
175
- if ((0, common_1.isLeft)(validationEither)) {
176
- return (0, common_1.left)({
177
- [ctx.propName]: (0, common_1.unwrapEither)(validationEither),
174
+ const { error } = (0, validation_1.validateProperty)(ctx.propName, value, ctx.property);
175
+ if (error) {
176
+ return common_1.Result.error({
177
+ [ctx.propName]: error,
178
178
  });
179
179
  }
180
180
  return value;
@@ -196,7 +196,7 @@ const moveFiles = async (value, ctx) => {
196
196
  _id: new mongodb_1.ObjectId(value.tempId),
197
197
  });
198
198
  if (!tempFile) {
199
- return (0, common_1.left)(TraverseError.InvalidTempfile);
199
+ return common_1.Result.error(TraverseError.InvalidTempfile);
200
200
  }
201
201
  if (ctx.root._id) {
202
202
  await disposeOldFiles(ctx);
@@ -212,8 +212,11 @@ const recurseDeep = async (value, ctx) => {
212
212
  return value;
213
213
  }
214
214
  if ('properties' in ctx.property) {
215
- const resultEither = await recurse(value, ctx);
216
- return (0, common_1.unwrapEither)(resultEither);
215
+ const { error, result } = await recurse(value, ctx);
216
+ if (error) {
217
+ return common_1.Result.error(error);
218
+ }
219
+ return result;
217
220
  }
218
221
  if ('items' in ctx.property) {
219
222
  if (!Array.isArray(value)) {
@@ -269,7 +272,7 @@ const recurse = async (target, ctx) => {
269
272
  // it contains MongoDB query operators
270
273
  if (Object.keys(value)[0]?.startsWith('$')) {
271
274
  if (!ctx.options.allowOperators) {
272
- return (0, common_1.left)(types_1.ACError.InsecureOperator);
275
+ return common_1.Result.error(types_1.ACError.InsecureOperator);
273
276
  }
274
277
  entries.push([
275
278
  propName,
@@ -280,11 +283,11 @@ const recurse = async (target, ctx) => {
280
283
  if (Array.isArray(value)) {
281
284
  const operations = [];
282
285
  for (const operation of value) {
283
- const operatorEither = await recurse(operation, ctx);
284
- if ((0, common_1.isLeft)(operatorEither)) {
285
- return (0, common_1.left)((0, common_1.unwrapEither)(operatorEither));
286
+ const { error, result } = await recurse(operation, ctx);
287
+ if (error) {
288
+ return common_1.Result.error(error);
286
289
  }
287
- operations.push((0, common_1.unwrapEither)(operatorEither));
290
+ operations.push(result);
288
291
  }
289
292
  entries.push([
290
293
  propName,
@@ -292,13 +295,13 @@ const recurse = async (target, ctx) => {
292
295
  ]);
293
296
  continue;
294
297
  }
295
- const operatorEither = await recurse(value, ctx);
296
- if ((0, common_1.isLeft)(operatorEither)) {
297
- return (0, common_1.left)((0, common_1.unwrapEither)(operatorEither));
298
+ const { error, result: operator } = await recurse(value, ctx);
299
+ if (error) {
300
+ return common_1.Result.error(error);
298
301
  }
299
302
  entries.push([
300
303
  propName,
301
- (0, common_1.unwrapEither)(operatorEither),
304
+ operator,
302
305
  ]);
303
306
  }
304
307
  if (property) {
@@ -307,7 +310,7 @@ const recurse = async (target, ctx) => {
307
310
  ? property.items
308
311
  : property;
309
312
  if ('$ref' in propCast && value && !(value instanceof mongodb_1.ObjectId)) {
310
- const targetDescription = await (0, preload_js_1.preloadDescription)((0, common_1.throwIfLeft)(await (0, assets_js_1.getCollectionAsset)(propCast.$ref, 'description')));
313
+ const targetDescription = await (0, preload_js_1.preloadDescription)((0, common_1.throwIfError)(await (0, assets_js_1.getCollectionAsset)(propCast.$ref, 'description')));
311
314
  if (Array.isArray(value)) {
312
315
  const documents = [];
313
316
  for (const elem of value) {
@@ -315,11 +318,11 @@ const recurse = async (target, ctx) => {
315
318
  documents.push(elem);
316
319
  continue;
317
320
  }
318
- const documentEither = await (0, exports.traverseDocument)(elem, targetDescription, ctx.options);
319
- if ((0, common_1.isLeft)(documentEither)) {
320
- return (0, common_1.left)((0, common_1.unwrapEither)(documentEither));
321
+ const { error, result } = await (0, exports.traverseDocument)(elem, targetDescription, ctx.options);
322
+ if (error) {
323
+ return common_1.Result.error(error);
321
324
  }
322
- documents.push((0, common_1.unwrapEither)(documentEither));
325
+ documents.push(result);
323
326
  }
324
327
  entries.push([
325
328
  propName,
@@ -327,13 +330,13 @@ const recurse = async (target, ctx) => {
327
330
  ]);
328
331
  continue;
329
332
  }
330
- const documentEither = await (0, exports.traverseDocument)(value, targetDescription, ctx.options);
331
- if ((0, common_1.isLeft)(documentEither)) {
332
- return (0, common_1.left)((0, common_1.unwrapEither)(documentEither));
333
+ const { error, result: document } = await (0, exports.traverseDocument)(value, targetDescription, ctx.options);
334
+ if (error) {
335
+ return common_1.Result.error(error);
333
336
  }
334
337
  entries.push([
335
338
  propName,
336
- (0, common_1.unwrapEither)(documentEither),
339
+ document,
337
340
  ]);
338
341
  continue;
339
342
  }
@@ -352,13 +355,13 @@ const recurse = async (target, ctx) => {
352
355
  ]);
353
356
  }
354
357
  }
355
- return (0, common_1.right)(Object.fromEntries(entries));
358
+ return common_1.Result.result(Object.fromEntries(entries));
356
359
  };
357
360
  const traverseDocument = async (what, description, _options) => {
358
361
  const options = Object.assign({}, _options);
359
362
  const functions = [];
360
363
  if (!options.validate && Object.keys(what).length === 0) {
361
- return (0, common_1.right)({});
364
+ return common_1.Result.result({});
362
365
  }
363
366
  if (options.recurseDeep) {
364
367
  functions.push(recurseDeep);
@@ -376,7 +379,7 @@ const traverseDocument = async (what, description, _options) => {
376
379
  }
377
380
  const wholenessError = (0, validation_1.validateWholeness)(what, descriptionCopy);
378
381
  if (wholenessError) {
379
- return (0, common_1.left)(wholenessError);
382
+ return common_1.Result.error(wholenessError);
380
383
  }
381
384
  functions.push(validate);
382
385
  }
@@ -402,21 +405,21 @@ const traverseDocument = async (what, description, _options) => {
402
405
  },
403
406
  }),
404
407
  });
405
- const resultEither = await recurse(what, {
408
+ const { error, result } = await recurse(what, {
406
409
  root: what,
407
410
  property: description,
408
411
  propPath: '',
409
412
  options,
410
413
  });
411
- if ((0, common_1.isLeft)(resultEither)) {
412
- return (0, common_1.left)((0, common_1.unwrapEither)(resultEither));
414
+ if (error) {
415
+ return common_1.Result.error(error);
413
416
  }
414
417
  if (validationError) {
415
- return (0, common_1.left)((0, validation_1.makeValidationError)({
418
+ return common_1.Result.error((0, validation_1.makeValidationError)({
416
419
  code: types_1.ValidationErrorCode.InvalidProperties,
417
420
  errors: validationError,
418
421
  }));
419
422
  }
420
- return (0, common_1.right)((0, common_1.unwrapEither)(resultEither));
423
+ return common_1.Result.result(result);
421
424
  };
422
425
  exports.traverseDocument = traverseDocument;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  import { ACError, ValidationErrorCode } from "@aeriajs/types";
3
- import { left, right, isLeft, unwrapEither, throwIfLeft, pipe, isReference, getValueFromPath, isObjectId } from "@aeriajs/common";
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 left("INVALID_DOCUMENT_ID" /* InvalidDocumentId */);
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 validationEither = validateProperty(ctx.propName, value, ctx.property);
141
- if (isLeft(validationEither)) {
142
- return left({
143
- [ctx.propName]: unwrapEither(validationEither)
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 left("INVALID_TEMPFILE" /* InvalidTempfile */);
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 resultEither = await recurse(value, ctx);
182
- return unwrapEither(resultEither);
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 left(ACError.InsecureOperator);
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 operatorEither2 = await recurse(operation, ctx);
246
- if (isLeft(operatorEither2)) {
247
- return left(unwrapEither(operatorEither2));
248
+ const { error: error2, result } = await recurse(operation, ctx);
249
+ if (error2) {
250
+ return Result.error(error2);
248
251
  }
249
- operations.push(unwrapEither(operatorEither2));
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 operatorEither = await recurse(value, ctx);
258
- if (isLeft(operatorEither)) {
259
- return left(unwrapEither(operatorEither));
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
- unwrapEither(operatorEither)
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(throwIfLeft(await getCollectionAsset(propCast.$ref, "description")));
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 documentEither2 = await traverseDocument(elem, targetDescription, ctx.options);
279
- if (isLeft(documentEither2)) {
280
- return left(unwrapEither(documentEither2));
281
+ const { error: error2, result } = await traverseDocument(elem, targetDescription, ctx.options);
282
+ if (error2) {
283
+ return Result.error(error2);
281
284
  }
282
- documents.push(unwrapEither(documentEither2));
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 documentEither = await traverseDocument(value, targetDescription, ctx.options);
291
- if (isLeft(documentEither)) {
292
- return left(unwrapEither(documentEither));
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
- unwrapEither(documentEither)
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 right(Object.fromEntries(entries));
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 right({});
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 left(wholenessError);
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 result = await fn(value, ctx);
348
- ctx.target[ctx.propName] = result;
349
- return result;
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 resultEither = await recurse(what, {
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 (isLeft(resultEither)) {
370
- return left(unwrapEither(resultEither));
372
+ if (error) {
373
+ return Result.error(error);
371
374
  }
372
375
  if (validationError) {
373
- return left(makeValidationError({
376
+ return Result.error(makeValidationError({
374
377
  code: ValidationErrorCode.InvalidProperties,
375
378
  errors: validationError
376
379
  }));
377
380
  }
378
- return right(unwrapEither(resultEither));
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, endpointError) => {
80
- return (0, common_1.error)(Object.assign({
79
+ context.error = (httpStatus, error) => {
80
+ return (0, common_1.endpointError)(Object.assign({
81
81
  httpStatus,
82
- }, endpointError));
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.throwIfLeft)(await getCollectionAsset(collectionName, 'description'));
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 { throwIfLeft, error } from "@aeriajs/common";
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, endpointError) => {
55
- return error(Object.assign({
54
+ context.error = (httpStatus, error) => {
55
+ return endpointError(Object.assign({
56
56
  httpStatus
57
- }, endpointError));
57
+ }, error));
58
58
  };
59
59
  context.limitRate = (params) => {
60
60
  return limitRate(params, context);
61
61
  };
62
62
  if (collectionName) {
63
- const description = throwIfLeft(await getCollectionAsset(collectionName, "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<any>;
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.throwIfLeft)(await security.beforeRead(payload));
9
+ const { filters } = (0, common_1.throwIfError)(await security.beforeRead(payload));
10
10
  const { $text, ...filtersRest } = filters;
11
- const traversedFilters = (0, common_1.throwIfLeft)(await (0, index_js_1.traverseDocument)(filtersRest, context.description, {
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 { throwIfLeft } from "@aeriajs/common";
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 } = throwIfLeft(await security.beforeRead(payload));
7
+ const { filters } = throwIfError(await security.beforeRead(payload));
8
8
  const { $text, ...filtersRest } = filters;
9
- const traversedFilters = throwIfLeft(await traverseDocument(filtersRest, context.description, {
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.throwIfLeft)(await security.beforeRead(payload))
11
+ ? (0, common_1.throwIfError)(await security.beforeRead(payload))
12
12
  : payload;
13
13
  if (Object.keys(filters).length === 0) {
14
- throw new Error('no filters were passed');
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.throwIfLeft)(await (0, index_js_1.traverseDocument)(filters, context.description, {
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 result = await context.collection.model.aggregate(pipeline).next();
38
- if (!result) {
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
- return (0, index_js_1.fill)((0, common_1.throwIfLeft)(await (0, index_js_1.traverseDocument)(result, context.description, {
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 { throwIfLeft } from "@aeriajs/common";
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 ? throwIfLeft(await security.beforeRead(payload)) : payload;
17
+ } = !options?.bypassSecurity ? throwIfError(await security.beforeRead(payload)) : payload;
18
18
  if (Object.keys(filters).length === 0) {
19
- throw new Error("no filters were passed");
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: throwIfLeft(await traverseDocument(filters, context.description, {
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 result = await context.collection.model.aggregate(pipeline).next();
43
- if (!result) {
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
- return fill(throwIfLeft(await traverseDocument(result, context.description, {
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<SchemaWithId<TContext["description"]>[]>;
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.throwIfLeft)(await security.beforeRead(payload))
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.throwIfLeft)(await (0, index_js_1.traverseDocument)(filtersRest, context.description, {
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.throwIfLeft)(await (0, index_js_1.traverseDocument)((0, index_js_1.fill)(document, context.description), context.description, {
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;