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

feat: add API headers from config #1442

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stale-files-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware/api-client": minor
patzick marked this conversation as resolved.
Show resolved Hide resolved
---

Add config default headers
patzick marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 24 additions & 0 deletions packages/nuxt3-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ All composable functions are fully typed with TypeScript and they are registed g

Internally, the module uses [API Client](https://npmjs.com/package/@shopware/api-client) and [Composables](https://npmjs.com/package/@shopware-pwa/composables-next) packages, configured together to make everything working well. If you need to check how it's working on a different version of one of them, install a package locally in your project (to be installed and available in project's `package.json` file), then the Nuxt module will use yours. Keep in mind that the different configuration may lead to unexpected behavior.

## API Default Headers

You can use Nuxt config to set the default API call headers.
More about Nuxt configuration can be found [HERE](https://nuxt.com/docs/getting-started/configuration).

```json
{
"runtimeConfig": {
"public": {
"apiClientConfig": {
"headers": {
"global-heder-example": "global-header-example-value"
}
}
},
"apiClientConfig": {
"headers": {
"ssr-heder-example": "ssr-header-example-value"
patzick marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
```

## Links

- [📘 Documentation](https://frontends.shopware.com)
Expand Down
6 changes: 5 additions & 1 deletion packages/nuxt3-module/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createAPIClient } from "@shopware/api-client";
import { getCookie } from "h3";
import { isMaintenanceMode } from "@shopware-pwa/helpers-next";
import type { ApiClient } from "#shopware";
import { defu } from "defu";

declare module "#app" {
interface NuxtApp {
Expand Down Expand Up @@ -62,6 +63,10 @@ export default defineNuxtPlugin((NuxtApp) => {
contextToken: shouldUseSessionContextInServerRender
? contextTokenFromCookie
: "",
defaultHeaders: defu(
runtimeConfig.apiClientConfig?.headers,
runtimeConfig.public?.apiClientConfig?.headers,
),
});

apiClient.hook("onContextChanged", (newContextToken) => {
Expand All @@ -85,7 +90,6 @@ export default defineNuxtPlugin((NuxtApp) => {
});

NuxtApp.vueApp.provide("apiClient", apiClient);

// Shopware context
const shopwareContext = createShopwareContext(NuxtApp.vueApp, {
enableDevtools: true,
Expand Down
7 changes: 7 additions & 0 deletions packages/nuxt3-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,21 @@ declare module "@nuxt/schema" {
interface NuxtOptions {
shopware?: ShopwareNuxtOptions;
}
interface ApiClientConfig {
headers?: {
[key: string]: string;
};
}

interface RuntimeConfig {
shopware?: Pick<
ShopwareNuxtOptions,
"endpoint" | "shopwareEndpoint" | "useUserContextInSSR"
>;
apiClientConfig?: ApiClientConfig;
}
interface PublicRuntimeConfig {
shopware: ShopwareNuxtOptions;
apiClientConfig?: ApiClientConfig;
}
}
17 changes: 17 additions & 0 deletions templates/vue-demo-store/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,25 @@ export default defineNuxtConfig({
// endpoint: "https://demo-frontends.shopware.store/store-api/",
// accessToken: "SWSCBHFSNTVMAWNZDNFKSHLAYW",
// devStorefrontUrl: "",
/**
* Example for API Client configuration.
*/
// apiClientConfig: {
// headers: {
// "global-heder-example": "global-header-example-value",
// },
// },
// },
/**
* Example for API Client configuration
*
*/

// apiClientConfig: {
// headers: {
// "ssr-heder-example": "ssr-header-example-value",
// },
// },
/**
* More about this feature you can find here: https://frontends.shopware.com/getting-started/features/broadcasting.html
*/
Expand Down
Loading