"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.raise = exports.handler = exports.addCors = exports.verifyWebhook = exports.hasShopHeaders = exports.hasShopCallback = exports.hasShopCharges = exports.hasShop = exports.hasBody = exports.onlyGET = exports.onlyPOST = void 0;

var crypto = _interopRequireWildcard(require("crypto"));

var _ramda = require("ramda");

var _cors = _interopRequireDefault(require("cors"));

var _config = require("./config");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const onlyPOST = req => req.method === "POST" ? req : raise(new Error(`Invalid HTTP method ${req.method}`));

exports.onlyPOST = onlyPOST;

const onlyGET = req => req.method === "GET" ? req : raise(new Error(`Invalid HTTP method ${req.method}`));

exports.onlyGET = onlyGET;

const hasBody = req => Object.keys(req.body).length !== 0 ? req : raise(new Error(`Missing body`));

exports.hasBody = hasBody;

const hasShop = req => req.body && req.body.shop && req.body.shop.length ? req : raise(new Error(`Missing shop in body`));

exports.hasShop = hasShop;

const hasShopCharges = req => {
  const {
    charge_id: chargeId,
    shop,
    token
  } = req.query;
  return chargeId && shop && token ? req : raise(new Error(`Missing shop, token or charge_id in query`));
};

exports.hasShopCharges = hasShopCharges;

const hasShopCallback = req => {
  const {
    code,
    shop,
    hmac,
    timestamp
  } = req.query;
  return code && shop && hmac && timestamp ? req : raise(new Error(`Missing shop, code, hmac or timestamp in query`));
};

exports.hasShopCallback = hasShopCallback;

const hasShopHeaders = req => req.get("x-shopify-shop-domain") && req.get("x-shopify-access-token") && req.get("x-shopify-api-version") ? req : raise(new Error(`Missing shopify headers`));

exports.hasShopHeaders = hasShopHeaders;

const verifyWebhook = req => {
  const hmac = req.headers["x-shopify-hmac-sha256"];
  const hash = crypto.createHmac("sha256", _config.SHOPIFY_API_SECRET).update(req.rawBody).digest("base64");
  return hash === hmac && req.headers["x-shopify-shop-domain"] ? req : raise(new Error("Signature could not be verified"));
};

exports.verifyWebhook = verifyWebhook;
const addCors = (0, _cors.default)({
  origin: true
});
exports.addCors = addCors;

const handler = (...fns) => (req, res) => {
  try {
    return (0, _ramda.pipe)(...fns)(req, res);
  } catch (err) {
    return res.status(200).json({
      status: "error",
      body: err.message
    });
  }
};

exports.handler = handler;

const raise = error => {
  throw error;
};

exports.raise = raise;
//# sourceMappingURL=utils.js.map