Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tknf/typefetcher
TypeScript-first and very simple wrapper of Axios.
https://github.com/tknf/typefetcher
axios nodejs typescript
Last synced: 8 days ago
JSON representation
TypeScript-first and very simple wrapper of Axios.
- Host: GitHub
- URL: https://github.com/tknf/typefetcher
- Owner: tknf
- License: mit
- Created: 2022-03-02T02:27:07.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-05-25T13:24:55.000Z (over 2 years ago)
- Last Synced: 2024-09-14T12:15:16.959Z (2 months ago)
- Topics: axios, nodejs, typescript
- Language: TypeScript
- Size: 75.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Typefetcher [![npm version](https://badge.fury.io/js/@tknf%2Ftypefetcher.svg)](https://badge.fury.io/js/@tknf%2Ftypefetcher)
This library provides a TypeScript-first and very simple wrapper around Axios.
[Axios](https://github.com/axios/axios)
## Usage
### Installation
```bash
$ yarn add @tknf/typefetcher@latest
# or
$ npm install --save-dev @tknf/typefetcher@latest
```### Basic usage
```typescript
import { createClient } from "@tknf/typefechter";type ErrorCodes = "BAD_REQUEST" | "DATABASE_ERROR"
const client = createClient();
interface IResponse {
items: {
name: string
}[]
}// default request
const response = await client.get({
url: "/",
query: {
q: "dog"
}
});console.log(response.items); // items: IResponse["items"]
// safe request
const [err, res] = await client.get({
url: "/",
safe: true
})if (!err) {
console.log(res); // IResponse
} else {
console.log(err, res); // RequestError, null
}
```### Configuration
```typescript
createClient(config: ClientConfig);interface ClientConfig {
auth?: {
username: string;
password: string;
};
baseURL?: string;
headers?: Record;
maxBodyLength?: number;
maxContentLength?: number;
}
````ErrorCodes` is optional TypeScript args.
If your request return some error codes, `err.code` will be the type you defined. `typefetcher` can read the property `code`, `err.code`, and `error.code` of the response object when it throws an error.