@aeriajs/security 0.0.137 → 0.0.139

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/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export * from './define.js';
2
- export * from './immutability.js';
3
- export * from './ownership.js';
4
- export * from './pagination.js';
2
+ export * from './middleware.js';
3
+ export * from './middlewares/index.js';
5
4
  export * from './rateLimiting.js';
6
5
  export * from './use.js';
package/dist/index.js CHANGED
@@ -15,8 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./define.js"), exports);
18
- __exportStar(require("./immutability.js"), exports);
19
- __exportStar(require("./ownership.js"), exports);
20
- __exportStar(require("./pagination.js"), exports);
18
+ __exportStar(require("./middleware.js"), exports);
19
+ __exportStar(require("./middlewares/index.js"), exports);
21
20
  __exportStar(require("./rateLimiting.js"), exports);
22
21
  __exportStar(require("./use.js"), exports);
package/dist/index.mjs CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  export * from "./define.mjs";
3
- export * from "./immutability.mjs";
4
- export * from "./ownership.mjs";
5
- export * from "./pagination.mjs";
3
+ export * from "./middleware.mjs";
4
+ export * from "./middlewares/index.mjs";
6
5
  export * from "./rateLimiting.mjs";
7
6
  export * from "./use.mjs";
@@ -0,0 +1,2 @@
1
+ import type { Middleware, MiddlewareNext, GenericMiddlewareNext, Context } from '@aeriajs/types';
2
+ export declare const iterableMiddlewares: <TReturn, TPayload, TReturnNext extends GenericMiddlewareNext<TReturn, TPayload> = MiddlewareNext>(middlewares: Middleware<TReturn, TPayload, TReturnNext>[], end?: (_: unknown, initial: TReturn) => TReturn) => (payload: TPayload, initial: TReturn, context: Context) => TReturn | Promise<TReturn>;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.iterableMiddlewares = void 0;
4
+ const iterableMiddlewares = function (middlewares, end = (_, initial) => initial) {
5
+ const [first, ...subsequent] = middlewares;
6
+ const it = function* () {
7
+ for (const middleware of subsequent.concat([end])) {
8
+ yield (payload, initial, context) => {
9
+ const { value: next } = it.next();
10
+ return middleware(payload, initial, context, next);
11
+ };
12
+ }
13
+ }();
14
+ return (payload, initial, context) => {
15
+ const { value: next } = it.next();
16
+ return first(payload, initial, context, next);
17
+ };
18
+ };
19
+ exports.iterableMiddlewares = iterableMiddlewares;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ export const iterableMiddlewares = function(middlewares, end = (_, initial) => initial) {
3
+ const [first, ...subsequent] = middlewares;
4
+ const it = function* () {
5
+ for (const middleware of subsequent.concat([end])) {
6
+ yield (payload, initial, context) => {
7
+ const { value: next } = it.next();
8
+ return middleware(payload, initial, context, next);
9
+ };
10
+ }
11
+ }();
12
+ return (payload, initial, context) => {
13
+ const { value: next } = it.next();
14
+ return first(payload, initial, context, next);
15
+ };
16
+ };
@@ -0,0 +1,13 @@
1
+ import type { Context, CollectionHookProps, GenericMiddlewareNext } from '@aeriajs/types';
2
+ import type { CollectionHookReadPayload, CollectionHookWritePayload } from '../types.js';
3
+ import { Result, ACError } from '@aeriajs/types';
4
+ export declare const checkImmutabilityRead: <T extends CollectionHookReadPayload>(props: CollectionHookProps<T>, initial: Result.Either<unknown, T>, context: Context, next: GenericMiddlewareNext<Result.Result<T>, CollectionHookProps<T>>) => Promise<Result.Result<T> | {
5
+ readonly _tag: "Error";
6
+ readonly error: ACError.TargetImmutable | ACError.ResourceNotFound;
7
+ readonly result: undefined;
8
+ }>;
9
+ export declare const checkImmutabilityWrite: <T extends CollectionHookWritePayload>(props: CollectionHookProps<T>, initial: Result.Either<unknown, T>, context: Context, next: GenericMiddlewareNext<Result.Result<T>, CollectionHookProps<T>>) => Promise<Result.Result<T> | {
10
+ readonly _tag: "Error";
11
+ readonly error: ACError.TargetImmutable | ACError.ResourceNotFound;
12
+ readonly result: undefined;
13
+ }>;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.checkImmutabilityWrite = exports.checkImmutabilityRead = void 0;
4
4
  const types_1 = require("@aeriajs/types");
