@vymalo/medusa-printful
Version:
Connect your MedusaJS store to Printful
58 lines • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ensurePrintfulWebhookSetup;
const core_1 = require("../../../core");
const client_gen_1 = require("../../../core/generated/printful/client.gen");
async function ensurePrintfulWebhookSetup({ logger, options, }) {
client_gen_1.client.instance.interceptors.request.use(async (config) => {
if (options.printfulAccessToken) {
config.headers.setAuthorization(`Bearer ${options.printfulAccessToken}`);
}
if (options.storeId) {
config.headers.set('X-PF-Store-Id', options.storeId);
}
logger.debug(`PRINTFUL -> ${config.method} -> ${config.url}`);
return config;
});
if (!options.enableWebhooks) {
logger.info('Printful webhooks are disabled');
}
else {
try {
await ensureWebhookExists(options, logger);
}
catch (e) {
logger.error(`Failed to ensure Printful webhook setup:`, e);
}
}
}
async function ensureWebhookExists({ backendUrl }, logger) {
const types = [
'product_synced',
'package_shipped',
'package_returned',
'order_created',
'order_updated',
'order_failed',
'order_canceled',
'product_updated',
'product_deleted',
'order_put_hold',
'order_put_hold_approval',
'order_remove_hold',
];
const { data: { result }, } = await core_1.WebhookApiService.getWebhooks();
if (result.url === `${backendUrl}/printful/webhook` &&
types.every((t) => result.types.includes(t))) {
logger.info('Printful webhook already set up');
return;
}
const { data: { result: newWebhook }, } = await core_1.WebhookApiService.createWebhook({
body: {
url: `${backendUrl}/printful/webhook`,
types,
},
});
logger.info(`Printful webhook set up ${newWebhook.types.join(', ')}`);
}
//# sourceMappingURL=ensure-printful-webhook-setup.js.map