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

Can't import moltin using es6 #68

Closed eashish93 closed 7 years ago

eashish93 commented 7 years ago

Hi, I can't be able to import moltin in react using es6. I got the following error: Uncaught TypeError: (0 , _moltin.gateway) is not a function

 // this doesn't work
import {gateway as MoltinGateway} from 'moltin'; 

It because I think you've not provided any index.js file in src folder.

The only way to make it work using dist folder:

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

Please add the index.js file in src folder.

ynnoj commented 7 years ago

@eashish93 Are you using webpack in your project?

The issue is the entry point defined in package.json. I'll be publishing a new release today which should fix this issue. I'm working with the master branch of the library locally in a Vue.js store (with webpack configuration).

eashish93 commented 7 years ago

Yes, I'm using official create-react-app which comes with webpack. Also, there's another problem in the sdk doesn't have a method for querying things like products?page[limit]=10.

ynnoj commented 7 years ago

@eashish93 We're still working to bring all API functionality to the SDK.

eashish93 commented 7 years ago

Currently I'm also using master branch with slight modifications to support querying like this:

export default class Collections {
    constructor(config) {
        this.config = config;
        this.request = new Request(config);
    }

    Tree(qs) { 
      // where qs  is a string with things like: `page[limit]=10`.
        return this.request.send(`collections?${qs}`, 'GET');
    }

}
ynnoj commented 7 years ago

@eashish93 Filtering, sorting and pagination will be separate methods which can be chained. For example:

Moltin.Products.Sort('name').Limit(10).Offset(20).All();
eashish93 commented 7 years ago

I've read the code. There's no such method in any files in master branch currently.

ynnoj commented 7 years ago

We're still working to bring all API functionality to the SDK.

@eashish93 As I said above ☝️ 🙃

ynnoj commented 7 years ago

@eashish93 Are you able to import the library now via the master branch? I'll go ahead and close this issue.

eashish93 commented 7 years ago

yes, but not as given in the doc. It should be:

import {moltin} from 'moltin';
const Moltin = moltin.gateway({
client_id: 'xxxx'
});
ynnoj commented 7 years ago

@eashish93 How is your project setup? Are you using Webpack?

eashish93 commented 7 years ago

I'm using facebook create-react-app which include webpack.

nclsmitchell commented 7 years ago

Hi @eashish93 ,

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 ;)