1.0.5 • Published 1 year ago

client-api-router v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Currently available methods

+ .get() / [prop].get()
+ .post() / [prop].post()
+ .put() / [prop].put()
+ .delete() / [prop].delete()
+ .patch() / [prop].patch()

Other functionality
+ Choosing a request method
+ URL parameters "?prop=value"
+ Send headers

Quick start

ApiRouter

import { ApiRouter } from "client-api-router"
const Api = ApiRouter()

Api.users.moderators.get() // users/moderators

Add base url

// Method one
const Api = ApiRouter("http://example.api")

Api.posts.get() // http://example.api/posts GET
// Method two
const Api = ApiRouter({ baseUrl: "http://example.api.com" })

Api.posts.get() // http://example.api/posts GET
// Method three
const Api = ApiRouter()
Api.setUrl("http://example.api")

Api.posts.get() // http://example.api/posts GET

GET

const Api = ApiRouter("https://jsonplaceholder.typicode.com")

// $1 params

// Get all posts
Api.posts.get({ json: true }) // https://jsonplaceholder.typicode.com/posts    GET

// Get one post
Api.posts[1].get({ json: true }) // https://jsonplaceholder.typicode.com/posts/1  GET

POST PUT PATCH DELETE

const Api = ApiRouter("https://dummyjson.com")

// $1 body, $2 params
Api.carts.add.post(JSON.stringify({
  userId: 1,
  products: [
    { id: 1, quantity: 1 },
    { id: 50, quantity: 2 }
  ]
}), { json: true }) // https://dummyjson.com/carts/add  POST

URL search parameters

const Api = ApiRouter("https://dummyjson.com")

const response = await Api.products.get({
  json: true,
  search: {
    limit: 2
  }
})

console.log(response) // Two products

Request headers

const Api = ApiRouter("https://dummyjson.com")

const response = await Api.products.get({
  json: true,
  search: { limit: 2 }
})

Default headers

const Api = ApiRouter({
  baseUrl: "https://dummyjson.com",
  baseHeaders: {
    "Content-type": "application/json",
    "Accept": "application/json"
  }
})

// Now default headers will be applied to all requests

const response = await Api.products.get({
  json: true,
  search: { limit: 2 }
})

setHeader, removeHeader

const Api = ApiRouter({
  baseUrl: "https://dummyjson.com",
  baseHeaders: {
    "Content-type": "application/json"
  }
})

Api.setHeader("Accept", "application/json")
Api.removeHeader("Content-type")
1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago