nest-jsonapi
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,3 +0,4 @@ | ||
import { ArgumentsHost, HttpServer } from '@nestjs/common'; | ||
import { ArgumentsHost, HttpException, HttpServer } from '@nestjs/common'; | ||
import { BaseExceptionFilter } from '@nestjs/core'; | ||
export declare const isHTTPException: (error: HttpException | unknown) => error is HttpException; | ||
export declare class JsonapiExceptionFilter extends BaseExceptionFilter { | ||
@@ -4,0 +5,0 @@ private readonly logger; |
@@ -13,3 +13,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.JsonapiExceptionFilter = void 0; | ||
exports.JsonapiExceptionFilter = exports.isHTTPException = void 0; | ||
const common_1 = require("@nestjs/common"); | ||
@@ -19,2 +19,9 @@ const core_1 = require("@nestjs/core"); | ||
const constants_1 = require("./constants"); | ||
const isHTTPException = (error) => { | ||
const httpException = error; | ||
return (httpException.getStatus !== undefined && | ||
httpException.getResponse !== undefined && | ||
Object.prototype.hasOwnProperty.call(httpException, 'message')); | ||
}; | ||
exports.isHTTPException = isHTTPException; | ||
let JsonapiExceptionFilter = JsonapiExceptionFilter_1 = class JsonapiExceptionFilter extends core_1.BaseExceptionFilter { | ||
@@ -39,3 +46,3 @@ constructor(applicationRef) { | ||
let detail; | ||
if (exception instanceof common_1.HttpException) { | ||
if (exports.isHTTPException(exception)) { | ||
status = exception.getStatus(); | ||
@@ -42,0 +49,0 @@ const errorResponse = exception.getResponse(); |
{ | ||
"name": "nest-jsonapi", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "a NestJS module that provides JSONAPI integration", | ||
@@ -24,2 +24,3 @@ "keywords": ["nestjs", "nest", "jsonapi", "json-api"], | ||
"clean": "rimraf lib reports .eslintcache && (jest --clearCache || true)", | ||
"docs": "typedoc --tsconfig tsconfig.json", | ||
"lint": "npm-run-all --aggregate-output --continue-on-error --parallel 'lint:!(fix)'", | ||
@@ -42,4 +43,4 @@ "lint:fix": "npm-run-all --aggregate-output --continue-on-error --parallel lint:*:fix", | ||
"peerDependencies": { | ||
"@nestjs/common": "^7.6.5", | ||
"@nestjs/core": "^7.6.5", | ||
"@nestjs/common": "^7.6.11", | ||
"@nestjs/core": "^7.6.11", | ||
"class-transformer": "^0.3.2", | ||
@@ -52,10 +53,10 @@ "reflect-metadata": "^0.1.13", | ||
"@commitlint/config-conventional": "11.0.0", | ||
"@nestjs/common": "7.6.5", | ||
"@nestjs/core": "7.6.5", | ||
"@nestjs/platform-express": "7.6.5", | ||
"@nestjs/testing": "7.6.5", | ||
"@nestjs/common": "7.6.11", | ||
"@nestjs/core": "7.6.11", | ||
"@nestjs/platform-express": "7.6.11", | ||
"@nestjs/testing": "7.6.11", | ||
"@types/dot-object": "2.1.2", | ||
"@types/faker": "5.1.5", | ||
"@types/faker": "5.1.6", | ||
"@types/jest": "26.0.20", | ||
"@types/node": "14.14.21", | ||
"@types/node": "14.14.25", | ||
"@types/validator": "13.1.3", | ||
@@ -68,4 +69,4 @@ "@typescript-eslint/eslint-plugin": "4.13.0", | ||
"eslint": "7.18.0", | ||
"eslint-config-prettier": "6.15.0", | ||
"eslint-plugin-prefer-arrow": "1.2.2", | ||
"eslint-config-prettier": "7.2.0", | ||
"eslint-plugin-prefer-arrow": "1.2.3", | ||
"eslint-plugin-prettier": "3.3.1", | ||
@@ -87,2 +88,3 @@ "faker": "5.1.0", | ||
"tslint": "6.1.3", | ||
"typedoc": "0.20.21", | ||
"typescript": "4.1.3" | ||
@@ -89,0 +91,0 @@ }, |
@@ -15,12 +15,7 @@ <p align="center"> | ||
## Reference Example | ||
## Usage | ||
[nestjs-jsonapi-example](https://github.com/tzellman/nestjs-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! | ||
## Quick Start | ||
Import the `JsonapiModule` into the root `AppModule` and use the `forRoot()` method to configure it: | ||
```typescript | ||
import { Module } from "@nestjs/common"; | ||
import { JsonapiModule } from "nest-jsonapi"; | ||
@@ -38,6 +33,5 @@ | ||
Afterward, the `JsonapiService` instance will be available to inject across entire project using the service token `JSONAPI_MODULE_SERVICE`: | ||
Inject `JsonapiService`: | ||
```typescript | ||
import { Controller, Inject } from "@nestjs/common"; | ||
import { JsonapiService, JSONAPI_MODULE_SERVICE } from "nest-jsonapi"; | ||
@@ -53,2 +47,81 @@ | ||
### 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. | ||
```typescript | ||
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: | ||
```typescript | ||
@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. | ||
```typescript | ||
@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. | ||
```typescript | ||
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. | ||
```typescript | ||
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](https://github.com/tzellman/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](https://tzellman.github.io/nest-jsonapi/index.html). | ||
## Contributing | ||
@@ -55,0 +128,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
72822
60
865
131
37