UNPKG

magicbell

Version:
97 lines 3.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withRequestHeaders = withRequestHeaders; const fetch_addons_1 = require("fetch-addons"); const pkg_js_1 = require("../lib/pkg.js"); function uuid4() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0; const v = c === 'x' ? r : (r & 0x3) | 0x8; return v.toString(16); }); } function getDefaultIdempotencyKey(method, maxRetries) { if (method.toUpperCase() !== 'POST' || maxRetries === 0) return; return `magicbell-retry-${uuid4()}`; } function setRequestHeaders(options, request) { const idempotencyKey = request.headers.get('idempotency-key') || options.idempotencyKey || getDefaultIdempotencyKey(request.method, options.maxRetries); // default headers request.headers.set('content-type', 'application/json'); request.headers.set('accept', 'application/json'); request.headers.set('idempotency-key', idempotencyKey); // this is the only endpoint that will ever work with the accept-version: v2 header if (new URL(request.url).pathname === '/notification_preferences') { request.headers.set('accept-version', 'v2'); } // user provided headers for (const [key, value] of Object.entries(options.headers || {})) { request.headers.set(key, value); } // can't set user-agent in the browser, see https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name request.headers.set('user-agent', typeof document !== 'undefined' ? '' : getUserAgent(options.appInfo)); // magicbell headers request.headers.set('x-magicbell-client-user-agent', getClientUserAgent(options.appInfo)); if (options.token) { request.headers.set('authorization', `Bearer ${options.token}`); (0, fetch_addons_1.deleteEmptyHeaders)(request.headers); return; } request.headers.set('x-magicbell-api-key', options.apiKey); if (options.apiSecret) { request.headers.set('x-magicbell-api-secret', options.apiSecret); } else if (options.userExternalId) { request.headers.set('x-magicbell-user-hmac', options.userHmac); request.headers.set('x-magicbell-user-external-id', options.userExternalId); } else if (options.userEmail) { request.headers.set('x-magicbell-user-hmac', options.userHmac); request.headers.set('x-magicbell-user-email', options.userEmail); } // remove empty headers, they can cause unexpected behavior (0, fetch_addons_1.deleteEmptyHeaders)(request.headers); } function withRequestHeaders(options) { return { beforeRequest: [(request) => setRequestHeaders(options, request)], }; } function getEnvInfo() { const common = { binding: pkg_js_1.pkg.name, binding_version: pkg_js_1.pkg.version, publisher: 'magicbell', }; if (typeof process === 'undefined') { return common; } return { ...common, binding: 'magicbell', runtime: process?.release?.name || 'node', runtime_version: process.version, platform: process.platform, arch: process.arch, }; } function getAppInfoAsString(appInfo) { if (!appInfo?.name) return ''; return [appInfo?.name, appInfo.version && `/${appInfo.version}`, appInfo.url && ` (${appInfo.url})`] .filter(Boolean) .join(''); } function getUserAgent(appInfo) { const env = getEnvInfo(); return [`${env.binding}`, 'runtime' in env && `${env.runtime}/${env.runtime_version}`, getAppInfoAsString(appInfo)] .filter(Boolean) .join(' '); } function getClientUserAgent(appInfo) { return JSON.stringify({ ...getEnvInfo(), application: appInfo }); } //# sourceMappingURL=headers.js.map