query-proxy
v0.0.1
Published
A concise and efficient abstraction layer built on top of the Fetch API.
Downloads
2
Maintainers
Readme
Query Proxy Js
A concise and efficient abstraction layer built on top of the Fetch API.
Installation
npm i query-proxy
Get Started
1. Initialize a new query with the base url for your APIs
import Query from "query-proxy";
const query = new Query("https://jsonplaceholder.typicode.com");
2. Make a request with your query
async function handleRequest() {
const { response, loading, duration } = await query.get("/todos", {
...options,
});
}
Guide
1. The query methods include :
query.get(); // GET REQUEST
query.post(); // POST REQUEST
query.put(); // PUT REQUEST
query.delete(); // DELETE REQUEST
query.request(); // OTHER REQUESTS
2. If you are working with typescript you can pass a generic to the query methods to type the response.json()
directly
interface Todo {
completed: boolean;
id: number;
title: string;
userId: number;
}
async function handleRequest() {
const { response, loading, duration } = await query.get<Todo[]>("/todos", {
...options,
});
const data = await response.json();
//data would inherit the type Todo[]
}
3. the query methods accept all the request props that a normal fetch would with the inclusion of method
only for the query.request()
method
import Query from "query-js";
interface RequestParams {
headers?: HeadersInit;
body?: BodyInit | null;
mode?: RequestMode;
credentials?: RequestCredentials;
cache?: RequestCache;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
integrity?: string;
keepalive?: boolean;
signal?: AbortSignal | null;
window?: any;
}
const options: RequestParams = {};
interface Todo {
completed: boolean;
id: number;
title: string;
userId: number;
}
const query = new Query("https://jsonplaceholder.typicode.com");
async function handleRequest() {
const { response, loading, duration } = await query.get<Todo[]>("/todos", {
...options,
});
const data = await response.json();
}