Skip to content

🌐 A concise and efficient abstraction layer built on top of the Javascript Fetch API

Notifications You must be signed in to change notification settings

techwithmanuel/query-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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();
}

Note. More features would be added soon

About

🌐 A concise and efficient abstraction layer built on top of the Javascript Fetch API

Resources

Stars

Watchers

Forks