This package has been deprecated

Author message:

useless and production-not ready, please dont use it

lewys

1.0.0 • Public • Published

Lewys

Manage your api nice and easely with Lewys. Fetch "under the hood" single point client.

Fetures

Installation

Npm: npm i lewys --save

Yarn: yarn add lewys

Importing

ES modules

import lewys from 'lewys'

Commonjs

const lewys = require('lewys')

Creating an instance

lewys.init(options)

const client = lewys.init({ /* Initial Options */})

Instance options

All of those options not required for initilizing

Prop Type Default 📝
baseURL string ➡️
timeout number 30000 ➡️
headers array or object ➡️
serializer function ➡️
beforeResponse function ➡️
beforeRequest function ➡️

You can also define those options after client initializing:

const client = lewys.init()

client.defaults.baseURL = 'https://lol.kek/api'
client.defaults['baseURL'] = 'https://lol.kek/api'

Base URL

This options defines base url for requests.

lewys.init({
    baseURL: 'https://lol.kek/api'
})

Timeout

This options sets time(in ms), after which your request will be aborted. Please read about Aborting requests.

lewys.init({
    timeout: 24000    
})

Headers

This options allows you to set your own request headers.

lewys.init({
    headers: { 'X-SOME-HEADER': 'LOL-KEK' }
})

Or:

lewys.init({
    headers: ['X-SOME-HEADER': 'LOL-KEK']
})

Serializer

This options is a intecepotor reqest params. You can handle params any way you want, but have to return string value. By defult your params will be converted to JSON.

import Qs from 'qs'

lewys.init({
    selrializer (params) {
        return Qs.serilize(params)
    }
})

Before Response

This options is a intecepotor for response. Will be called before every fetch resolving. You will get a Fetch Response instance. It is a pomise with same as you usually get after Fetching You can return any value and you will get it after resolve lewys requst method.

const client = lewys.init({
    beforeResponse (res) {
        return res.json().then(data => data)
    },
})

client.request(/* options */)
    .then(res => console.log(res))

Pay attention that Response instance can't be chaged, if you try - you will get error. It's readonly!!

lewys.init({
    beforeResponse (res) {
        res.body = 'something' // will throw error
    },
})

Before Request

This options is a intecepotor for request. Will be called before every request. You will get a Fetch Request instance. And you have to return Request istance as well, because Fetch API requeire it. You can pass the same instance you get in arguments:

lewys.init({
    beforeRequest (req) {
        console.log(req.method, req.url)
        return req
    },
})

Or return Another instance:

lewys.init({
    beforeRequest (req) {
        if (req.method === 'PUT') {
            return new Request(req.url, {
                method: 'PATCH'
            })
        } else {
            return another
        }
    },
})

Pay attention that Request instance, same to Response, can't be chaged, if you try - you will get error. It's readonly!!

lewys.init({
    beforeRequest (req) {
        req.body = 'something' // will throw error
    },
})

Instance Request method

lewys#request(options)

Examples:

const client = lewys.init({
    baseURL: 'https://some.cool'
})

const posts = client.request({
    url: '/api/posts'
    method: 'get',
    params: { kek: 'lol' }
})

const createPost = client.request({
    url: '/api/create/post'
    method: 'post',
    headers: { 'X-CUSTOM-HEADER': 'LOLOLOL' },
    body: JSON.stringify({ title: 'new post', text: 'text' })
})

Instance.request method returns you a Fetch Response promise by default, but you can intercept and handle it in beforeResponse

Request options

Only url property are required

Property Description
url Relative or absolute request URL
method Requst method. Default 'GET'
params Request params. You can handle it in paramsSerializer
headers Additional request Headers
body Only for POST, PATCH and PUT

Also you can pass any options you want just like that:

client.request({
    url: '/some/api',

    mode: 'origin',
    creditals: 'omit'
})

And they will be included to your Fetch Request.

Aborting Requests

There are only one way to abort Fetch Reqeusts - AbortCotroller API. Your requests will be aborted at the end of timeout, but only if your browser supports aborting Fetch.

Browser Support

Checkout Fetch browser support.

Package Sidebar

Install

npm i lewys

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • kwoosh