@drizzle-http/response-mapper-adapter
TypeScript icon, indicating that this package has built-in type declarations

3.1.0 • Public • Published

Response Mapper Adapter · ci npm (scoped) GitHub license

Map API responses to a different type. Good for situations where the api responds with envelope like payloads or the payload comes in a complex format.

Installation

Make sure we have the core module @Drizzle-Http/core installed.

npm i @drizzle-http/core
npm i @drizzle-http/response-mapper-adapter

Usage

import { GET } from '@drizzle-http/core'
import { Param } from '@drizzle-http/core'
import { Map } from '@drizzle-http/response-mapper-adapter'
import { MapTo } from '@drizzle-http/response-mapper-adapter'

class Project {
  private readonly id: string
  private readonly description: string

  constructor(response) {
    this.id = response.data.id
    this.description = response.data.description
  }
}

class API {
  @GET('/customers/{id}')
  @Map((response: { data: Customer }) => response.data)
  customerById(@Param('id') id: string): Promise<Customer> { }

  @GET('/projects/{id}')
  @MapTo(Project)
  projectById(@Param('id') id: string): Promise<Project> { }
}

Features

Map Function

Use the decorator @Map() and pass a function to map the response to another type.

Map To Class Instance

Use the decorator @MapTo() and pass a class reference to map to new instance of the same.
If you pass only the first argument, the class constructor will be used. On the second argument, you can provide a custom factory method, for example, a static method on the class.

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @drizzle-http/response-mapper-adapter

    Weekly Downloads

    0

    Version

    3.1.0

    License

    MIT

    Unpacked Size

    16.6 kB

    Total Files

    28

    Last publish

    Collaborators

    • vitor.salgado