Gathering detailed insights and metrics for nestjs-expanse
Gathering detailed insights and metrics for nestjs-expanse
Gathering detailed insights and metrics for nestjs-expanse
Gathering detailed insights and metrics for nestjs-expanse
🌌 A NestJS module that enables REST API consumers to expand related resources in the payload, similar to GraphQL
npm install nestjs-expanse
Typescript
Module System
Node Version
NPM Version
65.7
Supply Chain
95.6
Quality
79.9
Maintenance
100
Vulnerability
100
License
TypeScript (98.25%)
JavaScript (1.75%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
27 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Nov 28, 2024
Latest Version
1.2.8
Package Id
nestjs-expanse@1.2.8
Unpacked Size
118.70 kB
Size
33.67 kB
File Count
22
NPM Version
10.8.2
Node Version
20.17.0
Published on
Aug 30, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
6
A NestJS module that enables REST API consumers to expand related resources in the payload, similar to GraphQL.
Consider a REST API endpoint /users/1
that returns a user:
1{ 2 "id": 1, 3 "name": "Josephus Miller", 4 "posts": [{ 5 "id": 1, 6 "title": "Review of my new hat", 7 "categories": [{ 8 "id": 1, 9 "name": "Life" 10 }] 11 }], 12 "photos": [{ 13 "id": 1, 14 "url": "/avatar.jpg" 15 }] 16}
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
).
1npm install nestjs-expanse --save
As you decorate your relations with Expandable
, NestJS-Expanse will be able to detect all the available expansions (including the deep ones).
1import { Expandable } from 'nestjs-expanse'; 2 3export class UserEntity { 4 id!: number; 5 name!: string; 6 7 @Expandable(() => PostEntity) 8 posts?: PostEntity[]; 9}
1import { Expansions } from 'nestjs-expanse'; 2 3@Controller('users') 4export class UserController { 5 constructor(private readonly userService: UserService) {} 6 7 @Get('current') 8 async findCurrent( 9 @Expansions(UserEntity) expansions: string[], 10 ) { 11 // Do whatever you want with `expansions`. Pass it to a service for example: 12 this.userService.findCurrent(expansions); 13 } 14}
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
One of the library's features is its integration with ORMs, minimizing the need for boilerplate code.
1import { relationsFromExpansions } from 'nestjs-expanse/typeorm'; 2 3class UserService { 4 constructor( 5 @InjectRepository(User) 6 private readonly userRepository: Repository<User>, 7 ) {} 8 9 findAll(expansions: string[] = []) { 10 return this.userRepository.find({ 11 relations: relationsFromExpansions(expansions), 12 }); 13 } 14}
1import { includeFromExpansions } from 'nestjs-expanse/prisma'; 2 3class UserService { 4 constructor(private readonly prismaService: PrismaService) {} 5 6 findAll(expansions: string[] = []) { 7 return this.prismaService.user.findMany({ 8 include: includeFromExpansions(expansions), 9 }); 10 } 11}
No vulnerabilities found.
No security vulnerabilities found.