1.0.3 • Published 4 years ago

exovision v1.0.3

Weekly downloads
11
License
MIT
Repository
github
Last release
4 years ago

ExoVision

Call remote HTTP RESTful services in easy way, both in browser and Node.js.

import exovision from 'exovision' // const exovision = require('exovision').default

// using process.ENV
const myRemoteService = exovision('MY_REMOTE_SERVICE') // process.env.MY_REMOTE_SERVICE === https://myremoteservice.com

// or using uri
const myRemoteService2 = exovision('https://myremoteservice.com')

// now we have 4 methods
myRemoteService.get()
myRemoteService.post()
myRemoteService.put()
myRemoteService.del()

All methods have same signature:

type ExovisionBoundTransmit = (
  path: string, // path to add on baseurl, eg. /rabbids
  data?: any, // can be any POJO: if the method is get, it will be transformed to querystring otherwise to body
  options?: ITransmissionOptions, // see below
) => Promise<any>

This is ITransmissionOptions interface:

interface ITransmissionOptions {
  contentType?: string // defaults to 'application/json'
  responseType?: string // defaults to 'json'
  transcodeResponse?<O, T>(data: O): T | Promise<T>
  echoCall?: boolean // simulates the call and returns request (for testing and debugging)
  token?: string // bearer login token, added to headers['Authorization'] = `Bearer ${token}`
}

transcodeResponse: custom transformation function. For example, if you need to convert xml to json you can pass a transform function that will be invoked after the remote call is done:

import { parse } from 'fast-xml-parser'

const trasformXmlToJson = (data: any) =>
  parse(data, { ignoreAttributes: false, attributeNamePrefix: '' })

myRemoteService.get('/rabbids', {}, {
  contentType: 'text/xml',
  responseType: 'text',
  transcodeResponse: trasformXmlToJson
})

All methods return a promise with parsed content (content type is JSON) or whatever transcodeResponse custom function returns.

In case of error during remote call it will be rertuned a rejected promise with interference instance

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago