@dev-plus-plus/axios-transformer
An axios extension that aim to transform the response into a class instance.
It uses Axios to handle HTTP calls and Class-Transformer to transform plain objects from/to class-objects
Install
npm i @dev-plus-plus/axios-transformer axios class-transformer
Request Usage
Axios-transformer supports GET, POST, PUT, PATCH, DELETE and HEAD Http Methods. Just like Axios.
Import
import axiosT from '@dev-plus-plus/axios-transformer'
import {axiosT} from '@dev-plus-plus/axios-transformer'
import {AxiosTransformer} from '@dev-plus-plus/axios-transformer'
Create any class to use as result
class BlogPost {
id: number | null = null
title: string | null = null
body: string | null = null
userId: number | null = null
}
Make a GET request with a new Object response
const respBlogPost = await axiosT.get('https://jsonplaceholder.typicode.com/posts/1')
.as(BlogPost)
.fetchData()
Make a GET request with an Array response
const blogPosts = await axiosT.get('https://jsonplaceholder.typicode.com/posts')
.asArrayOf(BlogPost)
.fetchData()
Make a POST request filling an existing object on response
const myBlogPost = new BlogPost()
myBlogPost.body = 'no great news today, the rich are getting richer'
await axiosT.post('https://jsonplaceholder.typicode.com/posts/', myBlogPost)
.as(myBlogPost)
.fetchData()
Response types
You can use this methods to parse the response:
as(MyClass)
- Transforms to a new object of the choosen classas(myInstantiatedObject)
- Fills the properties of the choosen objectasArrayOf(MyClass)
- Transforms to an array of the choosen classasString()
- Returns as stringasNumber()
- Returns as numberasBoolean()
- Returns as booleanasAny()
- Returns as it isasVoid()
- Returns nothing
Retrive the Response information
const resp = await axiosT.delete('https://jsonplaceholder.typicode.com/posts/1')
.asVoid()
.fetchResponse()
Add a delay before the request
const resp = await Request.head('https://jsonplaceholder.typicode.com/posts/1')
.withDelay(2000)
.asString()
.fetchResponse()
Globally listen to requests
Useful for loading interactions
let startCbCount = 0
let endCbCount = 0
const startCb = () => startCbCount++
const endCb = () => endCbCount++
axiosT.event.on('requestStart', startCb)
axiosT.event.on('requestEnd', endCb)
const myBlogPost = await axiosT.get('https://jsonplaceholder.typicode.com/posts/1')
.withName('foo')
.as(BlogPost)
.fetchData()
axiosT.event.off('requestStart', startCb)
axiosT.event.clean()
Ignore fields and map names and types with Annotations
Using Class-Transformer we can control the serialization behaviour
@AxiosTransform(clsType)
or @Type(() => clsType)
Working with nested objects
@AxiosRequestExpose(name?)
or @Expose({ name, toPlainOnly: true })
Skipping depend of operation
@AxiosResponseExpose(name?)
or @Expose({ name, toClassOnly: true })
Skipping depend of operation
@AxiosExpose(name?)
or @Expose({ name })
Exposing properties with different names
@AxiosRequestExclude()
or @Exclude({ toPlainOnly: true })
Skipping depend of operation
@AxiosResponseExclude()
or @Exclude({ toClassOnly: true })
Skipping depend of operation
@AxiosExclude()
or @Exclude()
Skipping specific properties