class-request
v0.0.1
Published
request-layer
Downloads
1
Readme
Request Model
This library simplify restful call for both front-end/backend application!
Features including
- Declarative style - write once use anywhere
- Support Batching api call
- Support abort request,
- Support dynamic uri at runtime - regrex :param url in request
- Modern class based Decorator style declaration
- Work on frontend and backend
- Todo -- Set Headers
Example 1
import { Request, Http, Requests } from "class-request";
// Declare Repository
@Http({ baseURL: "https://reqres.in/", timeout: 3000 })
class SampleRepository {
@Request({ url: "api/:page" })
public stock!: Requests;
}
// usage
const result = new SampleRepository();
// support headers, params | query | data - for body
result.stock.get({ params: { page: 'sample' }}).then((e: any) => console.log(e.status));
result.stock.post({ params: { page: 'sample' }}).then((e: any) => console.log(e.status));
result.stock.put({ params: { page: 'sample' }}).then((e: any) => console.log(e.status));
result.stock.patch({ params: { page: 'sample' }}).then((e: any) => console.log(e.status));
Example 2
- Abort a request
// abort
const req = result.stock.get(payload: any)
req.abort()
Example 3
- Allow custom repository method
import { Request, Http, Requests } from "class-request";
@Http({ baseURL: "https://reqres.in/" })
export class PortfolioReposistory {
@Request({ url: "api/:page" })
public stock!: Requests;
@Request({ url: "api/:page" })
public account!: Requests;
getStockAndAccount(stockPayload: Object, accountPayload: Object) {
return Promise.all([
this.stock.get(stockPayload),
this.account.post(accountPayload),
]);
}
async getAndTransform(stockPayload: any) {
const data = await this.stock.get(stockPayload);
return data?.filter(x => x.age > 20)
}
}