Skip to content
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

docs: sdk 2.0.0 #7156

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/content/4.sdk/2.getting-started/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,26 @@ It is not necessary to name the file `sdk.config.ts` specifically or to keep it
import { contentfulModule } from "@vsf-enterprise/contentful-sdk";
import { CreateSdkOptions, createSdk } from "@vue-storefront/next";
import { UnifiedEndpoints } from "storefront-middleware/types";
import { env } from 'next-runtime-env';

const options: CreateSdkOptions = {
middleware: {
apiUrl: "http://localhost:4000",
},
};

const cdnCacheBustingId = env('NEXT_PUBLIC_CDN_CACHE_BUSTING_ID') ?? 'no-cache-busting-id-provided';

export const { getSdk } = createSdk(
options,
({ buildModule, middlewareUrl, middlewareModule, getRequestHeaders }) => ({
({ buildModule, middlewareUrl, middlewareModule, getRequestHeader, defaults }) => ({
commerce: buildModule(middlewareModule<UnifiedEndpoints>, {
apiUrl: middlewareUrl + "/commerce",
defaultRequestConfig: {
headers: getRequestHeaders(),
},
cdnCacheBustingId,
methodsRequestConfig: defaults.unifiedCommerce.middlewareModule.defaultMethodsRequestConfig,
}),
cms: buildModule(contentfulModule, {
apiUrl: middlewareUrl + "/cms",
Expand All @@ -81,6 +86,8 @@ Let's break down the code above:

- 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 `defaults` is the default configuration of the SDK.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also explain cdnCacheBustingId? Based on the next doc it seems like it's autogenerated during CI/CD? But it would be nice to explain what it is, why it's needed, and where the value comes from,

Also, how does someone use the defaults object? Is it always going to bedefaults.unifiedCommerce.middlewareModule.defaultMethodsRequestConfig or how does someone know which values to access

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults for now only has this one key, but it's a place to have more.

I think that I'll change cdnCacheBustingId to be optional as it increases the learning curve.


- The `getRequestHeaders` function is used to provide the incoming headers within your requests. You can use `getRequestHeaders` to access and proxy the initial cookie headers to SDK requests during SSR. Initial headers could be provided by the [`getSdk`](#getsdk) method.
Check out examples there:
- Next Pages Router [link](https://github.com/vuestorefront/vue-storefront/tree/main/packages/storefront/packages/next/__tests__/apps/pages-router)
Expand Down
15 changes: 14 additions & 1 deletion docs/content/4.sdk/2.getting-started/2.middleware-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ In Next, `middlewareModule` is available in the `createSdk` function.
// sdk/sdk.config.ts
import { CreateSdkOptions, createSdk } from "@vue-storefront/next";
import { UnifiedEndpoints } from "storefront-middleware/types";
import { env } from 'next-runtime-env';

const options: CreateSdkOptions = {
middleware: {
apiUrl: "http://localhost:4000",
},
};

const cdnCacheBustingId = env('NEXT_PUBLIC_CDN_CACHE_BUSTING_ID') ?? 'no-cache-busting-id-provided';

export const { getSdk } = createSdk(
options,
({ buildModule, middlewareUrl, middlewareModule, getRequestHeaders }) => ({
({ buildModule, middlewareUrl, middlewareModule, getRequestHeaders, defaults }) => ({
commerce: buildModule(middlewareModule<UnifiedEndpoints>, {
apiUrl: middlewareUrl + "/commerce",
defaultRequestConfig: {
headers: getRequestHeaders(),
},
cdnCacheBustingId,
methodsRequestConfig: defaults.unifiedCommerce.middlewareModule.defaultMethodsRequestConfig,
}),
})
);
Expand Down Expand Up @@ -61,6 +66,7 @@ import { UnifiedEndpoints } from "storefront-middleware/types";
export const sdk = initSDK({
commerce: buildModule(middlewareModule<UnifiedEndpoints>, {
apiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: /** pass the commit hash of the middleware here */,
}),
});
```
Expand All @@ -83,6 +89,7 @@ type Endpoints = {
const sdk = initSDK({
commerce: buildModule(middlewareModule<Endpoints>, {
apiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: /** pass the commit hash of the middleware here */,
}),
});

Expand All @@ -101,6 +108,7 @@ import { UnifiedEndpoints } from "storefront-middleware/types";
export const sdk = initSDK({
commerce: buildModule(middlewareModule<UnifiedEndpoints>, {
apiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: /** pass the commit hash of the middleware here */,
}),
});

Expand All @@ -115,6 +123,7 @@ import { Endpoints } from "@vsf-enterprise/sapcc-api";
export const sdk = initSDK({
commerce: buildModule(middlewareModule<Endpoints>, {
apiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: /** pass the commit hash of the middleware here */,
}),
});
```
Expand All @@ -139,6 +148,7 @@ export const sdk = initSDK({
commerce: buildModule(middlewareModule<UnifiedEndpoints>, {
apiUrl: "/api/commerce",
ssrApiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: /** pass the commit hash of the middleware here */,
defaultRequestConfig: {
headers: {
"X-Api-Key": "123",
Expand Down Expand Up @@ -263,6 +273,7 @@ import { SdkHttpError } from "@vue-storefront/sdk";
export const sdk = initSDK({
commerce: buildModule(middlewareModule<UnifiedEndpoints>, {
apiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: /** pass the commit hash of the middleware here */,
httpClient: async (url, params, config) => {
try {
const { data } = await axios(url, {
Expand Down Expand Up @@ -418,6 +429,7 @@ However, you can always specify the headers manually by passing them to the `def
export const sdk = initSDK({
commerce: buildModule(middlewareModule<UnifiedEndpoints>, {
apiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: /** pass the commit hash of the middleware here */,
defaultRequestConfig: {
headers: {
Cookie: "name=value",
Expand Down Expand Up @@ -466,6 +478,7 @@ import { CustomEndpoints } from "storefront-middleware/types";
export const sdk = initSDK({
custom: buildModule(middlewareModule<CustomEndpoints>, {
apiUrl: "http://localhost:8181/custom",
cdnCacheBustingId: /** pass the commit hash of the middleware here */,
}),
});
```
Expand Down
2 changes: 2 additions & 0 deletions docs/content/4.sdk/3.advanced/1.extending-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const sdk = initSDK({
middlewareModule<UnifiedEndpoints>,
{
apiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: /** pass the commit hash of the middleware here */,
},
{
extend: {
Expand All @@ -40,6 +41,7 @@ const sdk = initSDK({
middlewareModule<UnifiedEndpoints>,
{
apiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: /** pass the commit hash of the middleware here */,
},
(extensionParams, { methods, context }) => ({
extend: {
Expand Down