|
| 1 | +# @vue-storefront/sdk-nuxt |
| 2 | + |
| 3 | +## Quick Setup |
| 4 | + |
| 5 | +1. Add `@vue-storefront/sdk-nuxt` dependency to your project |
| 6 | + |
| 7 | +```bash |
| 8 | +# Using pnpm |
| 9 | +pnpm add -D @vue-storefront/sdk-nuxt |
| 10 | + |
| 11 | +# Using yarn |
| 12 | +yarn add --dev @vue-storefront/sdk-nuxt |
| 13 | + |
| 14 | +# Using npm |
| 15 | +npm install --save-dev @vue-storefront/sdk-nuxt |
| 16 | +``` |
| 17 | + |
| 18 | +2. Add `@vue-storefront/sdk-nuxt` to the `modules` section of `nuxt.config.ts` |
| 19 | + |
| 20 | +```js |
| 21 | +export default defineNuxtConfig({ |
| 22 | + modules: ["@vue-storefront/sdk-nuxt"], |
| 23 | +}); |
| 24 | +``` |
| 25 | + |
| 26 | +3. Configure the module |
| 27 | + |
| 28 | +There are two ways you can configure the SDK module. The first is by using the `vsfSdk` key in the Nuxt configuration object and providing necessary information such as the Middleware instance address: |
| 29 | + |
| 30 | +```ts |
| 31 | +export default defineNuxtConfig({ |
| 32 | + modules: ["@vue-storefront/sdk-nuxt"], |
| 33 | + vsfSdk: { |
| 34 | + apiBaseUrl: "localhost:4000", |
| 35 | + apiProtocol: "http", |
| 36 | + apiSubpath: "", |
| 37 | + isMultistoreEnabled: false, |
| 38 | + }, |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +The second is to use Runtime Config. You can set the same set of variables in Runtime Config, providing access to this data throughout the application. You can control the values of these variables through environment variables: |
| 43 | + |
| 44 | +```ts |
| 45 | +export default defineNuxtConfig({ |
| 46 | + modules: ["@vue-storefront/sdk-nuxt"], |
| 47 | + runtimeConfig: { |
| 48 | + public: { |
| 49 | + apiBaseUrl: "localhost:4000", |
| 50 | + }, |
| 51 | + }, |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +4. Create SDK config file - `sdk.config.ts` in root directory of your project: |
| 56 | + |
| 57 | +The `defineSdkConfig` function is used for this purpose. The parameter for calling this function should be an anonymous function that receives an injected context from the module, containing: |
| 58 | + |
| 59 | +- the `buildModule` function, |
| 60 | +- the middleware URL (`middlewareUrl`), |
| 61 | +- a function for retrieving the Set-Cookie header with cookie values (`getCookieHeader`). |
| 62 | +- a function that compose Middleware URL - in case you want to do it by yourself (`composeMiddlewareUrl`) |
| 63 | +- a module config |
| 64 | + |
| 65 | +You should import all other SDK configuration components. See the example below illustrating the SDK configuration with Unified and Contentful modules. |
| 66 | + |
| 67 | +```ts |
| 68 | +import { |
| 69 | + contentfulModule, |
| 70 | + ContentfulModuleType, |
| 71 | +} from "@vsf-enterprise/contentful-sdk"; |
| 72 | +import { unifiedModule } from "@vsf-enterprise/unified-sdk"; |
| 73 | +import type { UnifiedApiExtension } from "../storefront-middleware/middleware.config"; |
| 74 | + |
| 75 | +export default defineSdkConfig( |
| 76 | + ({ buildModule, middlewareUrl, getCookieHeader }) => ({ |
| 77 | + unified: buildModule(unifiedModule<UnifiedApiExtension>, { |
| 78 | + apiUrl: middlewareUrl + "/commerce", |
| 79 | + requestOptions: { |
| 80 | + headers: getCookieHeader, |
| 81 | + }, |
| 82 | + }), |
| 83 | + contentful: buildModule<ContentfulModuleType>(contentfulModule, { |
| 84 | + apiUrl: middlewareUrl + "/cntf", |
| 85 | + }), |
| 86 | + }), |
| 87 | +); |
| 88 | +``` |
| 89 | + |
| 90 | +That's it! You can now use VueStorefront SDK in your Nuxt app ✨ |
0 commit comments