reqos
v0.0.2
Published
Reqos is a data fetching utility that uses `fetch`. Reqos implements an automatic data parsing approach based on `content-type` which is helpful in speeding up development. It also handle errors better by eliminating the need of implementing `catch` block
Downloads
600
Readme
Reqos
Reqos is a data fetching utility that uses fetch. Reqos implements an automatic data parsing approach based on content-type which is helpful in speeding up development. It also handle errors better by eliminating the need of implementing catch blocks.
Getting Started
Install
npm i reqos@latestImport
import { FetchClient } from 'reqos';
const client = new FetchClient();
const { data, error } = await client.get('/api/users');Reqos checks the response content-type and parses the data automatically.
If reqos encounters an error, the data property becomes null and the response is populated in error property. Reqos handles client side malformations as well which can lead to errors sometimes. This eliminates the need of wrapping fetch inside a try...catch block.
Supported methods
client.get(...): Initiates a request with GET method.client.post(...): Initiates a request with POST method. Helpful in creating new records.client.put(...): Initiates a request with PUT method. Helpful in replacing existing record.client.delete(...): Initiates a request with DELETE method. Helpful in deleting existing record.client.patch(...): Initiates a request with PATCH method. Helpful in modifying existing record.
Creating methods of your own
Reqos provides support for popular fetch methods. However, if users want to use other fetch methods they can always extend the utility.
Supporting standard methods:
import { FetchClient, AssertMethod } from 'reqos';
import type { ReqInit } from 'reqos';
class CustomClient extends FetchClient {
@AssertMethod()
async option<D, E>(url: string, options?: ReqInit) {
return this.__api<D, E>(url, options);
}
}AssertMethod decorator wires the method using function name. It checks if the function name is valid or not, then applies the appropriate method while sending browser request.
Supporting non-standard methods:
At this point we are not sure if fetch supports non-standard methods or not (Probably not!). However, users are not limited to using only the standard REST methods. They can do whatever they want!
import { FetchClient } from 'reqos';
import type { ReqInit } from 'reqos';
class CustomClient extends FetchClient {
async option<D, E>(url: string, options?: ReqInit) {
return this.__api<D, E>(url, {
...options,
method: 'I_CAN_DO_WHATEVER_I_WANT_METHOD',
});
}
}Disclaimer
Reqos has been released as a proof of concept. Our ultimate goal is to turn this into a library that requires minimal setup and configuration. We encourage users to use this utility in their own projects and report any problems if they face. Users can also use it in production code as well if they want. At any point if the users think that the library did not work as intended, they can always fallback to native fetch (See example below).
const client = new FetchClient();
const response = await client.request('/api/user');This documentation is a "work in progress". New suggestions and PRs are always welcome.
Roadmap
Upcoming features:
- Provide support for
abortandretry. Aborting is already supported viaAbortControllerandsignal. We want to write our own wrapper that would let us do timeouts and retries. - Provide support for
useFetchClientreact hook forReactprojects. - Provide support for unit testing by adding test utilities.
