fast-client v0.4.2
Fast Client
This library is intended to be a facilitator when defining which API calls you want to make. The library behaves in a way that allows you to define all calls in one place, in a simple structure.
How to use
Let's assume you have an API with the endpoint /search
. Now you want to have a method that makes the call on this endpoint. All you need to do is:
const client = createFastClient({
base: 'https://api.example.com',
endpoints: {
search: {
method: 'GET',
href: '/search',
},
},
});
The createFastClient
function generates an object that will have functions with the endpoints names. The parameters of this functions will vary depending on the method provided:
const res: Promise<Response> = client.search({
query: { q: 'teste', limit: '10' },
});
You can also define path params using {}
in the href:
const client = createFastClient({
base: 'https://api.example.com',
endpoints: {
getUser: {
method: 'GET',
href: '/user/{id}',
},
},
});
This will allow you to pass the id parameter in the request:
client.getUser({ path: { id: '123' } });
// 'path' with property 'id' is required here
Parsers
To automatically convert the return type, just pass a parser function to the endpoint definition:
type User = { id: string; name: string };
const client = createFastClient({
base: 'https://api.example.com',
endpoints: {
getUsers: {
method: 'GET',
href: '/users',
parser: (res) => res.json() as Promise<User[]>,
},
},
});
Now, when you call getUsers
, the result will parsed and the return type will be of type User[]
:
const res: Promise<User[]> = client.getUsers({
query: { name: 'matheus', limit: '10', order: 'asc' },
});
Middlewares
Use a middleware to define a function to intercept the request and response of all endpoints:
const client = createFastClient({
base: 'https://api.example.com',
endpoints: { ... },
async middleware(req, next) {
// modify the request
req.headers.append('X-Application-Key', 'ABC123');
// call next to continue the pipeline
const res = await next(req);
// return the response
return res;
},
});