GithubHelp home page GithubHelp logo

rekuest's Introduction

rekuest

A functional style HTTP request composer, heavily inspired by the kewler color manipulation module.

Disclaimer! This is an untested proof of concept with the main aim to get me learning functional programming. It "should work" though! ;)

rekuest lets you compose HTTP request endlessly until you invoke a rekuest object without any arguments.

import { rekuest, method, url, uri, body, params } from 'rekuest'

const apiRequest = rekuest(url('http://api.com'))
const userUpdateRequest = apiRequest(uri('/users/:id'), method('put'))
const dryUserUpdateRequest = userUpdateRequest(body({ dry: 'sure-thing' }))
const dryUserUpdateRequestWithData = dryUserUpdateRequest(
  body(userData),
  params({ id: someUserId })
)

// It feels like we are ready to make the request.
dryUserUpdateRequestWithData().then(/* handle response */)

Examples

Note: We are largely omitting import of rekuest methods

Create a base request modifier, which we will use in the succeeding examples:

import { rekuest, url } from 'rekuest'

export const api = rekuest(url('http://my.api.com'))

Fetch some articles:

const getArticles = api(uri('/articles'))
getArticles().then(/* do stuff */)

Update a user:

const updateUser = api(
  uri('/users/:id'),
  params({ id: someUserId }),
  body({ name: 'Boop' })
)

updateUser().then(/* ... */)

Make some handy request helpers:

const get = api(method('get'))
const put = api(method('put'))
const post = api(method('post'), headers({ 'Content-Type': 'multipart/form-data' }))
const del = api(method('delete'))

Go nuts and make an API resource factory:

const resource = (name) => {
  return {
    list: () => get(uri(`/${name}`))(),
    get: (id) => get(uri(`/${name}/:id`), params({ id }))(),
    create: (data) => post(uri(`/${name}`), body(data))(),
    update: (id, data) => put(uri(`/${name}/:id`), params({ id }), body(data))(),
    remove: (id) => del(uri(`/${name}/:id`), params({ id }))(),
  }
}

// Later on...
const articles = resource('articles')

articles.list().then(response => console.log(response))
articles.get(421).then(/* ... */)
articles.create({ title: 'My article' }).then(/* ... */)
articles.update(421, { title: 'My improved article' }).then(/* ... */)
articles.remove(421).then(/* ... */)

rekuest's People

Contributors

alexanderwallin 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.