UNPKG

@shopify/cli-kit

Version:

A set of utilities, interfaces, and models that are common across all the platform features

68 lines 2.52 kB
import { CLI_KIT_VERSION } from '../../../public/common/version.js'; import { firstPartyDev } from '../../../public/node/context/local.js'; import { AbortError } from '../../../public/node/error.js'; import https from 'https'; class RequestClientError extends AbortError { constructor(message, statusCode) { const tryMessage = statusCode === 403 ? 'Ensure you are using the correct account. You can switch with `shopify auth login`' : undefined; super(message, tryMessage); this.statusCode = statusCode; } } export class GraphQLClientError extends RequestClientError { // eslint-disable-next-line @typescript-eslint/no-explicit-any constructor(message, statusCode, errors) { super(message, statusCode); this.errors = errors; this.stack = undefined; } } /** * Removes the sensitive data from the headers and outputs them as a string. * @param headers - HTTP headers. * @returns A sanitized version of the headers as a string. */ export function sanitizedHeadersOutput(headers) { const sanitized = {}; const keywords = ['token', 'authorization', 'subject_token']; Object.keys(headers).forEach((header) => { if (keywords.find((keyword) => header.toLocaleLowerCase().includes(keyword)) === undefined) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion sanitized[header] = headers[header]; } }); return Object.keys(sanitized) .map((header) => { return ` - ${header}: ${sanitized[header]}`; }) .join('\n'); } export function buildHeaders(token) { const userAgent = `Shopify CLI; v=${CLI_KIT_VERSION}`; const headers = { 'User-Agent': userAgent, 'Keep-Alive': 'timeout=30', // 'Sec-CH-UA': secCHUA, This header requires the Git sha. 'Sec-CH-UA-PLATFORM': process.platform, 'Content-Type': 'application/json', ...(firstPartyDev() && { 'X-Shopify-Cli-Employee': '1' }), }; if (token) { const authString = token.match(/^shp(at|ua|ca|tka)/) ? token : `Bearer ${token}`; headers.authorization = authString; headers['X-Shopify-Access-Token'] = authString; } return headers; } /** * This utility function returns the https.Agent to use for a given service. */ export async function httpsAgent() { return new https.Agent({ rejectUnauthorized: true, keepAlive: true, }); } //# sourceMappingURL=headers.js.map