@aeriajs/core 0.0.85 → 0.0.87
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets.d.ts +4 -4
- package/dist/assets.js +10 -9
- package/dist/assets.mjs +11 -10
- package/dist/collection/define.d.ts +8 -8
- package/dist/collection/traverseDocument.d.ts +2 -2
- package/dist/context.js +4 -2
- package/dist/context.mjs +4 -2
- package/dist/endpoints.js +10 -10
- package/dist/endpoints.mjs +10 -10
- package/dist/functions/builtin/insert.d.ts +1 -1
- package/package.json +8 -8
package/dist/assets.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Context, Collection, Token } from '@aeriajs/types';
|
|
2
|
-
import {
|
|
3
|
-
export declare const internalGetCollectionAsset: <TCollectionName extends string, TAssetName extends "item" | "description" | "security" | "functions" | "
|
|
4
|
-
export declare const getCollectionAsset: <TCollectionName extends string, TAssetName extends "item" | "description" | "security" | "functions" | "
|
|
2
|
+
import { ACError } from '@aeriajs/types';
|
|
3
|
+
export declare const internalGetCollectionAsset: <TCollectionName extends string, TAssetName extends "item" | "description" | "security" | "functions" | "contracts" | "exposedFunctions">(collectionName: TCollectionName, assetName: TAssetName) => Promise<import("@aeriajs/types").Left<ACError.ResourceNotFound> | import("@aeriajs/types").Left<ACError.AssetNotFound> | import("@aeriajs/types").Right<any>>;
|
|
4
|
+
export declare const getCollectionAsset: <TCollectionName extends string, TAssetName extends "item" | "description" | "security" | "functions" | "contracts" | "exposedFunctions">(collectionName: TCollectionName, assetName: TAssetName) => Promise<import("@aeriajs/types").Left<ACError.ResourceNotFound> | import("@aeriajs/types").Left<ACError.AssetNotFound> | import("@aeriajs/types").Right<NonNullable<Collection[TAssetName]>>>;
|
|
5
5
|
export declare const getFunction: <TCollectionName extends string, TFunctionName extends string>(collectionName: TCollectionName, functionName: TFunctionName, token?: Token, options?: {
|
|
6
6
|
exposedOnly: boolean;
|
|
7
|
-
}) => Promise<import("@aeriajs/types").Left<
|
|
7
|
+
}) => Promise<import("@aeriajs/types").Left<ACError.ResourceNotFound> | import("@aeriajs/types").Left<ACError.AssetNotFound> | import("@aeriajs/types").Left<ACError.FunctionNotFound> | import("@aeriajs/types").Left<ACError.FunctionNotExposed> | import("@aeriajs/types").Left<ACError.AuthorizationError> | import("@aeriajs/types").Right<(payload: unknown, context: Context) => Promise<any>>>;
|
package/dist/assets.js
CHANGED
|
@@ -14,9 +14,9 @@ const internalGetCollectionAsset = async (collectionName, assetName) => {
|
|
|
14
14
|
const asset = collection?.[assetName];
|
|
15
15
|
if (!asset) {
|
|
16
16
|
if (!collection) {
|
|
17
|
-
return (0, common_1.left)(types_1.
|
|
17
|
+
return (0, common_1.left)(types_1.ACError.ResourceNotFound);
|
|
18
18
|
}
|
|
19
|
-
return (0, common_1.left)(types_1.
|
|
19
|
+
return (0, common_1.left)(types_1.ACError.AssetNotFound);
|
|
20
20
|
}
|
|
21
21
|
return (0, common_1.right)(asset);
|
|
22
22
|
};
|
|
@@ -45,18 +45,18 @@ const getFunction = async (collectionName, functionName, token, options = {
|
|
|
45
45
|
}
|
|
46
46
|
const functions = (0, common_1.unwrapEither)(functionsEither);
|
|
47
47
|
if (!(functionName in functions)) {
|
|
48
|
-
return (0, common_1.left)(types_1.
|
|
48
|
+
return (0, common_1.left)(types_1.ACError.FunctionNotFound);
|
|
49
49
|
}
|
|
50
50
|
const collection = await (0, entrypoint_1.getCollection)(collectionName);
|
|
51
51
|
const fn = functions[functionName];
|
|
52
52
|
if (!collection) {
|
|
53
|
-
return (0, common_1.left)(types_1.
|
|
53
|
+
return (0, common_1.left)(types_1.ACError.ResourceNotFound);
|
|
54
54
|
}
|
|
55
55
|
if (options.exposedOnly) {
|
|
56
56
|
const exposedStatus = await (0, accessControl_js_1.isFunctionExposed)(collection, functionName, token);
|
|
57
57
|
switch (exposedStatus) {
|
|
58
|
-
case accessControl_js_1.FunctionExposedStatus.FunctionNotExposed: return (0, common_1.left)(types_1.
|
|
59
|
-
case accessControl_js_1.FunctionExposedStatus.FunctionNotGranted: return (0, common_1.left)(types_1.
|
|
58
|
+
case accessControl_js_1.FunctionExposedStatus.FunctionNotExposed: return (0, common_1.left)(types_1.ACError.FunctionNotExposed);
|
|
59
|
+
case accessControl_js_1.FunctionExposedStatus.FunctionNotGranted: return (0, common_1.left)(types_1.ACError.AuthorizationError);
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
const wrapper = async (payload, context) => {
|
|
@@ -65,9 +65,10 @@ const getFunction = async (collectionName, functionName, token, options = {
|
|
|
65
65
|
if (securityPolicy.rateLimiting) {
|
|
66
66
|
const rateLimitingEither = await (0, security_1.limitRate)(securityPolicy.rateLimiting, context);
|
|
67
67
|
if ((0, common_1.isLeft)(rateLimitingEither)) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
const error = (0, common_1.unwrapEither)(rateLimitingEither);
|
|
69
|
+
return context.error(types_1.HTTPStatus.TooManyRequests, {
|
|
70
|
+
code: error,
|
|
71
|
+
message: 'rate limit was achieved for this resource',
|
|
71
72
|
});
|
|
72
73
|
}
|
|
73
74
|
}
|
package/dist/assets.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
import {
|
|
2
|
+
import { ACError, HTTPStatus } from "@aeriajs/types";
|
|
3
3
|
import { left, right, isLeft, unwrapEither } from "@aeriajs/common";
|
|
4
4
|
import { limitRate } from "@aeriajs/security";
|
|
5
5
|
import { getCollection } from "@aeriajs/entrypoint";
|
|
@@ -12,9 +12,9 @@ export const internalGetCollectionAsset = async (collectionName, assetName) => {
|
|
|
12
12
|
const asset = collection?.[assetName];
|
|
13
13
|
if (!asset) {
|
|
14
14
|
if (!collection) {
|
|
15
|
-
return left(
|
|
15
|
+
return left(ACError.ResourceNotFound);
|
|
16
16
|
}
|
|
17
|
-
return left(
|
|
17
|
+
return left(ACError.AssetNotFound);
|
|
18
18
|
}
|
|
19
19
|
return right(asset);
|
|
20
20
|
};
|
|
@@ -41,20 +41,20 @@ export const getFunction = async (collectionName, functionName, token, options =
|
|
|
41
41
|
}
|
|
42
42
|
const functions = unwrapEither(functionsEither);
|
|
43
43
|
if (!(functionName in functions)) {
|
|
44
|
-
return left(
|
|
44
|
+
return left(ACError.FunctionNotFound);
|
|
45
45
|
}
|
|
46
46
|
const collection = await getCollection(collectionName);
|
|
47
47
|
const fn = functions[functionName];
|
|
48
48
|
if (!collection) {
|
|
49
|
-
return left(
|
|
49
|
+
return left(ACError.ResourceNotFound);
|
|
50
50
|
}
|
|
51
51
|
if (options.exposedOnly) {
|
|
52
52
|
const exposedStatus = await isFunctionExposed(collection, functionName, token);
|
|
53
53
|
switch (exposedStatus) {
|
|
54
54
|
case FunctionExposedStatus.FunctionNotExposed:
|
|
55
|
-
return left(
|
|
55
|
+
return left(ACError.FunctionNotExposed);
|
|
56
56
|
case FunctionExposedStatus.FunctionNotGranted:
|
|
57
|
-
return left(
|
|
57
|
+
return left(ACError.AuthorizationError);
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
const wrapper = async (payload, context) => {
|
|
@@ -63,9 +63,10 @@ export const getFunction = async (collectionName, functionName, token, options =
|
|
|
63
63
|
if (securityPolicy.rateLimiting) {
|
|
64
64
|
const rateLimitingEither = await limitRate(securityPolicy.rateLimiting, context);
|
|
65
65
|
if (isLeft(rateLimitingEither)) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
const error = unwrapEither(rateLimitingEither);
|
|
67
|
+
return context.error(HTTPStatus.TooManyRequests, {
|
|
68
|
+
code: error,
|
|
69
|
+
message: "rate limit was achieved for this resource"
|
|
69
70
|
});
|
|
70
71
|
}
|
|
71
72
|
}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import type { SchemaWithId, Collection,
|
|
1
|
+
import type { SchemaWithId, Collection, StrictContext, Contract, ContractToFunction, Description, CollectionSecurityPolicy, AccessCondition, RoleFromAccessCondition } from '@aeriajs/types';
|
|
2
2
|
export type ExtendCollection<TLeftCollection extends Collection, TRightCollection> = Omit<TLeftCollection & TRightCollection, 'item'> & {
|
|
3
3
|
item: SchemaWithId<(TLeftCollection & TRightCollection)['description']>;
|
|
4
4
|
};
|
|
5
|
-
export declare const defineCollection: <TCollection extends Omit<Collection<TCollection extends Collection ? TCollection : never>, "item" | "description" | "security" | "functions" | "exposedFunctions">, const TDescription extends Description<TDescription>, const
|
|
5
|
+
export declare const defineCollection: <TCollection extends Omit<Collection<TCollection extends Collection ? TCollection : never>, "item" | "description" | "security" | "functions" | "exposedFunctions">, const TDescription extends Description<TDescription>, const TContracts extends { [P in keyof TFunctions]?: Contract | undefined; }, const TExposedFunctions extends Partial<Record<keyof TFunctions, AccessCondition>>, const TFunctions extends Record<string, (payload: any, context: StrictContext<any>) => any> & { [P_1 in keyof TContracts | keyof TExposedFunctions]: ContractToFunction<P_1 extends keyof TContracts ? NonNullable<TContracts[P_1]> : any, StrictContext<P_1 extends keyof TExposedFunctions ? RoleFromAccessCondition<TExposedFunctions[P_1]> : never, TDescription>>; }>(collection: TCollection & {
|
|
6
6
|
description: TDescription;
|
|
7
7
|
functions?: TFunctions;
|
|
8
|
-
|
|
9
|
-
exposedFunctions?:
|
|
8
|
+
contracts?: TContracts;
|
|
9
|
+
exposedFunctions?: TExposedFunctions;
|
|
10
10
|
security?: CollectionSecurityPolicy<{
|
|
11
11
|
description: NoInfer<TDescription>;
|
|
12
|
-
functions: TFunctions
|
|
12
|
+
functions: NoInfer<TFunctions>;
|
|
13
13
|
}>;
|
|
14
14
|
}) => TCollection & {
|
|
15
15
|
item: SchemaWithId<TDescription>;
|
|
16
16
|
description: TDescription;
|
|
17
17
|
functions: TFunctions;
|
|
18
|
-
|
|
18
|
+
contracts: TContracts;
|
|
19
19
|
};
|
|
20
20
|
export declare const extendCollection: <const TLeftCollection extends Collection, const TRightCollection extends {
|
|
21
21
|
description?: Partial<Description> | undefined;
|
|
22
22
|
item?: Partial<any> | undefined;
|
|
23
23
|
security?: Partial<CollectionSecurityPolicy<any> | undefined>;
|
|
24
|
-
functions?: Partial<Record<string, (payload: any, context: Context, ...args: any[]) => any> | undefined>;
|
|
25
|
-
|
|
24
|
+
functions?: Partial<Record<string, (payload: any, context: import("@aeriajs/types").Context, ...args: any[]) => any> | undefined>;
|
|
25
|
+
contracts?: Partial<Record<string, Contract> | undefined>;
|
|
26
26
|
exposedFunctions?: Partial<Record<string, AccessCondition> | undefined>;
|
|
27
27
|
}>(left: TLeftCollection, right: TRightCollection) => ExtendCollection<TLeftCollection, TRightCollection>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Description, Property,
|
|
1
|
+
import type { Description, Property, ACError, ValidationError, Context } from '@aeriajs/types';
|
|
2
2
|
import { ObjectId } from 'mongodb';
|
|
3
3
|
export type TraverseOptions = {
|
|
4
4
|
autoCast?: boolean;
|
|
@@ -30,5 +30,5 @@ type PhaseContext = {
|
|
|
30
30
|
property: Property;
|
|
31
31
|
options: TraverseOptions & TraverseNormalized;
|
|
32
32
|
};
|
|
33
|
-
export declare const traverseDocument: <const TWhat extends Record<string, any>>(what: TWhat, description: Description, _options: TraverseOptions) => Promise<import("@aeriajs/types").Right<any> | import("@aeriajs/types").Left<
|
|
33
|
+
export declare const traverseDocument: <const TWhat extends Record<string, any>>(what: TWhat, description: Description, _options: TraverseOptions) => Promise<import("@aeriajs/types").Right<any> | import("@aeriajs/types").Left<ACError | ValidationError>>;
|
|
34
34
|
export {};
|
package/dist/context.js
CHANGED
|
@@ -76,8 +76,10 @@ const createContext = async (options = {}) => {
|
|
|
76
76
|
created_at: new Date(),
|
|
77
77
|
});
|
|
78
78
|
};
|
|
79
|
-
context.error = (endpointError) => {
|
|
80
|
-
return (0, common_1.error)(
|
|
79
|
+
context.error = (httpStatus, endpointError) => {
|
|
80
|
+
return (0, common_1.error)(Object.assign({
|
|
81
|
+
httpStatus,
|
|
82
|
+
}, endpointError), context);
|
|
81
83
|
};
|
|
82
84
|
context.limitRate = (params) => {
|
|
83
85
|
return (0, security_1.limitRate)(params, context);
|
package/dist/context.mjs
CHANGED
|
@@ -51,8 +51,10 @@ export const createContext = async (options = {}) => {
|
|
|
51
51
|
created_at: /* @__PURE__ */ new Date()
|
|
52
52
|
});
|
|
53
53
|
};
|
|
54
|
-
context.error = (endpointError) => {
|
|
55
|
-
return error(
|
|
54
|
+
context.error = (httpStatus, endpointError) => {
|
|
55
|
+
return error(Object.assign({
|
|
56
|
+
httpStatus
|
|
57
|
+
}, endpointError), context);
|
|
56
58
|
};
|
|
57
59
|
context.limitRate = (params) => {
|
|
58
60
|
return limitRate(params, context);
|
package/dist/endpoints.js
CHANGED
|
@@ -37,7 +37,7 @@ const getEndpoints = async () => {
|
|
|
37
37
|
const collection = typeof candidate === 'function'
|
|
38
38
|
? candidate()
|
|
39
39
|
: candidate;
|
|
40
|
-
const { description, functions: collectionFunctions,
|
|
40
|
+
const { description, functions: collectionFunctions, contracts, exposedFunctions = {}, } = collection;
|
|
41
41
|
if (collectionFunctions) {
|
|
42
42
|
for (const fnName in collectionFunctions) {
|
|
43
43
|
const exposedStatus = await (0, accessControl_js_1.isFunctionExposed)(collection, fnName);
|
|
@@ -51,22 +51,22 @@ const getEndpoints = async () => {
|
|
|
51
51
|
: exposed
|
|
52
52
|
? await (0, entrypoint_1.getAvailableRoles)()
|
|
53
53
|
: [];
|
|
54
|
-
const
|
|
54
|
+
const endpointContracts = {
|
|
55
55
|
POST: null,
|
|
56
56
|
};
|
|
57
57
|
if (roles.length) {
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
endpointContracts.POST ??= {};
|
|
59
|
+
endpointContracts.POST.roles = roles;
|
|
60
60
|
}
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
Object.assign(
|
|
61
|
+
if (contracts && fnName in contracts) {
|
|
62
|
+
endpointContracts.POST ??= {};
|
|
63
|
+
Object.assign(endpointContracts.POST, contracts[fnName]);
|
|
64
64
|
}
|
|
65
65
|
if (fnName in builtinFunctions) {
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
endpointContracts.POST ??= {};
|
|
67
|
+
endpointContracts.POST.builtin = true;
|
|
68
68
|
}
|
|
69
|
-
functions[endpoint] =
|
|
69
|
+
functions[endpoint] = endpointContracts;
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
}
|
package/dist/endpoints.mjs
CHANGED
|
@@ -13,7 +13,7 @@ export const getEndpoints = async () => {
|
|
|
13
13
|
const {
|
|
14
14
|
description,
|
|
15
15
|
functions: collectionFunctions,
|
|
16
|
-
|
|
16
|
+
contracts,
|
|
17
17
|
exposedFunctions = {}
|
|
18
18
|
} = collection;
|
|
19
19
|
if (collectionFunctions) {
|
|
@@ -25,22 +25,22 @@ export const getEndpoints = async () => {
|
|
|
25
25
|
const endpoint = `/${description.$id}/${fnName}`;
|
|
26
26
|
const exposed = exposedFunctions[fnName];
|
|
27
27
|
const roles = Array.isArray(exposed) ? exposed : exposed ? await getAvailableRoles() : [];
|
|
28
|
-
const
|
|
28
|
+
const endpointContracts = {
|
|
29
29
|
POST: null
|
|
30
30
|
};
|
|
31
31
|
if (roles.length) {
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
endpointContracts.POST ??= {};
|
|
33
|
+
endpointContracts.POST.roles = roles;
|
|
34
34
|
}
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
Object.assign(
|
|
35
|
+
if (contracts && fnName in contracts) {
|
|
36
|
+
endpointContracts.POST ??= {};
|
|
37
|
+
Object.assign(endpointContracts.POST, contracts[fnName]);
|
|
38
38
|
}
|
|
39
39
|
if (fnName in builtinFunctions) {
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
endpointContracts.POST ??= {};
|
|
41
|
+
endpointContracts.POST.builtin = true;
|
|
42
42
|
}
|
|
43
|
-
functions[endpoint] =
|
|
43
|
+
functions[endpoint] = endpointContracts;
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
}
|
|
@@ -2,4 +2,4 @@ import type { Context, SchemaWithId, InsertPayload } from '@aeriajs/types';
|
|
|
2
2
|
export type InsertOptions = {
|
|
3
3
|
bypassSecurity?: boolean;
|
|
4
4
|
};
|
|
5
|
-
export declare const insert: <TContext extends Context>(payload: InsertPayload<SchemaWithId<TContext['description']>>, context: TContext, options?: InsertOptions) => Promise<import("@aeriajs/types").Left<import("@aeriajs/types").
|
|
5
|
+
export declare const insert: <TContext extends Context>(payload: InsertPayload<SchemaWithId<TContext['description']>>, context: TContext, options?: InsertOptions) => Promise<import("@aeriajs/types").Left<import("@aeriajs/types").ACError | import("@aeriajs/types").ValidationError> | import("@aeriajs/types").Right<SchemaWithId<TContext["description"]>>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.87",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -41,13 +41,13 @@
|
|
|
41
41
|
"mongodb-memory-server": "^9.2.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@aeriajs/builtins": "^0.0.
|
|
45
|
-
"@aeriajs/common": "^0.0.
|
|
46
|
-
"@aeriajs/entrypoint": "^0.0.
|
|
47
|
-
"@aeriajs/http": "^0.0.
|
|
48
|
-
"@aeriajs/security": "^0.0.
|
|
49
|
-
"@aeriajs/types": "^0.0.
|
|
50
|
-
"@aeriajs/validation": "^0.0.
|
|
44
|
+
"@aeriajs/builtins": "^0.0.87",
|
|
45
|
+
"@aeriajs/common": "^0.0.53",
|
|
46
|
+
"@aeriajs/entrypoint": "^0.0.53",
|
|
47
|
+
"@aeriajs/http": "^0.0.61",
|
|
48
|
+
"@aeriajs/security": "^0.0.87",
|
|
49
|
+
"@aeriajs/types": "^0.0.50",
|
|
50
|
+
"@aeriajs/validation": "^0.0.56"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"mongodb": "^6.5.0",
|