@nest-extended/cli 0.0.2-beta-2 → 0.0.2-beta-4
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/package.json +1 -1
- package/src/commands/generate-app.d.ts +1 -0
- package/src/commands/generate-app.js +166 -0
- package/src/commands/generate-app.js.map +1 -0
- package/src/commands/generate-service.d.ts +1 -0
- package/src/commands/generate-service.js +48 -0
- package/src/commands/generate-service.js.map +1 -0
- package/src/commands/generate.js +4 -193
- package/src/commands/generate.js.map +1 -1
- package/src/templates/auth.template.js +34 -26
- package/src/templates/auth.template.js.map +1 -1
- package/src/templates/controller.template.js +4 -4
- package/src/templates/controller.template.js.map +1 -1
- package/src/templates/module.template.js +1 -1
- package/src/templates/module.template.js.map +1 -1
- package/src/templates/users.template.js +3 -4
- package/src/templates/users.template.js.map +1 -1
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const generateAppAction: (appName: string) => Promise<void>;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateAppAction = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const inquirer = require("inquirer");
|
|
6
|
+
const chalk = require("chalk");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
const fs = require("fs-extra");
|
|
9
|
+
const child_process_1 = require("child_process");
|
|
10
|
+
const module_template_1 = require("../templates/module.template");
|
|
11
|
+
const controller_template_1 = require("../templates/controller.template");
|
|
12
|
+
const dto_template_1 = require("../templates/dto.template");
|
|
13
|
+
const auth_template_1 = require("../templates/auth.template");
|
|
14
|
+
const users_template_1 = require("../templates/users.template");
|
|
15
|
+
const generateAppAction = (appName) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
const questions = [
|
|
17
|
+
{
|
|
18
|
+
type: 'list',
|
|
19
|
+
name: 'database',
|
|
20
|
+
message: 'Which database would you like to use?',
|
|
21
|
+
choices: ['mongo', 'sql'],
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
25
|
+
// @ts-expect-error
|
|
26
|
+
const answers = yield inquirer.prompt(questions);
|
|
27
|
+
const database = answers['database'];
|
|
28
|
+
if (database === 'sql') {
|
|
29
|
+
console.error(chalk.red('Error: We are not supporting sql now'));
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
console.log(chalk.blue(`Generating NestJS app: ${appName}`));
|
|
33
|
+
const appDir = path.join(process.cwd(), appName);
|
|
34
|
+
const pkgManager = fs.existsSync(path.join(process.cwd(), 'yarn.lock')) ? 'yarn' : 'npm';
|
|
35
|
+
// 1. Run nest new
|
|
36
|
+
yield new Promise((resolve, reject) => {
|
|
37
|
+
const child = (0, child_process_1.spawn)('npx', ['@nestjs/cli', 'new', appName, '--package-manager', pkgManager], {
|
|
38
|
+
stdio: 'inherit',
|
|
39
|
+
shell: true,
|
|
40
|
+
});
|
|
41
|
+
child.on('close', (code) => {
|
|
42
|
+
if (code === 0)
|
|
43
|
+
resolve();
|
|
44
|
+
else
|
|
45
|
+
reject(new Error(`nest new failed with code ${code}`));
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
const pkg = require('../../package.json');
|
|
49
|
+
const nestExtendedVersion = pkg.version;
|
|
50
|
+
console.log(chalk.blue('Installing additional dependencies...'));
|
|
51
|
+
// 2. Install dependencies
|
|
52
|
+
yield new Promise((resolve, reject) => {
|
|
53
|
+
const installArgs = pkgManager === 'yarn' ? ['add'] : ['install'];
|
|
54
|
+
const child = (0, child_process_1.spawn)(pkgManager, [
|
|
55
|
+
...installArgs,
|
|
56
|
+
'@nestjs/mongoose',
|
|
57
|
+
'mongoose',
|
|
58
|
+
'@nestjs/config',
|
|
59
|
+
'nestjs-cls',
|
|
60
|
+
'@nestjs/jwt',
|
|
61
|
+
'bcrypt',
|
|
62
|
+
`@nest-extended/core@${nestExtendedVersion}`,
|
|
63
|
+
`@nest-extended/mongoose@${nestExtendedVersion}`,
|
|
64
|
+
`@nest-extended/decorators@${nestExtendedVersion}`,
|
|
65
|
+
'zod'
|
|
66
|
+
], {
|
|
67
|
+
stdio: 'inherit',
|
|
68
|
+
cwd: appDir,
|
|
69
|
+
shell: true,
|
|
70
|
+
});
|
|
71
|
+
child.on('close', (code) => {
|
|
72
|
+
if (code === 0)
|
|
73
|
+
resolve();
|
|
74
|
+
else
|
|
75
|
+
reject(new Error(`${pkgManager} install failed with code ${code}`));
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
// 3. Install Dev dependencies
|
|
79
|
+
yield new Promise((resolve, reject) => {
|
|
80
|
+
const devArgs = pkgManager === 'yarn' ? ['add', '-D'] : ['install', '-D'];
|
|
81
|
+
const child = (0, child_process_1.spawn)(pkgManager, [...devArgs, '@types/bcrypt'], {
|
|
82
|
+
stdio: 'inherit',
|
|
83
|
+
cwd: appDir,
|
|
84
|
+
shell: true,
|
|
85
|
+
});
|
|
86
|
+
child.on('close', (code) => {
|
|
87
|
+
if (code === 0)
|
|
88
|
+
resolve();
|
|
89
|
+
else
|
|
90
|
+
reject(new Error(`${pkgManager} dev install failed with code ${code}`));
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
console.log(chalk.blue('Configuring project...'));
|
|
94
|
+
// 4. Update app.module.ts
|
|
95
|
+
const appModulePath = path.join(appDir, 'src/app.module.ts');
|
|
96
|
+
let appModuleContent = fs.readFileSync(appModulePath, 'utf8');
|
|
97
|
+
const importsToAdd = `
|
|
98
|
+
import { ConfigModule } from '@nestjs/config';
|
|
99
|
+
import { ClsModule } from 'nestjs-cls';
|
|
100
|
+
import { NestExtendedModule } from '@nest-extended/core';
|
|
101
|
+
import { MongooseModule } from '@nestjs/mongoose';
|
|
102
|
+
import { AuthModule } from './services/auth/auth.module';
|
|
103
|
+
import { UsersModule } from './services/users/users.module';
|
|
104
|
+
`;
|
|
105
|
+
appModuleContent = importsToAdd + appModuleContent;
|
|
106
|
+
const nestImports = `
|
|
107
|
+
ConfigModule.forRoot({
|
|
108
|
+
envFilePath: ['.env'],
|
|
109
|
+
isGlobal: true,
|
|
110
|
+
}),
|
|
111
|
+
ClsModule.forRoot({
|
|
112
|
+
global: true,
|
|
113
|
+
middleware: { mount: true },
|
|
114
|
+
}),
|
|
115
|
+
NestExtendedModule.forRoot({
|
|
116
|
+
softDelete: {
|
|
117
|
+
getQuery: () => ({ deleted: { $ne: true } }),
|
|
118
|
+
getData: (user: { _id?: string } | null) => ({
|
|
119
|
+
deleted: true,
|
|
120
|
+
deletedBy: user?._id,
|
|
121
|
+
deletedAt: new Date(),
|
|
122
|
+
}),
|
|
123
|
+
},
|
|
124
|
+
}),
|
|
125
|
+
MongooseModule.forRoot(process.env.MONGODB_URI || 'mongodb://localhost:27017/test'),
|
|
126
|
+
AuthModule,
|
|
127
|
+
UsersModule,`;
|
|
128
|
+
appModuleContent = appModuleContent.replace(/imports:\s*\[/, `imports: [\n${nestImports}`);
|
|
129
|
+
fs.writeFileSync(appModulePath, appModuleContent);
|
|
130
|
+
// Write .env file
|
|
131
|
+
fs.writeFileSync(path.join(appDir, '.env'), 'MONGODB_URI=mongodb://localhost:27017/test\nJWT_SECRET=super-secret-jwt-key\n');
|
|
132
|
+
// 5. Generate Users Service
|
|
133
|
+
const usersDir = path.join(appDir, 'src/services/users');
|
|
134
|
+
const schemasDir = path.join(appDir, 'src/schemas');
|
|
135
|
+
fs.ensureDirSync(usersDir);
|
|
136
|
+
fs.ensureDirSync(schemasDir);
|
|
137
|
+
fs.writeFileSync(path.join(schemasDir, 'users.schema.ts'), (0, users_template_1.getUsersSchema)());
|
|
138
|
+
fs.writeFileSync(path.join(usersDir, 'users.module.ts'), (0, module_template_1.getModule)('Users', 'users'));
|
|
139
|
+
fs.writeFileSync(path.join(usersDir, 'users.service.ts'), (0, users_template_1.getUsersService)());
|
|
140
|
+
fs.writeFileSync(path.join(usersDir, 'users.controller.ts'), (0, controller_template_1.getController)('Users', 'users', 'users'));
|
|
141
|
+
fs.ensureDirSync(path.join(usersDir, 'dto'));
|
|
142
|
+
fs.writeFileSync(path.join(usersDir, 'dto/users.dto.ts'), (0, dto_template_1.getDto)('Users'));
|
|
143
|
+
// 6. Generate Auth Service
|
|
144
|
+
const authDir = path.join(appDir, 'src/services/auth');
|
|
145
|
+
fs.ensureDirSync(authDir);
|
|
146
|
+
fs.ensureDirSync(path.join(authDir, 'constants'));
|
|
147
|
+
fs.ensureDirSync(path.join(authDir, 'decorators'));
|
|
148
|
+
fs.writeFileSync(path.join(authDir, 'auth.module.ts'), (0, auth_template_1.getAuthModule)());
|
|
149
|
+
fs.writeFileSync(path.join(authDir, 'auth.service.ts'), (0, auth_template_1.getAuthService)());
|
|
150
|
+
fs.writeFileSync(path.join(authDir, 'auth.controller.ts'), (0, auth_template_1.getAuthController)());
|
|
151
|
+
fs.writeFileSync(path.join(authDir, 'auth.guard.ts'), (0, auth_template_1.getAuthGuard)());
|
|
152
|
+
fs.writeFileSync(path.join(authDir, 'constants/jwt-constants.ts'), (0, auth_template_1.getJwtConstants)());
|
|
153
|
+
fs.writeFileSync(path.join(authDir, 'decorators/public.decorator.ts'), (0, auth_template_1.getPublicDecorator)());
|
|
154
|
+
console.log(chalk.blue('Running lint...'));
|
|
155
|
+
yield new Promise((resolve) => {
|
|
156
|
+
const lintChild = (0, child_process_1.spawn)(pkgManager, ['run', 'lint'], {
|
|
157
|
+
stdio: 'inherit',
|
|
158
|
+
cwd: appDir,
|
|
159
|
+
shell: true,
|
|
160
|
+
});
|
|
161
|
+
lintChild.on('close', () => resolve());
|
|
162
|
+
});
|
|
163
|
+
console.log(chalk.green('App generated successfully!'));
|
|
164
|
+
});
|
|
165
|
+
exports.generateAppAction = generateAppAction;
|
|
166
|
+
//# sourceMappingURL=generate-app.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-app.js","sourceRoot":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AAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QAChC,MAAM,SAAS,GAAG,IAAA,qBAAK,EAAC,UAAU,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE;YACjD,KAAK,EAAE,SAAS;YAChB,GAAG,EAAE,MAAM;YACX,KAAK,EAAE,IAAI;SACd,CAAC,CAAC;QACH,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAC,CAAC;AAC5D,CAAC,CAAA,CAAC;AAvKW,QAAA,iBAAiB,qBAuK5B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const generateServiceAction: (rawName: string) => Promise<void>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateServiceAction = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const chalk = require("chalk");
|
|
6
|
+
const path = require("path");
|
|
7
|
+
const fs = require("fs-extra");
|
|
8
|
+
const child_process_1 = require("child_process");
|
|
9
|
+
const create_file_1 = require("../lib/create-file");
|
|
10
|
+
const update_app_module_1 = require("../lib/update-app-module");
|
|
11
|
+
const module_template_1 = require("../templates/module.template");
|
|
12
|
+
const service_template_1 = require("../templates/service.template");
|
|
13
|
+
const controller_template_1 = require("../templates/controller.template");
|
|
14
|
+
const dto_template_1 = require("../templates/dto.template");
|
|
15
|
+
const schema_template_1 = require("../templates/schema.template");
|
|
16
|
+
const service_spec_template_1 = require("../templates/service.spec.template");
|
|
17
|
+
const controller_spec_template_1 = require("../templates/controller.spec.template");
|
|
18
|
+
const generateServiceAction = (rawName) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
// if arg have '-' change to camelCase (Logic from original index.js)
|
|
20
|
+
const argArray = rawName.split('-');
|
|
21
|
+
argArray.forEach((arg, index) => {
|
|
22
|
+
argArray[index] = arg[0].toUpperCase() + arg.slice(1).toLowerCase();
|
|
23
|
+
});
|
|
24
|
+
const Name = argArray.join(''); // PascalCase
|
|
25
|
+
const name = Name[0].toLowerCase() + Name.slice(1); // camelCase
|
|
26
|
+
console.log(`Generating service for: ${Name} (${name})`);
|
|
27
|
+
(0, create_file_1.createFileWithContent)(`src/schemas/${name}.schema.ts`, (0, schema_template_1.getSchema)(Name));
|
|
28
|
+
(0, create_file_1.createFileWithContent)(`src/services/${name}/${name}.module.ts`, (0, module_template_1.getModule)(Name, name));
|
|
29
|
+
(0, create_file_1.createFileWithContent)(`src/services/${name}/${name}.service.ts`, (0, service_template_1.getService)(Name, name));
|
|
30
|
+
(0, create_file_1.createFileWithContent)(`src/services/${name}/${name}.controller.ts`, (0, controller_template_1.getController)(Name, name, rawName));
|
|
31
|
+
(0, create_file_1.createFileWithContent)(`src/services/${name}/dto/${name}.dto.ts`, (0, dto_template_1.getDto)(Name));
|
|
32
|
+
(0, create_file_1.createFileWithContent)(`src/services/${name}/${name}.service.spec.ts`, (0, service_spec_template_1.getServiceSpec)(Name, name));
|
|
33
|
+
(0, create_file_1.createFileWithContent)(`src/services/${name}/${name}.controller.spec.ts`, (0, controller_spec_template_1.getControllerSpec)(Name, name));
|
|
34
|
+
yield (0, update_app_module_1.updateAppModule)(Name, name);
|
|
35
|
+
console.log(chalk.blue('Running lint...'));
|
|
36
|
+
const projectDir = process.cwd();
|
|
37
|
+
const pkgManager = fs.existsSync(path.join(projectDir, 'yarn.lock')) ? 'yarn' : 'npm';
|
|
38
|
+
yield new Promise((resolve) => {
|
|
39
|
+
const lintChild = (0, child_process_1.spawn)(pkgManager, ['run', 'lint'], {
|
|
40
|
+
stdio: 'inherit',
|
|
41
|
+
cwd: projectDir,
|
|
42
|
+
shell: true,
|
|
43
|
+
});
|
|
44
|
+
lintChild.on('close', () => resolve());
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
exports.generateServiceAction = generateServiceAction;
|
|
48
|
+
//# sourceMappingURL=generate-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-service.js","sourceRoot":"","sources":["../../../../../packages/cli/src/commands/generate-service.ts"],"names":[],"mappings":";;;;AAAA,+BAA+B;AAC/B,6BAA6B;AAC7B,+BAA+B;AAC/B,iDAAsC;AACtC,oDAA2D;AAC3D,gEAA2D;AAC3D,kEAAyD;AACzD,oEAA2D;AAC3D,0EAAiE;AACjE,4DAAmD;AACnD,kEAAyD;AACzD,8EAAoE;AACpE,oFAA0E;AAEnE,MAAM,qBAAqB,GAAG,CAAO,OAAe,EAAE,EAAE;IAC3D,qEAAqE;IACrE,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACpC,QAAQ,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE;QAC5B,QAAQ,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;IACxE,CAAC,CAAC,CAAC;IACH,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,aAAa;IAC7C,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY;IAEhE,OAAO,CAAC,GAAG,CAAC,2BAA2B,IAAI,KAAK,IAAI,GAAG,CAAC,CAAC;IAEzD,IAAA,mCAAqB,EAAC,eAAe,IAAI,YAAY,EAAE,IAAA,2BAAS,EAAC,IAAI,CAAC,CAAC,CAAC;IACxE,IAAA,mCAAqB,EAAC,gBAAgB,IAAI,IAAI,IAAI,Y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KAAK,EAAE,IAAI;SACd,CAAC,CAAC;QACH,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;AACP,CAAC,CAAA,CAAC;AAzCW,QAAA,qBAAqB,yBAyChC"}
|
package/src/commands/generate.js
CHANGED
|
@@ -1,207 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateCommand = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
4
|
const commander_1 = require("commander");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const path = require("path");
|
|
9
|
-
const fs = require("fs-extra");
|
|
10
|
-
const child_process_1 = require("child_process");
|
|
11
|
-
const create_file_1 = require("../lib/create-file");
|
|
12
|
-
const update_app_module_1 = require("../lib/update-app-module");
|
|
13
|
-
const module_template_1 = require("../templates/module.template");
|
|
14
|
-
const service_template_1 = require("../templates/service.template");
|
|
15
|
-
const controller_template_1 = require("../templates/controller.template");
|
|
16
|
-
const dto_template_1 = require("../templates/dto.template");
|
|
17
|
-
const schema_template_1 = require("../templates/schema.template");
|
|
18
|
-
const service_spec_template_1 = require("../templates/service.spec.template");
|
|
19
|
-
const controller_spec_template_1 = require("../templates/controller.spec.template");
|
|
20
|
-
const auth_template_1 = require("../templates/auth.template");
|
|
21
|
-
const users_template_1 = require("../templates/users.template");
|
|
5
|
+
const generate_service_1 = require("./generate-service");
|
|
6
|
+
const generate_app_1 = require("./generate-app");
|
|
22
7
|
exports.generateCommand = new commander_1.Command('generate')
|
|
23
8
|
.alias('g')
|
|
24
9
|
.description('Generate a new element');
|
|
25
10
|
exports.generateCommand
|
|
26
11
|
.command('service <name>')
|
|
27
12
|
.description('Generate a new service')
|
|
28
|
-
.action(
|
|
29
|
-
// if arg have '-' change to camelCase (Logic from original index.js)
|
|
30
|
-
const argArray = rawName.split('-');
|
|
31
|
-
argArray.forEach((arg, index) => {
|
|
32
|
-
argArray[index] = arg[0].toUpperCase() + arg.slice(1).toLowerCase();
|
|
33
|
-
});
|
|
34
|
-
const Name = argArray.join(''); // PascalCase
|
|
35
|
-
const name = Name[0].toLowerCase() + Name.slice(1); // camelCase
|
|
36
|
-
console.log(`Generating service for: ${Name} (${name})`);
|
|
37
|
-
(0, create_file_1.createFileWithContent)(`src/schemas/${name}.schema.ts`, (0, schema_template_1.getSchema)(Name));
|
|
38
|
-
(0, create_file_1.createFileWithContent)(`src/services/${name}/${name}.module.ts`, (0, module_template_1.getModule)(Name, name));
|
|
39
|
-
(0, create_file_1.createFileWithContent)(`src/services/${name}/${name}.service.ts`, (0, service_template_1.getService)(Name, name));
|
|
40
|
-
(0, create_file_1.createFileWithContent)(`src/services/${name}/${name}.controller.ts`, (0, controller_template_1.getController)(Name, name, rawName));
|
|
41
|
-
(0, create_file_1.createFileWithContent)(`src/services/${name}/dto/${name}.dto.ts`, (0, dto_template_1.getDto)(Name));
|
|
42
|
-
(0, create_file_1.createFileWithContent)(`src/services/${name}/${name}.service.spec.ts`, (0, service_spec_template_1.getServiceSpec)(Name, name));
|
|
43
|
-
(0, create_file_1.createFileWithContent)(`src/services/${name}/${name}.controller.spec.ts`, (0, controller_spec_template_1.getControllerSpec)(Name, name));
|
|
44
|
-
yield (0, update_app_module_1.updateAppModule)(Name, name);
|
|
45
|
-
console.log(chalk.blue('Running lint...'));
|
|
46
|
-
const projectDir = process.cwd();
|
|
47
|
-
const pkgManager = fs.existsSync(path.join(projectDir, 'yarn.lock')) ? 'yarn' : 'npm';
|
|
48
|
-
yield new Promise((resolve) => {
|
|
49
|
-
const lintChild = (0, child_process_1.spawn)(pkgManager, ['run', 'lint'], {
|
|
50
|
-
stdio: 'inherit',
|
|
51
|
-
cwd: projectDir,
|
|
52
|
-
shell: true,
|
|
53
|
-
});
|
|
54
|
-
lintChild.on('close', () => resolve());
|
|
55
|
-
});
|
|
56
|
-
}));
|
|
13
|
+
.action(generate_service_1.generateServiceAction);
|
|
57
14
|
exports.generateCommand
|
|
58
15
|
.command('app <name>')
|
|
59
16
|
.description('Generate a new application')
|
|
60
|
-
.action(
|
|
61
|
-
const questions = [
|
|
62
|
-
{
|
|
63
|
-
type: 'list',
|
|
64
|
-
name: 'database',
|
|
65
|
-
message: 'Which database would you like to use?',
|
|
66
|
-
choices: ['mongo', 'sql'],
|
|
67
|
-
},
|
|
68
|
-
];
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
const answers = yield inquirer.prompt(questions);
|
|
71
|
-
const database = answers['database'];
|
|
72
|
-
if (database === 'sql') {
|
|
73
|
-
console.error(chalk.red('Error: We are not supporting sql now'));
|
|
74
|
-
process.exit(1);
|
|
75
|
-
}
|
|
76
|
-
console.log(chalk.blue(`Generating NestJS app: ${appName}`));
|
|
77
|
-
// 1. Run nest new
|
|
78
|
-
yield new Promise((resolve, reject) => {
|
|
79
|
-
const child = (0, child_process_1.spawn)('npx', ['@nestjs/cli', 'new', appName], {
|
|
80
|
-
stdio: 'inherit',
|
|
81
|
-
shell: true,
|
|
82
|
-
});
|
|
83
|
-
child.on('close', (code) => {
|
|
84
|
-
if (code === 0)
|
|
85
|
-
resolve();
|
|
86
|
-
else
|
|
87
|
-
reject(new Error(`nest new failed with code ${code}`));
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
const appDir = path.join(process.cwd(), appName);
|
|
91
|
-
const pkgManager = fs.existsSync(path.join(appDir, 'yarn.lock')) ? 'yarn' : 'npm';
|
|
92
|
-
const pkg = require('../../package.json');
|
|
93
|
-
const nestExtendedVersion = pkg.version;
|
|
94
|
-
console.log(chalk.blue('Installing additional dependencies...'));
|
|
95
|
-
// 2. Install dependencies
|
|
96
|
-
yield new Promise((resolve, reject) => {
|
|
97
|
-
const installArgs = pkgManager === 'yarn' ? ['add'] : ['install'];
|
|
98
|
-
const child = (0, child_process_1.spawn)(pkgManager, [
|
|
99
|
-
...installArgs,
|
|
100
|
-
'@nestjs/mongoose',
|
|
101
|
-
'mongoose',
|
|
102
|
-
'@nestjs/config',
|
|
103
|
-
'nestjs-cls',
|
|
104
|
-
'@nestjs/jwt',
|
|
105
|
-
'bcrypt',
|
|
106
|
-
`@nest-extended/core@${nestExtendedVersion}`,
|
|
107
|
-
`@nest-extended/mongoose@${nestExtendedVersion}`,
|
|
108
|
-
`@nest-extended/decorators@${nestExtendedVersion}`,
|
|
109
|
-
'zod'
|
|
110
|
-
], {
|
|
111
|
-
stdio: 'inherit',
|
|
112
|
-
cwd: appDir,
|
|
113
|
-
shell: true,
|
|
114
|
-
});
|
|
115
|
-
child.on('close', (code) => {
|
|
116
|
-
if (code === 0)
|
|
117
|
-
resolve();
|
|
118
|
-
else
|
|
119
|
-
reject(new Error(`${pkgManager} install failed with code ${code}`));
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
// 3. Install Dev dependencies
|
|
123
|
-
yield new Promise((resolve, reject) => {
|
|
124
|
-
const devArgs = pkgManager === 'yarn' ? ['add', '-D'] : ['install', '-D'];
|
|
125
|
-
const child = (0, child_process_1.spawn)(pkgManager, [...devArgs, '@types/bcrypt'], {
|
|
126
|
-
stdio: 'inherit',
|
|
127
|
-
cwd: appDir,
|
|
128
|
-
shell: true,
|
|
129
|
-
});
|
|
130
|
-
child.on('close', (code) => {
|
|
131
|
-
if (code === 0)
|
|
132
|
-
resolve();
|
|
133
|
-
else
|
|
134
|
-
reject(new Error(`${pkgManager} dev install failed with code ${code}`));
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
console.log(chalk.blue('Configuring project...'));
|
|
138
|
-
// 4. Update app.module.ts
|
|
139
|
-
const appModulePath = path.join(appDir, 'src/app.module.ts');
|
|
140
|
-
let appModuleContent = fs.readFileSync(appModulePath, 'utf8');
|
|
141
|
-
const importsToAdd = `
|
|
142
|
-
import { ConfigModule } from '@nestjs/config';
|
|
143
|
-
import { ClsModule } from 'nestjs-cls';
|
|
144
|
-
import { NestExtendedModule } from '@nest-extended/core';
|
|
145
|
-
import { MongooseModule } from '@nestjs/mongoose';
|
|
146
|
-
import { AuthModule } from './services/auth/auth.module';
|
|
147
|
-
import { UsersModule } from './services/users/users.module';
|
|
148
|
-
`;
|
|
149
|
-
appModuleContent = importsToAdd + appModuleContent;
|
|
150
|
-
const nestImports = `
|
|
151
|
-
ConfigModule.forRoot({
|
|
152
|
-
envFilePath: ['.env'],
|
|
153
|
-
isGlobal: true,
|
|
154
|
-
}),
|
|
155
|
-
ClsModule.forRoot({
|
|
156
|
-
global: true,
|
|
157
|
-
middleware: { mount: true },
|
|
158
|
-
}),
|
|
159
|
-
NestExtendedModule.forRoot({
|
|
160
|
-
softDelete: {
|
|
161
|
-
getQuery: () => ({ deleted: { $ne: true } }),
|
|
162
|
-
getData: (user: any) => ({
|
|
163
|
-
deleted: true,
|
|
164
|
-
deletedBy: user?._id,
|
|
165
|
-
deletedAt: new Date(),
|
|
166
|
-
}),
|
|
167
|
-
},
|
|
168
|
-
}),
|
|
169
|
-
MongooseModule.forRoot(process.env.MONGODB_URI || 'mongodb://localhost:27017/test'),
|
|
170
|
-
AuthModule,
|
|
171
|
-
UsersModule,`;
|
|
172
|
-
appModuleContent = appModuleContent.replace(/imports:\s*\[/, `imports: [\n${nestImports}`);
|
|
173
|
-
fs.writeFileSync(appModulePath, appModuleContent);
|
|
174
|
-
// 5. Generate Users Service
|
|
175
|
-
const usersDir = path.join(appDir, 'src/services/users');
|
|
176
|
-
const schemasDir = path.join(appDir, 'src/schemas');
|
|
177
|
-
fs.ensureDirSync(usersDir);
|
|
178
|
-
fs.ensureDirSync(schemasDir);
|
|
179
|
-
fs.writeFileSync(path.join(schemasDir, 'users.schema.ts'), (0, users_template_1.getUsersSchema)());
|
|
180
|
-
fs.writeFileSync(path.join(usersDir, 'users.module.ts'), (0, module_template_1.getModule)('Users', 'users'));
|
|
181
|
-
fs.writeFileSync(path.join(usersDir, 'users.service.ts'), (0, users_template_1.getUsersService)());
|
|
182
|
-
fs.writeFileSync(path.join(usersDir, 'users.controller.ts'), (0, controller_template_1.getController)('Users', 'users', 'users'));
|
|
183
|
-
fs.ensureDirSync(path.join(usersDir, 'dto'));
|
|
184
|
-
fs.writeFileSync(path.join(usersDir, 'dto/users.dto.ts'), (0, dto_template_1.getDto)('Users'));
|
|
185
|
-
// 6. Generate Auth Service
|
|
186
|
-
const authDir = path.join(appDir, 'src/services/auth');
|
|
187
|
-
fs.ensureDirSync(authDir);
|
|
188
|
-
fs.ensureDirSync(path.join(authDir, 'constants'));
|
|
189
|
-
fs.ensureDirSync(path.join(authDir, 'decorators'));
|
|
190
|
-
fs.writeFileSync(path.join(authDir, 'auth.module.ts'), (0, auth_template_1.getAuthModule)());
|
|
191
|
-
fs.writeFileSync(path.join(authDir, 'auth.service.ts'), (0, auth_template_1.getAuthService)());
|
|
192
|
-
fs.writeFileSync(path.join(authDir, 'auth.controller.ts'), (0, auth_template_1.getAuthController)());
|
|
193
|
-
fs.writeFileSync(path.join(authDir, 'auth.guard.ts'), (0, auth_template_1.getAuthGuard)());
|
|
194
|
-
fs.writeFileSync(path.join(authDir, 'constants/jwt-constants.ts'), (0, auth_template_1.getJwtConstants)());
|
|
195
|
-
fs.writeFileSync(path.join(authDir, 'decorators/public.decorator.ts'), (0, auth_template_1.getPublicDecorator)());
|
|
196
|
-
console.log(chalk.blue('Running lint...'));
|
|
197
|
-
yield new Promise((resolve) => {
|
|
198
|
-
const lintChild = (0, child_process_1.spawn)(pkgManager, ['run', 'lint'], {
|
|
199
|
-
stdio: 'inherit',
|
|
200
|
-
cwd: appDir,
|
|
201
|
-
shell: true,
|
|
202
|
-
});
|
|
203
|
-
lintChild.on('close', () => resolve());
|
|
204
|
-
});
|
|
205
|
-
console.log(chalk.green('App generated successfully!'));
|
|
206
|
-
}));
|
|
17
|
+
.action(generate_app_1.generateAppAction);
|
|
207
18
|
//# sourceMappingURL=generate.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate.js","sourceRoot":"","sources":["../../../../../packages/cli/src/commands/generate.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"generate.js","sourceRoot":"","sources":["../../../../../packages/cli/src/commands/generate.ts"],"names":[],"mappings":";;;AACA,yCAAoC;AACpC,yDAA2D;AAC3D,iDAAmD;AAEtC,QAAA,eAAe,GAAG,IAAI,mBAAO,CAAC,UAAU,CAAC;KACjD,KAAK,CAAC,GAAG,CAAC;KACV,WAAW,CAAC,wBAAwB,CAAC,CAAC;AAE3C,uBAAe;KACV,OAAO,CAAC,gBAAgB,CAAC;KACzB,WAAW,CAAC,wBAAwB,CAAC;KACrC,MAAM,CAAC,wCAAqB,CAAC,CAAC;AAEnC,uBAAe;KACV,OAAO,CAAC,YAAY,CAAC;KACrB,WAAW,CAAC,4BAA4B,CAAC;KACzC,MAAM,CAAC,gCAAiB,CAAC,CAAC"}
|
|
@@ -14,6 +14,7 @@ const getAuthController = () => `import {
|
|
|
14
14
|
import { AuthService } from './auth.service';
|
|
15
15
|
import { Public } from './decorators/public.decorator';
|
|
16
16
|
import { UsersService } from '../users/users.service';
|
|
17
|
+
import { UsersDocument } from '../../schemas/users.schema';
|
|
17
18
|
|
|
18
19
|
@Controller('authentication')
|
|
19
20
|
export class AuthController {
|
|
@@ -25,7 +26,7 @@ export class AuthController {
|
|
|
25
26
|
@Public()
|
|
26
27
|
@HttpCode(HttpStatus.OK)
|
|
27
28
|
@Post('')
|
|
28
|
-
signIn(@Body() signInDto: Record<string,
|
|
29
|
+
signIn(@Body() signInDto: Record<string, string>) {
|
|
29
30
|
if (signInDto.strategy === 'local') {
|
|
30
31
|
return this.authService.signInLocal(signInDto.email, signInDto.password);
|
|
31
32
|
}
|
|
@@ -33,10 +34,10 @@ export class AuthController {
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
@Get('verify')
|
|
36
|
-
getProfile(@Request() req: any) {
|
|
37
|
+
getProfile(@Request() req: Record<string, any>) {
|
|
38
|
+
const user = req.user as UsersDocument;
|
|
37
39
|
return {
|
|
38
|
-
user: this.usersService.sanitizeUser(
|
|
39
|
-
organizationUsers: req.orgUsers,
|
|
40
|
+
user: this.usersService.sanitizeUser(user),
|
|
40
41
|
};
|
|
41
42
|
}
|
|
42
43
|
}
|
|
@@ -54,20 +55,26 @@ export class AuthService {
|
|
|
54
55
|
private jwtService: JwtService,
|
|
55
56
|
) { }
|
|
56
57
|
|
|
57
|
-
async signInLocal(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
58
|
+
async signInLocal(
|
|
59
|
+
email: string,
|
|
60
|
+
pass: string,
|
|
61
|
+
): Promise<Record<string, unknown>> {
|
|
62
|
+
const [user] = await this.usersService._find(
|
|
63
|
+
{
|
|
64
|
+
email,
|
|
65
|
+
$limit: 1,
|
|
66
|
+
$select: [
|
|
67
|
+
'_id',
|
|
68
|
+
'firstName',
|
|
69
|
+
'lastName',
|
|
70
|
+
'email',
|
|
71
|
+
'password',
|
|
72
|
+
'createdAt',
|
|
73
|
+
'updatedAt',
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
{ pagination: false },
|
|
77
|
+
);
|
|
71
78
|
|
|
72
79
|
if (!user) throw new UnauthorizedException();
|
|
73
80
|
|
|
@@ -77,7 +84,7 @@ export class AuthService {
|
|
|
77
84
|
}
|
|
78
85
|
|
|
79
86
|
const sanitizedUser = this.usersService.sanitizeUser(user);
|
|
80
|
-
const payload = { sub: { id: user._id }, user };
|
|
87
|
+
const payload = { sub: { id: user._id as unknown as string }, user };
|
|
81
88
|
return {
|
|
82
89
|
accessToken: await this.jwtService.signAsync(payload),
|
|
83
90
|
user: sanitizedUser,
|
|
@@ -125,7 +132,6 @@ const getAuthGuard = () => `import {
|
|
|
125
132
|
} from '@nestjs/common';
|
|
126
133
|
import { Reflector } from '@nestjs/core';
|
|
127
134
|
import { JwtService } from '@nestjs/jwt';
|
|
128
|
-
import { Request } from 'express';
|
|
129
135
|
import { IS_PUBLIC_KEY } from './decorators/public.decorator';
|
|
130
136
|
import { UsersService } from '../users/users.service';
|
|
131
137
|
import { jwtConstants } from './constants/jwt-constants';
|
|
@@ -148,18 +154,20 @@ export class AuthGuard implements CanActivate {
|
|
|
148
154
|
if (isPublic) {
|
|
149
155
|
return true;
|
|
150
156
|
}
|
|
151
|
-
const request = context.switchToHttp().getRequest();
|
|
152
|
-
const token = this.extractTokenFromHeader(request
|
|
157
|
+
const request = context.switchToHttp().getRequest<{ user?: Record<string, unknown>, headers: Record<string, string | undefined> }>();
|
|
158
|
+
const token = this.extractTokenFromHeader(request);
|
|
153
159
|
if (!token) {
|
|
154
160
|
throw new UnauthorizedException();
|
|
155
161
|
}
|
|
156
162
|
try {
|
|
157
|
-
const payload = await this.jwtService.verifyAsync(token, {
|
|
163
|
+
const payload = await this.jwtService.verifyAsync<{ sub: { id: string } }>(token, {
|
|
158
164
|
secret: jwtConstants.secret,
|
|
159
165
|
});
|
|
160
|
-
|
|
166
|
+
const user = (await this.usersService._get(
|
|
167
|
+
payload.sub.id,
|
|
168
|
+
)) as unknown as Record<string, unknown>;
|
|
161
169
|
if (user) {
|
|
162
|
-
request
|
|
170
|
+
request.user = user;
|
|
163
171
|
this.cls.set('user', user);
|
|
164
172
|
return true;
|
|
165
173
|
}
|
|
@@ -169,7 +177,7 @@ export class AuthGuard implements CanActivate {
|
|
|
169
177
|
throw new UnauthorizedException();
|
|
170
178
|
}
|
|
171
179
|
|
|
172
|
-
private extractTokenFromHeader(request:
|
|
180
|
+
private extractTokenFromHeader(request: { headers: Record<string, string | undefined> }): string | undefined {
|
|
173
181
|
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
|
174
182
|
return type === 'Bearer' ? token : undefined;
|
|
175
183
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.template.js","sourceRoot":"","sources":["../../../../../packages/cli/src/templates/auth.template.ts"],"names":[],"mappings":";;;AAAO,MAAM,iBAAiB,GAAG,GAAW,EAAE,CAAC
|
|
1
|
+
{"version":3,"file":"auth.template.js","sourceRoot":"","sources":["../../../../../packages/cli/src/templates/auth.template.ts"],"names":[],"mappings":";;;AAAO,MAAM,iBAAiB,GAAG,GAAW,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwC9C,CAAC;AAxCW,QAAA,iBAAiB,qBAwC5B;AAEK,MAAM,cAAc,GAAG,GAAW,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgD3C,CAAC;AAhDW,QAAA,cAAc,kBAgDzB;AAEK,MAAM,aAAa,GAAG,GAAW,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6B1C,CAAC;AA7BW,QAAA,aAAa,iBA6BxB;AAEK,MAAM,YAAY,GAAG,GAAW,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0DzC,CAAC;AA1DW,QAAA,YAAY,gBA0DvB;AAEK,MAAM,kBAAkB,GAAG,GAAW,EAAE,CAAC;;;CAG/C,CAAC;AAHW,QAAA,kBAAkB,sBAG7B;AAEK,MAAM,eAAe,GAAG,GAAW,EAAE,CAAC;;;;;CAK5C,CAAC;AALW,QAAA,eAAe,mBAK1B"}
|
|
@@ -13,7 +13,7 @@ const getController = (Name, name, url) => `import {
|
|
|
13
13
|
} from '@nestjs/common';
|
|
14
14
|
import { ${Name}Service } from './${name}.service';
|
|
15
15
|
import { User, ModifyBody, setCreatedBy } from '@nest-extended/decorators';
|
|
16
|
-
import { ${Name} } from '
|
|
16
|
+
import { ${Name} } from '../../schemas/${name}.schema';
|
|
17
17
|
|
|
18
18
|
@Controller('${url}')
|
|
19
19
|
export class ${Name}Controller {
|
|
@@ -38,15 +38,15 @@ export class ${Name}Controller {
|
|
|
38
38
|
|
|
39
39
|
@Patch('/:id')
|
|
40
40
|
async patch(
|
|
41
|
-
@Query() query,
|
|
41
|
+
@Query() query: Record<string, any>,
|
|
42
42
|
@Body() patch${Name}Dto: Partial<${Name}>,
|
|
43
|
-
@Param('id') id,
|
|
43
|
+
@Param('id') id: string,
|
|
44
44
|
) {
|
|
45
45
|
return await this.${name}Service._patch(id, patch${Name}Dto, query);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
@Delete('/:id')
|
|
49
|
-
async delete(@Param('id') id, @Query() query, @User() user) {
|
|
49
|
+
async delete(@Param('id') id: string, @Query() query: Record<string, any>, @User() user: Record<string, unknown>) {
|
|
50
50
|
return await this.${name}Service._remove(id, query, user);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.template.js","sourceRoot":"","sources":["../../../../../packages/cli/src/templates/controller.template.ts"],"names":[],"mappings":";;;AACO,MAAM,aAAa,GAAG,CAAC,IAAY,EAAE,IAAY,EAAE,GAAW,EAAU,EAAE,CAAC;;;;;;;;;;WAUvE,IAAI,qBAAqB,IAAI;;WAE7B,IAAI,
|
|
1
|
+
{"version":3,"file":"controller.template.js","sourceRoot":"","sources":["../../../../../packages/cli/src/templates/controller.template.ts"],"names":[],"mappings":";;;AACO,MAAM,aAAa,GAAG,CAAC,IAAY,EAAE,IAAY,EAAE,GAAW,EAAU,EAAE,CAAC;;;;;;;;;;WAUvE,IAAI,qBAAqB,IAAI;;WAE7B,IAAI,0BAA0B,IAAI;;eAE9B,GAAG;eACH,IAAI;iCACc,IAAI,YAAY,IAAI;;;;wBAI7B,IAAI;;;;;wBAKJ,IAAI;;;;;wCAKY,IAAI,QAAQ,IAAI;;wBAEhC,IAAI,yBAAyB,IAAI;;;;;;mBAMtC,IAAI,gBAAgB,IAAI;;;wBAGnB,IAAI,2BAA2B,IAAI;;;;;wBAKnC,IAAI;;;CAG3B,CAAC;AAjDW,QAAA,aAAa,iBAiDxB"}
|
|
@@ -5,7 +5,7 @@ const getModule = (Name, name) => `import { Module } from '@nestjs/common';
|
|
|
5
5
|
import { MongooseModule } from '@nestjs/mongoose';
|
|
6
6
|
import { ${Name}Controller } from './${name}.controller';
|
|
7
7
|
import { ${Name}Service } from './${name}.service';
|
|
8
|
-
import { ${Name}, ${Name}Schema } from '
|
|
8
|
+
import { ${Name}, ${Name}Schema } from '../../schemas/${name}.schema';
|
|
9
9
|
|
|
10
10
|
@Module({
|
|
11
11
|
imports: [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.template.js","sourceRoot":"","sources":["../../../../../packages/cli/src/templates/module.template.ts"],"names":[],"mappings":";;;AACO,MAAM,SAAS,GAAG,CAAC,IAAY,EAAE,IAAY,EAAU,EAAE,CAAC;;WAEtD,IAAI,wBAAwB,IAAI;WAChC,IAAI,qBAAqB,IAAI;WAC7B,IAAI,KAAK,IAAI,
|
|
1
|
+
{"version":3,"file":"module.template.js","sourceRoot":"","sources":["../../../../../packages/cli/src/templates/module.template.ts"],"names":[],"mappings":";;;AACO,MAAM,SAAS,GAAG,CAAC,IAAY,EAAE,IAAY,EAAU,EAAE,CAAC;;WAEtD,IAAI,wBAAwB,IAAI;WAChC,IAAI,qBAAqB,IAAI;WAC7B,IAAI,KAAK,IAAI,gCAAgC,IAAI;;;;yCAInB,IAAI,kBAAkB,IAAI;;kBAEjD,IAAI;;MAEhB,IAAI;;cAEI,IAAI;;eAEH,IAAI;CAClB,CAAC;AAjBW,QAAA,SAAS,aAiBpB"}
|
|
@@ -38,7 +38,7 @@ const getUsersService = () => `import { Model } from 'mongoose';
|
|
|
38
38
|
import { Injectable } from '@nestjs/common';
|
|
39
39
|
import { InjectModel } from '@nestjs/mongoose';
|
|
40
40
|
import { NestService } from '@nest-extended/mongoose';
|
|
41
|
-
import { Users, UsersDocument } from '
|
|
41
|
+
import { Users, UsersDocument } from '../../schemas/users.schema';
|
|
42
42
|
|
|
43
43
|
@Injectable()
|
|
44
44
|
export class UsersService extends NestService<Users, UsersDocument> {
|
|
@@ -48,9 +48,8 @@ export class UsersService extends NestService<Users, UsersDocument> {
|
|
|
48
48
|
super(usersModel);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
sanitizeUser(user:
|
|
52
|
-
|
|
53
|
-
const sanitized = user.toObject ? user.toObject() : { ...user };
|
|
51
|
+
sanitizeUser(user: UsersDocument) {
|
|
52
|
+
const sanitized = user.toObject() as unknown as Record<string, unknown>
|
|
54
53
|
delete sanitized['password'];
|
|
55
54
|
return sanitized;
|
|
56
55
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"users.template.js","sourceRoot":"","sources":["../../../../../packages/cli/src/templates/users.template.ts"],"names":[],"mappings":";;;AAAO,MAAM,cAAc,GAAG,GAAW,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+B3C,CAAC;AA/BW,QAAA,cAAc,kBA+BzB;AAEK,MAAM,eAAe,GAAG,GAAW,EAAE,CAAC
|
|
1
|
+
{"version":3,"file":"users.template.js","sourceRoot":"","sources":["../../../../../packages/cli/src/templates/users.template.ts"],"names":[],"mappings":";;;AAAO,MAAM,cAAc,GAAG,GAAW,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+B3C,CAAC;AA/BW,QAAA,cAAc,kBA+BzB;AAEK,MAAM,eAAe,GAAG,GAAW,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;CAoB5C,CAAC;AApBW,QAAA,eAAe,mBAoB1B"}
|