https://github.com/ariesclark/http
Simple, small and extendable http library for sending requests.
https://github.com/ariesclark/http
api http library rest
Last synced: 6 months ago
JSON representation
Simple, small and extendable http library for sending requests.
- Host: GitHub
- URL: https://github.com/ariesclark/http
- Owner: ariesclark
- License: apache-2.0
- Created: 2021-01-29T09:18:57.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-04-01T20:27:11.000Z (over 4 years ago)
- Last Synced: 2025-03-13T16:33:33.025Z (7 months ago)
- Topics: api, http, library, rest
- Language: TypeScript
- Homepage:
- Size: 80.1 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @rubybb/http
Simple, small and extendable http library for sending requests.[](https://discord.gg/WUgGJhS) []()   
## Install
Available on NPM: [@rubybb/http](https://www.npmjs.com/package/@rubybb/http)Ruby's recommended package manager:
[pnpm: 📦🚀 Fast, disk space efficient package manager](https://pnpm.js.org/).```
pnpm install @rubybb/http
```## Examples
```ts
import http, { HTTP } from "@rubybb/http";http.get("https://reqres.in/api/users/3", {resultType: "json"}).then((json) => {
console.log(json);
/* {
data: {
id: 3,
email: "emma.wong@reqres.in",
first_name: "Emma",
last_name: "Wong",
avatar: "https://reqres.in/img/faces/3-image.jpg"
}
} */
});const api = HTTP.create({
baseURL: "https://reqres.in/api/",
resultType: "json"
});api.get("users/3").then((json) => {...});
api.get("users/:id", {id: 3}).then((json) => {...});
```## API
**NOTE**: RequestInit refers to all [native fetch options](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request), and BodyInit refers to a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob), [BufferSource](https://developer.mozilla.org/en-US/docs/Web/API/BufferSource), [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData), [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams), [USVString](https://developer.mozilla.org/en-US/docs/Web/API/USVString), or [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) object.
```ts
export declare interface HTTPOptions extends RequestInit {
resultType: ResultType;
query: Record;
excludeDefaults: boolean;
baseURL: string;/* logs loads of information to console. */
debug: boolean;
/*
disables features like path name mutation,
request transforms, and other various things.
*/
minimal: boolean;/* prevents requests from throwing on response errors. */
nothrow: boolean;events?: {
override?: boolean;
pre?: (this: HTTP, path: string, options: PartialHTTPOptions) => Promise;
post?: (this: HTTP, result: any, path: string, options: PartialHTTPOptions) => Promise;
}/* for path params */
[key: string]: unknown
}// equivalent of new HTTP(options, immutable)
HTTP.create (options: Partial = {}, immutable? = false): HTTP;// mutate the current HTTP instance options.
http.mutate (options: Partial): HTTP;// copy the current instance options onto a new instance, and apply addtional options.
http.clone (options: Partial): HTTP;// send a request with the GET method.
http.get (path: string, options: Partial = {}): Promise;// send a request with the HEAD method.
http.head (path: string, options: Partial = {}): Promise;// send a request with the POST method and body.
http.post (path: string, body: BodyInit, options: Partial = {}): Promise;// send a request with the PATCH method and body.
http.patch (path: string, body: BodyInit, options: Partial = {}): Promise;// send a request with the PUT method and body.
http.put (path: string, body: BodyInit, options: Partial = {}): Promise;// send a request with the delete method and an optional body.
http.delete (path: string, body?: BodyInit, options: Partial = {}): Promise;
```