Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
/ moltin-request Public archive

๐ŸŽฎ Minimal Elastic Path Commerce Cloud API request library for Node

License

Notifications You must be signed in to change notification settings

moltin/moltin-request

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Elastic Path Commerce Cloud @moltin/request

npm version semantic-release code style: prettier License: MIT contributions welcome follow on Twitter

๐ŸŽฎ Minimal Elastic Path Commerce Cloud API request library for Node

Examples ยท API Reference

Installation

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

Quickstart (implicit)

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

const client = new MoltinClient({
  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 { MoltinClient } = require('@moltin/request')
// import { MoltinClient } from '@moltin/request'

const client = new MoltinClient({
  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)

Quickstart (with storage)

To prevent unnecessary authentication requests, you will want to use a storage adapter.

Node Local Storage

const { MoltinClient } = require('@moltin/request')
const NodeStorageAdapter = require('@moltin/node-storage-adapter')

const client = new MoltinClient({
  client_id: '...',
  storage: new NodeStorageAdapter('./localStorage')
})

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

window.localStorage

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

class LocalStorageAdapter {
  set(key, value) {
    return window.localStorage.setItem(key, value)
  }

  get(key) {
    return window.localStorage.getItem(key)
  }

  delete(key) {
    return window.localStorage.removeItem(key)
  }
}

const client = new MoltinClient({
  client_id: '...',
  storage: new LocalStorageAdapter()
})

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

Quickstart (with custom fetch)

This library uses cross-fetch to make requests. If you wish to change this library, you can pass a custom fetch when instantiating a new moltin client.

const { MoltinClient } = require('@moltin/request')
const fetchEverywhere = require('fetch-everywhere')

const client = new MoltinClient({
  client_id: '...',
  fetch: fetchEverywhere
})

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

Kitchen sink

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

const client = new MoltinClient({
  client_id: '...',
  client_secret: '...',
  fetch: customFetch,
  storage: new NodeStorageAdapter('./moltin'),
  host: '...',
  version: '...',
  application: '...',
  currency: '...',
  customer_token: '...',
  headers: {
    // ...
  }
})

Custom headers per request

The Elastic Path Commerce Cloud 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 Elastic Path Commerce Cloud.

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

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

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

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

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

Terms And Conditions

  • Any changes to this project must be reviewed and approved by the repository owner. For more information about contributing, see the Contribution Guide.
  • For more information about the license, see MIT License.