@api-client/core 0.18.57 → 0.18.58
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/build/src/modeling/ApiModel.d.ts +7 -5
- package/build/src/modeling/ApiModel.d.ts.map +1 -1
- package/build/src/modeling/ApiModel.js +35 -16
- package/build/src/modeling/ApiModel.js.map +1 -1
- package/build/src/modeling/ExposedEntity.d.ts +5 -2
- package/build/src/modeling/ExposedEntity.d.ts.map +1 -1
- package/build/src/modeling/ExposedEntity.js +11 -8
- package/build/src/modeling/ExposedEntity.js.map +1 -1
- package/build/src/modeling/actions/Action.d.ts +41 -0
- package/build/src/modeling/actions/Action.d.ts.map +1 -0
- package/build/src/modeling/actions/Action.js +64 -0
- package/build/src/modeling/actions/Action.js.map +1 -0
- package/build/src/modeling/actions/CreateAction.d.ts +18 -0
- package/build/src/modeling/actions/CreateAction.d.ts.map +1 -0
- package/build/src/modeling/actions/CreateAction.js +37 -0
- package/build/src/modeling/actions/CreateAction.js.map +1 -0
- package/build/src/modeling/actions/DeleteAction.d.ts +34 -0
- package/build/src/modeling/actions/DeleteAction.d.ts.map +1 -0
- package/build/src/modeling/actions/DeleteAction.js +57 -0
- package/build/src/modeling/actions/DeleteAction.js.map +1 -0
- package/build/src/modeling/actions/ListAction.d.ts +37 -0
- package/build/src/modeling/actions/ListAction.d.ts.map +1 -0
- package/build/src/modeling/actions/ListAction.js +70 -0
- package/build/src/modeling/actions/ListAction.js.map +1 -0
- package/build/src/modeling/actions/ReadAction.d.ts +18 -0
- package/build/src/modeling/actions/ReadAction.d.ts.map +1 -0
- package/build/src/modeling/actions/ReadAction.js +37 -0
- package/build/src/modeling/actions/ReadAction.js.map +1 -0
- package/build/src/modeling/actions/SearchAction.d.ts +24 -0
- package/build/src/modeling/actions/SearchAction.d.ts.map +1 -0
- package/build/src/modeling/actions/SearchAction.js +47 -0
- package/build/src/modeling/actions/SearchAction.js.map +1 -0
- package/build/src/modeling/actions/UpdateAction.d.ts +27 -0
- package/build/src/modeling/actions/UpdateAction.d.ts.map +1 -0
- package/build/src/modeling/actions/UpdateAction.js +47 -0
- package/build/src/modeling/actions/UpdateAction.js.map +1 -0
- package/build/src/modeling/actions/index.d.ts +24 -0
- package/build/src/modeling/actions/index.d.ts.map +1 -0
- package/build/src/modeling/actions/index.js +9 -0
- package/build/src/modeling/actions/index.js.map +1 -0
- package/build/src/modeling/index.d.ts +12 -0
- package/build/src/modeling/index.d.ts.map +1 -0
- package/build/src/modeling/index.js +12 -0
- package/build/src/modeling/index.js.map +1 -0
- package/build/src/modeling/rules/AccessRule.d.ts +17 -0
- package/build/src/modeling/rules/AccessRule.d.ts.map +1 -0
- package/build/src/modeling/rules/AccessRule.js +19 -0
- package/build/src/modeling/rules/AccessRule.js.map +1 -0
- package/build/src/modeling/rules/AllowAuthenticated.d.ts +19 -0
- package/build/src/modeling/rules/AllowAuthenticated.d.ts.map +1 -0
- package/build/src/modeling/rules/AllowAuthenticated.js +14 -0
- package/build/src/modeling/rules/AllowAuthenticated.js.map +1 -0
- package/build/src/modeling/rules/AllowPublic.d.ts +19 -0
- package/build/src/modeling/rules/AllowPublic.d.ts.map +1 -0
- package/build/src/modeling/rules/AllowPublic.js +14 -0
- package/build/src/modeling/rules/AllowPublic.js.map +1 -0
- package/build/src/modeling/rules/MatchEmailDomain.d.ts +25 -0
- package/build/src/modeling/rules/MatchEmailDomain.d.ts.map +1 -0
- package/build/src/modeling/rules/MatchEmailDomain.js +40 -0
- package/build/src/modeling/rules/MatchEmailDomain.js.map +1 -0
- package/build/src/modeling/rules/MatchResourceOwner.d.ts +29 -0
- package/build/src/modeling/rules/MatchResourceOwner.d.ts.map +1 -0
- package/build/src/modeling/rules/MatchResourceOwner.js +40 -0
- package/build/src/modeling/rules/MatchResourceOwner.js.map +1 -0
- package/build/src/modeling/rules/MatchUserProperty.d.ts +28 -0
- package/build/src/modeling/rules/MatchUserProperty.d.ts.map +1 -0
- package/build/src/modeling/rules/MatchUserProperty.js +49 -0
- package/build/src/modeling/rules/MatchUserProperty.js.map +1 -0
- package/build/src/modeling/rules/MatchUserRole.d.ts +29 -0
- package/build/src/modeling/rules/MatchUserRole.d.ts.map +1 -0
- package/build/src/modeling/rules/MatchUserRole.js +40 -0
- package/build/src/modeling/rules/MatchUserRole.js.map +1 -0
- package/build/src/modeling/rules/RateLimitRule.d.ts +61 -0
- package/build/src/modeling/rules/RateLimitRule.d.ts.map +1 -0
- package/build/src/modeling/rules/RateLimitRule.js +101 -0
- package/build/src/modeling/rules/RateLimitRule.js.map +1 -0
- package/build/src/modeling/rules/RateLimitingConfiguration.d.ts +18 -0
- package/build/src/modeling/rules/RateLimitingConfiguration.d.ts.map +1 -0
- package/build/src/modeling/rules/RateLimitingConfiguration.js +35 -0
- package/build/src/modeling/rules/RateLimitingConfiguration.js.map +1 -0
- package/build/src/modeling/rules/index.d.ts +13 -0
- package/build/src/modeling/rules/index.d.ts.map +1 -0
- package/build/src/modeling/rules/index.js +10 -0
- package/build/src/modeling/rules/index.js.map +1 -0
- package/build/src/modeling/types.d.ts +6 -257
- package/build/src/modeling/types.d.ts.map +1 -1
- package/build/src/modeling/types.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/data/models/example-generator-api.json +26 -26
- package/package.json +1 -1
- package/src/modeling/ApiModel.ts +21 -19
- package/src/modeling/ExposedEntity.ts +13 -18
- package/src/modeling/actions/Action.ts +64 -0
- package/src/modeling/actions/CreateAction.ts +30 -0
- package/src/modeling/actions/DeleteAction.ts +51 -0
- package/src/modeling/actions/ListAction.ts +58 -0
- package/src/modeling/actions/ReadAction.ts +32 -0
- package/src/modeling/actions/SearchAction.ts +38 -0
- package/src/modeling/actions/UpdateAction.ts +41 -0
- package/src/modeling/rules/AccessRule.ts +29 -0
- package/src/modeling/rules/AllowAuthenticated.ts +24 -0
- package/src/modeling/rules/AllowPublic.ts +24 -0
- package/src/modeling/rules/MatchEmailDomain.ts +39 -0
- package/src/modeling/rules/MatchResourceOwner.ts +43 -0
- package/src/modeling/rules/MatchUserProperty.ts +44 -0
- package/src/modeling/rules/MatchUserRole.ts +43 -0
- package/src/modeling/rules/RateLimitRule.ts +104 -0
- package/src/modeling/rules/RateLimitingConfiguration.ts +32 -0
- package/src/modeling/types.ts +6 -276
- package/tests/unit/modeling/actions/Action.spec.ts +109 -0
- package/tests/unit/modeling/actions/CreateAction.spec.ts +65 -0
- package/tests/unit/modeling/actions/DeleteAction.spec.ts +78 -0
- package/tests/unit/modeling/actions/ListAction.spec.ts +106 -0
- package/tests/unit/modeling/actions/ReadAction.spec.ts +77 -0
- package/tests/unit/modeling/actions/SearchAction.spec.ts +73 -0
- package/tests/unit/modeling/actions/UpdateAction.spec.ts +73 -0
- package/tests/unit/modeling/api_model.spec.ts +48 -3
- package/tests/unit/modeling/exposed_entity.spec.ts +73 -0
- package/tests/unit/modeling/rules/AccessRule.spec.ts +42 -0
- package/tests/unit/modeling/rules/AllowAuthenticated.spec.ts +28 -0
- package/tests/unit/modeling/rules/AllowPublic.spec.ts +28 -0
- package/tests/unit/modeling/rules/MatchEmailDomain.spec.ts +52 -0
- package/tests/unit/modeling/rules/MatchResourceOwner.spec.ts +37 -0
- package/tests/unit/modeling/rules/MatchUserProperty.spec.ts +58 -0
- package/tests/unit/modeling/rules/MatchUserRole.spec.ts +52 -0
- package/tests/unit/modeling/rules/RateLimitRule.spec.ts +70 -0
- package/tests/unit/modeling/rules/RateLimitingConfiguration.spec.ts +61 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Action, type ActionSchema } from './Action.js'
|
|
2
|
+
import { observed, toRaw } from '../../decorators/observed.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Enables modifying an existing resource.
|
|
6
|
+
* Endpoints: PUT or PATCH /[entity-collection-name]/{id}
|
|
7
|
+
*/
|
|
8
|
+
export interface UpdateActionSchema extends ActionSchema {
|
|
9
|
+
kind: 'update'
|
|
10
|
+
/**
|
|
11
|
+
* The allowed HTTP methods for updates. Default: PATCH only.
|
|
12
|
+
*
|
|
13
|
+
* These two methods represent the two common ways to update a resource:
|
|
14
|
+
* - PUT: Replaces the entire resource with the provided data.
|
|
15
|
+
* - PATCH: Applies a partial update to the resource, allowing for specific fields to be modified.
|
|
16
|
+
*/
|
|
17
|
+
allowedMethods: ('PUT' | 'PATCH')[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Enables modifying an existing resource.
|
|
22
|
+
* Endpoints: PUT or PATCH /[entity-collection-name]/{id}
|
|
23
|
+
*/
|
|
24
|
+
export class UpdateAction extends Action implements UpdateActionSchema {
|
|
25
|
+
@observed() override accessor kind: 'update'
|
|
26
|
+
@observed({ deep: true }) accessor allowedMethods: ('PUT' | 'PATCH')[]
|
|
27
|
+
|
|
28
|
+
constructor(state: Partial<UpdateActionSchema> = {}) {
|
|
29
|
+
super(state)
|
|
30
|
+
this.kind = 'update'
|
|
31
|
+
this.allowedMethods = state.allowedMethods ? [...state.allowedMethods] : ['PATCH']
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
override toJSON(): UpdateActionSchema {
|
|
35
|
+
return {
|
|
36
|
+
...(super.toJSON() as UpdateActionSchema),
|
|
37
|
+
kind: 'update',
|
|
38
|
+
allowedMethods: structuredClone(toRaw(this, this.allowedMethods)) as ('PUT' | 'PATCH')[],
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface AccessRuleSchema {
|
|
2
|
+
/**
|
|
3
|
+
* The unique identifier for the access rule.
|
|
4
|
+
* This is used to reference the rule in the API configuration.
|
|
5
|
+
*/
|
|
6
|
+
type: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base class for all access rules.
|
|
11
|
+
*/
|
|
12
|
+
export class AccessRule extends EventTarget implements AccessRuleSchema {
|
|
13
|
+
readonly type: string
|
|
14
|
+
|
|
15
|
+
constructor(state: Partial<AccessRuleSchema> = {}) {
|
|
16
|
+
super()
|
|
17
|
+
this.type = state.type ?? ''
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
toJSON(): AccessRuleSchema {
|
|
21
|
+
return {
|
|
22
|
+
type: this.type,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
notifyChange() {
|
|
27
|
+
this.dispatchEvent(new Event('change'))
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AccessRule, type AccessRuleSchema } from './AccessRule.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The action is allowed for any authenticated user.
|
|
5
|
+
* This rule does not impose any additional restrictions based on user properties or resource ownership.
|
|
6
|
+
* It is used for resources that should be accessible to all logged-in users.
|
|
7
|
+
*/
|
|
8
|
+
export interface AllowAuthenticatedAccessRuleSchema extends AccessRuleSchema {
|
|
9
|
+
type: 'authenticated'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The action is allowed for any authenticated user.
|
|
14
|
+
* This rule does not impose any additional restrictions based on user properties or resource ownership.
|
|
15
|
+
* It is used for resources that should be accessible to all logged-in users.
|
|
16
|
+
*/
|
|
17
|
+
export class AllowAuthenticatedAccessRule extends AccessRule implements AllowAuthenticatedAccessRuleSchema {
|
|
18
|
+
override readonly type: 'authenticated'
|
|
19
|
+
|
|
20
|
+
constructor(state: Partial<AllowAuthenticatedAccessRuleSchema> = {}) {
|
|
21
|
+
super(state)
|
|
22
|
+
this.type = 'authenticated'
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AccessRule, type AccessRuleSchema } from './AccessRule.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The action is allowed for all users, including unauthenticated ones.
|
|
5
|
+
* This is typically used for public APIs or resources that do not require authentication.
|
|
6
|
+
* It is the most permissive rule and should be used with caution.
|
|
7
|
+
*/
|
|
8
|
+
export interface AllowPublicAccessRuleSchema extends AccessRuleSchema {
|
|
9
|
+
type: 'public'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The action is allowed for all users, including unauthenticated ones.
|
|
14
|
+
* This is typically used for public APIs or resources that do not require authentication.
|
|
15
|
+
* It is the most permissive rule and should be used with caution.
|
|
16
|
+
*/
|
|
17
|
+
export class AllowPublicAccessRule extends AccessRule implements AllowPublicAccessRuleSchema {
|
|
18
|
+
override readonly type: 'public'
|
|
19
|
+
|
|
20
|
+
constructor(state: Partial<AllowPublicAccessRuleSchema> = {}) {
|
|
21
|
+
super(state)
|
|
22
|
+
this.type = 'public'
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AccessRule, type AccessRuleSchema } from './AccessRule.js'
|
|
2
|
+
import { observed, toRaw } from '../../decorators/observed.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The action is allowed if the authenticated user's email domain matches a specific domain.
|
|
6
|
+
* This is used to restrict access based on the user's email address.
|
|
7
|
+
* For example, only users with an email address from "my-company.com" can access certain resources.
|
|
8
|
+
*/
|
|
9
|
+
export interface MatchEmailDomainAccessRuleSchema extends AccessRuleSchema {
|
|
10
|
+
type: 'matchEmailDomain'
|
|
11
|
+
/**
|
|
12
|
+
* The email domains that the authenticated user's email must match.
|
|
13
|
+
*/
|
|
14
|
+
domains: string[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The action is allowed if the authenticated user's email domain matches a specific domain.
|
|
19
|
+
* This is used to restrict access based on the user's email address.
|
|
20
|
+
* For example, only users with an email address from "my-company.com" can access certain resources.
|
|
21
|
+
*/
|
|
22
|
+
export class MatchEmailDomainAccessRule extends AccessRule implements MatchEmailDomainAccessRuleSchema {
|
|
23
|
+
override readonly type: 'matchEmailDomain'
|
|
24
|
+
|
|
25
|
+
@observed({ deep: true }) accessor domains: string[]
|
|
26
|
+
|
|
27
|
+
constructor(state: Partial<MatchEmailDomainAccessRuleSchema> = {}) {
|
|
28
|
+
super(state)
|
|
29
|
+
this.type = 'matchEmailDomain'
|
|
30
|
+
this.domains = state.domains ? [...state.domains] : []
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
override toJSON(): MatchEmailDomainAccessRuleSchema {
|
|
34
|
+
return {
|
|
35
|
+
...(super.toJSON() as MatchEmailDomainAccessRuleSchema),
|
|
36
|
+
domains: structuredClone(toRaw(this, this.domains)) as string[],
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { AccessRule, type AccessRuleSchema } from './AccessRule.js'
|
|
2
|
+
import { observed } from '../../decorators/observed.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The action is allowed if the authenticated user's ID matches a specific property on the resource.
|
|
6
|
+
* This is typically used to restrict access to resources owned by the user.
|
|
7
|
+
* For example, a user can only access their own profile or documents.
|
|
8
|
+
*/
|
|
9
|
+
export interface MatchResourceOwnerAccessRuleSchema extends AccessRuleSchema {
|
|
10
|
+
type: 'resourceOwner'
|
|
11
|
+
/**
|
|
12
|
+
* The property on the resource that should match the authenticated user's ID.
|
|
13
|
+
* This is typically the ID of the user who owns the resource.
|
|
14
|
+
*
|
|
15
|
+
* The domain model should annotate this property with the "ResourceOwnerIdentifier" semantic
|
|
16
|
+
* to indicate that it is used for ownership checks.
|
|
17
|
+
*/
|
|
18
|
+
property: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The action is allowed if the authenticated user's ID matches a specific property on the resource.
|
|
23
|
+
* This is typically used to restrict access to resources owned by the user.
|
|
24
|
+
* For example, a user can only access their own profile or documents.
|
|
25
|
+
*/
|
|
26
|
+
export class MatchResourceOwnerAccessRule extends AccessRule implements MatchResourceOwnerAccessRuleSchema {
|
|
27
|
+
override readonly type: 'resourceOwner'
|
|
28
|
+
|
|
29
|
+
@observed() accessor property: string
|
|
30
|
+
|
|
31
|
+
constructor(state: Partial<MatchResourceOwnerAccessRuleSchema> = {}) {
|
|
32
|
+
super(state)
|
|
33
|
+
this.type = 'resourceOwner'
|
|
34
|
+
this.property = state.property ?? ''
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
override toJSON(): MatchResourceOwnerAccessRuleSchema {
|
|
38
|
+
return {
|
|
39
|
+
...(super.toJSON() as MatchResourceOwnerAccessRuleSchema),
|
|
40
|
+
property: this.property,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { AccessRule, type AccessRuleSchema } from './AccessRule.js'
|
|
2
|
+
import { observed } from '../../decorators/observed.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The action is allowed if a specific property on the authenticated user matches an expected value.
|
|
6
|
+
* This is used to enforce other user-specific restrictions.
|
|
7
|
+
*/
|
|
8
|
+
export interface MatchUserPropertyAccessRuleSchema extends AccessRuleSchema {
|
|
9
|
+
type: 'matchUserProperty'
|
|
10
|
+
/**
|
|
11
|
+
* The property on the authenticated user that should match the expected value.
|
|
12
|
+
*/
|
|
13
|
+
property: string
|
|
14
|
+
/**
|
|
15
|
+
* The expected value for the user property.
|
|
16
|
+
*/
|
|
17
|
+
value: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The action is allowed if a specific property on the authenticated user matches an expected value.
|
|
22
|
+
* This is used to enforce other user-specific restrictions.
|
|
23
|
+
*/
|
|
24
|
+
export class MatchUserPropertyAccessRule extends AccessRule implements MatchUserPropertyAccessRuleSchema {
|
|
25
|
+
override readonly type: 'matchUserProperty'
|
|
26
|
+
|
|
27
|
+
@observed() accessor property: string
|
|
28
|
+
@observed() accessor value: string
|
|
29
|
+
|
|
30
|
+
constructor(state: Partial<MatchUserPropertyAccessRuleSchema> = {}) {
|
|
31
|
+
super(state)
|
|
32
|
+
this.type = 'matchUserProperty'
|
|
33
|
+
this.property = state.property ?? ''
|
|
34
|
+
this.value = state.value ?? ''
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
override toJSON(): MatchUserPropertyAccessRuleSchema {
|
|
38
|
+
return {
|
|
39
|
+
...(super.toJSON() as MatchUserPropertyAccessRuleSchema),
|
|
40
|
+
property: this.property,
|
|
41
|
+
value: this.value,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { AccessRule, type AccessRuleSchema } from './AccessRule.js'
|
|
2
|
+
import { observed, toRaw } from '../../decorators/observed.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The action is allowed if the authenticated user has a specific role.
|
|
6
|
+
* This is used to enforce role-based access control (RBAC).
|
|
7
|
+
* For example, only users with the "admin" role can perform certain actions.
|
|
8
|
+
*/
|
|
9
|
+
export interface MatchUserRoleAccessRuleSchema extends AccessRuleSchema {
|
|
10
|
+
type: 'matchUserRole'
|
|
11
|
+
/**
|
|
12
|
+
* The role that the authenticated user must have to access the resource.
|
|
13
|
+
* This is typically a property on the user entity that defines their role.
|
|
14
|
+
*
|
|
15
|
+
* The domain model should annotate this property with the "UserRole" semantic
|
|
16
|
+
* to indicate that it is used for role-based access control.
|
|
17
|
+
*/
|
|
18
|
+
role: string[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The action is allowed if the authenticated user has a specific role.
|
|
23
|
+
* This is used to enforce role-based access control (RBAC).
|
|
24
|
+
* For example, only users with the "admin" role can perform certain actions.
|
|
25
|
+
*/
|
|
26
|
+
export class MatchUserRoleAccessRule extends AccessRule implements MatchUserRoleAccessRuleSchema {
|
|
27
|
+
override readonly type: 'matchUserRole'
|
|
28
|
+
|
|
29
|
+
@observed({ deep: true }) accessor role: string[]
|
|
30
|
+
|
|
31
|
+
constructor(state: Partial<MatchUserRoleAccessRuleSchema> = {}) {
|
|
32
|
+
super(state)
|
|
33
|
+
this.type = 'matchUserRole'
|
|
34
|
+
this.role = state.role ? [...state.role] : []
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
override toJSON(): MatchUserRoleAccessRuleSchema {
|
|
38
|
+
return {
|
|
39
|
+
...(super.toJSON() as MatchUserRoleAccessRuleSchema),
|
|
40
|
+
role: structuredClone(toRaw(this, this.role)) as string[],
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { observed, toRaw } from '../../decorators/observed.js'
|
|
2
|
+
|
|
3
|
+
export type RateLimitRuleKey = { type: 'ip' } | { type: 'userId' } | { type: 'role'; value: string }
|
|
4
|
+
export type RateLimitRuleInterval = 'second' | 'minute' | 'hour' | 'day'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Represents a single rate limiting rule that applies to a specific
|
|
8
|
+
* type of client, using a token bucket algorithm.
|
|
9
|
+
*/
|
|
10
|
+
export interface RateLimitRuleSchema {
|
|
11
|
+
/**
|
|
12
|
+
* A human-readable description of what this rule is for.
|
|
13
|
+
* e.g., "Limit anonymous users to 60 requests per hour."
|
|
14
|
+
*/
|
|
15
|
+
description?: string
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Defines how to group requests for rate limiting. This determines
|
|
19
|
+
* who the limit applies to.
|
|
20
|
+
*
|
|
21
|
+
* - 'ip': Keys on the client's IP address. Best for anonymous traffic.
|
|
22
|
+
* - 'userId': Keys on the authenticated user's ID. Best for logged-in users.
|
|
23
|
+
* - 'role': Applies a shared limit to all users of a specific role.
|
|
24
|
+
*/
|
|
25
|
+
key?: RateLimitRuleKey
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The number of requests allowed over the defined interval.
|
|
29
|
+
* This is the rate at which tokens are added to the bucket.
|
|
30
|
+
*/
|
|
31
|
+
rate?: number
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The time interval for the rate.
|
|
35
|
+
*/
|
|
36
|
+
interval?: RateLimitRuleInterval
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The maximum number of requests that can be made in a burst.
|
|
40
|
+
* This represents the "bucket size." A larger burst allows for
|
|
41
|
+
* more requests to be made in a short period before throttling begins.
|
|
42
|
+
*/
|
|
43
|
+
burst?: number
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Represents a single rate limiting rule that applies to a specific
|
|
48
|
+
* type of client, using a token bucket algorithm.
|
|
49
|
+
*/
|
|
50
|
+
export class RateLimitRule extends EventTarget implements RateLimitRuleSchema {
|
|
51
|
+
@observed() accessor description: string | undefined
|
|
52
|
+
@observed({ deep: true }) accessor key: RateLimitRuleKey | undefined
|
|
53
|
+
@observed() accessor rate: number | undefined
|
|
54
|
+
@observed() accessor interval: RateLimitRuleInterval | undefined
|
|
55
|
+
@observed() accessor burst: number | undefined
|
|
56
|
+
|
|
57
|
+
constructor(state: Partial<RateLimitRuleSchema> = {}) {
|
|
58
|
+
super()
|
|
59
|
+
this.description = state.description
|
|
60
|
+
this.key = RateLimitRule.isRateLimitRuleKey(state.key) ? structuredClone(state.key) : undefined
|
|
61
|
+
this.rate = typeof state.rate === 'number' ? state.rate : undefined
|
|
62
|
+
this.interval = RateLimitRule.isRateLimitRuleInterval(state.interval) ? state.interval : undefined
|
|
63
|
+
this.burst = typeof state.burst === 'number' ? state.burst : undefined
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
toJSON(): RateLimitRuleSchema {
|
|
67
|
+
const result: RateLimitRuleSchema = {}
|
|
68
|
+
if (this.description) {
|
|
69
|
+
result.description = this.description
|
|
70
|
+
}
|
|
71
|
+
if (RateLimitRule.isRateLimitRuleKey(this.key)) {
|
|
72
|
+
result.key = structuredClone(toRaw(this, this.key))
|
|
73
|
+
}
|
|
74
|
+
if (typeof this.rate === 'number') {
|
|
75
|
+
result.rate = this.rate
|
|
76
|
+
}
|
|
77
|
+
if (RateLimitRule.isRateLimitRuleInterval(this.interval)) {
|
|
78
|
+
result.interval = this.interval
|
|
79
|
+
}
|
|
80
|
+
if (typeof this.burst === 'number') {
|
|
81
|
+
result.burst = this.burst
|
|
82
|
+
}
|
|
83
|
+
return result
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
notifyChange() {
|
|
87
|
+
this.dispatchEvent(new Event('change'))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
static isRateLimitRuleInterval(value: unknown): value is RateLimitRuleInterval {
|
|
91
|
+
if (typeof value !== 'string') {
|
|
92
|
+
return false
|
|
93
|
+
}
|
|
94
|
+
return ['second', 'minute', 'hour', 'day'].includes(value)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
static isRateLimitRuleKey(value: unknown): value is RateLimitRuleKey {
|
|
98
|
+
if (typeof value !== 'object' || value === null) {
|
|
99
|
+
return false
|
|
100
|
+
}
|
|
101
|
+
const key = value as Record<string, unknown>
|
|
102
|
+
return 'type' in key && (key.type === 'ip' || key.type === 'userId' || key.type === 'role')
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { RateLimitRule, type RateLimitRuleSchema } from './RateLimitRule.js'
|
|
2
|
+
import { observed } from '../../decorators/observed.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Defines the rate limiting and throttling policies for the entire API.
|
|
6
|
+
*/
|
|
7
|
+
export interface RateLimitingConfigurationSchema {
|
|
8
|
+
/**
|
|
9
|
+
* An ordered list of rules. The first rule that matches an incoming
|
|
10
|
+
* request will be applied.
|
|
11
|
+
*/
|
|
12
|
+
rules: RateLimitRuleSchema[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class RateLimitingConfiguration extends EventTarget implements RateLimitingConfigurationSchema {
|
|
16
|
+
@observed({ deep: true }) accessor rules: RateLimitRule[]
|
|
17
|
+
|
|
18
|
+
constructor(state: Partial<RateLimitingConfigurationSchema> = {}) {
|
|
19
|
+
super()
|
|
20
|
+
this.rules = state.rules?.map((rule) => new RateLimitRule(rule)) || []
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
toJSON(): RateLimitingConfigurationSchema {
|
|
24
|
+
return {
|
|
25
|
+
rules: this.rules.map((rule) => rule.toJSON()),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
notifyChange() {
|
|
30
|
+
this.dispatchEvent(new Event('change'))
|
|
31
|
+
}
|
|
32
|
+
}
|