npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@startupdevhouse/typescript-functional-extensions-nestjs

v0.3.0

Published

NestJS utilities for working with typescript-functional-extensions Result monads

Downloads

1,272

Readme

NestJS typescript-functional-extensions utilities

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.

Installation

# using npm
npm install --save @startupdevhouse/typescript-functional-extensions-nestjs

# or using yarn
yarn add @startupdevhouse/typescript-functional-extensions-nestjs

Configuration

Import the module

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;
  }
}

Default module configuration

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.

Handling Result errors

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 {}

Custom wrapping/mapping values

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 {}

Contributing

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.

Startup Development House

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 ❤️