mongodb-dynamic-api 2.3.0 → 2.3.2
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/CHANGELOG.md +4 -0
- package/package.json +2 -3
- package/src/dynamic-api.module.d.ts +0 -1
- package/src/dynamic-api.module.js +9 -31
- package/src/guards/base-policies.guard.d.ts +28 -3
- package/src/guards/base-policies.guard.js +8 -7
- package/src/interfaces/dynamic-api-ability.interface.d.ts +2 -2
- package/src/interfaces/dynamic-api-global-state.interface.d.ts +1 -0
- package/src/interfaces/dynamic-api-policy-handler.interface.d.ts +27 -3
- package/src/interfaces/dynamic-api-route-response.type.d.ts +7 -0
- package/src/interfaces/dynamic-api-route-response.type.js +2 -0
- package/src/interfaces/dynamic-api-service-callback.interface.d.ts +38 -5
- package/src/interfaces/index.d.ts +1 -0
- package/src/interfaces/index.js +1 -0
- package/src/mixins/create-policies-guard.mixin.d.ts +1 -2
- package/src/mixins/create-policies-guard.mixin.js +8 -5
- package/src/modules/auth/auth.helper.d.ts +3 -3
- package/src/modules/auth/auth.helper.js +3 -3
- package/src/modules/auth/auth.module.d.ts +2 -1
- package/src/modules/auth/auth.module.js +32 -5
- package/src/modules/auth/interfaces/auth-options.interface.d.ts +11 -12
- package/src/modules/auth/mixins/auth-register-policies-guard.mixin.d.ts +2 -2
- package/src/modules/auth/services/base-auth.service.js +8 -8
- package/src/routes/delete-many/delete-many-controller.interface.d.ts +2 -2
- package/src/routes/delete-many/delete-many-service.interface.d.ts +2 -2
- package/src/routes/delete-one/delete-one-controller.interface.d.ts +3 -2
- package/src/routes/delete-one/delete-one-service.interface.d.ts +3 -5
- package/src/services/base/base.service.d.ts +14 -6
- package/src/services/base/base.service.js +58 -28
- package/src/services/dynamic-api-global-state/dynamic-api-global-state.service.d.ts +6 -4
- package/src/services/dynamic-api-global-state/dynamic-api-global-state.service.js +18 -12
- package/src/version.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
Changelog
|
|
2
2
|
|
|
3
|
+
## [2.3.2](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v2.3.1...v2.3.2) (2024-05-07)
|
|
4
|
+
|
|
5
|
+
## [2.3.1](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v2.3.0...v2.3.1) (2024-05-05)
|
|
6
|
+
|
|
3
7
|
## [2.3.0](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v2.2.1...v2.3.0) (2024-05-02)
|
|
4
8
|
|
|
5
9
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongodb-dynamic-api",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.2",
|
|
4
4
|
"description": "Auto generated CRUD API for MongoDB using NestJS",
|
|
5
5
|
"readmeFilename": "README.md",
|
|
6
6
|
"main": "index.js",
|
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
"jest-mock-extended": "^3.0.5",
|
|
87
87
|
"jest-sonar-reporter": "^2.0.0",
|
|
88
88
|
"prettier": "^3.2.5",
|
|
89
|
-
"release-it": "^17.
|
|
89
|
+
"release-it": "^17.2.1",
|
|
90
90
|
"source-map-support": "^0.5.21",
|
|
91
91
|
"supertest": "^6.3.4",
|
|
92
92
|
"ts-jest": "^29.1.2",
|
|
@@ -97,7 +97,6 @@
|
|
|
97
97
|
},
|
|
98
98
|
"jest": {
|
|
99
99
|
"clearMocks": true,
|
|
100
|
-
"resetMocks": true,
|
|
101
100
|
"moduleFileExtensions": [
|
|
102
101
|
"js",
|
|
103
102
|
"json",
|
|
@@ -7,6 +7,5 @@ export declare class DynamicApiModule {
|
|
|
7
7
|
static forRoot<Entity extends BaseEntity = any>(uri: string, { useGlobalCache, cacheOptions, useAuth, routesConfig, }?: DynamicApiForRootOptions<Entity>): DynamicModule;
|
|
8
8
|
static forFeature<Entity extends BaseEntity>({ entity, controllerOptions, routes, }: DynamicApiForFeatureOptions<Entity>): Promise<DynamicModule>;
|
|
9
9
|
private static buildStateFromOptions;
|
|
10
|
-
private static initializeAuthOptions;
|
|
11
10
|
private static setDefaultRoutes;
|
|
12
11
|
}
|
|
@@ -28,7 +28,7 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
28
28
|
}
|
|
29
29
|
this.state.set([
|
|
30
30
|
'partial',
|
|
31
|
-
this.buildStateFromOptions(useGlobalCache, cacheOptions, useAuth, routesConfig),
|
|
31
|
+
this.buildStateFromOptions(uri, useGlobalCache, cacheOptions, useAuth, routesConfig),
|
|
32
32
|
]);
|
|
33
33
|
return {
|
|
34
34
|
module: DynamicApiModule_1,
|
|
@@ -36,8 +36,8 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
36
36
|
modules_1.DynamicApiConfigModule.register(this.state.get()),
|
|
37
37
|
cache_manager_1.CacheModule.register({ isGlobal: true, ...cacheOptions }),
|
|
38
38
|
mongoose_1.MongooseModule.forRoot(uri, { connectionName: this.state.get('connectionName') }),
|
|
39
|
-
...(useAuth?.
|
|
40
|
-
modules_1.AuthModule.forRoot(
|
|
39
|
+
...(useAuth?.userEntity ? [
|
|
40
|
+
modules_1.AuthModule.forRoot(useAuth),
|
|
41
41
|
] : []),
|
|
42
42
|
],
|
|
43
43
|
exports: [modules_1.DynamicApiConfigModule],
|
|
@@ -46,7 +46,7 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
46
46
|
static forFeature({ entity, controllerOptions, routes = [], }) {
|
|
47
47
|
const schema = (0, helpers_1.buildSchemaFromEntity)(entity);
|
|
48
48
|
const databaseModule = mongoose_1.MongooseModule.forFeature([{ name: entity.name, schema }], this.state.get('connectionName'));
|
|
49
|
-
services_1.DynamicApiGlobalStateService.addEntitySchema(entity
|
|
49
|
+
services_1.DynamicApiGlobalStateService.addEntitySchema(entity, schema);
|
|
50
50
|
return new Promise((resolve, reject) => {
|
|
51
51
|
const waitInitializedStateInterval = setInterval(async () => {
|
|
52
52
|
const stateInitialized = await (0, rxjs_1.firstValueFrom)(services_1.DynamicApiGlobalStateService.onInitialized());
|
|
@@ -116,17 +116,18 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
116
116
|
}, 5000);
|
|
117
117
|
});
|
|
118
118
|
}
|
|
119
|
-
static buildStateFromOptions(useGlobalCache, cacheOptions, useAuth, routesConfig) {
|
|
119
|
+
static buildStateFromOptions(uri, useGlobalCache, cacheOptions, useAuth, routesConfig) {
|
|
120
120
|
const routesConfigState = this.state.get('routesConfig');
|
|
121
121
|
return {
|
|
122
|
+
uri,
|
|
122
123
|
initialized: true,
|
|
123
124
|
isGlobalCacheEnabled: useGlobalCache,
|
|
124
125
|
...(cacheOptions?.excludePaths ? { cacheExcludedPaths: cacheOptions?.excludePaths } : {}),
|
|
125
|
-
...(useAuth?.
|
|
126
|
+
...(useAuth?.userEntity ? {
|
|
126
127
|
isAuthEnabled: true,
|
|
127
128
|
credentials: {
|
|
128
|
-
loginField: !useAuth.
|
|
129
|
-
passwordField: !useAuth.
|
|
129
|
+
loginField: !useAuth.login?.loginField ? 'email' : String(useAuth.login.loginField),
|
|
130
|
+
passwordField: !useAuth.login?.passwordField ? 'password' : String(useAuth.login.passwordField),
|
|
130
131
|
},
|
|
131
132
|
jwtSecret: useAuth.jwt?.secret ?? 'dynamic-api-jwt-secret',
|
|
132
133
|
} : {}),
|
|
@@ -138,29 +139,6 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
138
139
|
} : {}),
|
|
139
140
|
};
|
|
140
141
|
}
|
|
141
|
-
static initializeAuthOptions(useAuth) {
|
|
142
|
-
return {
|
|
143
|
-
user: {
|
|
144
|
-
entity: useAuth.user.entity,
|
|
145
|
-
loginField: useAuth.user.loginField ?? 'email',
|
|
146
|
-
passwordField: useAuth.user.passwordField ?? 'password',
|
|
147
|
-
requestAdditionalFields: useAuth.user.requestAdditionalFields ?? [],
|
|
148
|
-
},
|
|
149
|
-
jwt: {
|
|
150
|
-
secret: useAuth.jwt?.secret ?? 'dynamic-api-jwt-secret',
|
|
151
|
-
expiresIn: useAuth.jwt?.expiresIn ?? '1d',
|
|
152
|
-
},
|
|
153
|
-
register: useAuth.register ? {
|
|
154
|
-
...useAuth.register,
|
|
155
|
-
protected: useAuth.register.protected ?? !!useAuth.register.abilityPredicate,
|
|
156
|
-
} : {
|
|
157
|
-
protected: false,
|
|
158
|
-
additionalFields: [],
|
|
159
|
-
},
|
|
160
|
-
login: useAuth.login ?? {},
|
|
161
|
-
resetPassword: useAuth.resetPassword ?? {},
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
142
|
static setDefaultRoutes(stateRoutesConfig, controllerRoutesConfig = {}, routes = []) {
|
|
165
143
|
const defaults = controllerRoutesConfig.defaults ?? stateRoutesConfig.defaults;
|
|
166
144
|
const excluded = controllerRoutesConfig.excluded ?? stateRoutesConfig.excluded;
|
|
@@ -1,12 +1,37 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
1
25
|
import { CanActivate, ExecutionContext, Type } from '@nestjs/common';
|
|
26
|
+
import { Model } from 'mongoose';
|
|
2
27
|
import { AbilityPredicate, RouteType } from '../interfaces';
|
|
3
28
|
import { BaseEntity } from '../models';
|
|
4
29
|
import { BaseService } from '../services';
|
|
5
|
-
export declare abstract class BasePoliciesGuard<Entity extends BaseEntity
|
|
6
|
-
protected readonly
|
|
30
|
+
export declare abstract class BasePoliciesGuard<Entity extends BaseEntity> extends BaseService<Entity> implements CanActivate {
|
|
31
|
+
protected readonly model: Model<Entity>;
|
|
7
32
|
protected routeType: RouteType;
|
|
8
33
|
protected entity: Type<Entity>;
|
|
9
34
|
protected abilityPredicate: AbilityPredicate<Entity> | undefined;
|
|
10
|
-
protected constructor(
|
|
35
|
+
protected constructor(model: Model<Entity>);
|
|
11
36
|
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
12
37
|
}
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BasePoliciesGuard = void 0;
|
|
4
4
|
const common_1 = require("@nestjs/common");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
const services_1 = require("../services");
|
|
6
|
+
class BasePoliciesGuard extends services_1.BaseService {
|
|
7
|
+
constructor(model) {
|
|
8
|
+
super(model);
|
|
9
|
+
this.model = model;
|
|
8
10
|
}
|
|
9
11
|
async canActivate(context) {
|
|
10
12
|
let { user, query, params } = context.switchToHttp().getRequest();
|
|
@@ -12,13 +14,12 @@ class BasePoliciesGuard {
|
|
|
12
14
|
if (!user) {
|
|
13
15
|
throw new common_1.ForbiddenException('Access Denied');
|
|
14
16
|
}
|
|
15
|
-
this.
|
|
16
|
-
this.service.user = user;
|
|
17
|
+
this.user = user;
|
|
17
18
|
if (params?.id) {
|
|
18
|
-
await this.
|
|
19
|
+
await this.findOneDocumentWithAbilityPredicate(params.id, query);
|
|
19
20
|
}
|
|
20
21
|
else {
|
|
21
|
-
await this.
|
|
22
|
+
await this.findManyDocumentsWithAbilityPredicate(query);
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
return true;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BaseEntity } from '../models';
|
|
2
2
|
import { RouteType } from './dynamic-api-route-type.type';
|
|
3
3
|
type AbilityPredicate<Entity extends BaseEntity, User = any> = (entity: Entity, user: User) => boolean;
|
|
4
|
-
type
|
|
4
|
+
type AuthAbilityPredicate<User = any> = (user: User) => boolean;
|
|
5
5
|
type ControllerAbilityPredicate<Entity extends BaseEntity> = {
|
|
6
6
|
targets: RouteType[];
|
|
7
7
|
predicate: AbilityPredicate<Entity>;
|
|
8
8
|
};
|
|
9
|
-
export { ControllerAbilityPredicate,
|
|
9
|
+
export { ControllerAbilityPredicate, AuthAbilityPredicate, AbilityPredicate, };
|
|
@@ -1,8 +1,32 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
1
25
|
import { ExecutionContext } from '@nestjs/common';
|
|
26
|
+
import { Model } from 'mongoose';
|
|
2
27
|
import { BaseEntity } from '../models';
|
|
3
|
-
|
|
4
|
-
interface PoliciesGuard<Entity extends BaseEntity> {
|
|
28
|
+
interface PoliciesGuard {
|
|
5
29
|
canActivate(context: ExecutionContext): boolean | Promise<boolean>;
|
|
6
30
|
}
|
|
7
|
-
type PoliciesGuardConstructor<Entity extends BaseEntity> = new (
|
|
31
|
+
type PoliciesGuardConstructor<Entity extends BaseEntity> = new (model: Model<Entity>) => PoliciesGuard;
|
|
8
32
|
export { PoliciesGuardConstructor, PoliciesGuard };
|
|
@@ -1,12 +1,45 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
import { Type } from '@nestjs/common';
|
|
26
|
+
import { FilterQuery } from 'mongoose';
|
|
1
27
|
import { BaseEntity } from '../models';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
28
|
+
import { DeleteResult, UpdateResult } from './dynamic-api-route-response.type';
|
|
29
|
+
type DynamicApiCallbackMethods = {
|
|
30
|
+
findManyDocuments<T>(entity: Type<T>, query: FilterQuery<T>): Promise<T[]>;
|
|
31
|
+
findOneDocument<T>(entity: Type<T>, query: FilterQuery<T>): Promise<T | undefined>;
|
|
32
|
+
createManyDocuments<T>(entity: Type<T>, data: Partial<T>[]): Promise<T[]>;
|
|
33
|
+
createOneDocument<T>(entity: Type<T>, data: Partial<T>): Promise<T>;
|
|
34
|
+
updateManyDocuments<T>(entity: Type<T>, query: FilterQuery<T>, data: Partial<T>): Promise<UpdateResult>;
|
|
35
|
+
updateOneDocument<T>(entity: Type<T>, query: FilterQuery<T>, data: Partial<T>): Promise<UpdateResult>;
|
|
36
|
+
deleteManyDocuments<T>(entity: Type<T>, ids: string[]): Promise<DeleteResult>;
|
|
37
|
+
deleteOneDocument<T>(entity: Type<T>, id: string): Promise<DeleteResult>;
|
|
5
38
|
};
|
|
6
|
-
type DynamicApiServiceCallback<Entity extends BaseEntity> = (entity: Entity, methods: DynamicApiCallbackMethods
|
|
39
|
+
type DynamicApiServiceCallback<Entity extends BaseEntity> = (entity: Entity, methods: DynamicApiCallbackMethods) => Promise<void>;
|
|
7
40
|
type DynamicApiResetPasswordCallbackMethods<Entity extends BaseEntity, UpdateBy = 'userId'> = {
|
|
8
41
|
findUserByEmail: (email: string) => Promise<Entity | undefined>;
|
|
9
|
-
updateUserByEmail: (email: string, update: Partial<Entity>) => Promise<Entity>;
|
|
42
|
+
updateUserByEmail: (email: string, update: Partial<Entity>) => Promise<Entity | undefined>;
|
|
10
43
|
};
|
|
11
44
|
type DynamicApiResetPasswordCallback<Entity extends BaseEntity> = (_: {
|
|
12
45
|
resetPasswordToken: string;
|
|
@@ -9,6 +9,7 @@ export * from './dynamic-api-policy-handler.interface';
|
|
|
9
9
|
export * from './dynamic-api-route-config.interface';
|
|
10
10
|
export * from './dynamic-api-route-dtos-bundle.type';
|
|
11
11
|
export * from './dynamic-api-route-module.type';
|
|
12
|
+
export * from './dynamic-api-route-response.type';
|
|
12
13
|
export * from './dynamic-api-route-type.type';
|
|
13
14
|
export * from './dynamic-api-schema-options.interface';
|
|
14
15
|
export * from './dynamic-api-service-callback.interface';
|
package/src/interfaces/index.js
CHANGED
|
@@ -25,6 +25,7 @@ __exportStar(require("./dynamic-api-policy-handler.interface"), exports);
|
|
|
25
25
|
__exportStar(require("./dynamic-api-route-config.interface"), exports);
|
|
26
26
|
__exportStar(require("./dynamic-api-route-dtos-bundle.type"), exports);
|
|
27
27
|
__exportStar(require("./dynamic-api-route-module.type"), exports);
|
|
28
|
+
__exportStar(require("./dynamic-api-route-response.type"), exports);
|
|
28
29
|
__exportStar(require("./dynamic-api-route-type.type"), exports);
|
|
29
30
|
__exportStar(require("./dynamic-api-schema-options.interface"), exports);
|
|
30
31
|
__exportStar(require("./dynamic-api-service-callback.interface"), exports);
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
2
|
import { AbilityPredicate, PoliciesGuardConstructor, RouteType } from '../interfaces';
|
|
3
3
|
import { BaseEntity } from '../models';
|
|
4
|
-
|
|
5
|
-
declare function CreatePoliciesGuardMixin<Entity extends BaseEntity, Service extends BaseService<Entity>>(entity: Type<Entity>, routeType: RouteType, version: string | undefined, abilityPredicate: AbilityPredicate<Entity> | undefined): PoliciesGuardConstructor<Entity>;
|
|
4
|
+
declare function CreatePoliciesGuardMixin<Entity extends BaseEntity>(entity: Type<Entity>, routeType: RouteType, version: string | undefined, abilityPredicate: AbilityPredicate<Entity> | undefined): PoliciesGuardConstructor<Entity>;
|
|
6
5
|
export { CreatePoliciesGuardMixin };
|
|
@@ -14,13 +14,16 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.CreatePoliciesGuardMixin = void 0;
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
|
+
const mongoose_1 = require("@nestjs/mongoose");
|
|
18
|
+
const mongoose_2 = require("mongoose");
|
|
19
|
+
const dynamic_api_module_1 = require("../dynamic-api.module");
|
|
17
20
|
const guards_1 = require("../guards");
|
|
18
21
|
const helpers_1 = require("../helpers");
|
|
19
22
|
function CreatePoliciesGuardMixin(entity, routeType, version, abilityPredicate) {
|
|
20
23
|
let RoutePoliciesGuard = class RoutePoliciesGuard extends guards_1.BasePoliciesGuard {
|
|
21
|
-
constructor(
|
|
22
|
-
super(
|
|
23
|
-
this.
|
|
24
|
+
constructor(model) {
|
|
25
|
+
super(model);
|
|
26
|
+
this.model = model;
|
|
24
27
|
this.routeType = routeType;
|
|
25
28
|
this.entity = entity;
|
|
26
29
|
this.abilityPredicate = abilityPredicate;
|
|
@@ -28,8 +31,8 @@ function CreatePoliciesGuardMixin(entity, routeType, version, abilityPredicate)
|
|
|
28
31
|
};
|
|
29
32
|
RoutePoliciesGuard = __decorate([
|
|
30
33
|
(0, common_1.Injectable)(),
|
|
31
|
-
__param(0, (0,
|
|
32
|
-
__metadata("design:paramtypes", [
|
|
34
|
+
__param(0, (0, mongoose_1.InjectModel)(entity.name, dynamic_api_module_1.DynamicApiModule.state.get('connectionName'))),
|
|
35
|
+
__metadata("design:paramtypes", [mongoose_2.Model])
|
|
33
36
|
], RoutePoliciesGuard);
|
|
34
37
|
Object.defineProperty(RoutePoliciesGuard, 'name', {
|
|
35
38
|
value: `${(0, helpers_1.provideName)(routeType, entity.name, version, 'PoliciesGuard')}`,
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Type, ValidationPipeOptions } from '@nestjs/common';
|
|
2
2
|
import { DynamicApiServiceCallback, DynamicAPIServiceProvider } from '../../interfaces';
|
|
3
3
|
import { BaseEntity } from '../../models';
|
|
4
|
-
import { AuthControllerConstructor, DynamicApiRegisterOptions, DynamicApiResetPasswordOptions } from './interfaces';
|
|
4
|
+
import { AuthControllerConstructor, DynamicApiLoginOptions, DynamicApiRegisterOptions, DynamicApiResetPasswordOptions } from './interfaces';
|
|
5
5
|
declare const authServiceProviderName = "DynamicApiAuthService";
|
|
6
6
|
declare const localStrategyProviderName = "DynamicApiLocalStrategy";
|
|
7
7
|
declare function createLocalStrategyProvider<Entity extends BaseEntity>(loginField: keyof Entity, passwordField: keyof Entity): DynamicAPIServiceProvider;
|
|
8
|
-
declare function createAuthServiceProvider<Entity extends BaseEntity>(userEntity: Type<Entity>, loginField
|
|
9
|
-
declare function createAuthController<Entity extends BaseEntity>(userEntity: Type<Entity>, loginField
|
|
8
|
+
declare function createAuthServiceProvider<Entity extends BaseEntity>(userEntity: Type<Entity>, { loginField, passwordField, additionalFields, callback: loginCallback }: DynamicApiLoginOptions<Entity>, registerCallback: DynamicApiServiceCallback<Entity> | undefined, resetPasswordOptions: DynamicApiResetPasswordOptions<Entity> | undefined): DynamicAPIServiceProvider;
|
|
9
|
+
declare function createAuthController<Entity extends BaseEntity>(userEntity: Type<Entity>, { loginField, passwordField, additionalFields }: DynamicApiLoginOptions<Entity>, registerOptions: DynamicApiRegisterOptions<Entity> | undefined, validationPipeOptions: ValidationPipeOptions | undefined, resetPasswordOptions: DynamicApiResetPasswordOptions<Entity> | undefined): AuthControllerConstructor<Entity>;
|
|
10
10
|
export { authServiceProviderName, createAuthController, createAuthServiceProvider, createLocalStrategyProvider, localStrategyProviderName, };
|
|
@@ -56,7 +56,7 @@ function createLocalStrategyProvider(loginField, passwordField) {
|
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
58
|
exports.createLocalStrategyProvider = createLocalStrategyProvider;
|
|
59
|
-
function createAuthServiceProvider(userEntity, loginField, passwordField, additionalFields,
|
|
59
|
+
function createAuthServiceProvider(userEntity, { loginField, passwordField, additionalFields, callback: loginCallback }, registerCallback, resetPasswordOptions) {
|
|
60
60
|
let AuthService = class AuthService extends services_2.BaseAuthService {
|
|
61
61
|
constructor(model, jwtService, bcryptService) {
|
|
62
62
|
super(model, jwtService, bcryptService);
|
|
@@ -83,8 +83,8 @@ function createAuthServiceProvider(userEntity, loginField, passwordField, additi
|
|
|
83
83
|
};
|
|
84
84
|
}
|
|
85
85
|
exports.createAuthServiceProvider = createAuthServiceProvider;
|
|
86
|
-
function createAuthController(userEntity, loginField, passwordField,
|
|
87
|
-
let AuthController = class AuthController extends (0, mixins_1.AuthControllerMixin)(userEntity, loginField, passwordField,
|
|
86
|
+
function createAuthController(userEntity, { loginField, passwordField, additionalFields }, registerOptions, validationPipeOptions, resetPasswordOptions) {
|
|
87
|
+
let AuthController = class AuthController extends (0, mixins_1.AuthControllerMixin)(userEntity, loginField, passwordField, additionalFields, registerOptions ?? {}, resetPasswordOptions) {
|
|
88
88
|
constructor(service) {
|
|
89
89
|
super(service);
|
|
90
90
|
this.service = service;
|
|
@@ -3,10 +3,11 @@ import { BcryptService } from '../../services';
|
|
|
3
3
|
import { DynamicApiAuthOptions } from './interfaces';
|
|
4
4
|
import { JwtStrategy } from './strategies';
|
|
5
5
|
export declare class AuthModule {
|
|
6
|
-
static forRoot<Entity extends BaseEntity>(
|
|
6
|
+
static forRoot<Entity extends BaseEntity>(options: DynamicApiAuthOptions<Entity>, extraImports?: any[]): {
|
|
7
7
|
module: typeof AuthModule;
|
|
8
8
|
imports: any[];
|
|
9
9
|
providers: (typeof BcryptService | import("../../interfaces/dynamic-api-service-provider.interface").DynamicAPIServiceProvider | typeof JwtStrategy)[];
|
|
10
10
|
controllers: import("./interfaces/auth-controller.interface").AuthControllerConstructor<Entity>[];
|
|
11
11
|
};
|
|
12
|
+
private static initializeAuthOptions;
|
|
12
13
|
}
|
|
@@ -18,13 +18,14 @@ const services_1 = require("../../services");
|
|
|
18
18
|
const auth_helper_1 = require("./auth.helper");
|
|
19
19
|
const strategies_1 = require("./strategies");
|
|
20
20
|
let AuthModule = AuthModule_1 = class AuthModule {
|
|
21
|
-
static forRoot(
|
|
22
|
-
const {
|
|
21
|
+
static forRoot(options, extraImports = []) {
|
|
22
|
+
const { userEntity, login: { loginField, passwordField, ...login }, register, resetPassword, jwt: { secret, expiresIn }, validationPipeOptions, } = this.initializeAuthOptions(options);
|
|
23
|
+
const { resetPasswordCallback, changePasswordCallback, emailField, expirationInMinutes, abilityPredicate, } = resetPassword;
|
|
23
24
|
const resetPasswordOptions = resetPasswordCallback
|
|
24
|
-
? { resetPasswordCallback, changePasswordCallback, emailField,
|
|
25
|
+
? { resetPasswordCallback, changePasswordCallback, emailField, expirationInMinutes, abilityPredicate }
|
|
25
26
|
: undefined;
|
|
26
|
-
const AuthController = (0, auth_helper_1.createAuthController)(userEntity, loginField, passwordField,
|
|
27
|
-
const AuthServiceProvider = (0, auth_helper_1.createAuthServiceProvider)(userEntity, loginField, passwordField,
|
|
27
|
+
const AuthController = (0, auth_helper_1.createAuthController)(userEntity, { loginField, passwordField, ...login }, register, validationPipeOptions, resetPasswordOptions);
|
|
28
|
+
const AuthServiceProvider = (0, auth_helper_1.createAuthServiceProvider)(userEntity, { loginField, passwordField, ...login }, register.callback, resetPasswordOptions);
|
|
28
29
|
const LocalStrategyProvider = (0, auth_helper_1.createLocalStrategyProvider)(loginField, passwordField);
|
|
29
30
|
return {
|
|
30
31
|
module: AuthModule_1,
|
|
@@ -52,6 +53,32 @@ let AuthModule = AuthModule_1 = class AuthModule {
|
|
|
52
53
|
controllers: [AuthController],
|
|
53
54
|
};
|
|
54
55
|
}
|
|
56
|
+
static initializeAuthOptions({ userEntity, login, register, resetPassword, jwt, validationPipeOptions, }) {
|
|
57
|
+
return {
|
|
58
|
+
userEntity: userEntity,
|
|
59
|
+
login: {
|
|
60
|
+
...login,
|
|
61
|
+
loginField: (login?.loginField ?? 'email'),
|
|
62
|
+
passwordField: (login?.passwordField ?? 'password'),
|
|
63
|
+
additionalFields: login?.additionalFields ?? [],
|
|
64
|
+
},
|
|
65
|
+
register: {
|
|
66
|
+
...register,
|
|
67
|
+
additionalFields: register?.additionalFields ?? [],
|
|
68
|
+
protected: register?.protected ?? !!register?.abilityPredicate,
|
|
69
|
+
},
|
|
70
|
+
resetPassword: {
|
|
71
|
+
...resetPassword,
|
|
72
|
+
emailField: (!resetPassword?.emailField ? 'email' : String(resetPassword.emailField)),
|
|
73
|
+
expirationInMinutes: resetPassword?.expirationInMinutes ?? 10,
|
|
74
|
+
},
|
|
75
|
+
jwt: {
|
|
76
|
+
secret: jwt?.secret ?? 'dynamic-api-jwt-secret',
|
|
77
|
+
expiresIn: jwt?.expiresIn ?? '1d',
|
|
78
|
+
},
|
|
79
|
+
validationPipeOptions: validationPipeOptions,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
55
82
|
};
|
|
56
83
|
exports.AuthModule = AuthModule;
|
|
57
84
|
exports.AuthModule = AuthModule = AuthModule_1 = __decorate([
|
|
@@ -1,40 +1,39 @@
|
|
|
1
1
|
import { Type, ValidationPipeOptions } from '@nestjs/common';
|
|
2
|
-
import { DynamicApiResetPasswordCallback, DynamicApiServiceCallback,
|
|
2
|
+
import { DynamicApiResetPasswordCallback, DynamicApiServiceCallback, AuthAbilityPredicate } from '../../../interfaces';
|
|
3
3
|
import { BaseEntity } from '../../../models';
|
|
4
4
|
type DynamicApiJWTOptions = {
|
|
5
5
|
secret: string;
|
|
6
6
|
expiresIn?: string | number;
|
|
7
7
|
};
|
|
8
8
|
type DynamicApiLoginOptions<Entity extends BaseEntity = any> = {
|
|
9
|
+
loginField?: keyof Entity;
|
|
10
|
+
passwordField?: keyof Entity;
|
|
9
11
|
callback?: DynamicApiServiceCallback<Entity>;
|
|
12
|
+
abilityPredicate?: AuthAbilityPredicate;
|
|
13
|
+
additionalFields?: (keyof Entity)[];
|
|
10
14
|
};
|
|
11
15
|
type DynamicApiRegisterOptions<Entity extends BaseEntity = any> = {
|
|
12
16
|
callback?: DynamicApiServiceCallback<Entity>;
|
|
13
17
|
protected?: boolean;
|
|
14
|
-
abilityPredicate?:
|
|
18
|
+
abilityPredicate?: AuthAbilityPredicate;
|
|
15
19
|
additionalFields?: (keyof Entity | {
|
|
16
20
|
name: keyof Entity;
|
|
17
21
|
required?: boolean;
|
|
18
22
|
})[];
|
|
19
23
|
};
|
|
20
|
-
type DynamicApiUserOptions<Entity extends BaseEntity = any> = {
|
|
21
|
-
entity: Type<Entity>;
|
|
22
|
-
loginField?: keyof Entity;
|
|
23
|
-
passwordField?: keyof Entity;
|
|
24
|
-
requestAdditionalFields?: (keyof Entity)[];
|
|
25
|
-
};
|
|
26
24
|
type DynamicApiResetPasswordOptions<Entity extends BaseEntity = any> = {
|
|
25
|
+
emailField: keyof Entity | string;
|
|
26
|
+
expirationInMinutes: number;
|
|
27
27
|
resetPasswordCallback: DynamicApiResetPasswordCallback<Entity>;
|
|
28
28
|
changePasswordCallback: DynamicApiServiceCallback<Entity>;
|
|
29
|
-
|
|
30
|
-
expiresInMinutes: number;
|
|
29
|
+
abilityPredicate?: AuthAbilityPredicate;
|
|
31
30
|
};
|
|
32
31
|
type DynamicApiAuthOptions<Entity extends BaseEntity = any> = {
|
|
33
|
-
|
|
32
|
+
userEntity: Type<Entity>;
|
|
34
33
|
login?: DynamicApiLoginOptions<Entity>;
|
|
35
34
|
register?: DynamicApiRegisterOptions<Entity>;
|
|
36
35
|
jwt?: DynamicApiJWTOptions;
|
|
37
36
|
resetPassword?: Partial<DynamicApiResetPasswordOptions<Entity>>;
|
|
38
37
|
validationPipeOptions?: ValidationPipeOptions;
|
|
39
38
|
};
|
|
40
|
-
export type { DynamicApiAuthOptions, DynamicApiRegisterOptions,
|
|
39
|
+
export type { DynamicApiAuthOptions, DynamicApiRegisterOptions, DynamicApiJWTOptions, DynamicApiLoginOptions, DynamicApiResetPasswordOptions, };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
|
-
import { PoliciesGuardConstructor,
|
|
2
|
+
import { PoliciesGuardConstructor, AuthAbilityPredicate } from '../../../interfaces';
|
|
3
3
|
import { BaseEntity } from '../../../models';
|
|
4
|
-
declare function AuthRegisterPoliciesGuardMixin<Entity extends BaseEntity>(entity: Type<Entity>, abilityPredicate:
|
|
4
|
+
declare function AuthRegisterPoliciesGuardMixin<Entity extends BaseEntity>(entity: Type<Entity>, abilityPredicate: AuthAbilityPredicate | undefined): PoliciesGuardConstructor<Entity>;
|
|
5
5
|
export { AuthRegisterPoliciesGuardMixin };
|
|
@@ -42,10 +42,10 @@ class BaseAuthService extends services_1.BaseService {
|
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
44
|
async register(userToCreate) {
|
|
45
|
-
const hashedPassword = await this.bcryptService.hashPassword(userToCreate[this.passwordField]);
|
|
46
45
|
try {
|
|
46
|
+
const hashedPassword = await this.bcryptService.hashPassword(userToCreate[this.passwordField]);
|
|
47
47
|
const { _id } = await this.model.create({ ...userToCreate, [this.passwordField]: hashedPassword });
|
|
48
|
-
const user = await this.
|
|
48
|
+
const user = await this.findOneDocumentWithAbilityPredicate(_id);
|
|
49
49
|
if (this.registerCallback) {
|
|
50
50
|
await this.registerCallback(user, this.callbackMethods);
|
|
51
51
|
}
|
|
@@ -56,7 +56,7 @@ class BaseAuthService extends services_1.BaseService {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
async getAccount({ id }) {
|
|
59
|
-
const user = await this.
|
|
59
|
+
const user = await this.findOneDocumentWithAbilityPredicate(id);
|
|
60
60
|
const fieldsToBuild = [
|
|
61
61
|
'_id',
|
|
62
62
|
this.loginField,
|
|
@@ -81,13 +81,13 @@ class BaseAuthService extends services_1.BaseService {
|
|
|
81
81
|
updateUserByEmail: async (email, data) => {
|
|
82
82
|
const user = await this.model.findOneAndUpdate({ [this.resetPasswordOptions.emailField]: email }, data, { new: true }).lean().exec();
|
|
83
83
|
if (!user) {
|
|
84
|
-
|
|
84
|
+
return;
|
|
85
85
|
}
|
|
86
86
|
return this.buildInstance(user);
|
|
87
87
|
},
|
|
88
88
|
};
|
|
89
|
-
const { resetPasswordCallback,
|
|
90
|
-
const resetPasswordToken = this.jwtService.sign({ email }, { expiresIn:
|
|
89
|
+
const { resetPasswordCallback, expirationInMinutes } = this.resetPasswordOptions;
|
|
90
|
+
const resetPasswordToken = this.jwtService.sign({ email }, { expiresIn: expirationInMinutes * 60 });
|
|
91
91
|
await resetPasswordCallback({ resetPasswordToken, email }, this.resetPasswordCallbackMethods);
|
|
92
92
|
}
|
|
93
93
|
async changePassword(resetPasswordToken, newPassword) {
|
|
@@ -110,7 +110,7 @@ class BaseAuthService extends services_1.BaseService {
|
|
|
110
110
|
}
|
|
111
111
|
let userId;
|
|
112
112
|
try {
|
|
113
|
-
const { _id } = await this.
|
|
113
|
+
const { _id } = await this.findOneDocumentWithAbilityPredicate(undefined, { [this.resetPasswordOptions.emailField]: email });
|
|
114
114
|
userId = _id.toString();
|
|
115
115
|
}
|
|
116
116
|
catch (error) {
|
|
@@ -122,7 +122,7 @@ class BaseAuthService extends services_1.BaseService {
|
|
|
122
122
|
const hashedPassword = await this.bcryptService.hashPassword(newPassword);
|
|
123
123
|
await this.model.updateOne({ _id: userId }, { [this.passwordField]: hashedPassword });
|
|
124
124
|
if (this.resetPasswordOptions?.changePasswordCallback) {
|
|
125
|
-
const user = await this.
|
|
125
|
+
const user = await this.findOneDocumentWithAbilityPredicate(userId);
|
|
126
126
|
await this.resetPasswordOptions.changePasswordCallback(this.buildInstance(user), this.callbackMethods);
|
|
127
127
|
}
|
|
128
128
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { DeleteResult } from '../../interfaces';
|
|
1
2
|
import { BaseEntity } from '../../models';
|
|
2
|
-
import { DeletedCount } from '../delete-one';
|
|
3
3
|
import { DeleteManyService } from './delete-many-service.interface';
|
|
4
4
|
interface DeleteManyController<Entity extends BaseEntity> {
|
|
5
|
-
deleteMany(ids: string[]): Promise<
|
|
5
|
+
deleteMany(ids: string[]): Promise<DeleteResult>;
|
|
6
6
|
}
|
|
7
7
|
type DeleteManyControllerConstructor<Entity extends BaseEntity> = new (service: DeleteManyService<Entity>) => DeleteManyController<Entity>;
|
|
8
8
|
export type { DeleteManyController, DeleteManyControllerConstructor };
|