Skip to content

Commit b990c56

Browse files
authored
fix(json-api-nestjs): Route name override
2 parents 385933a + 0ca2a60 commit b990c56

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

libs/json-api-nestjs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import {ExampleService} from '../../service/example/example.service';
8383
@JsonApi(Users, {
8484
allowMethod: excludeMethod(['deleteRelationship']),
8585
requiredSelectField: true,
86+
overrideRoute: 'user',
8687
})
8788
export class ExtendUserController extends JsonBaseController<Users> {
8889
@InjectService() public service: JsonApiService<Users>;

libs/json-api-nestjs/src/lib/decorators/json-api/json-api.decorator.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,18 @@ describe('InjectServiceDecorator', () => {
5151
Object.keys(Bindings).filter((k) => !example.includes(k))
5252
);
5353
});
54+
55+
it('should save options in class and correctly set overrideRoute', () => {
56+
const testedEntity = class SomeEntity {};
57+
const apiOptions: DecoratorOptions = {
58+
allowMethod: ['getAll', 'deleteRelationship'],
59+
overrideRoute: '123'
60+
};
61+
62+
@JsonApi(testedEntity, apiOptions)
63+
class SomeClass {}
64+
65+
const data = Reflect.getMetadata(JSON_API_DECORATOR_OPTIONS, SomeClass);
66+
expect(data).toEqual(apiOptions);
67+
});
5468
});

libs/json-api-nestjs/src/lib/helper/swagger/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export function setSwaggerDecorator(
2323
if (!apiTag) {
2424
const entityName =
2525
entity instanceof Function ? entity.name : entity.options.name;
26-
ApiTags(camelToKebab(entityName))(controller);
26+
27+
ApiTags(config?.['overrideRoute'] || `${camelToKebab(entityName)}`)(
28+
controller
29+
);
2730
}
2831
ApiExtraModels(FilterOperand)(controller);
2932
ApiExtraModels(createApiModels(entity))(controller);

libs/json-api-nestjs/src/lib/mixin/module/module.mixin.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
getProviderName,
1919
camelToKebab,
2020
} from '../../helper';
21-
import { JsonBaseController } from '../controller';
2221
import { typeormMixin, transformMixin } from '../';
2322
import {
2423
JSON_API_DECORATOR_OPTIONS,
@@ -46,9 +45,17 @@ BaseModuleClass.forRoot = function (options): DynamicModule {
4645
const controllerClass =
4746
controller ||
4847
nameIt(getProviderName(entity, JSON_API_CONTROLLER_POSTFIX), class {});
48+
49+
const decoratorOptions: DecoratorOptions = Reflect.getMetadata(
50+
JSON_API_DECORATOR_OPTIONS,
51+
controllerClass
52+
);
53+
4954
const transformService = transformMixin(entity, connectionName);
5055
const serviceClass = typeormMixin(entity, connectionName, transformService);
51-
Controller(`${camelToKebab(entityName)}`)(controllerClass);
56+
Controller(decoratorOptions?.['overrideRoute'] || `${camelToKebab(entityName)}`)(
57+
controllerClass
58+
);
5259
UseInterceptors(ErrorInterceptors)(controllerClass);
5360
Inject(serviceClass)(controllerClass.prototype, 'serviceMixin');
5461
const properties = Reflect.getMetadata(
@@ -73,10 +80,7 @@ BaseModuleClass.forRoot = function (options): DynamicModule {
7380
controllerClass
7481
);
7582
}
76-
const decoratorOptions: DecoratorOptions = Reflect.getMetadata(
77-
JSON_API_DECORATOR_OPTIONS,
78-
controllerClass
79-
);
83+
8084
const moduleConfig: ConfigParam = {
8185
...ConfigParamDefault,
8286
...options.config,

libs/json-api-nestjs/src/lib/types/module.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface ConfigParam {
1818
debug: boolean;
1919
maxExecutionTime: number;
2020
pipeForId: PipeMixin;
21+
overrideRoute?: string;
2122
}
2223

2324
export interface ModuleOptions {

0 commit comments

Comments
 (0)