-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
the modules with controllers were modified to be dynamic
- Loading branch information
1 parent
461868c
commit 2339996
Showing
13 changed files
with
18,130 additions
and
20,353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { DynamicModule, Module } from '@nestjs/common'; | ||
import { AiService } from './ai.service'; | ||
import { AiController } from './ai.controller'; | ||
import { createControllerWithPrefix } from '../../utils/controllers'; | ||
|
||
@Module({ | ||
providers: [AiService], | ||
controllers: [AiController], | ||
exports: [AiService], | ||
}) | ||
export class AiModule {} | ||
export class AiModule { | ||
static register(prefix: string): DynamicModule { | ||
return { | ||
module: AiModule, | ||
providers: [ | ||
AiService, | ||
{ | ||
provide: 'PREFIX', | ||
useValue: prefix, | ||
}, | ||
], | ||
controllers: [createControllerWithPrefix(AiController, prefix)], | ||
exports: [AiService], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,28 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { DynamicModule } from '@nestjs/common'; | ||
import { AiModule } from '../ai'; | ||
import { RunModule } from '../run'; | ||
import { ChatHelpers } from './chat.helpers'; | ||
import { ChatService } from './chat.service'; | ||
import { SocketModule } from '@nestjs/websockets/socket-module'; | ||
import { ChatController } from './chat.controller'; | ||
import { createControllerWithPrefix } from '../../utils/controllers'; | ||
|
||
export const sharedServices = [ChatHelpers, ChatService]; | ||
|
||
@Module({ | ||
imports: [SocketModule, AiModule, RunModule], | ||
providers: [...sharedServices], | ||
controllers: [ChatController], | ||
exports: [...sharedServices], | ||
}) | ||
export class ChatModule {} | ||
export class ChatModule { | ||
static register(prefix: string): DynamicModule { | ||
return { | ||
module: ChatModule, | ||
imports: [SocketModule, AiModule, RunModule], | ||
providers: [ | ||
...sharedServices, | ||
{ | ||
provide: 'PREFIX', | ||
useValue: prefix, | ||
}, | ||
], | ||
controllers: [createControllerWithPrefix(ChatController, prefix)], | ||
exports: [...sharedServices], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { DynamicModule, Module } from '@nestjs/common'; | ||
import { FilesService } from './files.service'; | ||
import { FilesController } from './files.controller'; | ||
import { AiModule } from '../ai'; | ||
import { createControllerWithPrefix } from '../../utils/controllers'; | ||
|
||
@Module({ | ||
imports: [AiModule], | ||
providers: [FilesService], | ||
controllers: [FilesController], | ||
exports: [FilesService], | ||
}) | ||
export class FilesModule {} | ||
export class FilesModule { | ||
static register(prefix: string): DynamicModule { | ||
return { | ||
module: FilesModule, | ||
providers: [ | ||
AiModule, | ||
{ | ||
provide: 'PREFIX', | ||
useValue: prefix, | ||
}, | ||
], | ||
controllers: [createControllerWithPrefix(FilesController, prefix)], | ||
exports: [FilesService], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { DynamicModule, Module } from '@nestjs/common'; | ||
import { ThreadsController } from './threads.controller'; | ||
import { ThreadsService } from './threads.service'; | ||
import { AiModule } from '../ai'; | ||
import { createControllerWithPrefix } from '../../utils/controllers'; | ||
|
||
@Module({ | ||
imports: [AiModule], | ||
providers: [ThreadsService], | ||
controllers: [ThreadsController], | ||
exports: [ThreadsService], | ||
}) | ||
export class ThreadsModule {} | ||
export class ThreadsModule { | ||
static register(prefix: string): DynamicModule { | ||
return { | ||
module: ThreadsModule, | ||
providers: [ | ||
ThreadsService, | ||
{ | ||
provide: 'PREFIX', | ||
useValue: prefix, | ||
}, | ||
], | ||
controllers: [createControllerWithPrefix(ThreadsController, prefix)], | ||
exports: [ThreadsService], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { createControllerWithPrefix } from './controllers'; | ||
import 'reflect-metadata'; | ||
|
||
@Controller('test') | ||
class TestController { | ||
@Get() | ||
getTest(): string { | ||
return 'Original Controller'; | ||
} | ||
} | ||
|
||
describe('createControllerWithPrefix', () => { | ||
it('should create a new controller with the prefixed route', () => { | ||
const PrefixedController = createControllerWithPrefix( | ||
TestController, | ||
'api', | ||
); | ||
|
||
const originalPath = Reflect.getMetadata('path', TestController); | ||
const prefixedPath = Reflect.getMetadata('path', PrefixedController); | ||
|
||
expect(originalPath).toBe('test'); | ||
|
||
expect(prefixedPath).toBe('api/test'); | ||
}); | ||
|
||
it('should throw an error if the controller does not have @Controller decorator', () => { | ||
class InvalidController {} | ||
|
||
expect(() => createControllerWithPrefix(InvalidController, 'api')).toThrow( | ||
'The provided controller does not have a @Controller decorator.', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { Type } from '@nestjs/common/interfaces'; | ||
import 'reflect-metadata'; | ||
|
||
export function createControllerWithPrefix<T extends object>( | ||
controller: Type<T>, | ||
prefix: string, | ||
): Type<T> { | ||
const originalPath = Reflect.getMetadata('path', controller) as | ||
| string | ||
| undefined; | ||
|
||
if (!originalPath) { | ||
throw new Error( | ||
'The provided controller does not have a @Controller decorator.', | ||
); | ||
} | ||
|
||
const DynamicController = class extends controller {}; | ||
|
||
Controller(`${prefix}/${originalPath}`)(DynamicController); | ||
|
||
return DynamicController; | ||
} |
Oops, something went wrong.