Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nest-jsonapi

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nest-jsonapi

a NestJS module that provides JSONAPI integration

  • 0.1.1
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

Nest Logo

A Nest module that provides JSONAPI integration.

Installation

npm install --save nest-jsonapi

Usage

Import the JsonapiModule into the root AppModule and use the forRoot() method to configure it:

import { JsonapiModule } from "nest-jsonapi";

@Module({
    imports: [
        JsonapiModule.forRoot({
            // options
        }),
    ],
})
export class AppModule {}

Inject JsonapiService:

import { JsonapiService, JSONAPI_MODULE_SERVICE } from "nest-jsonapi";

@Controller("photos")
export class PhotosController {
    constructor(@Inject(JSONAPI_MODULE_SERVICE) private readonly jsonapiService: JsonapiService) {}
}

Note that JsonapiModule is a global module, therefore it will be available in all modules.

Middleware

If you plan on using the JsonapiPayload decorator (more info below), you must use the JsonapiMiddleware in your application. This does 2 things:

  1. enable parsing jsonapi content as JSON
  2. creates a request-scoped holder for tracking metadata

e.g.

export class AppModule implements NestModule {
    public configure(consumer: MiddlewareConsumer): void {
        consumer.apply(JsonapiMiddleware).forRoutes(PhotosController);
    }
}

Interceptor

The JsonapiInterceptor is used to properly transform your controller result data to JSONAPI. You can decorate at a class or method level:

@UseInterceptors(JsonapiInterceptor)
@Controller("photos")
export default class PhotosController {}

Payload Metadata

In order for the JsonapiInterceptor to know how to transform your data, you need to decorate your methods.

@JsonapiPayload({ resource: RESOURCE_PHOTOS })
@Get()
public async findPhotos(@Query() query: FindOptions): Promise<Photo[]>

Exception Filter

In order to support error responses compliant with the JSONAPI specification, the JsonapiExceptionFilter exists.

const { httpAdapter } = app.get(HttpAdapterHost);
app.useGlobalFilters(new JsonapiExceptionFilter(httpAdapter));

Schema Registration

The JsonapiService requires schemas for the resources it is going to handle. You have control of how that is configured, by defining a schematic. We provide a thin wrapper around the transformalizer library.

Typically you will want to register your schemas on module initialization.

export class AppModule implements OnModuleInit {
    constructor(@Inject(JSONAPI_MODULE_SERVICE) private readonly jsonapiService: JsonapiService) {}

    public async onModuleInit(): Promise<void> {
        this.jsonapiService.register({
            name: RESOURCE_PHOTOS,
            schema: new SchemaBuilder<Photo>(RESOURCE_PHOTOS)
                .data(
                    new SchemaDataBuilder<Photo>(RESOURCE_PHOTOS)
                        .untransformAttributes({ deny: ["createdAt", "updatedAt"] })
                        .build()
                )
                .build(),
        });
    }
}

Reference Example

nest-jsonapi-example is an example project that demonstrates the usage of this module. Since not all aspects of the module have been fully tested yet (coming soon!), I highly suggest checking this out!

API Docs

For detailed API information please visit the API documentation.

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

Keywords

FAQs

Package last updated on 05 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc