UNPKG

@shopify/shopify-api

Version:

Shopify API Library for Node - accelerate development with support for authentication, graphql proxy, webhooks

104 lines (101 loc) 3.83 kB
import { ShopifyError } from './error.mjs'; import { LogSeverity } from './types.mjs'; import { AuthScopes } from './auth/scopes/index.mjs'; import { logger } from './logger/index.mjs'; function validateConfig(params) { const config = { apiKey: '', apiSecretKey: '', hostName: '', hostScheme: 'https', isEmbeddedApp: true, isCustomStoreApp: false, logger: { log: defaultLogFunction, level: LogSeverity.Info, httpRequests: false, timestamps: false, }, future: {}, _logDisabledFutureFlags: true, }; // Make sure that the essential params actually have content in them const mandatory = [ 'apiSecretKey', 'hostName', 'apiVersion', ]; if (!('isCustomStoreApp' in params) || !params.isCustomStoreApp) { mandatory.push('apiKey'); } if ('isCustomStoreApp' in params && params.isCustomStoreApp) { if (!('adminApiAccessToken' in params) || params.adminApiAccessToken?.length === 0) { mandatory.push('adminApiAccessToken'); } } const missing = []; mandatory.forEach((key) => { if (!notEmpty(params[key])) { missing.push(key); } }); if (missing.length) { throw new ShopifyError(`Cannot initialize Shopify API Library. Missing values for: ${missing.join(', ')}. For apiVersion, please specify an explicit API version (e.g., ApiVersion.July25). See https://shopify.dev/docs/api/usage/versioning for more information.`); } const { hostScheme, isCustomStoreApp, adminApiAccessToken, userAgentPrefix, logger: logger$1, privateAppStorefrontAccessToken, customShopDomains, billing, future, ...mandatoryParams } = params; let scopes; if (params.scopes === undefined) { scopes = undefined; } else if (params.scopes instanceof AuthScopes) { scopes = params.scopes; } else { scopes = new AuthScopes(params.scopes); } Object.assign(config, mandatoryParams, { hostName: params.hostName.replace(/\/$/, ''), scopes, hostScheme: hostScheme ?? config.hostScheme, isCustomStoreApp: isCustomStoreApp ?? config.isCustomStoreApp, adminApiAccessToken: adminApiAccessToken ?? config.adminApiAccessToken, userAgentPrefix: userAgentPrefix ?? config.userAgentPrefix, logger: { ...config.logger, ...(logger$1 || {}) }, privateAppStorefrontAccessToken: privateAppStorefrontAccessToken ?? config.privateAppStorefrontAccessToken, customShopDomains: customShopDomains ?? config.customShopDomains, billing: billing ?? config.billing, future: future ?? config.future, }); if (config.isCustomStoreApp && params.adminApiAccessToken === params.apiSecretKey) { logger(config).warning("adminApiAccessToken is set to the same value as apiSecretKey. adminApiAccessToken should be set to the Admin API access token for custom store apps; apiSecretKey should be set to the custom store app's API secret key."); } return config; } function notEmpty(value) { if (value == null) { return false; } return typeof value === 'string' || Array.isArray(value) ? value.length > 0 : true; } function defaultLogFunction(severity, message) { switch (severity) { case LogSeverity.Debug: console.debug(message); break; case LogSeverity.Info: console.log(message); break; case LogSeverity.Warning: console.warn(message); break; case LogSeverity.Error: console.error(message); break; } } export { validateConfig }; //# sourceMappingURL=config.mjs.map