Socket
Socket
Sign inDemoInstall

@tsed/platform-response-filter

Package Overview
Dependencies
Maintainers
0
Versions
505
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tsed/platform-response-filter

Platform response filter module for Ts.ED Framework


Version published
Weekly downloads
17K
increased by0.41%
Maintainers
0
Weekly downloads
 
Created
Source

Ts.ED logo

@tsed/platform-response-filter

Build & Release PR Welcome npm version semantic-release code style: prettier github opencollective

Website   •   Getting started   •   Slack   •   Twitter

A package of Ts.ED framework. See website: https://tsed.io/

Features

Transform data returned by a method to a formatted response based on the stored metadata.

Installation

npm install --save @tsed/di @tsed/platform-views @tsed/schema @tsed/json-schema @tsed/platform-response-filter

Usage

Define a class that return data:

import {Injectable} from "@tsed/di";
import {Returns} from "@tsed/schema";
import {MyModel} from "../models/MyModel.js";

@Injectable()
class MyService {
  @Returns(200, MyModel)
  async getData() {
    return new MyModel({id: "id", test: "test"});
  }

  @Returns(200, MyModel)
  @View("myview.ejs")
  async getDataView() {
    return new MyModel({id: "id", test: "test"});
  }
}

Add a response filter for a specific content-type:

import {ResponseFilter, Context, ResponseFilterMethods} from "@tsed/common";

@ResponseFilter("application/json")
export class WrapperResponseFilter implements ResponseFilterMethods {
  transform(data: any, ctx: Context) {
    return {data, errors: [], links: {}};
  }
}

Then call the service in you module:

import {Module, DIContext} from "@tsed/di";
import {ResponseFilter} from "@tsed/platform-response-filter";
import "./filters/WrapperResponseFilter";

@Module()
class MyModule {
  @Inject()
  injector: InjectorService;

  @Inject()
  responseFilter: ResponseFilter;

  async onRequest(req: any, res: any) {
    const context = new DIContext({
      id: uuid.v4(),
      injector: this.injector,
      logger: this.injector.logger
    });

    // must implement these methods
    context.request = {
      accepts(...args: any[]) {
        return req.accepts(...args);
      },
      get(key: string) {
        return req.get(key);
      }
    };
    context.response = {
      contentType(contentType: string) {
        res.contentType(contentType);
      }
    };

    const service = this.injector.get<MyService>(MyService);
    let data = await service.getData();

    // serialize data (map Model to Plain object)
    data = await this.responseFilter.serialize(data, context);

    // call filter based on the right content type
    data = await this.responseFilter.transform(data, context);

    if (isObject(data)) {
      res.json(data);
    } else {
      res.send(data);
    }
  }
}

This example will call the getData() method, serialize the instance MyModel to a plain object then call the WrapperResponseFilter, to produce the following response:

{
  "data": {
    "id": "id",
    "test": "test"
  },
  "error": [],
  "links": {}
}

Contributors

Please read contributing guidelines here.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

The MIT License (MIT)

Copyright (c) 2016 - 2022 Romain Lenzotti

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

FAQs

Package last updated on 10 Sep 2024

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