@w.dental/request
v0.1.1
Published
A fetch handler
Downloads
8
Readme
Request
A tiny request handler to facilitate writing requests
Installation
npm i @w.dental/request
or yarn add @w.dental/request
CDN Installation
This package is available on CDNs too (JSDelivr and UNPKG).
<!-- Using JSDelivr -->
<script src="https://cdn.jsdelivr.net/npm/@w.dental/request"></script>
<!-- Using UNPKG -->
<script src="https://unpkg.com/@w.dental/request"></script>
<script>
const req = Request.default('https://YOUR.API.com')
req.get({ url: '/search' })
.then(data => console.log(data))
</script>
How to use
import request from 'request'
const handler = request('base_url')
type ResponseData = {
attr: number
}
type BodyParams = {
param: number
}
handler.get<ResponseData>({
url: '/api',
params: { param: 1 }
});
handler.del<ResponseData>({
url: '/api',
params: { param: 1 }
});
handler.patch<ResponseData, BodyParams>({
url: '/api',
options: { body: { param: 1 } }
});
handler.post<ResponseData, BodyParams>({
url: '/api',
options: { body: { param: 1 } }
});
handler.put<ResponseData, BodyParams>({
url: '/api',
options: { body: { param: 1 } }
});
You can do your own custom request
import { doRequest } from 'request'
doRequest<{ a: number }>({
url: '/',
options: { method: 'get' }
})
request
offers some function helpers to auxiliate you to create some options and urls to your requests
import {
getFormatedUrl,
getHeaders,
objectToQueryString
} from 'request'
objectToQueryString('a=1')
objectToQueryString('?a=1')
objectToQueryString({ a: 1 })
// ?a=1
getFormatedUrl({ url: 'api/', params: { a: 1 } })
// this method uses objectToQueryString
// and returns api/?a=1
getHeaders()
/* return a new Headers which contains
{
'content-type': 'application/json',
'Authorization': 'bearer {{token in localstorage}}'
}
*/