5
+ const common_1 = require("@aeriajs/common");
5
6
  const checkImmutability = async (docId, props, context) => {
6
7
  if (!context.description.immutable) {
7
8
  return types_1.Result.result(props.payload);
@@ -23,11 +24,21 @@ const checkImmutability = async (docId, props, context) => {
23
24
  }
24
25
  return types_1.Result.result(props.payload);
25
26
  };
26
- const checkImmutabilityRead = async (props, context) => {
27
- return checkImmutability(props.payload.filters._id, props, context);
27
+ const checkImmutabilityRead = async (props, initial, context, next) => {
28
+ const originalPayload = (0, common_1.throwIfError)(initial);
29
+ const { result: payload, error } = await checkImmutability(originalPayload.filters._id, props, context);
30
+ if (error) {
31
+ return types_1.Result.error(error);
32
+ }
33
+ return next(payload, types_1.Result.result(payload), context);
28
34
  };
29
35
  exports.checkImmutabilityRead = checkImmutabilityRead;
30
- const checkImmutabilityWrite = async (props, context) => {
31
- return checkImmutability(props.payload.what._id, props, context);
36
+ const checkImmutabilityWrite = async (props, initial, context, next) => {
37
+ const originalPayload = (0, common_1.throwIfError)(initial);
38
+ const { result: payload, error } = await checkImmutability(originalPayload.what._id, props, context);
39
+ if (error) {
40
+ return types_1.Result.error(error);
41
+ }
42
+ return next(payload, types_1.Result.result(payload), context);
32
43
  };
33
44
  exports.checkImmutabilityWrite = checkImmutabilityWrite;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ import { Result, ACError } from "@aeriajs/types";
3
+ import { throwIfError } from "@aeriajs/common";
4
+ const checkImmutability = async (docId, props, context) => {
5
+ if (!context.description.immutable) {
6
+ return Result.result(props.payload);
7
+ }
8
+ if (docId) {
9
+ if (typeof context.description.immutable === "function") {
10
+ const doc = await context.collection.model.findOne({
11
+ _id: docId
12
+ });
13
+ if (!doc) {
14
+ return Result.error(ACError.ResourceNotFound);
15
+ }
16
+ const isImmutable = await context.description.immutable(doc);
17
+ return isImmutable ? Result.error(ACError.TargetImmutable) : Result.result(props.payload);
18
+ }
19
+ return Result.error(ACError.TargetImmutable);
20
+ }
21
+ return Result.result(props.payload);
22
+ };
23
+ export const checkImmutabilityRead = async (props, initial, context, next) => {
24
+ const originalPayload = throwIfError(initial);
25
+ const { result: payload, error } = await checkImmutability(originalPayload.filters._id, props, context);
26
+ if (error) {
27
+ return Result.error(error);
28
+ }
29
+ return next(payload, Result.result(payload), context);
30
+ };
31
+ export const checkImmutabilityWrite = async (props, initial, context, next) => {
32
+ const originalPayload = throwIfError(initial);
33
+ const { result: payload, error } = await checkImmutability(originalPayload.what._id, props, context);
34
+ if (error) {
35
+ return Result.error(error);
36
+ }
37
+ return next(payload, Result.result(payload), context);
38
+ };
@@ -0,0 +1,3 @@
1
+ export * from './immutability.js';
2
+ export * from './ownership.js';
3
+ export * from './pagination.js';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./immutability.js"), exports);
18
+ __exportStar(require("./ownership.js"), exports);
19
+ __exportStar(require("./pagination.js"), exports);
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ export * from "./immutability.mjs";
3
+ export * from "./ownership.mjs";
4
+ export * from "./pagination.mjs";
@@ -0,0 +1,9 @@
1
+ import type { GenericMiddlewareNext, Context, CollectionHookProps } from '@aeriajs/types';
2
+ import type { CollectionHookReadPayload, CollectionHookWritePayload } from '../types.js';
3
+ import { Result, ACError } from '@aeriajs/types';
4
+ export declare const checkOwnershipRead: <T extends CollectionHookReadPayload>(props: CollectionHookProps<T>, initial: Result.Either<unknown, T>, context: Context, next: GenericMiddlewareNext<Result.Result<T>, CollectionHookProps<T>>) => Promise<Result.Result<T>>;
5
+ export declare const checkOwnershipWrite: <T extends CollectionHookWritePayload>(props: CollectionHookProps<T>, initial: Result.Either<unknown, T>, context: Context, next: GenericMiddlewareNext<Result.Result<T>, CollectionHookProps<T>>) => Promise<Result.Result<T> | {
6
+ readonly _tag: "Error";
7
+ readonly error: ACError.OwnershipError;
8
+ readonly result: undefined;
9
+ }>;
@@ -2,32 +2,33 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.checkOwnershipWrite = exports.checkOwnershipRead = void 0;
4
4
  const types_1 = require("@aeriajs/types");
5
- const checkOwnershipRead = async (props, context) => {
5
+ const common_1 = require("@aeriajs/common");
6
+ const checkOwnershipRead = async (props, initial, context, next) => {
6
7
  const { token, description } = context;
7
- const payload = Object.assign({}, props.payload);
8
+ const payload = (0, common_1.throwIfError)(initial);
8
9
  if (token.authenticated && description.owned) {
9
10
  if (!token.roles.includes('root')) {
10
11
  payload.filters.owner = token.sub;
11
12
  }
12
13
  }
13
- return types_1.Result.result(payload);
14
+ return next(props, types_1.Result.result(payload), context);
14
15
  };
15
16
  exports.checkOwnershipRead = checkOwnershipRead;
16
- const checkOwnershipWrite = async (props, context) => {
17
+ const checkOwnershipWrite = async (props, initial, context, next) => {
17
18
  const { token, description } = context;
18
19
  const { parentId } = props;
19
- const payload = Object.assign({}, props.payload);
20
+ const payload = (0, common_1.throwIfError)(initial);
20
21
  if (token.authenticated && description.owned) {
21
22
  if (!payload.what._id || description.owned === 'always') {
22
23
  payload.what.owner = token.sub;
23
24
  }
24
25
  else {
25
- return types_1.Result.result(payload);
26
+ return next(props, types_1.Result.result(payload), context);
26
27
  }
27
28
  }
28
29
  if ((!payload.what.owner && !parentId) && context.description.owned) {
29
30
  return types_1.Result.error(types_1.ACError.OwnershipError);
30
31
  }
31
- return types_1.Result.result(payload);
32
+ return next(props, types_1.Result.result(payload), context);
32
33
  };
33
34
  exports.checkOwnershipWrite = checkOwnershipWrite;
@@ -1,28 +1,29 @@
1
1
  "use strict";
2
2
  import { Result, ACError } from "@aeriajs/types";
3
- export const checkOwnershipRead = async (props, context) => {
3
+ import { throwIfError } from "@aeriajs/common";
4
+ export const checkOwnershipRead = async (props, initial, context, next) => {
4
5
  const { token, description } = context;
5
- const payload = Object.assign({}, props.payload);
6
+ const payload = throwIfError(initial);
6
7
  if (token.authenticated && description.owned) {
7
8
  if (!token.roles.includes("root")) {
8
9
  payload.filters.owner = token.sub;
9
10
  }
10
11
  }
11
- return Result.result(payload);
12
+ return next(props, Result.result(payload), context);
12
13
  };
13
- export const checkOwnershipWrite = async (props, context) => {
14
+ export const checkOwnershipWrite = async (props, initial, context, next) => {
14
15
  const { token, description } = context;
15
16
  const { parentId } = props;
16
- const payload = Object.assign({}, props.payload);
17
+ const payload = throwIfError(initial);
17
18
  if (token.authenticated && description.owned) {
18
19
  if (!payload.what._id || description.owned === "always") {
19
20
  payload.what.owner = token.sub;
20
21
  } else {
21
- return Result.result(payload);
22
+ return next(props, Result.result(payload), context);
22
23
  }
23
24
  }
24
25
  if (!payload.what.owner && !parentId && context.description.owned) {
25
26
  return Result.error(ACError.OwnershipError);
26
27
  }
27
- return Result.result(payload);
28
+ return next(props, Result.result(payload), context);
28
29
  };
@@ -0,0 +1,8 @@
1
+ import type { CollectionHookProps, GenericMiddlewareNext, Context } from '@aeriajs/types';
2
+ import type { CollectionHookReadPayload } from '../types.js';
3
+ import { Result, ACError } from '@aeriajs/types';
4
+ export declare const checkPagination: <T extends CollectionHookReadPayload>(props: CollectionHookProps<T>, initial: Result.Either<unknown, T>, context: Context, next: GenericMiddlewareNext<Result.Result<T>, CollectionHookProps<T>>) => Promise<Result.Result<T> | {
5
+ readonly _tag: "Error";
6
+ readonly error: ACError.InvalidLimit;
7
+ readonly result: undefined;
8
+ }>;
@@ -2,13 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.checkPagination = void 0;
4
4
  const types_1 = require("@aeriajs/types");
5
- const checkPagination = async (props) => {
6
- const { payload } = props;
5
+ const common_1 = require("@aeriajs/common");
6
+ const checkPagination = async (props, initial, context, next) => {
7
+ const payload = (0, common_1.throwIfError)(initial);
7
8
  if (payload.limit) {
8
9
  if (payload.limit <= 0 || payload.limit > 150) {
9
10
  return types_1.Result.error(types_1.ACError.InvalidLimit);
10
11
  }
11
12
  }
12
- return types_1.Result.result(payload);
13
+ return next(props, types_1.Result.result(payload), context);
13
14
  };
14
15
  exports.checkPagination = checkPagination;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ import { Result, ACError } from "@aeriajs/types";
3
+ import { throwIfError } from "@aeriajs/common";
4
+ export const checkPagination = async (props, initial, context, next) => {
5
+ const payload = throwIfError(initial);
6
+ if (payload.limit) {
7
+ if (payload.limit <= 0 || payload.limit > 150) {
8
+ return Result.error(ACError.InvalidLimit);
9
+ }
10
+ }
11
+ return next(props, Result.result(payload), context);
12
+ };
package/dist/use.d.ts CHANGED
@@ -1,26 +1,7 @@
1
- import type { Context, Description } from '@aeriajs/types';
1
+ import type { Context, Description, ACError } from '@aeriajs/types';
2
2
  import type { CollectionHookReadPayload, CollectionHookWritePayload } from './types.js';
3
+ import { Result } from '@aeriajs/types';
3
4
  export declare const useSecurity: <TDescription extends Description>(context: Context<TDescription>) => {
4
- beforeRead: <TPayload extends Partial<CollectionHookReadPayload>>(payload?: TPayload) => Promise<{
5
- readonly _tag: "Error";
6
- readonly error: import("@aeriajs/types").ACError;
7
- readonly result: undefined;
8
- } | {
9
- readonly _tag: "Result";
10
- readonly error: undefined;
11
- readonly result: {
12
- filters: {};
13
- } & TPayload;
14
- }>;
15
- beforeWrite: <TPayload extends CollectionHookWritePayload>(payload?: TPayload) => Promise<{
16
- readonly _tag: "Error";
17
- readonly error: import("@aeriajs/types").ACError;
18
- readonly result: undefined;
19
- } | {
20
- readonly _tag: "Result";
21
- readonly error: undefined;
22
- readonly result: {
23
- what: {};
24
- } & TPayload;
25
- }>;
5
+ secureReadPayload: <TPayload extends Partial<CollectionHookReadPayload>>(payload?: TPayload) => Promise<Result.Either<ACError.InvalidLimit, TPayload & CollectionHookReadPayload>>;
6
+ secureWritePayload: <TPayload extends CollectionHookWritePayload>(payload?: TPayload) => Promise<Result.Either<ACError, TPayload>>;
26
7
  };
package/dist/use.js CHANGED
@@ -2,51 +2,39 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useSecurity = void 0;
4
4
  const types_1 = require("@aeriajs/types");
5
- const index_js_1 = require("./index.js");
6
- const chainFunctions = async (_props, context, functions) => {
7
- const props = Object.assign({}, _props);
8
- for (const fn of functions) {
9
- if (!fn) {
10
- continue;
11
- }
12
- const { error, result } = await fn(props, context);
13
- if (error) {
14
- return types_1.Result.error(error);
15
- }
16
- Object.assign(props.payload, result);
17
- }
18
- return types_1.Result.result(props.payload);
19
- };
5
+ const middleware_js_1 = require("./middleware.js");
6
+ const index_js_1 = require("./middlewares/index.js");
20
7
  const useSecurity = (context) => {
21
- const beforeRead = async (payload) => {
8
+ const secureReadPayload = async (payload) => {
22
9
  const newPayload = Object.assign({
23
10
  filters: {},
24
11
  }, payload);
25
12
  const props = {
26
13
  payload: newPayload,
27
14
  };
28
- return chainFunctions(props, context, [
29
- index_js_1.checkPagination,
30
- context.description.owned === 'on-write'
31
- ? null
32
- : index_js_1.checkOwnershipRead,
33
- ]);
15
+ const middlewares = [index_js_1.checkPagination];
16
+ if (context.description.owned !== 'on-write') {
17
+ middlewares.push(index_js_1.checkOwnershipRead);
18
+ }
19
+ const start = (0, middleware_js_1.iterableMiddlewares)(middlewares);
20
+ return start(props, types_1.Result.result(newPayload), context);
34
21
  };
35
- const beforeWrite = async (payload) => {
22
+ const secureWritePayload = async (payload) => {
36
23
  const newPayload = Object.assign({
37
24
  what: {},
38
25
  }, payload);
39
26
  const props = {
40
27
  payload: newPayload,
41
28
  };
42
- return chainFunctions(props, context, [
29
+ const start = (0, middleware_js_1.iterableMiddlewares)([
43
30
  index_js_1.checkOwnershipWrite,
44
31
  index_js_1.checkImmutabilityWrite,
45
32
  ]);
33
+ return start(props, types_1.Result.result(newPayload), context);
46
34
  };
47
35
  return {
48
- beforeRead,
49
- beforeWrite,
36
+ secureReadPayload,
37
+ secureWritePayload,
50
38
  };
51
39
  };
52
40
  exports.useSecurity = useSecurity;
package/dist/use.mjs CHANGED
@@ -1,52 +1,42 @@
1
1
  "use strict";
2
2
  import { Result } from "@aeriajs/types";
3
+ import { iterableMiddlewares } from "./middleware.mjs";
3
4
  import {
4
5
  checkImmutabilityWrite,
5
6
  checkOwnershipRead,
6
7
  checkOwnershipWrite,
7
8
  checkPagination
8
- } from "./index.mjs";
9
- const chainFunctions = async (_props, context, functions) => {
10
- const props = Object.assign({}, _props);
11
- for (const fn of functions) {
12
- if (!fn) {
13
- continue;
14
- }
15
- const { error, result } = await fn(props, context);
16
- if (error) {
17
- return Result.error(error);
18
- }
19
- Object.assign(props.payload, result);
20
- }
21
- return Result.result(props.payload);
22
- };
9
+ } from "./middlewares/index.mjs";
23
10
  export const useSecurity = (context) => {
24
- const beforeRead = async (payload) => {
11
+ const secureReadPayload = async (payload) => {
25
12
  const newPayload = Object.assign({
26
13
  filters: {}
27
14
  }, payload);
28
15
  const props = {
29
16
  payload: newPayload
30
17
  };
31
- return chainFunctions(props, context, [
32
- checkPagination,
33
- context.description.owned === "on-write" ? null : checkOwnershipRead
34
- ]);
18
+ const middlewares = [checkPagination];
19
+ if (context.description.owned !== "on-write") {
20
+ middlewares.push(checkOwnershipRead);
21
+ }
22
+ const start = iterableMiddlewares(middlewares);
23
+ return start(props, Result.result(newPayload), context);
35
24
  };
36
- const beforeWrite = async (payload) => {
25
+ const secureWritePayload = async (payload) => {
37
26
  const newPayload = Object.assign({
38
27
  what: {}
39
28
  }, payload);
40
29
  const props = {
41
30
  payload: newPayload
42
31
  };
43
- return chainFunctions(props, context, [
32
+ const start = iterableMiddlewares([
44
33
  checkOwnershipWrite,
45
34
  checkImmutabilityWrite
46
35
  ]);
36
+ return start(props, Result.result(newPayload), context);
47
37
  };
48
38
  return {
49
- beforeRead,
50
- beforeWrite
39
+ secureReadPayload,
40
+ secureWritePayload
51
41
  };
52
42
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/security",
3
- "version": "0.0.137",
3
+ "version": "0.0.139",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -28,9 +28,9 @@
28
28
  "mongodb": "^6.5.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "@aeriajs/core": "^0.0.137",
32
- "@aeriajs/common": "^0.0.86",
33
- "@aeriajs/types": "^0.0.74",
31
+ "@aeriajs/core": "^0.0.139",
32
+ "@aeriajs/common": "^0.0.88",
33
+ "@aeriajs/types": "^0.0.76",
34
34
  "mongodb": "^6.5.0"
35
35
  },
36
36
  "scripts": {
@@ -1,29 +0,0 @@
1
- import type { Context, CollectionHookProps } from '@aeriajs/types';
2
- import type { CollectionHookReadPayload, CollectionHookWritePayload } from './types.js';
3
- import { ACError } from '@aeriajs/types';
4
- export declare const checkImmutabilityRead: (props: CollectionHookProps<CollectionHookReadPayload>, context: Context) => Promise<{
5
- readonly _tag: "Result";
6
- readonly error: undefined;
7
- readonly result: any;
8
- } | {
9
- readonly _tag: "Error";
10
- readonly error: ACError.ResourceNotFound;
11
- readonly result: undefined;
12
- } | {
13
- readonly _tag: "Error";
14
- readonly error: ACError.TargetImmutable;
15
- readonly result: undefined;
16
- }>;
17
- export declare const checkImmutabilityWrite: (props: CollectionHookProps<CollectionHookWritePayload>, context: Context) => Promise<{
18
- readonly _tag: "Result";
19
- readonly error: undefined;
20
- readonly result: any;
21
- } | {
22
- readonly _tag: "Error";
23
- readonly error: ACError.ResourceNotFound;
24
- readonly result: undefined;
25
- } | {
26
- readonly _tag: "Error";
27
- readonly error: ACError.TargetImmutable;
28
- readonly result: undefined;
29
- }>;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- import { Result, ACError } from "@aeriajs/types";
3
- const checkImmutability = async (docId, props, context) => {
4
- if (!context.description.immutable) {
5
- return Result.result(props.payload);
6
- }
7
- if (docId) {
8
- if (typeof context.description.immutable === "function") {
9
- const doc = await context.collection.model.findOne({
10
- _id: docId
11
- });
12
- if (!doc) {
13
- return Result.error(ACError.ResourceNotFound);
14
- }
15
- const isImmutable = await context.description.immutable(doc);
16
- return isImmutable ? Result.error(ACError.TargetImmutable) : Result.result(props.payload);
17
- }
18
- return Result.error(ACError.TargetImmutable);
19
- }
20
- return Result.result(props.payload);
21
- };
22
- export const checkImmutabilityRead = async (props, context) => {
23
- return checkImmutability(props.payload.filters._id, props, context);
24
- };
25
- export const checkImmutabilityWrite = async (props, context) => {
26
- return checkImmutability(props.payload.what._id, props, context);
27
- };
@@ -1,17 +0,0 @@
1
- import type { Context, CollectionHookProps } from '@aeriajs/types';
2
- import type { CollectionHookReadPayload, CollectionHookWritePayload } from './types.js';
3
- import { ACError } from '@aeriajs/types';
4
- export declare const checkOwnershipRead: <T extends CollectionHookReadPayload>(props: CollectionHookProps<T>, context: Context) => Promise<{
5
- readonly _tag: "Result";
6
- readonly error: undefined;
7
- readonly result: {} & T;
8
- }>;
9
- export declare const checkOwnershipWrite: <T extends CollectionHookWritePayload>(props: CollectionHookProps<T>, context: Context) => Promise<{
10
- readonly _tag: "Result";
11
- readonly error: undefined;
12
- readonly result: {} & T;
13
- } | {
14
- readonly _tag: "Error";
15
- readonly error: ACError.OwnershipError;
16
- readonly result: undefined;
17
- }>;
@@ -1,12 +0,0 @@
1
- import type { CollectionHookProps } from '@aeriajs/types';
2
- import type { CollectionHookReadPayload } from './types.js';
3
- import { ACError } from '@aeriajs/types';
4
- export declare const checkPagination: <T extends CollectionHookReadPayload>(props: CollectionHookProps<T>) => Promise<{
5
- readonly _tag: "Error";
6
- readonly error: ACError.InvalidLimit;
7
- readonly result: undefined;
8
- } | {
9
- readonly _tag: "Result";
10
- readonly error: undefined;
11
- readonly result: T;
12
- }>;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- import { Result, ACError } from "@aeriajs/types";
3
- export const checkPagination = async (props) => {
4
- const { payload } = props;
5
- if (payload.limit) {
6
- if (payload.limit <= 0 || payload.limit > 150) {
7
- return Result.error(ACError.InvalidLimit);
8
- }
9
- }
10
- return Result.result(payload);
11
- };