|
| 1 | +import type { Nuxt } from '@nuxt/schema' |
| 2 | +import type { z } from 'zod' |
| 3 | + |
| 4 | +import type { configObjectSchema } from './config' |
| 5 | + |
| 6 | +import { ShopifyClientType } from './config' |
| 7 | +import { createStoreDomain } from '../runtime/utils/clients/transport' |
| 8 | +import { isInstalled } from '../utils/install' |
| 9 | +import { useLogger } from '../utils/log' |
| 10 | +import { SESSION_PASSWORD_ENV, generateSessionPassword, persistSessionPassword } from '../utils/session' |
| 11 | + |
| 12 | +type ShopifyConfig = z.output<typeof configObjectSchema> |
| 13 | + |
| 14 | +const HYDROGEN = '@shopify/hydrogen' |
| 15 | +const HYDROGEN_REACT = '@shopify/hydrogen-react' |
| 16 | + |
| 17 | +const proxyableClients = [ShopifyClientType.Storefront, ShopifyClientType.CustomerAccount] as const |
| 18 | + |
| 19 | +function resolveRequirements(config: ShopifyConfig) { |
| 20 | + const logger = useLogger() |
| 21 | + |
| 22 | + if (config.clients[ShopifyClientType.CustomerAccount] && !isInstalled(HYDROGEN)) { |
| 23 | + logger.error(`The customer account client is configured but \`${HYDROGEN}\` is not installed. Install it (e.g. \`npm i ${HYDROGEN}\`) or remove \`shopify.clients.customerAccount\`. Disabling customer account client.`) |
| 24 | + |
| 25 | + config.clients[ShopifyClientType.CustomerAccount] = undefined |
| 26 | + } |
| 27 | + |
| 28 | + if (config.analytics && !isInstalled(HYDROGEN_REACT)) { |
| 29 | + logger.error(`Analytics is enabled but \`${HYDROGEN_REACT}\` is not installed. Install it (e.g. \`npm i ${HYDROGEN_REACT}\`) or disable \`shopify.analytics\`. Disabling analytics.`) |
| 30 | + |
| 31 | + config.analytics = false |
| 32 | + } |
| 33 | + |
| 34 | + if ( |
| 35 | + config.analytics |
| 36 | + && !config.clients[ShopifyClientType.Storefront]?.publicAccessToken |
| 37 | + && !config.analytics.consent?.storefrontAccessToken |
| 38 | + ) { |
| 39 | + logger.error('Analytics is enabled but no public storefront access token is set. Set `clients.storefront.publicAccessToken` or `analytics.consent.storefrontAccessToken`. Disabling analytics.') |
| 40 | + |
| 41 | + config.analytics = false |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function resolveProxies(config: ShopifyConfig, nuxt: Nuxt) { |
| 46 | + if (nuxt.options.ssr && !(nuxt.options as { _generate?: boolean })._generate) return |
| 47 | + |
| 48 | + const logger = useLogger() |
| 49 | + |
| 50 | + for (const clientType of proxyableClients) { |
| 51 | + const client = config.clients[clientType] |
| 52 | + |
| 53 | + if (!client?.proxy) continue |
| 54 | + |
| 55 | + logger.info(`Disabling the ${clientType} proxy: server-side request proxying requires SSR. Requests are sent to Shopify directly.`) |
| 56 | + |
| 57 | + client.proxy = false |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +async function resolveCustomerAccountApiUrl(config: ShopifyConfig) { |
| 62 | + const customerAccount = config.clients[ShopifyClientType.CustomerAccount] |
| 63 | + |
| 64 | + if (!customerAccount) return |
| 65 | + |
| 66 | + const logger = useLogger() |
| 67 | + |
| 68 | + if (customerAccount.apiUrl) { |
| 69 | + logger.debug(`Using the configured customer account API URL: ${customerAccount.apiUrl}`) |
| 70 | + |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + const wellKnownURL = createStoreDomain(config.name) + '/.well-known/customer-account-api' |
| 75 | + |
| 76 | + const apiUrl = await fetch(wellKnownURL) |
| 77 | + .then(async res => (await res.json() as { graphql_api: string }).graphql_api) |
| 78 | + .catch(() => undefined) |
| 79 | + |
| 80 | + if (apiUrl) { |
| 81 | + logger.debug(`Resolved customer account API URL: ${apiUrl}`) |
| 82 | + } |
| 83 | + else { |
| 84 | + logger.warn( |
| 85 | + `Could not resolve the customer account API URL from \`${wellKnownURL}\` - ` |
| 86 | + + 'customer account requests will fail (is the Customer Account API enabled for your store?, ' |
| 87 | + + 'or set `clients.customerAccount.apiUrl` explicitly)', |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + customerAccount.apiUrl = apiUrl |
| 92 | +} |
| 93 | + |
| 94 | +async function resolveSessionPassword(config: ShopifyConfig, nuxt: Nuxt) { |
| 95 | + const session = config.clients[ShopifyClientType.CustomerAccount]?.session |
| 96 | + |
| 97 | + if (!session || session.password) return |
| 98 | + |
| 99 | + const logger = useLogger() |
| 100 | + const envPassword = process.env[SESSION_PASSWORD_ENV] |
| 101 | + |
| 102 | + if (envPassword) { |
| 103 | + session.password = envPassword |
| 104 | + |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + if (!nuxt.options.dev) { |
| 109 | + logger.warn(`No customer account session password set - customer account sessions will fail until the \`${SESSION_PASSWORD_ENV}\` environment variable is set`) |
| 110 | + |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + session.password = generateSessionPassword() |
| 115 | + |
| 116 | + await persistSessionPassword(nuxt.options.rootDir, session.password) |
| 117 | + |
| 118 | + logger.info('Generated a customer account session password in `.env`') |
| 119 | +} |
| 120 | + |
| 121 | +export async function resolveConfig(config: ShopifyConfig, nuxt?: Nuxt): Promise<ShopifyConfig> { |
| 122 | + if (!nuxt) return config |
| 123 | + |
| 124 | + resolveRequirements(config) |
| 125 | + resolveProxies(config, nuxt) |
| 126 | + |
| 127 | + await resolveCustomerAccountApiUrl(config) |
| 128 | + await resolveSessionPassword(config, nuxt) |
| 129 | + |
| 130 | + return config |
| 131 | +} |
0 commit comments