šŸŽ® Minimal Moltin API request library for Node
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Initial commit Aug 2, 2018
examples example: zeit micro + now (#77) Dec 17, 2018
src fix: throw error when fetch is not ok (#81) Dec 21, 2018
.gitignore Initial commit Aug 2, 2018
.prettierignore Initial commit Aug 2, 2018
.prettierrc Initial commit Aug 2, 2018
LICENSE Feat: Custom host (#5) Aug 2, 2018
README.md update examples list Dec 21, 2018
jest.config.js Initial commit Aug 2, 2018
package.json chore(deps): update dependency @types/fetch-mock to v7 (#80) Jan 2, 2019
renovate.json
tsconfig.json
yarn.lock chore(deps): update dependency @types/fetch-mock to v7 (#80) Jan 2, 2019

README.md

@moltin/request

npm version semantic-release code style: prettier

šŸŽ® Minimal Moltin API request library for Node

Installation

yarn add @moltin/request # npm install @moltin/request

Quickstart (implicit)

const { createClient } = require('@moltin/request')
// import { createClient } from '@moltin/request'

const client = new createClient({
  client_id: '...'
})

client
  .get('products')
  .then(console.log)
  .catch(console.error)

Quickstart (client credentials)

āš ļø You should not use client credentials on the client-side. You will expose your client secret, read more about authentication here.

const { createClient } = require('@moltin/request')
// import { createClient } from '@moltin/request'

const client = new createClient({
  client_id: '...',
  client_secret: '...'
})

client
  .post('brands', {
    name: 'My Special Brand',
    slug: 'my-special-brand',
    status: 'live'
  })
  .then(console.log)
  .catch(console.error)

client
  .put('brands/:id', {
    name: 'My Special Brand!'
  })
  .then(console.log)
  .catch(console.error)

client
  .delete('brands/:id')
  .then(console.log)
  .catch(console.error)

Kitchen sink

const { createClient } = require('@moltin/request')
// import { createClient } from '@moltin/request'

const client = new createClient({
  client_id: '...',
  client_secret: '...',
  host: '...',
  version: '...',
  application: '...',
  currency: '...',
  customer_token: '...',
  headers: {
    // ...
  }
})

Custom headers per request

The Moltin API provides you the ability to send various request headers that change the way data is stored or retrieved.

By default this library will encode all data as JSON, however you can customise this by setting your own Content-Type header as an additional argument to get, post, put and delete.

This argument can be used to get products by enabled currency, language or even use for uploading files to Moltin.

Note: If you add the Content-Type custom header to post, put or delete you will need to encode data yourself.

const { createClient } = require('@moltin/request')

const client = new createClient({
  client_id: '...'
})

const headers = {
  "X-Moltin-Currency": "gbp"
}

client
  .get('products', headers)
  .then(console.log)
  .catch(console.error)

Examples

The examples below demonstrate how you connect this library with other frameworks and tools.