@aspida/fetch
Getting Started
Installation
Make HTTP request from application
src/index.ts
import aspida, { HTTPError } from "@aspida/fetch";
import api from "../api/$api";
const fetchConfig = {
credentials: "include",
baseURL: "https://example.com/api",
throwHttpErrors: true,
};
const client = api(aspida(fetch, fetchConfig));
(async () => {
const userId = 0;
const limit = 10;
await client.v1.users.post({ name: "mario" });
const res = await client.v1.users.get({ query: { limit } });
console.log(res);
try {
const user = await client.v1.users._userId(userId).$get();
console.log(user);
} catch (e) {
if (e instanceof HTTPError) {
console.log(e.response instanceof Response);
} else {
console.log(e.message);
}
}
})();
Serialize GET parameters manually
src/index.ts
import aspida, { HTTPError } from "@aspida/fetch";
import qs from "qs";
import api from "../api/$api";
const fetchConfig = {
paramsSerializer: params => qs.stringify(params),
};
const client = api(aspida(fetch, fetchConfig));
(async () => {
const users = await client.v1.users.$get({
query: { ids: [1, 2, 3] },
});
console.log(users);
})();
License
@aspida/fetch is licensed under a MIT License.