/**
 * Pizzly(JS) > Integration
 */ import fetch from 'node-fetch' ; import Types from './types' ; import { cleanHeaders , toURL } from './utils' ; export default class PizzlyAPI { private key : string ; private integration : string ; private origin : string ; constructor ( integration , key , origin ) { this . integration = integration ; this . origin = origin ; this . key = key ; } /**
     * `get` perform get request
     */ public get = ( endpoint , parameters ? ) => { return this . request ( 'GET' , endpoint , parameters ) ; } ; /**
     * `head` perform head request
     */ public head = ( endpoint , parameters ? ) => { return this . request ( 'HEAD' , endpoint , parameters ) ; } ; /**
     * `post` perform post request
     */ public post = ( endpoint , parameters ? ) => { return this . request ( 'POST' , endpoint , parameters ) ; } ; /**
     * `put` perform put request
     */ public put = ( endpoint , parameters ? ) => { return this . request ( 'PUT' , endpoint , parameters ) ; } ; /**
     * `delete` perform delete request
     */ public delete = ( endpoint , parameters ? ) => { return this . request ( 'DELETE' , endpoint , parameters ) ; } ; /**
     * `patch` perform patch request
     */ public patch = ( endpoint , parameters ? ) => { return this . request ( 'PATCH' , endpoint , parameters ) ; } ; /**
     * Make the HTTP request
     * (using node-fetch behind the scene)
     */ public request = ( method , endpoint , parameters = { } ) => { if ( parameters && typeof parameters !== 'object' ) { throw new Error ( 'Unable to trigger API request. Request parameters should be an object in the form "{ headers: { "Foo": "bar" }, body: "My body" }' ) ; } const headers = { } ; if ( this . key ) { const authentication = 'Basic ' + Buffer . from ( this . key + ':' ) . toString ( 'base64' ) ; headers [ 'Authorization' ] = authentication ; } const url = toURL ( this . origin , `/api/${ this . integration }` , endpoint , parameters . query ) ; return fetch ( url . toString ( ) , { method , headers : cleanHeaders ( headers ) , body : parameters && parameters . body } ) ; } ; }