@aeriajs/types 0.0.29 → 0.0.30

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,4 @@
1
- import type { Collection } from '.';
1
+ import type { AuthenticatedToken, Collection, Token, UserRole } from '.';
2
2
  export declare enum ACErrors {
3
3
  AssetNotFound = "ASSET_NOT_FOUND",
4
4
  AuthenticationError = "AUTHENTICATION_ERROR",
@@ -18,14 +18,16 @@ export type Role<TCollection extends Collection = any, TAccessControl extends Ac
18
18
  grant?: readonly (keyof TCollection['functions'])[];
19
19
  forbid?: readonly (keyof TCollection['functions'])[];
20
20
  };
21
- export type Roles<TCollection extends Collection = any, TAccessControl extends AccessControl<TCollection> = any> = Record<string, Role<TCollection, TAccessControl>>;
22
- export type InternalAccessControl<TCollection extends Collection = any, TAccessControl extends AccessControl<TCollection, TAccessControl> = any> = {
23
- roles?: Roles<TCollection, TAccessControl>;
21
+ export type AccessControl<TCollection extends Collection = any, TAccessControl extends AccessControl<TCollection, TAccessControl> = any> = {
22
+ roles?: Partial<Record<string, Role<TCollection, TAccessControl>>>;
24
23
  availableRoles?: keyof TAccessControl['roles'];
25
24
  parent?: TAccessControl['roles'];
26
25
  };
27
- export type AccessControl<TCollection extends Collection = any, TAccessControl extends AccessControl<TCollection, TAccessControl> = any> = InternalAccessControl<TCollection, TAccessControl>;
28
- export type ACProfile = {
29
- readonly roles?: string[];
30
- readonly allowed_functions?: string[];
26
+ export type NonCircularAccessControl<TCollection extends Collection = any, TAccessControl extends AccessControl<TCollection, TAccessControl> = any> = {
27
+ roles?: Record<string, Role<TCollection, TAccessControl>>;
28
+ availableRoles?: keyof TAccessControl['roles'];
29
+ parent?: TAccessControl['roles'];
31
30
  };
31
+ export type ExpectToken<TToken extends Token, TRole extends UserRole | UserRole[]> = (TRole extends any[] ? TRole[number] : TRole) extends infer NormalizedRole ? TToken extends AuthenticatedToken ? NormalizedRole extends TToken['roles'][number] ? TToken : Omit<TToken, 'roles'> & {
32
+ roles: readonly NormalizedRole[];
33
+ } : TToken : never;
package/dist/api.d.ts CHANGED
@@ -1,28 +1,25 @@
1
1
  import type { ObjectId } from 'mongodb';
2
- import type { Context, Contract, Description, SecurityPolicy, AccessControl, PackReferences } from '.';
2
+ import type { Context, Contract, Description, CollectionSecurityPolicy, AccessControl, PackReferences } from '.';
3
3
  export type Collection<TCollection extends Collection = any> = {
4
4
  description: Description;
5
5
  item?: any;
6
- security?: SecurityPolicy;
6
+ security?: CollectionSecurityPolicy<TCollection>;
7
7
  accessControl?: AccessControl<TCollection>;
8
8
  functions?: Record<string, (payload: any, context: Context, ...args: any[]) => any>;
9
9
  functionContracts?: Record<string, Contract>;
10
10
  };
11
11
  export type AssetType = keyof Collection;
12
12
  export type FunctionPath = `${string}@${string}`;
13
- export type UserACProfile = {
14
- readonly roles: string[];
15
- readonly allowed_functions?: string[];
16
- };
17
- export type AuthenticatedToken = {
13
+ export type UserRole = Collections['user']['item']['roles'][number];
14
+ export type AuthenticatedToken<TAcceptedRole = string> = {
18
15
  authenticated: true;
19
16
  sub: ObjectId;
20
- roles: string[];
17
+ roles: readonly TAcceptedRole[];
18
+ allowed_functions?: readonly FunctionPath[];
21
19
  userinfo: PackReferences<Collections['user']['item']>;
22
- allowed_functions?: FunctionPath[];
23
20
  };
24
21
  export type UnauthenticatedToken = {
25
22
  authenticated: false;
26
23
  sub: null;
27
24
  };
28
- export type DecodedToken = AuthenticatedToken | UnauthenticatedToken;
25
+ export type Token<TAcceptedRole = string> = AuthenticatedToken<TAcceptedRole> | UnauthenticatedToken;
package/dist/config.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { RouteContext, RouteUri, RateLimitingParams } from '.';
2
2
  export type ApiConfig = {
3
3
  secret?: string;
4
- apiUrl?: string;
5
- apiBase?: RouteUri;
4
+ baseUrl?: RouteUri;
5
+ publicUrl?: string;
6
6
  port?: number;
7
7
  paginationLimit?: number;
8
8
  database?: {
package/dist/context.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Collection as MongoCollection } from 'mongodb';
2
2
  import type { GenericRequest, GenericResponse } from './http';
3
- import type { Either, Description, PackReferences, SchemaWithId, FunctionPath, DecodedToken, ApiConfig, CollectionDocument, CollectionFunctions, RateLimitingParams, RateLimitingErrors } from '.';
3
+ import type { Either, Description, PackReferences, SchemaWithId, FunctionPath, Token, ApiConfig, CollectionDocument, CollectionFunctions, RateLimitingParams, RateLimitingErrors } from '.';
4
4
  export type CollectionModel<TDescription extends Description> = MongoCollection<Omit<PackReferences<SchemaWithId<TDescription>>, '_id'>>;
5
5
  type OmitContextParameter<TFunction> = TFunction extends () => any ? TFunction : TFunction extends (payload: undefined, ...args: any[]) => infer Return ? () => Return : TFunction extends (payload: infer Payload, context: Context, ...args: infer Rest) => infer Return ? (payload: Payload, ...args: Rest) => Return : never;
6
6
  type RestParameters<TFunction> = TFunction extends (payload: any, context: Context, ...args: infer Rest) => any ? Rest : never;
@@ -22,14 +22,14 @@ export type ContextOptions = {
22
22
  config?: ApiConfig;
23
23
  parentContext?: RouteContext | Context;
24
24
  collectionName?: string;
25
- token?: DecodedToken;
25
+ token?: Token;
26
26
  inherited?: boolean;
27
27
  calledFunction?: string;
28
28
  };
29
- export type RouteContext = {
29
+ export type RouteContext<TAcceptedRole = string> = {
30
30
  collections: IndepthCollections;
31
31
  functionPath: FunctionPath;
32
- token: DecodedToken;
32
+ token: Token<TAcceptedRole>;
33
33
  request: GenericRequest;
34
34
  response: GenericResponse;
35
35
  log: (message: string, details?: any) => Promise<any>;
@@ -1,9 +1,9 @@
1
- import type { Property, InferProperty, InferResponse, Context } from '.';
1
+ import type { Property, InferProperty, InferResponse, Context, UserRole } from '.';
2
2
  export type ContractBase = {
3
3
  builtin?: boolean;
4
4
  };
5
5
  export type ContractRoles = {
6
- roles?: (Collections['user']['item']['roles'][number] | 'root' | 'guest')[];
6
+ roles?: (UserRole | 'root' | 'guest')[];
7
7
  };
8
8
  export type Contract = ContractBase & ({
9
9
  response: Property | Property[];
@@ -1,15 +1,27 @@
1
+ import type { Collection } from './api.js';
1
2
  export declare enum RateLimitingErrors {
2
3
  Unauthenticated = "UNAUTHENTICATED",
3
4
  LimitReached = "LIMIT_REACHED"
4
5
  }
6
+ export type DiscriminationStrategy = 'tenant' | 'ip';
7
+ export type RateLimitingWithScale = {
8
+ scale: number;
9
+ };
10
+ export type RateLimitingWithLimit = {
11
+ limit: number;
12
+ };
5
13
  export type RateLimitingParams = {
6
- strategy: 'tenant' | 'ip';
7
- limit?: number;
8
- scale?: number;
14
+ strategy: DiscriminationStrategy;
9
15
  increment?: number;
16
+ } & (RateLimitingWithLimit | RateLimitingWithScale | (RateLimitingWithLimit & RateLimitingWithScale));
17
+ export type LoggingLevel = 'debug' | 'info' | 'error' | 'critical';
18
+ export type LoggingParams = {
19
+ strategy: DiscriminationStrategy;
20
+ level: LoggingLevel;
10
21
  };
11
22
  export type SecurityPolicy = {
12
23
  allowQueryOperators?: string[];
13
- rateLimiting?: Record<string, RateLimitingParams>;
14
- accessControl?: any;
24
+ rateLimiting?: RateLimitingParams;
25
+ logging?: LoggingParams;
15
26
  };
27
+ export type CollectionSecurityPolicy<TCollection extends Collection = any> = Partial<Record<keyof TCollection['functions'], SecurityPolicy>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/types",
3
- "version": "0.0.29",
3
+ "version": "0.0.30",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",