mongodb-dynamic-api 2.2.1 → 2.3.1
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 +9 -0
- package/package.json +3 -1
- package/src/decorators/api-endpoint-visibility.decorator.d.ts +3 -0
- package/src/decorators/api-endpoint-visibility.decorator.js +10 -0
- package/src/decorators/index.d.ts +1 -0
- package/src/decorators/index.js +1 -0
- package/src/dynamic-api.module.js +25 -24
- package/src/guards/base-policies.guard.d.ts +28 -3
- package/src/guards/base-policies.guard.js +8 -7
- package/src/helpers/index.d.ts +2 -0
- package/src/helpers/index.js +2 -0
- 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 +23 -3
- 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 +4 -3
- package/src/modules/auth/auth.module.d.ts +1 -1
- package/src/modules/auth/auth.module.js +7 -3
- package/src/modules/auth/dtos/change-password.dto.d.ts +4 -0
- package/src/modules/auth/dtos/change-password.dto.js +29 -0
- package/src/modules/auth/dtos/reset-password.dto.d.ts +3 -0
- package/src/modules/auth/dtos/reset-password.dto.js +24 -0
- package/src/modules/auth/interfaces/auth-options.interface.d.ts +15 -9
- package/src/modules/auth/interfaces/auth-service.interface.d.ts +2 -1
- package/src/modules/auth/mixins/auth-controller.mixin.d.ts +2 -2
- package/src/modules/auth/mixins/auth-controller.mixin.js +28 -2
- package/src/modules/auth/mixins/auth-register-policies-guard.mixin.d.ts +2 -2
- package/src/modules/auth/services/base-auth.service.d.ts +6 -3
- package/src/modules/auth/services/base-auth.service.js +67 -9
- package/src/routes/create-many/base-create-many.service.js +1 -1
- package/src/routes/create-many/create-many-controller.mixin.js +1 -2
- package/src/routes/create-many/create-many.helper.js +1 -0
- package/src/routes/create-many/create-many.module.d.ts +1 -1
- package/src/routes/create-many/create-many.module.js +1 -1
- package/src/routes/create-one/base-create-one.service.js +1 -1
- package/src/routes/create-one/create-one-controller.mixin.js +1 -2
- package/src/routes/create-one/create-one.helper.js +1 -0
- package/src/routes/delete-many/delete-many-controller.interface.d.ts +2 -2
- package/src/routes/delete-many/delete-many-controller.mixin.js +1 -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-controller.mixin.js +1 -2
- package/src/routes/delete-one/delete-one-service.interface.d.ts +3 -5
- package/src/routes/duplicate-many/base-duplicate-many.service.js +1 -1
- package/src/routes/duplicate-many/duplicate-many-controller.mixin.js +1 -2
- package/src/routes/duplicate-one/base-duplicate-one.service.js +1 -1
- package/src/routes/duplicate-one/duplicate-one-controller.mixin.js +1 -2
- package/src/routes/get-many/base-get-many.service.js +1 -1
- package/src/routes/get-many/get-many-controller.mixin.js +1 -2
- package/src/routes/get-one/base-get-one.service.js +1 -1
- package/src/routes/get-one/get-one-controller.mixin.js +1 -2
- package/src/routes/replace-one/base-replace-one.service.js +1 -1
- package/src/routes/replace-one/replace-one-controller.mixin.js +1 -2
- package/src/routes/update-many/base-update-many.service.js +1 -1
- package/src/routes/update-many/update-many-controller.mixin.js +1 -2
- package/src/routes/update-one/base-update-one.service.js +1 -1
- package/src/routes/update-one/update-one-controller.mixin.js +1 -2
- package/src/services/base/base.service.d.ts +17 -5
- package/src/services/base/base.service.js +61 -12
- 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,14 @@
|
|
|
1
1
|
Changelog
|
|
2
2
|
|
|
3
|
+
## [2.3.1](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v2.3.0...v2.3.1) (2024-05-05)
|
|
4
|
+
|
|
5
|
+
## [2.3.0](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v2.2.1...v2.3.0) (2024-05-02)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### api
|
|
9
|
+
|
|
10
|
+
* **api:** add reset password process with callbacks ([9e8bf56](https://github.com/MikeDev75015/mongodb-dynamic-api/commit/9e8bf562ee9717ea1c0fe387bb621e9f724955ac))
|
|
11
|
+
|
|
3
12
|
## [2.2.1](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v2.2.0...v2.2.1) (2024-04-30)
|
|
4
13
|
|
|
5
14
|
## [2.2.0](https://github.com/MikeDev75015/mongodb-dynamic-api/compare/v2.1.10...v2.2.0) (2024-04-30)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongodb-dynamic-api",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "Auto generated CRUD API for MongoDB using NestJS",
|
|
5
5
|
"readmeFilename": "README.md",
|
|
6
6
|
"main": "index.js",
|
|
@@ -96,6 +96,8 @@
|
|
|
96
96
|
"typescript": "^5.3.3"
|
|
97
97
|
},
|
|
98
98
|
"jest": {
|
|
99
|
+
"clearMocks": true,
|
|
100
|
+
"resetMocks": true,
|
|
99
101
|
"moduleFileExtensions": [
|
|
100
102
|
"js",
|
|
101
103
|
"json",
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiEndpointVisibility = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const swagger_1 = require("@nestjs/swagger");
|
|
6
|
+
function ApiEndpointVisibility(condition, decorator) {
|
|
7
|
+
const decoratorIfValid = decorator ?? ((_) => undefined);
|
|
8
|
+
return (0, common_1.applyDecorators)(!condition ? (0, swagger_1.ApiExcludeEndpoint)() : decoratorIfValid);
|
|
9
|
+
}
|
|
10
|
+
exports.ApiEndpointVisibility = ApiEndpointVisibility;
|
package/src/decorators/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./api-endpoint-visibility.decorator"), exports);
|
|
17
18
|
__exportStar(require("./public.decorator"), exports);
|
|
18
19
|
__exportStar(require("./schema-options.decorator"), exports);
|
|
19
20
|
__exportStar(require("./validator-pipe.decorator"), exports);
|
|
@@ -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,7 +36,7 @@ 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?.
|
|
39
|
+
...(useAuth?.userEntity ? [
|
|
40
40
|
modules_1.AuthModule.forRoot(this.initializeAuthOptions(useAuth)),
|
|
41
41
|
] : []),
|
|
42
42
|
],
|
|
@@ -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());
|
|
@@ -80,10 +80,6 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
80
80
|
...routes.map((routeConfig) => {
|
|
81
81
|
const { type, description: routeDescription, version: routeVersion, validationPipeOptions: routeValidationPipeOptions, } = routeConfig;
|
|
82
82
|
const module = moduleByRouteType.get(type);
|
|
83
|
-
if (!module) {
|
|
84
|
-
reject(new Error(`Route module for ${type} not found`));
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
83
|
const description = routeDescription ?? (0, helpers_1.getDefaultRouteDescription)(type, entity.name);
|
|
88
84
|
const version = routeVersion ?? controllerVersion;
|
|
89
85
|
if (version && !(0, helpers_1.isValidVersion)(version)) {
|
|
@@ -120,17 +116,18 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
120
116
|
}, 5000);
|
|
121
117
|
});
|
|
122
118
|
}
|
|
123
|
-
static buildStateFromOptions(useGlobalCache, cacheOptions, useAuth, routesConfig) {
|
|
119
|
+
static buildStateFromOptions(uri, useGlobalCache, cacheOptions, useAuth, routesConfig) {
|
|
124
120
|
const routesConfigState = this.state.get('routesConfig');
|
|
125
121
|
return {
|
|
122
|
+
uri,
|
|
126
123
|
initialized: true,
|
|
127
124
|
isGlobalCacheEnabled: useGlobalCache,
|
|
128
125
|
...(cacheOptions?.excludePaths ? { cacheExcludedPaths: cacheOptions?.excludePaths } : {}),
|
|
129
|
-
...(useAuth?.
|
|
126
|
+
...(useAuth?.userEntity ? {
|
|
130
127
|
isAuthEnabled: true,
|
|
131
128
|
credentials: {
|
|
132
|
-
loginField: !useAuth.
|
|
133
|
-
passwordField: !useAuth.
|
|
129
|
+
loginField: !useAuth.login?.loginField ? 'email' : String(useAuth.login.loginField),
|
|
130
|
+
passwordField: !useAuth.login?.passwordField ? 'password' : String(useAuth.login.passwordField),
|
|
134
131
|
},
|
|
135
132
|
jwtSecret: useAuth.jwt?.secret ?? 'dynamic-api-jwt-secret',
|
|
136
133
|
} : {}),
|
|
@@ -144,24 +141,28 @@ let DynamicApiModule = DynamicApiModule_1 = class DynamicApiModule {
|
|
|
144
141
|
}
|
|
145
142
|
static initializeAuthOptions(useAuth) {
|
|
146
143
|
return {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
144
|
+
userEntity: useAuth.userEntity,
|
|
145
|
+
login: {
|
|
146
|
+
...useAuth.login,
|
|
147
|
+
loginField: useAuth.login?.loginField ?? 'email',
|
|
148
|
+
passwordField: useAuth.login?.passwordField ?? 'password',
|
|
149
|
+
additionalFields: useAuth.login?.additionalFields ?? [],
|
|
150
|
+
},
|
|
151
|
+
register: {
|
|
152
|
+
...useAuth.register,
|
|
153
|
+
additionalFields: useAuth.register?.additionalFields ?? [],
|
|
154
|
+
protected: useAuth.register?.protected ?? !!useAuth.register.abilityPredicate,
|
|
155
|
+
},
|
|
156
|
+
resetPassword: {
|
|
157
|
+
...useAuth.resetPassword,
|
|
158
|
+
emailField: !useAuth.resetPassword?.emailField ? 'email' : String(useAuth.resetPassword.emailField),
|
|
159
|
+
expirationInMinutes: useAuth.resetPassword?.expirationInMinutes ?? 10,
|
|
152
160
|
},
|
|
153
161
|
jwt: {
|
|
154
162
|
secret: useAuth.jwt?.secret ?? 'dynamic-api-jwt-secret',
|
|
155
163
|
expiresIn: useAuth.jwt?.expiresIn ?? '1d',
|
|
156
164
|
},
|
|
157
|
-
|
|
158
|
-
...useAuth.register,
|
|
159
|
-
protected: useAuth.register.protected ?? !!useAuth.register.abilityPredicate,
|
|
160
|
-
} : {
|
|
161
|
-
protected: false,
|
|
162
|
-
additionalFields: [],
|
|
163
|
-
},
|
|
164
|
-
login: useAuth.login ?? {},
|
|
165
|
+
validationPipeOptions: useAuth.validationPipeOptions,
|
|
165
166
|
};
|
|
166
167
|
}
|
|
167
168
|
static setDefaultRoutes(stateRoutesConfig, controllerRoutesConfig = {}, routes = []) {
|
|
@@ -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;
|
package/src/helpers/index.d.ts
CHANGED
package/src/helpers/index.js
CHANGED
|
@@ -14,6 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./controller-ability-predicates.helper"), exports);
|
|
18
|
+
__exportStar(require("./controller-mixin.helper"), exports);
|
|
17
19
|
__exportStar(require("./format.helper"), exports);
|
|
18
20
|
__exportStar(require("./route-decorators.helper"), exports);
|
|
19
21
|
__exportStar(require("./route-description.helper"), exports);
|
|
@@ -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 };
|
|
@@ -22,7 +22,27 @@
|
|
|
22
22
|
/// <reference types="mongoose/types/validation" />
|
|
23
23
|
/// <reference types="mongoose/types/virtuals" />
|
|
24
24
|
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
-
import {
|
|
25
|
+
import { Type } from '@nestjs/common';
|
|
26
|
+
import { FilterQuery } from 'mongoose';
|
|
26
27
|
import { BaseEntity } from '../models';
|
|
27
|
-
|
|
28
|
-
|
|
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>;
|
|
38
|
+
};
|
|
39
|
+
type DynamicApiServiceCallback<Entity extends BaseEntity> = (entity: Entity, methods: DynamicApiCallbackMethods) => Promise<void>;
|
|
40
|
+
type DynamicApiResetPasswordCallbackMethods<Entity extends BaseEntity, UpdateBy = 'userId'> = {
|
|
41
|
+
findUserByEmail: (email: string) => Promise<Entity | undefined>;
|
|
42
|
+
updateUserByEmail: (email: string, update: Partial<Entity>) => Promise<Entity | undefined>;
|
|
43
|
+
};
|
|
44
|
+
type DynamicApiResetPasswordCallback<Entity extends BaseEntity> = (_: {
|
|
45
|
+
resetPasswordToken: string;
|
|
46
|
+
email: string;
|
|
47
|
+
}, methods: DynamicApiResetPasswordCallbackMethods<Entity>) => Promise<void>;
|
|
48
|
+
export type { DynamicApiServiceCallback, DynamicApiResetPasswordCallback, DynamicApiCallbackMethods, DynamicApiResetPasswordCallbackMethods, };
|
|
@@ -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 } 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, registerCallback,
|
|
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);
|
|
@@ -68,6 +68,7 @@ function createAuthServiceProvider(userEntity, loginField, passwordField, additi
|
|
|
68
68
|
this.passwordField = passwordField;
|
|
69
69
|
this.registerCallback = registerCallback;
|
|
70
70
|
this.loginCallback = loginCallback;
|
|
71
|
+
this.resetPasswordOptions = resetPasswordOptions;
|
|
71
72
|
}
|
|
72
73
|
};
|
|
73
74
|
AuthService = __decorate([
|
|
@@ -82,8 +83,8 @@ function createAuthServiceProvider(userEntity, loginField, passwordField, additi
|
|
|
82
83
|
};
|
|
83
84
|
}
|
|
84
85
|
exports.createAuthServiceProvider = createAuthServiceProvider;
|
|
85
|
-
function createAuthController(userEntity, loginField, passwordField,
|
|
86
|
-
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) {
|
|
87
88
|
constructor(service) {
|
|
88
89
|
super(service);
|
|
89
90
|
this.service = service;
|
|
@@ -3,7 +3,7 @@ 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>({ userEntity, login: { loginField, passwordField, ...login }, register, resetPassword, jwt: { secret, expiresIn }, validationPipeOptions, }: 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)[];
|
|
@@ -18,9 +18,13 @@ 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
|
|
23
|
-
const
|
|
21
|
+
static forRoot({ userEntity, login: { loginField, passwordField, ...login }, register, resetPassword, jwt: { secret, expiresIn }, validationPipeOptions, }, extraImports = []) {
|
|
22
|
+
const { resetPasswordCallback, changePasswordCallback, emailField, expirationInMinutes } = resetPassword;
|
|
23
|
+
const resetPasswordOptions = resetPasswordCallback
|
|
24
|
+
? { resetPasswordCallback, changePasswordCallback, emailField, expirationInMinutes }
|
|
25
|
+
: undefined;
|
|
26
|
+
const AuthController = (0, auth_helper_1.createAuthController)(userEntity, { loginField, passwordField, ...login }, register, validationPipeOptions, resetPasswordOptions);
|
|
27
|
+
const AuthServiceProvider = (0, auth_helper_1.createAuthServiceProvider)(userEntity, { loginField, passwordField, ...login }, register.callback, resetPasswordOptions);
|
|
24
28
|
const LocalStrategyProvider = (0, auth_helper_1.createLocalStrategyProvider)(loginField, passwordField);
|
|
25
29
|
return {
|
|
26
30
|
module: AuthModule_1,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ChangePasswordDto = void 0;
|
|
13
|
+
const swagger_1 = require("@nestjs/swagger");
|
|
14
|
+
const class_validator_1 = require("class-validator");
|
|
15
|
+
class ChangePasswordDto {
|
|
16
|
+
}
|
|
17
|
+
exports.ChangePasswordDto = ChangePasswordDto;
|
|
18
|
+
__decorate([
|
|
19
|
+
(0, class_validator_1.IsString)(),
|
|
20
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
21
|
+
(0, swagger_1.ApiProperty)(),
|
|
22
|
+
__metadata("design:type", String)
|
|
23
|
+
], ChangePasswordDto.prototype, "resetPasswordToken", void 0);
|
|
24
|
+
__decorate([
|
|
25
|
+
(0, class_validator_1.IsString)(),
|
|
26
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
27
|
+
(0, swagger_1.ApiProperty)(),
|
|
28
|
+
__metadata("design:type", String)
|
|
29
|
+
], ChangePasswordDto.prototype, "newPassword", void 0);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ResetPasswordDto = void 0;
|
|
13
|
+
const swagger_1 = require("@nestjs/swagger");
|
|
14
|
+
const class_validator_1 = require("class-validator");
|
|
15
|
+
class ResetPasswordDto {
|
|
16
|
+
}
|
|
17
|
+
exports.ResetPasswordDto = ResetPasswordDto;
|
|
18
|
+
__decorate([
|
|
19
|
+
(0, class_validator_1.IsString)(),
|
|
20
|
+
(0, class_validator_1.IsNotEmpty)(),
|
|
21
|
+
(0, class_validator_1.IsEmail)(),
|
|
22
|
+
(0, swagger_1.ApiProperty)(),
|
|
23
|
+
__metadata("design:type", String)
|
|
24
|
+
], ResetPasswordDto.prototype, "email", void 0);
|
|
@@ -1,33 +1,39 @@
|
|
|
1
1
|
import { Type, ValidationPipeOptions } from '@nestjs/common';
|
|
2
|
-
import { 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
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
type DynamicApiResetPasswordOptions<Entity extends BaseEntity = any> = {
|
|
25
|
+
emailField: keyof Entity | string;
|
|
26
|
+
expirationInMinutes: number;
|
|
27
|
+
resetPasswordCallback: DynamicApiResetPasswordCallback<Entity>;
|
|
28
|
+
changePasswordCallback: DynamicApiServiceCallback<Entity>;
|
|
29
|
+
abilityPredicate?: AuthAbilityPredicate;
|
|
25
30
|
};
|
|
26
31
|
type DynamicApiAuthOptions<Entity extends BaseEntity = any> = {
|
|
27
|
-
|
|
32
|
+
userEntity: Type<Entity>;
|
|
28
33
|
login?: DynamicApiLoginOptions<Entity>;
|
|
29
34
|
register?: DynamicApiRegisterOptions<Entity>;
|
|
30
35
|
jwt?: DynamicApiJWTOptions;
|
|
36
|
+
resetPassword?: Partial<DynamicApiResetPasswordOptions<Entity>>;
|
|
31
37
|
validationPipeOptions?: ValidationPipeOptions;
|
|
32
38
|
};
|
|
33
|
-
export type { DynamicApiAuthOptions, DynamicApiRegisterOptions,
|
|
39
|
+
export type { DynamicApiAuthOptions, DynamicApiRegisterOptions, DynamicApiJWTOptions, DynamicApiLoginOptions, DynamicApiResetPasswordOptions, };
|