Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

magik-api

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magik-api

Fluent API client based on rxjs

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by150%
Maintainers
1
Weekly downloads
 
Created
Source

Magik API

Fluent API client based on rxjs

Install

yarn add magik-api
npm install --save magik-api

Usage


// Start building
const api = magikApi()
  // Configure a base url
  .baseUrl('/v1')
  // Enabel tralling slash?
  .trailingSlash(true)
  // Confgure request
  // https://github.com/ReactiveX/rxjs/blob/6.x/src/internal/observable/dom/AjaxObservable.ts#L7
  .request({
    timeout: 23,
    responseType: 'text',
  })
  // Set query params
  .query({
    name: 'Mike'
  })
  // Configure headers
  .headers({
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  })
  // Map response, default is:
  .mapResponse(r => response)
  // Configure auth headers default is:
  .authHeaders(token => ({
    Authorization: token
  }))


api.get(
  // URL
  '/foo',
  // Query Params
  { name: 'Arturo' }
)

api.post( // Same .put .patch
  // URL
  '/foo',
  // BODY
  { name: 'Arturo' }
)

// Content-Type headers is set default to: application/json
// or nothing if U give FormData

api.delete(
  // URL
  '/foo',
  // Query Params
  { name: 'Arturo' }
)

// Authenticate a request
api.auth('Secret').get('/me')

// Curry an url
const getHello = api.url('/hello').get // Same for .put, .path, .post, .delete

getHello(
  // Query params
  { q: 'best' }
)

// Curry auth on url
const getAuthHello = api.url('/hello').curryAuth().get

getHello('TOKEN')(
  // Query params
  { q: 'best' }
)

// Resource
const todosApi = api.resource('/todos')

todosApi.list({ mode: 'dirty' })
// GET /todos?mode=dirty

todosApi.detail(23, { mode: 'dirty' })
// GET /todos/23?mode=dirty

todosApi.create({ mode: 'dirty' })
// POST /todos

todosApi.update(2, { mode: 'dirty' })
// PUT /todos/2

todosApi.partialUpdate(2, { mode: 'dirty' })
// PATCH /todos/2

todosApi.delete(2, { mode: 'dirty' })
// DELETE /todos/2

// ehehe this is a shorcut
todosApi.deleteId(2)
// DELETE /todos/2
// map response to ({ id: 2 }) util for 204 No Content styles responses

// You can use the other methods as well
todosApi.put('/23/toggle')
// PUT /todos/23/toggle

// Curry auth on resource
const todosApiAuth = api.resource('/todos').curryAuth()

todosApiAuth.detail('TOKEN')(33)
// Same for list, detail, update, partialUpdate, delete, deleteId

Keywords

FAQs

Package last updated on 14 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc