0.0.1 • Published 6 years ago

@copropr/base-service v0.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
6 years ago

Base Service

The base service to be extended by api's. It is mostly a wrapper to request-promise.

Installation

# NPM
$ npm i -S @copropr/base-service

# Yarn
$ yarn add @copropr/base-service

Usage

// Commonjs
const BaseService = require('@copropr/base-service').default

// ES6 module way
import BaseService from '@copropr/base-service'

class TestService extends BaseService {
  constructor (args) {
    super(args)
    // Extend the constructor here
  }

  async interceptRequest (request) {
    // Modify, log, or manipulate the request
    return request
  }

  async interceptResponse (response) {
    // Modify, log, or manipulate the response
    return response
  }
}

const service = new TestService({
  baseUrl: 'https://httpstat.us/',
  resolveWithFullResponse: true
})

const response = await service.get('/200')

*Note: Make sure the interceptRequest & interceptResponse return a promise or are using async/await to make sure any async operation that would modify the request or response are correctly intercepted.*

API

get method

A wrapper to request-promise get method

example:

const response = service.get('/uri', options)

post method

A wrapper to request-promise post method

example:

const response = service.post('/uri', options)

put method

A wrapper to request-promise put method

example:

const response = service.put('/uri', options)

patch method

A wrapper to request-promise patch method

example:

const response = service.patch('/uri', options)

delete method

A wrapper to request-promise delete method

example:

const response = service.delete('/uri', options)