-
Notifications
You must be signed in to change notification settings - Fork 1
Documenation
This Github Wiki serves as documentation for AxleJS.
Documentation for version 1.1.0.
The Axle Object is the default export of AxleJS. It includes all HTTP Restful Methods, middleware, middleware options, cancelMark, all (Promise.all), use, and useOptions.
const Axle = {
post: post,
get: get,
delete: deleteReq,
put: put,
patch: patch,
head: head,
all: (promises: Promise<AxleResponse | undefined>[]) => {
return Promise.all(promises);
},
cancelMark: AxleCancelMark,
use: use,
useOptions: useOptions,
middleware: {
timeTook: timeTook,
},
middlewareOptions: {
cors: cors,
kneepads: kneepads,
},
};
AxleTypes is the 2nd export of AxleJS. It has all types for AxleJS and is only for Typescript users. It includes AxleOptions, AxleMiddleware, and AxleError.
export namespace AxleTypes {
export interface AxleOptions {
mode?: 'no-cors' | 'cors' | 'same-origin';
cache?:
| 'no-cache'
| 'default'
| 'reload'
| 'force-cache'
| 'only-if-cached';
credentials?: RequestCredentials;
headers?: AxleHeaders;
redirect?: 'manual' | 'follow' | 'error';
referrer?: string;
referrerPolicy?:
| 'no-referrer'
| 'no-referrer-when-downgrade'
| 'origin'
| 'origin-when-cross-origin'
| 'same-origin'
| 'strict-origin'
| 'strict-origin-when-cross-origin'
| 'unsafe-url';
body?: string | FormData;
keepalive?: boolean;
signal?: AbortSignal | null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window?: any;
integrity?: string;
handleStatus?: (status: number, statusMessage: string) => boolean;
}
export type AxleMiddleware = (
req: AxleRequest,
res: AxleResponse
) => unknown;
export interface AxleError {
status: number;
message: string;
response: AxleResponse;
request: AxleRequest;
}
}
AxleJS has all HTTP Restful Methods which is GET, POST, PUT, PATCH, and DELETE. AxleJS also has the HEAD method.
Example:
import Axle from 'pathToAxle';
(async function () {
const res = await Axle.post('https://example.com/test'); // response is returned if no errors
const json = await res.json();
return json;
})()
Makes a GET request to the provided URL.
- | 1 | 2 |
---|---|---|
Parameter | URL | Options |
Type | String | AxleOptions |
Default | - | { mode: 'cors', cache: 'default' } |
AxleResponse
Makes a HEAD request to the provided URL.
- | 1 | 2 |
---|---|---|
Parameter | URL | Options |
Type | String | AxleOptions |
Default | - | { mode: 'cors', cache: 'default' } |
AxleResponse
Makes a POST request to the provided URL.
- | 1 | 2 | 3 |
---|---|---|---|
Parameter | URL | Data | Options |
Type | String | Object | FormData | AxleOptions |
Default | - | {} | mode: 'cors', cache: 'default', credentials: 'same-origin', headers: { 'Content-Type': 'application/json'}}` |
AxleResponse
Makes a PUT request to the provided URL.
- | 1 | 2 | 3 |
---|---|---|---|
Parameter | URL | Data | Options |
Type | String | Object | FormData | AxleOptions |
Default | - | {} | mode: 'cors', cache: 'default', credentials: 'same-origin', headers: { 'Content-Type': 'application/json'}}` |
AxleResponse
Makes a PATCH request to the provided URL.
- | 1 | 2 | 3 |
---|---|---|---|
Parameter | URL | Data | Options |
Type | String | Object | FormData | AxleOptions |
Default | - | {} | mode: 'cors', cache: 'default', credentials: 'same-origin', headers: { 'Content-Type': 'application/json'}}` |
AxleResponse
Makes a DELETE request to the provided URL.
- | 1 | 2 | 3 |
---|---|---|---|
Parameter | URL | Data | Options |
Type | String | Object | FormData | AxleOptions |
Default | - | {} | mode: 'cors', cache: 'default', credentials: 'same-origin', headers: { 'Content-Type': 'application/json'}}` |
AxleResponse
AxleJS Middleware is a function callback that is applied/ run on every request.
- timeTook - Logs and measures the time it took to complete the request.
AxleJS has build in middleware such as timeTook located in the middleware object in Axle.
To use middleware, call Axle.use().
Example:
import Axle from 'pathToAxle';
Axle.use(Axle.middleware.timeTook());
Axle.use takes a callback function with two parameters being AxleRequest, and AxleResponse.
Example:
import Axle from 'pathToAxle';
Axle.use((req, res) => {
console.log(res.url, res.status);
});
A middleware function must return a callback that is a AxleMiddleware
.
Example:
import Axle from 'pathToAxle';
function customMiddlewareFunction() {
// return callback
return (req, res) => {
res.redirect('https://example.com');
}
}
Axle.use(customMiddlewareFunction());
AxleJS Middleware Options is an AxleOptions
object that is applied to every request's options.
- cors - Sets mode -> cors, and cache -> default
- kneepads - Sets credentials -> omit, referrer -> '', and referrerPolicy -> no-referrer
AxleJS allows to store AxleOptions
as a return in functions to use as a Axle Middleware Option.
AxleJS has built-in middleware options such as cors, and kneepads.
Example:
import Axle from 'pathToAxle';
Axle.useOptions({
...Axle.middlewareOptions.cors(),
...Axle.middlewareOptions.kneepads(),
})
// OR
Axle.useOptions(Axle.middlewareOptions.cors())
Axle.useOptions(Axle.middlewareOptions.kneepads())
Axle.useOptions takes an AxleOptions
object.
Example:
import Axle from 'pathToAxle';
Axle.useOptions({
// custom options
mode: 'no-cors',
cache: 'default'
})
Returns parsed JSON that was received.
Returns the text that was received.
Returns the data that was read from the bodyReader.
Redirects to a URL.
Automatically follows redirect by new Location Header.
Returns the Blob that was received.
Returns the query parameter by name or null if not found.
Returns ReadableStream | null from the received body.
Returns a boolean based on if the body has been processed.
Returns the status of the response.
Returns the status message of the response.
Returns the URL of the response.
Returns an object of the query string in the response url.
Returns headers of the response in AxleHeaders.
Returns the amount of milliseconds it took to complete the request.
Returns the response type of the response.
Returns a normal fetch response instead of using AxleResponse.
Appends a header.
Deletes a header. Returns boolean based on if deleted or not.
Deletes a header by value. Returns boolean based on if deleted or not.
Returns the value of a header by its name.
Returns the name of a header by its value.
Sets the value of a header by its name.
Checks if a header exists by its name.
Checks if a header exists by its value.
Loops through each header and calls a callback passing the name, value, and index.
Returns the names of the headers.
Returns the values of the headers.
Returns the names of the headers as an array.
Returns the values of the headers as an array.
Converts Headers
into an Object
.
Checks if a header is equal to a value. Returns boolean if exists and null if header does not exist.
Checks if a header's value has part of a passed value. Returns boolean if exists and null if header does not exist.
Returns the header's entries.
Returns Headers instead of AxleHeaders.
Runs AbortController.abort() to cancel the request and if a message is passed it also runs console.error(message).
The AbortSignal to pass to your request's signal option. Needed to run CancelMark.cancel().
CancelMark uses an AbortController and an AbortSignal to cancel a request.
Example:
import Axle from 'pathToAxle'
const cancelMark = new Axle.CancelMark();
const cancelSignal = cancelMark.signal;
Axle.get('https://example.com/test', {
signal: cancelSignal
// ... your options
}).then((res) => {
// ...
}).catch((e) => {
// ...
})
// cancel
cancelMark.cancel('Canceled request!')
Uses Promise.all() to run multiple AxleRequests.
- | 1 |
---|---|
Parameter | promises |
Type | Promise[] |
Default | - |
Promise<AxleRequest>[]
Creates and Returns an instance of a CancelMark.
- | - |
---|---|
Parameter | - |
Type | - |
Default | - |
CancelMark
Tells Axle to use a middleware
- | 1 |
---|---|
Parameter | middleware |
Type | AxleMiddleware |
Default | - |
Number
Tells Axle to use a middleware option
- | 1 |
---|---|
Parameter | middlewareOption |
Type | AxleOptions |
Default | - |
Number