-
Notifications
You must be signed in to change notification settings - Fork 115
refactor(theme): disable @vue-storefront/core $vsf context plugin #741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { IncomingMessage } from 'node:http'; | ||
| import { Context as NuxtContext } from '@nuxt/types'; | ||
| import merge from 'lodash.merge'; | ||
|
|
||
| export type ApiClientMethod = (...args: any) => Promise<any>; | ||
|
|
||
| interface CreateProxiedApiParams { | ||
| givenApi: Record<string, ApiClientMethod>; | ||
| client: any; | ||
| tag: string; | ||
| } | ||
|
|
||
| export const getBaseUrl = (req: IncomingMessage, basePath: string | undefined = '/'): string => { | ||
| if (!req) return `${basePath}api/`; | ||
| const { headers } = req; | ||
| // eslint-disable-next-line global-require, unicorn/prefer-module | ||
| const isHttps = require('is-https')(req); | ||
| const scheme = isHttps ? 'https' : 'http'; | ||
| const host = headers['x-forwarded-host'] || headers.host; | ||
|
|
||
| return `${scheme}://${host}${basePath}api/`; | ||
| }; | ||
|
|
||
| export const createProxiedApi = ({ givenApi, client, tag }: CreateProxiedApiParams) => new Proxy(givenApi, { | ||
| get: (target, prop, receiver) => { | ||
| const functionName = String(prop); | ||
| if (Reflect.has(target, functionName)) { | ||
| return Reflect.get(target, prop, receiver); | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/require-await | ||
| return async (...args) => client | ||
| .post(`/${tag}/${functionName}`, args) | ||
| .then((r) => r.data); | ||
| }, | ||
| }); | ||
|
|
||
| export const getCookies = (context: NuxtContext) => context?.req?.headers?.cookie ?? ''; | ||
|
|
||
| export const getIntegrationConfig = (context: NuxtContext, configuration: any) => { | ||
| const cookie = getCookies(context); | ||
| const initialConfig = merge({ | ||
| axios: { | ||
| baseURL: getBaseUrl(context?.req, context?.base), | ||
| headers: { | ||
| ...(cookie ? { cookie } : {}), | ||
| }, | ||
| }, | ||
| }, configuration); | ||
|
|
||
| return initialConfig; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /** | ||
sethidden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * It extends given integartion, defined by `tag` in the context. | ||
| */ | ||
| export const createExtendIntegrationInCtx = ({ tag, nuxtCtx, inject }) => (integrationProperties) => { | ||
| const integrationKey = '$' + tag; | ||
|
|
||
| if (!nuxtCtx.$vsf || !nuxtCtx.$vsf[integrationKey]) { | ||
| inject('vsf', { [integrationKey]: {} }); | ||
| } | ||
|
|
||
| Object.keys(integrationProperties) | ||
| .filter(k => !['api', 'client', 'config'].includes(k)) | ||
| .forEach(key => { | ||
| nuxtCtx.$vsf[integrationKey][key] = integrationProperties[key]; | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * It creates a function that adds an integration to the context under the given name, defined by `tag`. | ||
| */ | ||
| export const createAddIntegrationToCtx = ({ tag, nuxtCtx, inject }) => (integrationProperties) => { | ||
| const integrationKey = '$' + tag; | ||
|
|
||
| if (nuxtCtx.$vsf && !nuxtCtx.$vsf[integrationKey]) { | ||
| nuxtCtx.$vsf[integrationKey] = integrationProperties; | ||
| return; | ||
| } | ||
|
|
||
| inject('vsf', { [integrationKey]: integrationProperties }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { Context as NuxtContext, Plugin as NuxtPlugin } from '@nuxt/types'; | ||
sethidden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import axios from 'axios'; | ||
| import { createExtendIntegrationInCtx, createAddIntegrationToCtx } from './context'; | ||
| import { getIntegrationConfig, createProxiedApi } from './_proxyUtils'; | ||
|
|
||
| type InjectFn = (key: string, value: any) => void; | ||
| export type IntegrationPlugin = (pluginFn: NuxtPlugin) => NuxtPlugin; | ||
|
|
||
| const parseCookies = (cookieString: string): Record<string, string> => Object.fromEntries(cookieString | ||
| .split(';') | ||
| // eslint-disable-next-line unicorn/no-array-callback-reference | ||
| .filter(String) | ||
| .map((item) => item.split('=').map((part) => part.trim())) | ||
| .map(([name, value]) => [name, value])); | ||
|
|
||
| const setCookieValues = (cookieValues: Record<string, string>, cookieString = '') => { | ||
| const parsed = parseCookies(cookieString); | ||
|
|
||
| // eslint-disable-next-line no-return-assign | ||
| Object.entries(cookieValues).forEach(([name, value]) => parsed[name] = value); | ||
|
|
||
| return Object.entries(parsed).map(([name, value]) => `${name}=${value}`).join('; '); | ||
| }; | ||
|
|
||
| export const integrationPlugin = (pluginFn: NuxtPlugin) => (nuxtCtx: NuxtContext, inject: InjectFn) => { | ||
| const configure = (tag, configuration) => { | ||
| const injectInContext = createAddIntegrationToCtx({ tag, nuxtCtx, inject }); | ||
| const config = getIntegrationConfig(nuxtCtx, configuration); | ||
| const { middlewareUrl, ssrMiddlewareUrl } = (nuxtCtx as any).$config; | ||
|
|
||
| if (middlewareUrl) { | ||
| config.axios.baseURL = process.server ? ssrMiddlewareUrl || middlewareUrl : middlewareUrl; | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
| const client = axios.create(config.axios); | ||
| const api = createProxiedApi({ givenApi: configuration.api || {}, client, tag }); | ||
|
|
||
| if ((nuxtCtx.app.i18n as any).cookieValues) { | ||
| // @ts-ignore | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
| client.defaults.headers.cookie = setCookieValues((nuxtCtx.app.i18n as any).cookieValues, (client.defaults.headers as any).cookie); | ||
| } | ||
|
|
||
| injectInContext({ api, client, config }); | ||
| }; | ||
|
|
||
| const extend = (tag, integrationProperties) => { | ||
| createExtendIntegrationInCtx({ tag, nuxtCtx, inject })(integrationProperties); | ||
| }; | ||
|
|
||
| const integration = { configure, extend }; | ||
|
|
||
| pluginFn({ ...nuxtCtx, integration } as NuxtContext, inject); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import path from 'node:path'; | ||
|
|
||
| export default function VueStorefrontContextShimModule() { | ||
| this.addPlugin(path.resolve(__dirname, './plugins/context.js')); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { configureContext } from '@vue-storefront/core'; | ||
| import { useContext } from '@nuxtjs/composition-api'; | ||
|
|
||
| const setUpBackwardsCompatibilityForSharedRefs = () => { | ||
| const useVSFContext = () => { | ||
| const { $sharedRefsMap } = useContext(); | ||
| return { $sharedRefsMap }; | ||
| }; | ||
| configureContext({ useVSFContext }); | ||
| }; | ||
|
|
||
| export default (_ctx, inject) => { | ||
| setUpBackwardsCompatibilityForSharedRefs(); | ||
| inject('sharedRefsMap', new Map()); // for sharedRef | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.