1.0.2 • Published 5 years ago

mws-utils v1.0.2

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

Build Status Coverage Status

mws-utils

As the name suggests, this library provides some useful functions for dealing with requests to Amazon's MWS APIs.

Example usage:

import https from "https";
import {
  compose,
  sortEncode,
  withGlobals,
  withAction,
  withDate,
  withList,
  withSellerInfo,
  withSignature,
  withHeaders,
} from "mws-utils";

function getOrdersById(ids = []) {
  const endpointOptions = {
    host: "mws.amazonservices.com",
    path: "/Orders/2013-09-01",
    method: "POST",
  };
  const signedRequestPayload = compose(
    withSignature("SECRET_ACCESS_KEY")(endpointOptions), // You always want to sign last
    withList("AmazonOrderId.Id.")(ids),
    withAction("GetOrder", "2013-09-01"),
    withDate("Timestamp"),
    withSellerInfo({
      AWSAccessKeyId: "AWS_ACCESS_KEY_ID",
      SellerId: "SELLER_ID",
      MWSAuthToken: "MWS_AUTH_TOKEN",
    }),
    withGlobals
  )();
  return [
    sortEncode(signedRequestPayload), // And then url encode if you need to
    withHeaders(signedRequestPayload, endpointOptions),
  ];
}

const [signedRequestPayload, options] = getOrdersById(["SOME_ORDER_ID"]);

const r = https.request(options, (res) => {
  res.setEncoding("utf8");
  let xml = "";
  res.on("data", function (chunk) {
    xml = xml + chunk;
  });
  res.on("end", () => {
    console.log(res.statusCode);
    console.log(xml);
  });
});

r.on("error", (err) => {
  throw err;
});
r.write(signedRequestPayload);
r.end();
1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago