npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

magento2-client

v1.3.1

Published

This is a client for connecting to a Magento2 API.

Downloads

129

Readme

magento2-client

Easily access Magento2 API

Install with npm

npm install magento2-client

Usage

Parameters

You can create a Magento2-Client with these parameters:

| Parameter | Data Type | Definition | | ------------- | ----------- | --------------------------------------------------------------------------------- | | baseUrl | String | This is the URL of the front end of the Magento installation. Example: 'https://www.example.com'. This can optionally contain a base path like: 'https://www.example.com/rel-5.3.2' | | username | String | This is the username to login to the Magento2 admin console. | | password | String | This is the password for the supplied Magento2 admin username. | | options | JSON Object | Optional Contains any optional connection parameters. |

Options

These options are allowed in the options object parameter

| Name | Data Type | Default | Definition | | ------------------ | --------- | ------- | -------------------------------------------------------------------- | | version | String | V1 | Magento API version to use when getting the authorization token. | | rejectUnauthorized | boolean | true | If set to false, self signed or bad SSL certificates will be allowed |

Example, create magento-client

const Magento = require('magento2-client');

var magento = new Magento('https://www.example.com', 'username', 'password', {version: "v1", rejectUnauthorized: true});

Magento API documentation: http://devdocs.magento.com/guides/v2.0/rest/list.html.

To use the magento client created above to make requests, use the request interface:

magento.request(method, url, urlParams, data, callback);

where the parameters are defined as follows:

| Parameter | Data Type | Definition | | ----------| ----------- | -------------------------------------------------------------------- | | method | String | HTTP method to use, 'POST', 'GET', 'PUT', 'DELETE' | url | String | The REST endpoint. Example: '/V1/products'. | urlParams | JSON Object | Key value pairs representing URL parameters. Example: { 'searchCriteria[pageSize]': 10, 'searchCriteria[currentPage]': 1 } | data | JSON Object | The body to send to the request. Example: {"attributeSet":{"attribute_set_name":"Pants","entity_type_id":4},"skeletonId":4} | callback | function | The callback function to trigger after completing the request. Follows the standard (err, data) model

Examples, call request method

With callback:

magento.request('GET',                      //method
                '/V1/categories',           //url
                { searchCriteria: "\'\'" }, //urlParams
                {},                         //data
                function(err, data) {       //callback
  if (err) {
    // TODO handle errors
  }
  console.log('categories = ' + JSON.stringify(data, null, 2));
});

magento.request('GET',                      //method
                '/V1/orders',               //url
                {                           //urlParams
                  'searchCriteria[pageSize]': 10,
                  'searchCriteria[currentPage]': 1 
                },
                {},                         //data
                function(err, data) {       //callback
  if (err) {
    // TODO handle errors
  }
  console.log('orders = ' + JSON.stringify(data, null, 2));
});

With promises:

magento.request('GET',                    //method
                '/V1/orders',             //url
                {                         //urlParams
                  'searchCriteria[pageSize]': 10,
                  'searchCriteria[currentPage]': 1 
                }, 
                {})                       //data
  .then((data) => { 
    console.log('orders = ' + JSON.stringify(data, null, 2));
  })
  .catch((err) => {
    // TODO handle errors
    console.log(err);
  });

magento.request('GET',                    //method
                '/V1/products',           //url
                {                         //urlParams
                  'searchCriteria[pageSize]': 10,
                  'searchCriteria[currentPage]': 1 
                }, 
                {})                       //data
  .then((data) => { 
    console.log('products = ' + JSON.stringify(data, null, 2));
  })
  .catch((err) => {
    // TODO handle errors
  });

Notes:

  1. A real Magento install should use https protocol. This module will work with an http site with a warning... Sometimes, you just need to test things out...
  2. An error with a message 'unable to verify the first certificate' indicates that the server is using a self signed certificate. Setting the rejectUnauthorized option to false will bypass this and display a warning, but this should only be used in test environments.
var magento = new Magento("https://www.example.com", "username", "password", {"rejectUnauthorized": false});
  1. There is a DEFAULT_VERSION set at 'V1'. This is used to get the authorization token. If you need to overide this, you can set it in the options of the new parameters. For example, if you want to use 'V2', you could do this. This is only for getting the Authorization token with the username and password. All the other routes are sent in the url parameter of the request method.
var magento = new Magento("https://www.example.com", "username", "password", {"options": "V2"});
  1. For convenience, there is a getBaseUrl function:
let baseUrl = magento.getBaseUrl();