Commit 7a8f982c authored by nanahira's avatar nanahira

add prefix to restful

parent 3428f578
...@@ -37,6 +37,7 @@ import { RenameClass } from '../utility/rename-class'; ...@@ -37,6 +37,7 @@ import { RenameClass } from '../utility/rename-class';
export interface RestfulFactoryOptions<T> { export interface RestfulFactoryOptions<T> {
fieldsToOmit?: (keyof T)[]; fieldsToOmit?: (keyof T)[];
prefix?: string;
} }
export class RestfulFactory<T> { export class RestfulFactory<T> {
...@@ -93,9 +94,28 @@ export class RestfulFactory<T> { ...@@ -93,9 +94,28 @@ export class RestfulFactory<T> {
private options: RestfulFactoryOptions<T> = {}, private options: RestfulFactoryOptions<T> = {},
) {} ) {}
private usePrefix(
methodDec: (path?: string) => MethodDecorator,
path?: string,
) {
if (path) {
if (this.options.prefix) {
return methodDec(`${this.options.prefix}/${path}`);
} else {
return methodDec(path);
}
} else {
if (this.options.prefix) {
return methodDec(this.options.prefix);
} else {
return methodDec();
}
}
}
create(extras: Partial<OperationObject> = {}): MethodDecorator { create(extras: Partial<OperationObject> = {}): MethodDecorator {
return MergeMethodDecorators([ return MergeMethodDecorators([
Post(), this.usePrefix(Post),
HttpCode(200), HttpCode(200),
ApiOperation({ ApiOperation({
summary: `Create a new ${this.entityClass.name}`, summary: `Create a new ${this.entityClass.name}`,
...@@ -116,7 +136,7 @@ export class RestfulFactory<T> { ...@@ -116,7 +136,7 @@ export class RestfulFactory<T> {
findOne(extras: Partial<OperationObject> = {}): MethodDecorator { findOne(extras: Partial<OperationObject> = {}): MethodDecorator {
return MergeMethodDecorators([ return MergeMethodDecorators([
Get(':id'), this.usePrefix(Get, ':id'),
ApiOperation({ ApiOperation({
summary: `Find a ${this.entityClass.name} by id`, summary: `Find a ${this.entityClass.name} by id`,
...extras, ...extras,
...@@ -140,7 +160,7 @@ export class RestfulFactory<T> { ...@@ -140,7 +160,7 @@ export class RestfulFactory<T> {
findAll(extras: Partial<OperationObject> = {}): MethodDecorator { findAll(extras: Partial<OperationObject> = {}): MethodDecorator {
return MergeMethodDecorators([ return MergeMethodDecorators([
Get(), this.usePrefix(Get),
ApiOperation({ summary: `Find all ${this.entityClass.name}`, ...extras }), ApiOperation({ summary: `Find all ${this.entityClass.name}`, ...extras }),
ApiOkResponse({ type: this.entityArrayReturnMessageDto }), ApiOkResponse({ type: this.entityArrayReturnMessageDto }),
]); ]);
...@@ -152,7 +172,7 @@ export class RestfulFactory<T> { ...@@ -152,7 +172,7 @@ export class RestfulFactory<T> {
update(extras: Partial<OperationObject> = {}): MethodDecorator { update(extras: Partial<OperationObject> = {}): MethodDecorator {
return MergeMethodDecorators([ return MergeMethodDecorators([
Patch(':id'), this.usePrefix(Patch, ':id'),
HttpCode(200), HttpCode(200),
ApiOperation({ ApiOperation({
summary: `Update a ${this.entityClass.name} by id`, summary: `Update a ${this.entityClass.name} by id`,
...@@ -182,7 +202,7 @@ export class RestfulFactory<T> { ...@@ -182,7 +202,7 @@ export class RestfulFactory<T> {
delete(extras: Partial<OperationObject> = {}): MethodDecorator { delete(extras: Partial<OperationObject> = {}): MethodDecorator {
return MergeMethodDecorators([ return MergeMethodDecorators([
Delete(':id'), this.usePrefix(Delete, ':id'),
HttpCode(200), HttpCode(200),
ApiOperation({ ApiOperation({
summary: `Delete a ${this.entityClass.name} by id`, summary: `Delete a ${this.entityClass.name} by id`,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment