Pizzly Node.js client
Node.js client to query any APIs using Pizzly.
Installation
npm install pizzly-node
yarn add pizzly-node
Usage
Calling an API endpoint (proxy)
Once a user is connected, you can use its authId
to query any endpoint of the API.
const Pizzly = require('pizzly-node')
const pizzly = new Pizzly({ host: 'pizzly.example.org' })
const myAPI = pizzly.integration('x-api')
myAPI
.auth('x-auth-id')
.get('/x-endpoint')
.then(response => console.log)
.catch(console.error)
Most common HTTP methods are supported out-of-the-box, including .get()
, .post()
, .put()
and .delete()
.
Handling the response
Under the hood, we use node-fetch
to send requests. As a consequence, each response
from the API are Response
class of the node-fetch package. When the API uses JSON response type, you can retrieve the JSON response as follow:
myAPI
.auth('x-auth-id')
.get('/x-endpoint')
.then(response => response.json())
.then(data => console.log(data))
.catch(console.error)
Advanced usage
Using your secret key
It's highly recommended to secure your Pizzly instance after deployment (learn more). Once you've added a secret key, pass it to the client as follow:
const Pizzly = require('pizzly-node')
const pizzly = new Pizzly({
host: 'x-replace-with-your-pizzly-instance',
secretKey: 'x-replace-with-your-secret-key'
})
Here's how to send headers to the API:
myAPI
.auth('x-auth-id')
.post('/x-endpoint', { headers: { 'Content-Type': 'multipart/form-data; boundary=something' } })
.then(console.log)
.catch(console.error)
Here's how to send query string to the API:
myAPI
.auth('x-auth-id')
.post('/x-endpoint', { query: { search: 'some keywords' } })
.then(console.log)
.catch(console.error)
Sending body works the same way:
myAPI
.auth('x-auth-id')
.post('/x-endpoint', { body: 'My body' })
.then(console.log)
.catch(console.error)
Async / await
Using async/await
is supported to improve code readability:
const response = await pizzly.integration('x-api').get('/x-endpoint')
In that snippet, response
will be a Response
interface of the node-fetch package.
Dealing with multiple configurations
By default, each request made through Pizzly uses the latest configuration that you have saved. If you have multiple configurations in place for the same API, you can tell Pizzly which configuration should be used.
const config1 = '...'
const config2 = '...'
const github1 = pizzly.integration('github').setup(config1)
const github2 = pizzly.integration('github').setup(config2)
github1.get('/')
github2.get('/')
Reference
Pizzly Node.js client's reference:
const Pizzly = (options) => {
integration: {
setup: (setupId) => {},
auth: (authId) => {},
get: (endpoint[, options]) => {},
post: (endpoint[, options]) => {},
put: (endpoint[, options]) => {},
delete: (endpoint[, options]) => {},
head: (endpoint[, options]) => {},
patch: (endpoint[, options]) => {},
}
}