UNPKG

@shopify/shopify-app-express

Version:

Shopify Express Middleware - to simplify the building of Shopify Apps with Express

99 lines (96 loc) 4.27 kB
import semver from 'semver'; import '@shopify/shopify-api/adapters/node'; import { shopifyApi, LATEST_API_VERSION, FeatureDeprecatedError } from '@shopify/shopify-api'; import { MemorySessionStorage } from '@shopify/shopify-app-session-storage-memory'; import { SHOPIFY_EXPRESS_LIBRARY_VERSION } from './version.mjs'; import { validateAuthenticatedSession } from './middlewares/validate-authenticated-session.mjs'; import { ensureInstalled } from './middlewares/ensure-installed-on-shop.mjs'; import { cspHeaders } from './middlewares/csp-headers.mjs'; import { redirectToShopifyOrAppRoot } from './middlewares/redirect-to-shopify-or-app-root.mjs'; import { auth } from './auth/index.mjs'; import { processWebhooks } from './webhooks/index.mjs'; import { redirectOutOfApp } from './redirect-out-of-app.mjs'; function shopifyApp(config) { const { api: apiConfig, ...appConfig } = config; const api = shopifyApi(apiConfigWithDefaults(apiConfig ?? {})); const validatedConfig = validateAppConfig(appConfig, api); return { config: validatedConfig, api: api, auth: auth({ api, config: validatedConfig }), processWebhooks: processWebhooks({ api, config: validatedConfig }), validateAuthenticatedSession: validateAuthenticatedSession({ api, config: validatedConfig, }), cspHeaders: cspHeaders({ api }), ensureInstalledOnShop: ensureInstalled({ api, config: validatedConfig, }), redirectToShopifyOrAppRoot: redirectToShopifyOrAppRoot({ api, config: validatedConfig, }), redirectOutOfApp: redirectOutOfApp({ api, config: validatedConfig }), }; } function apiConfigWithDefaults(apiConfig) { let userAgent = `Shopify Express Library v${SHOPIFY_EXPRESS_LIBRARY_VERSION}`; if (apiConfig.userAgentPrefix) { userAgent = `${apiConfig.userAgentPrefix} | ${userAgent}`; } /* eslint-disable no-process-env */ return { apiKey: process.env.SHOPIFY_API_KEY, apiSecretKey: process.env.SHOPIFY_API_SECRET, scopes: process.env.SCOPES?.split(','), hostScheme: process.env.HOST?.split('://')[0], hostName: process.env.HOST?.replace(/https?:\/\//, ''), isEmbeddedApp: true, apiVersion: LATEST_API_VERSION, ...(process.env.SHOP_CUSTOM_DOMAIN && { customShopDomains: [process.env.SHOP_CUSTOM_DOMAIN], }), ...apiConfig, userAgentPrefix: userAgent, }; /* eslint-enable no-process-env */ } function validateAppConfig(config, api) { const { sessionStorage, ...configWithoutSessionStorage } = config; return { // We override the API package's logger to add the right package context by default (and make the call simpler) logger: overrideLoggerPackage(api.logger), useOnlineTokens: false, exitIframePath: '/exitiframe', sessionStorage: (sessionStorage ?? new MemorySessionStorage()), ...configWithoutSessionStorage, auth: config.auth, webhooks: config.webhooks, }; } function overrideLoggerPackage(logger) { const baseContext = { package: 'shopify-app' }; const warningFunction = (message, context = {}) => logger.warning(message, { ...baseContext, ...context }); return { ...logger, log: (severity, message, context = {}) => logger.log(severity, message, { ...baseContext, ...context }), debug: (message, context = {}) => logger.debug(message, { ...baseContext, ...context }), info: (message, context = {}) => logger.info(message, { ...baseContext, ...context }), warning: warningFunction, error: (message, context = {}) => logger.error(message, { ...baseContext, ...context }), deprecated: deprecated(warningFunction), }; } function deprecated(warningFunction) { return function (version, message) { if (semver.gte(SHOPIFY_EXPRESS_LIBRARY_VERSION, version)) { throw new FeatureDeprecatedError(`Feature was deprecated in version ${version}`); } return warningFunction(`[Deprecated | ${version}] ${message}`); }; } export { shopifyApp }; //# sourceMappingURL=index.mjs.map