@agnostack/next-shopify
Version:
Please contact agnoStack via info@agnostack.com for any questions
74 lines • 4.01 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleAppUninstalled = void 0;
const verifyd_1 = require("@agnostack/verifyd");
const utils_1 = require("../../utils");
const shared_1 = require("../../../shared");
// NOTE: typically handling: "/api/webhooks/app/uninstalled"
const handleAppUninstalled = (serverRuntimeConfig, data) => {
const { returnResponse = true } = data !== null && data !== void 0 ? data : {};
return (req, res) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const handleResponse = (body, statusCode = 200) => {
if (returnResponse) {
return res.status(statusCode).json(body);
}
return { statusCode, body };
};
try {
const { 'x-shop-domain': shopDomain, 'x-shopify-shop-domain': shopifyShopDomain, 'x-shopify-shop': shop = shopifyShopDomain !== null && shopifyShopDomain !== void 0 ? shopifyShopDomain : shopDomain, } = (_a = req.headers) !== null && _a !== void 0 ? _a : {};
if ((0, shared_1.stringEmpty)(shop)) {
return handleResponse({ handler: 'uninstall', error: 'Missing shop' });
}
const { shopify, findSessionIds, deleteSessions } = yield (0, utils_1.getShopifyHelpers)(serverRuntimeConfig, { shop });
// TODO: explore getVerificationHelpers?
const rawBody = yield (0, verifyd_1.ensureRawBody)(req);
const _b = yield shopify.webhooks.validate({
rawBody,
rawRequest: req,
rawResponse: res,
}), { valid } = _b, webhookCheck = __rest(_b, ["valid"]);
if (valid) {
try {
const sessionsIds = yield findSessionIds(shop);
if ((0, shared_1.arrayNotEmpty)(sessionsIds)) {
yield deleteSessions(sessionsIds);
}
return handleResponse({ handler: 'uninstall', message: 'success' });
}
catch (error) {
console.error('Error handling uninstall', error);
return handleResponse({ handler: 'uninstall', error: (error === null || error === void 0 ? void 0 : error.message) || 'Error handling uninstall' });
}
}
console.warn('Unable to validate uninstall webhook', webhookCheck);
return handleResponse({ handler: 'uninstall', error: 'Unable to validate uninstall webhook' });
}
catch (error) {
console.error('Error handling uninstall', error);
return handleResponse({ handler: 'uninstall', error: 'Error handling uninstall' });
}
});
};
exports.handleAppUninstalled = handleAppUninstalled;
//# sourceMappingURL=uninstall.js.map