-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
382 additions
and
258 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
Logger, | ||
HttpException, | ||
HttpStatus, | ||
NotFoundException, | ||
ConflictException, | ||
applyDecorators, | ||
} from '@nestjs/common' | ||
import { ApiHeader } from '@nestjs/swagger' | ||
import { AbstractService } from './abstract.service' | ||
|
||
const knownExceptions = new Set([NotFoundException, ConflictException]) | ||
|
||
export function ApiAuthHeaders() { | ||
return applyDecorators( | ||
ApiHeader({ | ||
name: 'username', | ||
description: 'Username for authentication', | ||
required: true, | ||
}), | ||
ApiHeader({ | ||
name: 'password', | ||
description: 'Password for authentication', | ||
required: true, | ||
}) | ||
) | ||
} | ||
|
||
export abstract class AbstractController<T> { | ||
protected readonly logger = new Logger(AbstractController.name) | ||
protected readonly service: AbstractService<T> | ||
|
||
constructor(service: AbstractService<T>) { | ||
this.service = service | ||
} | ||
|
||
protected handleHttpException(error: any): Error { | ||
this.logger.error(`Error processing request: ${error.message}`) | ||
this.logger.verbose(error.stack) | ||
if (!knownExceptions.has(error.constructor)) { | ||
throw new HttpException( | ||
`Internal Server Error: ${error.constructor.name} - ${error.message}`, | ||
HttpStatus.INTERNAL_SERVER_ERROR | ||
) | ||
} | ||
return error | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { DynamicModule, InjectionToken, Type } from '@nestjs/common' | ||
|
||
interface GenericModuleOptions { | ||
controller: Type<any>; | ||
service: Type<any>; | ||
serviceToken: InjectionToken; | ||
dbImplementation: Type<any>; | ||
dbToken: InjectionToken; | ||
} | ||
|
||
export class AbstractModule { | ||
static configure(options: GenericModuleOptions): DynamicModule { | ||
return { | ||
module: AbstractModule, | ||
controllers: [options.controller], | ||
providers: [ | ||
{ | ||
provide: options.serviceToken, | ||
useClass: options.service, | ||
}, | ||
{ | ||
provide: options.dbToken, | ||
useClass: options.dbImplementation, | ||
}, | ||
], | ||
exports: [options.serviceToken, options.dbToken], | ||
} | ||
} | ||
} |
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,17 @@ | ||
import { Logger } from '@nestjs/common' | ||
import { AbstractDatabase } from './abstract.database' | ||
|
||
export abstract class AbstractService<T> { | ||
protected readonly logger = new Logger(AbstractService.name) | ||
protected readonly database: AbstractDatabase<T> | ||
|
||
constructor(database: AbstractDatabase<T>) { | ||
this.database = database | ||
} | ||
|
||
abstract findById(id: string): Promise<T>; | ||
abstract findAll(): Promise<T[]>; | ||
abstract create(entity: T): Promise<T>; | ||
abstract update(id: string, entity: T): Promise<T>; | ||
abstract delete(id: string): Promise<T>; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.