Skip to content

Commit 7f3628f

Browse files
committed
feat: added classTransformOptions option to auth
1 parent f6a754f commit 7f3628f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

packages/crud/src/crud/crud-routes.factory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export class CrudRoutesFactory {
8383
if (isUndefined(this.options.auth.groups)) {
8484
this.options.auth.groups = CrudConfigService.config.auth.groups;
8585
}
86+
if (isUndefined(this.options.auth.classTransformOptions)) {
87+
this.options.auth.classTransformOptions = CrudConfigService.config.auth.classTransformOptions;
88+
}
8689

8790
// merge query config
8891
const query = isObjectFull(this.options.query) ? this.options.query : {};

packages/crud/src/interceptors/crud-response.interceptor.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ export class CrudResponseInterceptor extends CrudBaseInterceptor implements Nest
5050

5151
const options: ClassTransformOptions = {};
5252
/* istanbul ignore else */
53-
if (isFunction(crudOptions.auth?.groups)) {
53+
if (isFunction(crudOptions.auth?.classTransformOptions)) {
5454
const userOrRequest = crudOptions.auth.property ? req[crudOptions.auth.property] : req;
55+
Object.assign(options, crudOptions.auth.classTransformOptions(userOrRequest));
56+
}
5557

58+
/* istanbul ignore else */
59+
if (isFunction(crudOptions.auth?.groups)) {
60+
const userOrRequest = crudOptions.auth.property ? req[crudOptions.auth.property] : req;
5661
options.groups = crudOptions.auth.groups(userOrRequest);
5762
}
5863

packages/crud/src/interfaces/auth-options.interface.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { SCondition } from '@nestjsx/crud-request/lib/types/request-query.types';
22
import { ObjectLiteral } from '@nestjsx/util';
3+
import { ClassTransformOptions } from 'class-transformer';
34

45
export interface AuthGlobalOptions {
56
property?: string;
7+
/** Get options for the `classToPlain` function (response) */
8+
classTransformOptions?: (req: any) => ClassTransformOptions;
9+
/** Get `groups` value for the `classToPlain` function options (response) */
610
groups?: (req: any) => string[];
711
}
812

913
export interface AuthOptions {
1014
property?: string;
15+
/** Get options for the `classToPlain` function (response) */
16+
classTransformOptions?: (req: any) => ClassTransformOptions;
1117
/** Get `groups` value for the `classToPlain` function options (response) */
1218
groups?: (req: any) => string[];
1319
filter?: (req: any) => SCondition | void;

0 commit comments

Comments
 (0)