Coder Social home page Coder Social logo

kolektor's Introduction

Kolektor

A wrapper for the default fetch api to make some more complex use cases easier to work with, without adding too much bloat.

Usage:

const { kolekt } = useKolekt({
    url: "https://jsonplaceholder.typicode.com/posts", // Request url
    method: "POST", 
    middleware?: (requestData)=>{ return requestData.map((e)=>{
        return {title:e.title}
    })},
    body: JSON.stringify({
    title: 'foo',
    body: 'bar',
    userId: 1,
  }),
   headers: {
    'Content-type': 'application/json; charset=UTF-8',
  },
    onRequestStart?: () => {
        console.log("Request started");
    },
    onRequestEnd?: (returnData) =>{
        console.log("Response data:");
        conosle.log(returnData);
        console.log("Request done");
    },
});

kolekt();

useKolekt

This works similar to a react hook. When calling useKolekt only 2 params are required.

url, method

Calling useKolekt returns the kolekt function and doesn't invoke the request it self.

To invoke the request call the kolekt function.


middleware:

This is a optional callback. It is called on the response data on every request.

onRequestStart:

This is a lifecycle function called before the request is made.

onRequestEnd:

This is a lifecycle function called after the request is over. It is passed the response data after running the middleware function.

body:

Optional param, this is the request body.

headers:

Optional param, these are the request headers.

kolekt():

Calling this functions sends the request defined when calling the useKolekt hook.
This functions accepts an optional middleware param. it can be invoked normally.

    await kolekt() 

or with the middleware callback

//Currently the type of the responseData param is any.
    await kolekt((responseData)=>{
        return parseData(responseData);
    }) 

This is called on the specific request and is independent from the middleware passed to useKolekt.
The middleware passed to useKolekt is called after the middleware passed to kolekt directly.


Response data and typing:

There is full support for typescript. The function useKolket can take in an expected return type:

const { kolekt } = useKolekt<someType>({...})

const res = await kolekt();
// the type of res is Promise<IReturnType<someType>>

where return type is:

interface IReturnType<T> {
  status: { code: number, message: string }
  data?: T 
} 

or

interface IReturnType<someType> {
  status: { code: number, message: string }
  data?: someType 
}

The default type for the response data, is any.

kolektor's People

Contributors

asentientbanana avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.