Code Monkey home page Code Monkey logo

moltin-request's Introduction

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.

moltin-request's People

Contributors

notrab avatar outrunthewolf avatar renovate-bot avatar renovate[bot] avatar shaunmaharaj avatar ynnoj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

moltin-request's Issues

GET requests fails

After upgrading to the latest version I'm having trouble doing get-requests. It looks like a body is attached to the request which makes it fail. Here's the error I'm getting:

TypeError: Request with GET/HEAD method cannot have body
at new Request (/user_code/node_modules/@moltin/request/node_modules/node-fetch/lib/index.js:1191:10)
at /user_code/node_modules/@moltin/request/node_modules/node-fetch/lib/index.js:1369:19
at Object.fetch (/user_code/node_modules/@moltin/request/node_modules/node-fetch/lib/index.js:1367:9)
at Object.fetch (/user_code/node_modules/@moltin/request/node_modules/cross-fetch/dist/node-ponyfill.js:10:20)
at createClient. (/user_code/node_modules/@moltin/request/dist/index.js:89:46)
at step (/user_code/node_modules/@moltin/request/dist/index.js:43:23)
at Object.next (/user_code/node_modules/@moltin/request/dist/index.js:24:53)
at fulfilled (/user_code/node_modules/@moltin/request/dist/index.js:15:58)
at process._tickDomainCallback (internal/process/next_tick.js:135:7) }"

How to POST a file?

I have a Blob object and I want to send it to server.
Here is my code based on your docs:
`
const formData = new FormData();
formData.append("file_name", productName);
formData.append("public", "true");
formData.append("mime_type", "image/png");

canvas.toBlob(blob => {
formData.append("file", blob, productName);
});

const headers = {
"Content-Type": "image/png"
};
const data = {
body: formData
};
moltin
.post("files", data, headers)
.then(data => console.log(data))
.catch(err => console.error(err));
`

I am always getting {status: 500, title: "InternalError", detail: "Cannot read property 'file' of undefined", request_id: "xxx..."}

Any solution please!

Thanks in advance!

Dependency Dashboard

This issue provides visibility into Renovate updates and their statuses. Learn more

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update dependency @types/fetch-mock to v7.3.5
  • chore(deps): update dependency typescript to v3.9.10
  • fix(deps): update dependency cross-fetch to v3.1.5
  • chore(deps): update dependency semantic-release to v19

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.


  • Check this box to trigger a request for Renovate to run again on this repository

request_1.MoltinClient is not a constructor

Using version 2.0.0 with the following code:

import { MoltinClient } from '@moltin/request'

const client = new MoltinClient({
    client_id: process.env.MOLTIN_CLIENT_ID,
    client_secret: process.env.MOLTIN_CLIENT_SECRET
})

I think docs need some update!

I want to create a whishlist to my customers,

I follow this steps and I am stack in the #8 step. How to get: CUSTOMER_FLOW_ID

Even when I add the wishlist it is not shown in the customers custome field there is no field added.
And one other think. Please add array filed in filed type in the flows filed I think this will solve a lot of problems.

And, We are waiting more tutorials from your youtube channel.

Thanks!

Error if host is specified

If I try specifying a host, I get an error.

Example:

const client = new MoltinClient({
  client_id: 'some id',
  host: 'api.moltin.com',
});

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

I get this error:

error: TypeError: Only absolute URLs are supported
    at getNodeRequestOptions (/Users/user/dev/some-project/functions/node_modules/node-fetch/lib/index.js:1299:9)
    at /Users/user/dev/some-project/functions/node_modules/node-fetch/lib/index.js:1404:19
    at new Promise (<anonymous>)
    at MoltinClient.fetch (/Users/user/dev/some-project/functions/node_modules/node-fetch/lib/index.js:1401:9)
    at MoltinClient.fetch (/Users/user/dev/some-project/functions/node_modules/cross-fetch/dist/node-ponyfill.js:10:20)
    at MoltinClient.<anonymous> (/Users/user/dev/some-project/functions/node_modules/@moltin/request/dist/index.js:136:51)
    at step (/Users/user/dev/some-project/functions/node_modules/@moltin/request/dist/index.js:43:23)
    at Object.next (/Users/user/dev/some-project/functions/node_modules/@moltin/request/dist/index.js:24:53)
    at /Users/user/dev/some-project/functions/node_modules/@moltin/request/dist/index.js:18:71
    at new Promise (<anonymous>)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.