Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat(nuxt-module): enrich API client config with timeout (#1119)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucmus authored Oct 9, 2020
1 parent bd6012c commit 4d2a87a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docs/guide/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ See: [Context-awareness](/landing/fundamentals/#context-awareness) section. It's
You should check all your imports for @shopware-pwa/shopware-6-client and add apiInstance as the last parameter.

See: [Context-awareness](/landing/fundamentals/#context-awareness) section. It's explained in details.

### Issue: The API Client's timeout is too low. I'm getting HTTP 408 errors.

- Edit _shopware-pwa.config.js_ file
- Add entry:
```js
shopwareApiClient: {
timeout: 10000 // 10 seconds of axios timeout setting
}
```
3 changes: 3 additions & 0 deletions docs/landing/getting-started/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Instead of using the interactive CLI to configure your backend connection, you c
module.exports = {
shopwareEndpoint: "https://shopware6-demo.vuestorefront.io",
shopwareAccessToken: "SWSCVJJET0RQAXFNBMTDZTV1OQ",
shopwareApiClient: { // optional, allow to override the default settings
timeout: 5000, // timeout limit in ms
}
};
```

Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/extensions/shopware-pwa-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const defaultConfig = {
shopwareEndpoint: "https://pwa-demo-api.shopware.com",
shopwareAccessToken: "SWSC40-LJTNO6COUEN7CJMXKLA",
theme: "@shopware-pwa/default-theme",
shopwareApiClient: {
timeout: 10000,
},
};
// add your CLI-specific functionality here, which will then be accessible
// to your commands
Expand All @@ -30,7 +33,9 @@ module.exports = (toolbox: GluegunToolbox) => {
const nodePackagePathExist = require("fs").existsSync(nodePackagePath);
if (nodePackagePathExist) return nodePackagePath;

throw new Error(`No theme found for "${directPath}". Please make sure that path is correct or theme is installed from NPM.`);
throw new Error(
`No theme found for "${directPath}". Please make sure that path is correct or theme is installed from NPM.`
);
};

toolbox.checkThemePath = () => {
Expand Down
31 changes: 31 additions & 0 deletions packages/nuxt-module/__tests__/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,37 @@ describe("nuxt-module - ShopwarePWAModule runModule", () => {
);
});

it("should have extra API client related config", async () => {
mockedUtils.loadConfig.mockResolvedValueOnce({
shopwareEndpoint:
"https://shopware-pwa.storefrontcloud.io/sales-channel-api/v1",
shopwareAccessToken: "mockedToken",
theme: "@shopware-pwa/default-theme",
shopwareApiClient: {
timeout: 5,
},
});
const pathForApiClientPlugin = path.join(
__dirname,
"..",
"plugins",
"api-client.js"
);
await runModule(moduleObject, {});
expect(moduleObject.addPlugin).toBeCalledWith({
fileName: "api-client.js",
options: {
shopwareAccessToken: "mockedToken",
shopwareEndpoint:
"https://shopware-pwa.storefrontcloud.io/sales-channel-api/v1",
shopwareApiClient: {
timeout: 5,
},
},
src: pathForApiClientPlugin,
});
});

it("should add cookies plugin", async () => {
await runModule(moduleObject, {});
expect(moduleObject.addPlugin).toBeCalled();
Expand Down
6 changes: 6 additions & 0 deletions packages/nuxt-module/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ describe("nuxt-module - utils", () => {
shopwareAccessToken: "qweqwe",
shopwareEndpoint: "https://instance.com",
theme: "@shopware-pwa/default-theme",
shopwareApiClient: {
timeout: 10000,
},
});
});

Expand All @@ -70,6 +73,9 @@ describe("nuxt-module - utils", () => {
shopwareAccessToken: "SWSC40-LJTNO6COUEN7CJMXKLA",
shopwareEndpoint: "https://pwa-demo-api.shopware.com",
theme: "@shopware-pwa/default-theme",
shopwareApiClient: {
timeout: 10000,
},
});
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt-module/plugins/api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default async ({ app }, inject) => {
const instance = createInstance({
endpoint: "<%= options.shopwareEndpoint %>",
accessToken: "<%= options.shopwareAccessToken %>",
timeout: "<%= options.shopwareApiClient.timeout %>",
contextToken,
languageId,
});
Expand Down
5 changes: 5 additions & 0 deletions packages/nuxt-module/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ export interface WebpackContext {
isLegacy: boolean;
}

export interface ShopwareApiClientConfig {
timeout?: number;
}

export interface ShopwarePwaConfigFile {
shopwareEndpoint: string;
shopwareAccessToken: string;
theme: string;
defaultLanguageCode?: string;
shopwareApiClient?: ShopwareApiClientConfig;
apiDefaults?: {
[composableName: string]: {
includes?: Includes;
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt-module/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export async function runModule(
options: {
shopwareEndpoint: shopwarePwaConfig.shopwareEndpoint,
shopwareAccessToken: shopwarePwaConfig.shopwareAccessToken,
shopwareApiClient: shopwarePwaConfig.shopwareApiClient,
},
});

Expand Down
5 changes: 4 additions & 1 deletion packages/nuxt-module/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ export function invokeRebuild(moduleObject: NuxtModuleOptions) {
}

// TODO move to commons with shopware-pwa-extension.ts
const defaultConfig = {
const defaultConfig: ShopwarePwaConfigFile = {
shopwareEndpoint: "https://pwa-demo-api.shopware.com",
shopwareAccessToken: "SWSC40-LJTNO6COUEN7CJMXKLA",
theme: "@shopware-pwa/default-theme",
shopwareApiClient: {
timeout: 10000,
},
};

export async function loadConfig(
Expand Down

1 comment on commit 4d2a87a

@vercel
Copy link

@vercel vercel bot commented on 4d2a87a Oct 9, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.