Code Monkey home page Code Monkey logo

fetchr's Introduction

Fetchr

window.fetch wrapper with cacheStorage support, abortController, timeout and body transformation

A Fetchr request is a basic Request, with 3 additional options

  • cacheable: number // duration in ms of the CacheStorage entry, only for GET request
  • cancellable: boolean // give the request the power to be aborted
  • timeout: number // fix limit to bad performance

You can pass data to your request with

  • body: Standard implementation, you got the control
  • json: the object will be transformed with JSON.stringify
  • form: the object will be transformed with URLSearchParams
  • blob: will come soon

GET

await Fetchr({
    url: 'https://jsonplaceholder.typicode.com/users/7',
    cacheable: 1000 * 60 * 60, // 1 hour
    cancellable: true,
    timeout: 5000,
    headers: {
        Authorization: 'Bearer 1234abcd',
        ClientId: "1234",
        ClientSecret: "abcdef"
    }
}).then((res: FetchrResponseType) => {...})

POST as JSON

await Fetchr({
    url: 'https://jsonplaceholder.typicode.com/users/7',
    method: "POST",
    json: {
        username: "JohnD",
        firstname: "John",
        lastname: "Doe"
    },
    cancellable: true,
    timeout: 5000,
    headers: {
        Authorization: 'Bearer 1234abcd',
        ClientId: "1234",
        ClientSecret: "abcdef"
    }
}).then((res: FetchrResponseType) => {...})

POST as FormData

await Fetchr({
    url: 'https://jsonplaceholder.typicode.com/users/7',
    method: "POST",
    form: {
        username: "JohnD",
        firstname: "John",
        lastname: "Doe"
    },
    cancellable: true,
    timeout: 5000,
    headers: {
        Authorization: 'Bearer 1234abcd',
        ClientId: "1234",
        ClientSecret: "abcdef"
    }
}).then((res: FetchrResponseType) => {...})

Client

A Fetchr wrapper with sugar

Create your own configuration for one particular endpoint

  • shared configuration for all request
  • syntaxic sugar for duration
  • response type management (json, form, text, buffer, blob)
  • and more to come...

type="json" (default)

let myApi = new Client({
    headers: {
        clientId: '1a2b3c4d',
        clientSecret: 'azertyuiop'
    },
    cacheable: "1h",
    timeout: "10s"
})

myApi.GET('https://jsonplaceholder.typicode.com/users/7').then((res: FetchrResponseType) => {...})
myApi.GET({
    url: 'https://jsonplaceholder.typicode.com/users/7',
    cacheable: "5m"
}).then((res: FetchrResponseType) => {
    console.log(res.data) // { "id": 7, "name": "Kurtis Weissnat"...}
})

type="blob"

myApi.GET({
    url: 'https://mydomain.com/myImage.jpeg',
    cacheable: "5m",
    type: 'blob'
}).then((res: FetchrResponseType) => {
    console.log(res.data) // "data:image/jpeg;base64..."
})

type="text"

myApi.GET({
    url: 'https://mydomain.com/myFile.txt',
    cacheable: "5m",
    type: "text"
}).then((res: FetchrResponseType) => {
    console.log(res.data) // "lorem ipsum..."
})

fetchr's People

Contributors

tibolan avatar

Watchers

 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.