moltin / js-sdk

JavaScript & Node.js SDKs for the Elastic Path Commerce Cloud eCommerce API
http://documentation.elasticpath.com/
MIT License
173 stars 77 forks source link

Library disfunctional using Create-React-App #77

Closed BoyPersoon closed 7 years ago

BoyPersoon commented 7 years ago

I am currently working on a new project I already started using Moltin and manage to get the Authentication working plus a few commands, however I am running into tons of issues! I have added the node: { fs: 'empty' }to the module webpack config file. Let me talk you through the Code I tried using:

normal import as in the git docs doesn't seem to work

import { gateway as MoltinGateway } from 'moltin';  
       const Moltin = MoltinGateway({}) // is not a function

so I used:

       import {gateway as MoltinGateway} from 'moltin/dist/moltin.cjs.js';

        const Moltin = MoltinGateway({
            grant_type: 'client_credentials',
            clientId: 'XXXX',
            client_secret: 'XXXXX',
       });

// MoltinGateway doesn't set the clientId in the config object ???

      Moltin.config.clientId = 'XXXXXX';

      Moltin.Authenticate().then((response) => {
      console.log('authenticated', response); // getting an implicit connection instead of the client_credentials
      });

Great the connection worked! But now the Cart commands just don't exist, except Cart.Get call ?

// not working commands!

      Moltin.Cart.AddProduct
      Moltin.Cart.Items
      Moltin.Cart.Checkout

From the offical moltin js docs using MoltinClient already seems to fail as it doesn't exist:


      npm install moltin

      import { MoltinClient } from 'moltin';

       const moltin = MoltinClient({
            client_id: 'XXXX'
      }); //MoltinClient doesn't exist. 

I need this library to function correctly! Could anybody tell me one of the following things? -Why wasn't the clientId set in MoltenGateway? -how to access the Cart functions? -Why are both the official docs plus the github docs not leading to a correct import of the Moltin.Gateway module or MoltenClient (official docs)? -Does this have to do with webpack within Create-react-app?

Thank you in advance!

nclsmitchell commented 7 years ago

Hi @BoyPersoon,

I encountered the same problem as you, I fixed it by using the following:

import {gateway as MoltinGateway} from 'moltin'

and then:

const Moltin = MoltinGateway({
    client_id: 'XXXX',
})

The syntax client_id is the key ;)

ynnoj commented 7 years ago

As stated above, client_id is the correct key to use when instantiating:

import { gateway as MoltinGateway } from '@moltin/sdk';

const Moltin = MoltinGateway({
  client_id: 'XXX',
});

I've fixed the documentation, thanks for highlighting the inconsistencies!