From c73de3e246bbbc01be950b8a9ef82d47b096754d Mon Sep 17 00:00:00 2001 From: Wojciech Sikora Date: Fri, 15 Mar 2024 13:22:02 +0100 Subject: [PATCH 01/17] update getting started --- .../4.sdk/2.getting-started/1.index.md | 193 ++++++++++-------- .../4.sdk/3.advanced/1.middleware-module.md | 25 +++ ...ending-module.md => 2.extending-module.md} | 0 .../4.sdk/3.advanced/3.custom-modules.md | 14 ++ 4 files changed, 142 insertions(+), 90 deletions(-) create mode 100644 docs/content/4.sdk/3.advanced/1.middleware-module.md rename docs/content/4.sdk/3.advanced/{extending-module.md => 2.extending-module.md} (100%) create mode 100644 docs/content/4.sdk/3.advanced/3.custom-modules.md diff --git a/docs/content/4.sdk/2.getting-started/1.index.md b/docs/content/4.sdk/2.getting-started/1.index.md index 23d6a1fc3c..30dfb2c4c9 100644 --- a/docs/content/4.sdk/2.getting-started/1.index.md +++ b/docs/content/4.sdk/2.getting-started/1.index.md @@ -1,8 +1,8 @@ -# Getting Started +# Getting Started -If you're setting your Vue Storefront application from scratch, you'll need to configure the SDK Core to create a type-safe SDK that communicates with your [Server Middleware](/middleware). +If you're setting your Vue Storefront application from scratch, you'll need to configure a type-safe SDK that communicates with your [Server Middleware](/middleware). -There are various ways to configure the SDK, depending on your chosen framework. For Next.js and Nuxt, you can use the `@vue-storefront/next` and `@vue-storefront/nuxt` modules respectively. If you're looking for framework agnostic experience, you can use the `@vue-storefront/core` module. +There are various ways to configure the SDK, depending on your chosen framework. For Next.js and Nuxt, you can use the `@vue-storefront/next` and `@vue-storefront/nuxt` packages respectively. If you're looking for framework agnostic experience, you can use the `@vue-storefront/sdk` package. ::tabs{:titles='["Next.js", "Nuxt", "Other"]' class="mt-8"} @@ -27,21 +27,20 @@ npm install --save-dev @vue-storefront/next To use SDK in our application, we need to initialize it first. To do so, follow these steps: -1. Create SDK Config file - `sdk.config.ts` in root directory of your project. +1. Create an `sdk` directory in the root of your project. + +1. Create SDK Config file - `sdk.config.ts` in the `sdk` directory. ::info -It is not necessary to name the file `sdk.config.ts` specifically or to keep it in the root of your project, but it is recommended to keep it consistent with the rest of the Vue Storefront project. +It is not necessary to name the file `sdk.config.ts` specifically or to keep it in the `sdk` directory, but it is recommended to keep it consistent with the rest of the Vue Storefront project. :: -2. Create the SDK configuration by importing the `createSdk` function from the Next.js SDK and the modules you want to use. +2. Create the SDK configuration by importing the `createSdk` function from the Next.js SDK and using the `middlewareModule` it provides. You should also import other modules you want to use. ```ts[sdk.config.ts] -import { sapccModule } from '@vsf-enterprise/sapcc-sdk'; -import { - contentfulModule, - ContentfulModuleType, -} from "@vsf-enterprise/contentful-sdk"; +import { contentfulModule } from "@vsf-enterprise/contentful-sdk"; import { CreateSdkOptions, createSdk } from "@vue-storefront/next"; +import { UnifiedEndpoints } from "storefront-middleware/types"; const options: CreateSdkOptions = { middleware: { @@ -49,13 +48,16 @@ const options: CreateSdkOptions = { } }; -export const { getSdk, createSdkContext } = createSdk( +export const { getSdk } = createSdk( options, - ({ buildModule, middlewareUrl, getRequestHeaders }) => ({ - commerce: buildModule(sapccModule, { + ({ buildModule, middlewareUrl, middlewareModule, getRequestHeaders }) => ({ + commerce: buildModule(middlewareModule, { apiUrl: middlewareUrl + "/commerce", + defaultRequestConfig: { + headers: getRequestHeaders(), + }, }), - cms: buildModule(contentfulModule, { + cms: buildModule(contentfulModule, { apiUrl: middlewareUrl + "/cms", }), }), @@ -65,6 +67,7 @@ export const { getSdk, createSdkContext } = createSdk( Let's break down the code above: - The `createSdk` function expects + - base SDK options including the middleware and (optionally) the multistore configuration as a first argument, - and a factory function for the SDK configuration as a second argument. Those factory function receives a context, useful for creating the SDK configuration. @@ -72,21 +75,21 @@ Let's break down the code above: - The `middlewareUrl` is the URL of the middleware instance. -- The `getRequestHeaders` function is used to retrieve the Cookie header with cookie values. +- The `middlewareModule` is an SDK module that ensures communication with the Server Middleware. It takes the `UnifiedEndpoints` type as a generic parameter. The `UnifiedEndpoints` type is a type that represents the endpoints of the Server Middleware. -- The `createSdk` function returns - - the `getSdk` function, which is used to create the new SDK instance, - - and the `createSdkContext` function, which is used to create the SDK context, to share the same SDK instance on the Client side. +- The `getRequestHeaders` function is used to retrieve the `Cookie` header with cookie values. + +- The `createSdk` function returns the `getSdk` function, which is used to retreive the new SDK instance. ## Registering the SDK -Once you have initialized the SDK, you can register it in your application. +Once you have initialized the SDK, you can register it in your application. Vue Storefront SDK can be used in two ways: -- **getSdk** - returns the SDK instance, which can be used to call the SDK methods directly. This is useful for server-side rendering, as it allows you to call the SDK methods directly in your application. +- `getSdk` - returns the SDK instance, which can be used to call the SDK methods directly. This is useful for server-side rendering, as it allows you to call the SDK methods directly in your application. -- **createSdkContext** - returns the SDK context, which can be used to share the same SDK instance on the Client side. This is useful for client-side rendering, as it allows you to share the same SDK instance across your application. +- `createSdkContext` - returns the SDK context, which can be used to share the same SDK instance on the Client side. This is useful for client-side rendering, as it allows you to share the same SDK instance across your application. ### getSdk @@ -95,27 +98,34 @@ Vue Storefront SDK can be used in two ways: Below is an example of how you can use `getSdk` in your application: ```ts -import { getSdk } from "../sdk.config"; +import { getSdk } from "@/sdk/sdk.config"; const sdk = getSdk(); ``` ### createSdkContext -For client-side rendering, you can use `createSdkContext`. In order to use it, you'll need to create a new file in your application, for example `hooks/sdk.ts`: +For client-side rendering, you can use `createSdkContext`. To use it, you'll need to create a new file in your application, for example `sdk/SdkProvider.tsx`: ```ts import { createSdkContext } from "@vue-storefront/next/client"; -import { getSdk } from "../sdk.config"; +import { getSdk } from "@/sdk/sdk.config"; export const [SdkProvider, useSdk] = createSdkContext(getSdk()); ``` +To achieve easier importing, you can also create an `index.ts` file in the `sdk` directory: + +```ts [sdk/index.tsx] +export * from "./SdkProvider"; +export * from "./sdk.config"; +``` + Once you have created the SDK context, you can register it in your application. For example, if you're using the Pages Router, you can register it in `pages/_app.tsx`: ```tsx import type { AppProps } from "next/app"; -import { SdkProvider } from "../hooks"; +import { SdkProvider } from "@/sdk"; export default function App({ Component, pageProps }: AppProps) { return ( @@ -128,7 +138,7 @@ export default function App({ Component, pageProps }: AppProps) { If you're using the App Router, you can register it in `app/layout.tsx`: -```tsx[app/layout.tsx] +```tsx [app/layout.tsx] import { ReactNode } from "react"; import { Providers } from "./providers"; @@ -143,11 +153,11 @@ export default function RootLayout({ children }: { children: ReactNode }) { } ``` -```tsx[app/providers.tsx] +```tsx [app/providers.tsx] "use client"; import { ReactNode } from "react"; -import { SdkProvider } from "../hooks"; +import { SdkProvider } from "@/sdk"; export function Providers({ children }: { children: ReactNode }) { return {children}; @@ -163,8 +173,9 @@ Don't be alarmed if you see a `use client` directive in the `app/providers.tsx` Once you have registered the SDK in your application, you can start using it. Here's an example of how you can use the SAP Commerce Cloud SDK module in your application: ::code-group -```tsx[Pages Router] -import { getSdk } from "../sdk.config"; + +```tsx [Pages Router] +import { getSdk } from "@/sdk"; export function getServersideProps() { const sdk = getSdk(); @@ -177,16 +188,18 @@ export function getServersideProps() { }; } ``` -```tsx[App Router] -import { getSdk } from "../sdk.config"; + +```tsx [App Router] +import { getSdk } from "@/sdk"; const sdk = getSdk(); const { products } = await sdk.commerce.searchProduct(); ``` -```tsx[Client Side Rendering] + +```tsx [Client Side Rendering] import { useEffect, useState } from "react"; -import { useSdk } from "../hooks"; +import { useSdk } from "@/sdk"; export function ClientComponentUsingSDK() { const sdk = useSdk(); @@ -195,8 +208,8 @@ export function ClientComponentUsingSDK() { useEffect(() => { const fetchCart = async () => { const newCart = await sdk.commerce.getCart({ - cartId: '', - fields: 'code,guid,user(FULL)' + cartId: "", + fields: "code,guid,user(FULL)", }); setCart(newCart); }; @@ -205,6 +218,7 @@ export function ClientComponentUsingSDK() { }, []); } ``` + :: Code above is just an example of how you can use the SDK in your application. For more information about the avaialble methods, please refer to the respective [Integration's documentation](https://docs.vuestorefront.io/integrations). @@ -215,7 +229,7 @@ That's it! You can now use VueStorefront SDK Module in your Next.js app ✨ ## Installation -To get started with the SDK within Next.js, first you have to install and configure the `@vue-storefront/nuxt` module. +To get started with the SDK within Next.js, first you have to install and configure the `@vue-storefront/nuxt` module. 1. In the root of your Storefront project run: @@ -232,7 +246,7 @@ npm install --save-dev @vue-storefront/nuxt 2. Add `@vue-storefront/nuxt` to the `modules` section of `nuxt.config.ts` -```ts[nuxt.config.ts] +```ts [nuxt.config.ts] export default defineNuxtConfig({ modules: ["@vue-storefront/nuxt"], }); @@ -242,13 +256,13 @@ export default defineNuxtConfig({ To configure the module, use `vsf` key in the Nuxt configuration object and provide necessary information such as the Middleware instance address: -```ts[nuxt.config.ts] +```ts [nuxt.config.ts] export default defineNuxtConfig({ modules: ["@vue-storefront/nuxt"], vsf: { middleware: { - apiUrl: "http://localhost:4000" - } + apiUrl: "http://localhost:4000", + }, }, }); ``` @@ -257,7 +271,7 @@ export default defineNuxtConfig({ To use SDK in our application, we need to initialize it first. To do so, follow these steps: -Create SDK Config file - `sdk.config.ts` in root directory of your project. +Create SDK Config file - `sdk.config.ts` in root directory of your project. ::info For Nuxt framework it's necessary to name the file `sdk.config.ts` and keep it in the root of your project. @@ -265,22 +279,22 @@ For Nuxt framework it's necessary to name the file `sdk.config.ts` and keep it i You should import all other SDK configuration components. See the example below illustrating the SDK configuration with SAP Commerce Cloud and Contentful modules. -```ts[sdk.config.ts] -import { sapccModule } from '@vsf-enterprise/sapcc-sdk'; -import { - contentfulModule, - ContentfulModuleType, -} from "@vsf-enterprise/contentful-sdk"; +```ts [sdk.config.ts] +import { contentfulModule } from "@vsf-enterprise/contentful-sdk"; +import { UnifiedEndpoints } from "storefront-middleware/types"; export default defineSdkConfig( - ({ buildModule, middlewareUrl, getCookieHeader }) => ({ - commerce: buildModule(sapccModule, { - apiUrl: middlewareUrl + "/commerce", + ({ buildModule, middlewareUrl, middlewareModule getRequestHeaders }) => ({ + commerce: buildModule(middlewareModule, { + apiUrl: middlewareUrl + "/commerce", // SAP Commerce Cloud integration is available at /commerce endpoint + defaultRequestConfig: { + headers: getRequestHeaders(), + }, }), - cms: buildModule(contentfulModule, { + cms: buildModule(contentfulModule, { apiUrl: middlewareUrl + "/cms", }), - }), + }) ); ``` @@ -290,16 +304,15 @@ The `defineSdkConfig` function is used for intializing the SDK. The parameter fo - the `buildModule` function, - the middleware URL (`middlewareUrl`), -- a function for retrieving the Cookie header with cookie values (`getCookieHeader`). +- the `middlewareModule` - an SDK module that ensures communication with the Server Middleware. It takes the `UnifiedEndpoints` type as a generic parameter. The `UnifiedEndpoints` type is a type that represents the endpoints of the Server Middleware, +- a function for retrieving request header, including cookie header (`getRequestHeaders`). ## Usage Once you have initialized the SDK, you can start using it. Here's an example of how you can use the SAP Commerce Cloud SDK module in your application: ```vue - +