This package contains utilities for working with typescript-functional-extensions in NestJS projects. Notably, it has the ResultResponseInterceptorModule
with ResultResponseInterceptor
that processes Result
monads returned by controller methods and converts them to HTTP responses.
# using npm
npm install --save @startupdevhouse/typescript-functional-extensions-nestjs
# or using yarn
yarn add @startupdevhouse/typescript-functional-extensions-nestjs
To convert Result
monads to HTTP responses register the ResultResponseInterceptorModule
in your AppModule
:
@Module({
imports: [
ResultResponseInterceptorModule.register(),
],
})
export class AppModule {}
Then return a Result
in your controller:
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getData() {
const result = Result.success(this.appService.getData());
return result;
}
}
By default the module will return HTTP 200 OK for success results returned by application controllers. Additionally, if the result returned contained a value other than Unit.Instance
it will be unwrapped and returned in the response body.
All failures will be returned as HTTP 400 Bad Request. Default implementation will unwrap the error and return it in the response body.
ResultResponseInterceptorModule
accepts the handleFn
property in configuration options. You can override the default result handling logic there. In the following example all errors ending with .DOES_NOT_EXIST
postfix will be processed as HTTP 404 responses:
function handleResult(result: Result<unknown, any>) {
if (result.isSuccess) {
const value = result.getValueOrDefault(Unit.Instance);
if (value === Unit.Instance) {
return;
}
return value;
}
const error = result.getErrorOrThrow() as string;
const errorParts = error.split('_');
if (error.endsWith('.DOES_NOT_EXIST')) {
throw new NotFoundException(error);
}
throw new BadRequestException(error);
}
@Module({
imports: [
ResultResponseInterceptorModule.register({
handleFn: handleResult,
}),
],
})
export class AppModule {}
ResultResponseInterceptorModule
accepts the mappingFn
property in configuration options. You can override the default result mapping logic there. In the following example the value is additionally wrapped in a wrapper object:
@Module({
imports: [
ResultResponseInterceptorModule.register({
mappingFn: (data) => ({
wrapped: data,
}),
}),
],
})
export class AppModule {}
If you've found a bug or have a feature request, please open an issue on GitHub.
If you'd like to make a contribution, you can create a Pull Request on GitHub.
This package was initially created for projects in Startup Development House. We love using Monads so we decided to share this love with the community ❤️