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

nestjs-expanse

v1.2.8

Published

[![NPM package](https://img.shields.io/npm/v/nestjs-expanse.svg)](https://www.npmjs.org/package/nestjs-expanse) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)

Downloads

71

Readme

🌌 NestJS-Expanse

NPM package License: MIT

A NestJS module that enables REST API consumers to expand related resources in the payload, similar to GraphQL.

Motivation

Consider a REST API endpoint /users/1 that returns a user:

{
  "id": 1,
  "name": "Josephus Miller",
  "posts": [{
    "id": 1,
    "title": "Review of my new hat",
    "categories": [{
      "id": 1,
      "name": "Life"
    }]
  }],
  "photos": [{
    "id": 1,
    "url": "/avatar.jpg"
  }]
}

Note that the response body will always include posts and photos fields, and each post will contain categories, which might not be necessary for every API consumer use case. While this approach will work well for small projects, as the API grows, you may start noticing performance issues because there is no way to conditionally load or unload related entities.

At this moment, you might think of GraphQL. Although it's a powerful technology, it might not be suitable for every use case due to its complexity. Many large API providers have opted not using GraphQL, instead implementing an expansion system on top of REST (e.g. X and Atlassian). This library aims to solve the problem in a similar way.

To get the same payload as mentioned above, the API consumer can now call the endpoint as follows: /users/1?expand=posts.categories,photos (or /users/1?expand[]=posts.categories&expand[]=photos).

Quick Guide

Installation

npm install nestjs-expanse --save

Entity Decorators

As you decorate your relations with Expandable, NestJS-Expanse will be able to detect all the available expansions (including the deep ones).

import { Expandable } from 'nestjs-expanse';

export class UserEntity {
  id!: number;
  name!: string;

  @Expandable(() => PostEntity)
  posts?: PostEntity[];
}

Obtaining Requested Expansions

import { Expansions } from 'nestjs-expanse';

@Controller('users')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get('current')
  async findCurrent(
    @Expansions(UserEntity) expansions: string[],
  ) {
    // Do whatever you want with `expansions`. Pass it to a service for example:
    this.userService.findCurrent(expansions);
  }
}

Behavior On Error

NestJS-Expanse throws an InvalidExpansionException if any of the requested expansions are unavailable. This exception class extends BadRequestException and provides a sensible default message, resulting in an HTTP 400 response with a human-readable error by default, which should suffice for most cases. However, you can handle the exception yourself using Nest's exception filters

ORM Integration

One of the library's features is its integration with ORMs, minimizing the need for boilerplate code.

TypeORM

import { relationsFromExpansions } from 'nestjs-expanse/typeorm';

class UserService {
  constructor(
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,
  ) {}

  findAll(expansions: string[] = []) {
    return this.userRepository.find({
      relations: relationsFromExpansions(expansions),
    });
  }
}

Prisma

import { includeFromExpansions } from 'nestjs-expanse/prisma';

class UserService {
  constructor(private readonly prismaService: PrismaService) {}

  findAll(expansions: string[] = []) {
    return this.prismaService.user.findMany({
      include: includeFromExpansions(expansions),
    });
  }
}