@aeriajs/security 0.0.136 → 0.0.137

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,10 +1,23 @@
1
- import type { Context } from '@aeriajs/types';
2
- import type { SecurityCheckProps, SecurityCheckReadPayload, SecurityCheckWritePayload } from './types.js';
1
+ import type { Context, CollectionHookProps } from '@aeriajs/types';
2
+ import type { CollectionHookReadPayload, CollectionHookWritePayload } from './types.js';
3
3
  import { ACError } from '@aeriajs/types';
4
- export declare const checkImmutability: (props: SecurityCheckProps<SecurityCheckReadPayload | SecurityCheckWritePayload>, context: Context) => Promise<{
4
+ export declare const checkImmutabilityRead: (props: CollectionHookProps<CollectionHookReadPayload>, context: Context) => Promise<{
5
5
  readonly _tag: "Result";
6
6
  readonly error: undefined;
7
- readonly result: SecurityCheckReadPayload | SecurityCheckWritePayload;
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;
8
21
  } | {
9
22
  readonly _tag: "Error";
10
23
  readonly error: ACError.ResourceNotFound;
@@ -1,14 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkImmutability = void 0;
3
+ exports.checkImmutabilityWrite = exports.checkImmutabilityRead = void 0;
4
4
  const types_1 = require("@aeriajs/types");
5
- const checkImmutability = async (props, context) => {
5
+ const checkImmutability = async (docId, props, context) => {
6
6
  if (!context.description.immutable) {
7
7
  return types_1.Result.result(props.payload);
8
8
  }
9
- const docId = 'filters' in props.payload
10
- ? props.payload.filters._id
11
- : props.payload.what._id;
12
9
  if (docId) {
13
10
  if (typeof context.description.immutable === 'function') {
14
11
  const doc = await context.collection.model.findOne({
@@ -26,4 +23,11 @@ const checkImmutability = async (props, context) => {
26
23
  }
27
24
  return types_1.Result.result(props.payload);
28
25
  };
29
- exports.checkImmutability = checkImmutability;
26
+ const checkImmutabilityRead = async (props, context) => {
27
+ return checkImmutability(props.payload.filters._id, props, context);
28
+ };
29
+ exports.checkImmutabilityRead = checkImmutabilityRead;
30
+ const checkImmutabilityWrite = async (props, context) => {
31
+ return checkImmutability(props.payload.what._id, props, context);
32
+ };
33
+ exports.checkImmutabilityWrite = checkImmutabilityWrite;
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  import { Result, ACError } from "@aeriajs/types";
3
- export const checkImmutability = async (props, context) => {
3
+ const checkImmutability = async (docId, props, context) => {
4
4
  if (!context.description.immutable) {
5
5
  return Result.result(props.payload);
6
6
  }
7
- const docId = "filters" in props.payload ? props.payload.filters._id : props.payload.what._id;
8
7
  if (docId) {
9
8
  if (typeof context.description.immutable === "function") {
10
9
  const doc = await context.collection.model.findOne({
@@ -20,3 +19,9 @@ export const checkImmutability = async (props, context) => {
20
19
  }
21
20
  return Result.result(props.payload);
22
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,15 +1,15 @@
1
- import type { Context, InsertPayload } from '@aeriajs/types';
2
- import type { SecurityCheckProps, SecurityCheckReadPayload } from './types.js';
1
+ import type { Context, CollectionHookProps } from '@aeriajs/types';
2
+ import type { CollectionHookReadPayload, CollectionHookWritePayload } from './types.js';
3
3
  import { ACError } from '@aeriajs/types';
4
- export declare const checkOwnershipRead: (props: SecurityCheckProps<SecurityCheckReadPayload>, context: Context) => Promise<{
4
+ export declare const checkOwnershipRead: <T extends CollectionHookReadPayload>(props: CollectionHookProps<T>, context: Context) => Promise<{
5
5
  readonly _tag: "Result";
6
6
  readonly error: undefined;
7
- readonly result: SecurityCheckReadPayload;
7
+ readonly result: {} & T;
8
8
  }>;
9
- export declare const checkOwnershipWrite: (props: SecurityCheckProps<InsertPayload<any>>, context: Context) => Promise<{
9
+ export declare const checkOwnershipWrite: <T extends CollectionHookWritePayload>(props: CollectionHookProps<T>, context: Context) => Promise<{
10
10
  readonly _tag: "Result";
11
11
  readonly error: undefined;
12
- readonly result: InsertPayload<any>;
12
+ readonly result: {} & T;
13
13
  } | {
14
14
  readonly _tag: "Error";
15
15
  readonly error: ACError.OwnershipError;
@@ -1,11 +1,12 @@
1
- import type { SecurityCheckProps, SecurityCheckReadPayload } from './types.js';
1
+ import type { CollectionHookProps } from '@aeriajs/types';
2
+ import type { CollectionHookReadPayload } from './types.js';
2
3
  import { ACError } from '@aeriajs/types';
3
- export declare const checkPagination: (props: SecurityCheckProps<SecurityCheckReadPayload>) => Promise<{
4
- readonly _tag: "Result";
5
- readonly error: undefined;
6
- readonly result: SecurityCheckReadPayload;
7
- } | {
4
+ export declare const checkPagination: <T extends CollectionHookReadPayload>(props: CollectionHookProps<T>) => Promise<{
8
5
  readonly _tag: "Error";
9
6
  readonly error: ACError.InvalidLimit;
10
7
  readonly result: undefined;
8
+ } | {
9
+ readonly _tag: "Result";
10
+ readonly error: undefined;
11
+ readonly result: T;
11
12
  }>;
package/dist/types.d.ts CHANGED
@@ -1,17 +1,9 @@
1
- import type { Result, Context, GetAllPayload, InsertPayload, ACError } from '@aeriajs/types';
2
- export type SecurityCheckReadPayload = {
1
+ export type CollectionHookReadPayload = {
3
2
  filters: Record<string, any>;
4
3
  sort?: Record<string, any>;
5
4
  limit?: number;
6
5
  offset?: number;
7
6
  };
8
- export type SecurityCheckWritePayload = {
7
+ export type CollectionHookWritePayload = {
9
8
  what: Record<string, any>;
10
9
  };
11
- export type SecurityCheckProps<TPayload extends Record<string, any> = any> = {
12
- propertyName?: string;
13
- parentId?: string;
14
- childId?: string;
15
- payload: TPayload;
16
- };
17
- export type SecurityCheck = (props: SecurityCheckProps, context: Context) => Promise<Result.Either<ACError, GetAllPayload<any> | InsertPayload<any>>>;
package/dist/use.d.ts CHANGED
@@ -1,21 +1,26 @@
1
- import type { Context, Description, GetAllPayload, InsertPayload } from '@aeriajs/types';
1
+ import type { Context, Description } from '@aeriajs/types';
2
+ import type { CollectionHookReadPayload, CollectionHookWritePayload } from './types.js';
2
3
  export declare const useSecurity: <TDescription extends Description>(context: Context<TDescription>) => {
3
- beforeRead: <TPayload extends Partial<GetAllPayload<any>>>(payload?: TPayload) => Promise<{
4
+ beforeRead: <TPayload extends Partial<CollectionHookReadPayload>>(payload?: TPayload) => Promise<{
4
5
  readonly _tag: "Error";
5
6
  readonly error: import("@aeriajs/types").ACError;
6
7
  readonly result: undefined;
7
8
  } | {
8
9
  readonly _tag: "Result";
9
10
  readonly error: undefined;
10
- readonly result: any;
11
+ readonly result: {
12
+ filters: {};
13
+ } & TPayload;
11
14
  }>;
12
- beforeWrite: <TPayload extends Partial<InsertPayload<any>>>(payload?: TPayload) => Promise<{
15
+ beforeWrite: <TPayload extends CollectionHookWritePayload>(payload?: TPayload) => Promise<{
13
16
  readonly _tag: "Error";
14
17
  readonly error: import("@aeriajs/types").ACError;
15
18
  readonly result: undefined;
16
19
  } | {
17
20
  readonly _tag: "Result";
18
21
  readonly error: undefined;
19
- readonly result: any;
22
+ readonly result: {
23
+ what: {};
24
+ } & TPayload;
20
25
  }>;
21
26
  };
package/dist/use.js CHANGED
@@ -4,9 +4,7 @@ exports.useSecurity = void 0;
4
4
  const types_1 = require("@aeriajs/types");
5
5
  const index_js_1 = require("./index.js");
6
6
  const chainFunctions = async (_props, context, functions) => {
7
- const props = Object.assign({
8
- filters: {},
9
- }, _props);
7
+ const props = Object.assign({}, _props);
10
8
  for (const fn of functions) {
11
9
  if (!fn) {
12
10
  continue;
@@ -21,8 +19,9 @@ const chainFunctions = async (_props, context, functions) => {
21
19
  };
22
20
  const useSecurity = (context) => {
23
21
  const beforeRead = async (payload) => {
24
- const newPayload = Object.assign({}, payload);
25
- newPayload.filters ??= {};
22
+ const newPayload = Object.assign({
23
+ filters: {},
24
+ }, payload);
26
25
  const props = {
27
26
  payload: newPayload,
28
27
  };
@@ -42,7 +41,7 @@ const useSecurity = (context) => {
42
41
  };
43
42
  return chainFunctions(props, context, [
44
43
  index_js_1.checkOwnershipWrite,
45
- index_js_1.checkImmutability,
44
+ index_js_1.checkImmutabilityWrite,
46
45
  ]);
47
46
  };
48
47
  return {
package/dist/use.mjs CHANGED
@@ -1,15 +1,13 @@
1
1
  "use strict";
2
2
  import { Result } from "@aeriajs/types";
3
3
  import {
4
- checkImmutability,
4
+ checkImmutabilityWrite,
5
5
  checkOwnershipRead,
6
6
  checkOwnershipWrite,
7
7
  checkPagination
8
8
  } from "./index.mjs";
9
9
  const chainFunctions = async (_props, context, functions) => {
10
- const props = Object.assign({
11
- filters: {}
12
- }, _props);
10
+ const props = Object.assign({}, _props);
13
11
  for (const fn of functions) {
14
12
  if (!fn) {
15
13
  continue;
@@ -24,8 +22,9 @@ const chainFunctions = async (_props, context, functions) => {
24
22
  };
25
23
  export const useSecurity = (context) => {
26
24
  const beforeRead = async (payload) => {
27
- const newPayload = Object.assign({}, payload);
28
- newPayload.filters ??= {};
25
+ const newPayload = Object.assign({
26
+ filters: {}
27
+ }, payload);
29
28
  const props = {
30
29
  payload: newPayload
31
30
  };
@@ -43,7 +42,7 @@ export const useSecurity = (context) => {
43
42
  };
44
43
  return chainFunctions(props, context, [
45
44
  checkOwnershipWrite,
46
- checkImmutability
45
+ checkImmutabilityWrite
47
46
  ]);
48
47
  };
49
48
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/security",
3
- "version": "0.0.136",
3
+ "version": "0.0.137",
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.136",
32
- "@aeriajs/common": "^0.0.85",
33
- "@aeriajs/types": "^0.0.73",
31
+ "@aeriajs/core": "^0.0.137",
32
+ "@aeriajs/common": "^0.0.86",
33
+ "@aeriajs/types": "^0.0.74",
34
34
  "mongodb": "^6.5.0"
35
35
  },
36
36
  "scripts": {