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

refactor: fix middleware URL generation #6688

Merged
merged 78 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
a3223fd
refactor(core): fix missing getBaseUrl method
bloodf Mar 26, 2022
a9f197d
refactor(core): changed URL fetch methods
bloodf Mar 26, 2022
5d34836
refactor(core): removed unused test
bloodf Mar 26, 2022
8244726
refactor(core): simplified the logic for URL
bloodf Mar 26, 2022
c601eab
Update packages/core/core/src/utils/nuxt/index.ts
bloodf Mar 28, 2022
420877d
Update packages/core/core/src/utils/nuxt/_proxyUtils.ts
bloodf Mar 29, 2022
a01f548
refactor(core): changed the way to fetch url from config
bloodf Mar 29, 2022
c859284
Merge branch 'main' into fix-missing-baseUrl
bloodf Mar 29, 2022
a0f5436
refactor(core): added URL to generate Urls
bloodf Mar 29, 2022
7b03a04
refactor(core): removed URL checking, only validantion
bloodf Mar 29, 2022
a3db3d3
refactor(nuxt-module): added URL check as a hook
bloodf Mar 29, 2022
3f5341f
refactor(nuxt-module): fixed URL generation in hook
bloodf Mar 29, 2022
76cf61b
refactor(core): slim down the code for URL generation
bloodf Mar 29, 2022
b4845dd
chore: removed non-used plugin `is-https`
bloodf Mar 29, 2022
48c6f80
refactor(core): fixed regex
bloodf Mar 29, 2022
45540e3
refactor(core): fixed tests for new approach
bloodf Mar 29, 2022
8598230
refactor(nuxt-module): fixed configuration merge
bloodf Mar 30, 2022
e444ca1
refactor(nuxt-module): fixed configuration merge
bloodf Mar 30, 2022
3abe973
refactor(nuxt-module): changed merge strategy
bloodf Mar 30, 2022
4e74a3d
refactor(nuxt-module): changed merge strategy
bloodf Mar 30, 2022
6dcf0b4
chore: version bump for core and nuxt module
lukasborawski Mar 30, 2022
a8ef11f
chore: version bumop for missing packages
lukasborawski Mar 30, 2022
359447e
docs: added documentation for the new release
bloodf Mar 30, 2022
6f3de28
docs: migration guide update
lukasborawski Mar 30, 2022
1f07ea0
docs: migration guide update
lukasborawski Mar 30, 2022
1286d12
docs: migration guide update
lukasborawski Mar 30, 2022
6bbc99c
docs: migration guide update
lukasborawski Mar 30, 2022
431bcc9
docs: migration guide update
lukasborawski Mar 30, 2022
459c247
refactor(nuxt-module): improved validation in server-url
bloodf Mar 30, 2022
455aebb
refactor(core): added error if the middlewareURL fails
bloodf Mar 30, 2022
d54d60d
docs: added documentation for the new changes
bloodf Mar 30, 2022
588cb60
refactor: moved error to url generator
bloodf Mar 30, 2022
841e5eb
chore: moved worming of missing middlwareUrl, docs update
lukasborawski Mar 30, 2022
15abb4e
refactor(nuxt-module): fixed host and port checking
bloodf Mar 30, 2022
2b1dd7f
chore: merge conflict resolve
lukasborawski Mar 30, 2022
0e41445
chore: merge conflict resolve
lukasborawski Mar 30, 2022
97b1e5a
chore: copy
lukasborawski Mar 30, 2022
eb30ec4
chore: copy
lukasborawski Mar 31, 2022
3cf48b4
docs: copy
lukasborawski Mar 31, 2022
b965ad7
revert: middlware url notation
lukasborawski Mar 31, 2022
bbdd4bf
refactor(nuxt-module): applied URL to generate url of API
bloodf Apr 2, 2022
407d4da
Merge remote-tracking branch 'origin/fix-missing-baseUrl' into fix-mi…
bloodf Apr 2, 2022
f08109a
refactor(nuxt-module): removed serverUrl from package
bloodf Apr 3, 2022
15aa819
refactor(middleware): added serverUrl logic to package
bloodf Apr 3, 2022
1f14b09
chore: version bump to 2.5.12-c
lukasborawski Apr 4, 2022
6ff71ae
feat(middleware): added missing Helmet package in nuxt and API
bloodf Apr 4, 2022
0503a3f
Merge remote-tracking branch 'origin/fix-missing-baseUrl' into fix-mi…
bloodf Apr 4, 2022
83964fc
refactor(core): added browser URL fetch for API
bloodf Apr 4, 2022
7363097
chore: version bump to 2.5.12-c
lukasborawski Apr 4, 2022
b2f8dc0
refactor(middleware): added HELMET options and default
bloodf Apr 4, 2022
e4b6cc2
docs(middleware): added documentation for Helmet.js and middlewareUrl
bloodf Apr 4, 2022
3a3ca77
refactor: fixed logging information
bloodf Apr 4, 2022
c4d665e
refactor(middleware): added missing flag to control `helmet`
bloodf Apr 4, 2022
b9a5603
Merge branch 'main' into fix-missing-baseUrl
bloodf Apr 4, 2022
fdb73ae
chore: types
lukasborawski Apr 4, 2022
d8376be
chore: version bump to 2.5.12-c
lukasborawski Apr 5, 2022
6e6a232
fix: configs, data transfering
lukasborawski Apr 5, 2022
a0c00f3
chore: version bump to 2.5.12-c
lukasborawski Apr 5, 2022
b7246f3
docs: update
filipsobol Apr 5, 2022
fd85e60
Merge remote-tracking branch 'origin/fix-missing-baseUrl' into fix-mi…
filipsobol Apr 5, 2022
7c125ee
Update packages/core/docs/security/api-url.md
filipsobol Apr 5, 2022
57b90e6
chore: disabling helmet by defult
lukasborawski Apr 6, 2022
fb8828f
test: proxyUtils base url fix
lukasborawski Apr 6, 2022
894331f
chore: helmet options object brackets
lukasborawski Apr 6, 2022
f691c51
revert: console log
lukasborawski Apr 6, 2022
00c0561
Merge branch 'main' into fix-missing-baseUrl
lukasborawski Apr 6, 2022
bc8b757
docs: headers-security.md update
lukasborawski Apr 6, 2022
44643aa
docs: headers-security.md update
lukasborawski Apr 6, 2022
27d6c3b
docs: headers-security.md update
lukasborawski Apr 6, 2022
5c22975
docs: headers-security.md update
lukasborawski Apr 6, 2022
5801cfa
chore: version bump to 2.5.12-c
lukasborawski Apr 6, 2022
8627931
chore: versions alignment
lukasborawski Apr 6, 2022
d1435b8
docs: update api-url.md
lukasborawski Apr 6, 2022
7b2ee67
docs: update api-url.md
lukasborawski Apr 6, 2022
4474d9a
chore: removed un chaining
lukasborawski Apr 7, 2022
35e6493
chore: interpolation
lukasborawski Apr 7, 2022
bd488d5
chore: types
lukasborawski Apr 7, 2022
b555b0e
chore: condition fix
lukasborawski Apr 7, 2022
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
4 changes: 2 additions & 2 deletions packages/core/cache/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vue-storefront/cache",
"version": "2.5.7",
"version": "2.5.12",
"license": "MIT",
"main": "lib/index.cjs.js",
"module": "lib/index.es.js",
Expand All @@ -12,7 +12,7 @@
"prepublish": "yarn build"
},
"dependencies": {
"@vue-storefront/core": "~2.5.7"
"@vue-storefront/core": "~2.5.11"
},
"peerDependencies": {
"@nuxtjs/composition-api": "^0.29.3"
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/__tests__/utils/nuxt/proxyUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('[CORE - utils] _proxyUtils', () => {
{ someGivenOption: 1 }
)).toEqual({
axios: {
baseURL: 'some-url',
baseURL: 'some-url/api/',
headers: {}
},
someGivenOption: 1
Expand All @@ -57,7 +57,7 @@ describe('[CORE - utils] _proxyUtils', () => {
{}
)).toEqual({
axios: {
baseURL: 'some-url',
baseURL: 'some-url/api/',
headers: {
cookie: 'xxx'
}
Expand Down
3 changes: 1 addition & 2 deletions packages/core/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vue-storefront/core",
"version": "2.5.8",
"version": "2.5.12-c.3",
"sideEffects": false,
"main": "lib/index.cjs.js",
"module": "lib/index.es.js",
Expand All @@ -15,7 +15,6 @@
"dependencies": {
"axios": "0.21.1",
"express": "^4.17.1",
"is-https": "^3.0.2",
"lodash-es": "^4.17.15",
"vue": "^2.6.11"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/core/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Ref } from '@nuxtjs/composition-api';
import type { Request, Response } from 'express';
import { HelmetOptions } from 'helmet';

/**
* Default name of the cookie storing active localization code
Expand Down Expand Up @@ -845,6 +846,7 @@ export type IntegrationsSection = Record<string, Integration>

export interface MiddlewareConfig {
integrations: Record<string, Integration>;
helmet: Readonly<HelmetOptions>;
}

export interface ApiClientFactoryParams<T, F = any> {
Expand Down
16 changes: 6 additions & 10 deletions packages/core/core/src/utils/nuxt/_proxyUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Context as NuxtContext } from '@nuxt/types';
import merge from 'lodash-es/merge';
import { ApiClientMethod } from './../../types';
import { Logger } from './../logger';

interface CreateProxiedApiParams {
givenApi: Record<string, ApiClientMethod>;
Expand All @@ -25,25 +26,20 @@ export const createProxiedApi = ({ givenApi, client, tag }: CreateProxiedApiPara
export const getCookies = (context: NuxtContext) => context?.req?.headers?.cookie ?? '';

export const getIntegrationConfig = (context: NuxtContext, configuration: any) => {
const baseURL = process.server ? context?.$config?.middlewareUrl : window.location.origin;
const cookie = getCookies(context);

if (context?.$config?.middlewareUrl) {
const { middlewareUrl } = context.$config;
return merge({
axios: {
baseURL: middlewareUrl,
headers: {
...(cookie ? { cookie } : {})
}
}
}, configuration);
if (process.server && context?.$config?.middlewareUrl) {
Logger.info('Applied middlewareUrl as ', context?.$config?.middlewareUrl);
lukasborawski marked this conversation as resolved.
Show resolved Hide resolved
}

return merge({
bloodf marked this conversation as resolved.
Show resolved Hide resolved
axios: {
baseURL: new URL(/\/api\//gi.test(baseURL) ? '' : 'api', baseURL).toString(),
headers: {
...(cookie ? { cookie } : {})
}
}
}, configuration);
};

1 change: 1 addition & 0 deletions packages/core/core/src/utils/nuxt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const integrationPlugin = (pluginFn: NuxtPlugin) => (nuxtCtx: NuxtContext
const injectInContext = createAddIntegrationToCtx({ tag, nuxtCtx, inject });
const config = getIntegrationConfig(nuxtCtx, configuration);
const client = axios.create(config.axios);

const api = createProxiedApi({ givenApi: configuration.api || {}, client, tag });

if (nuxtCtx.app.i18n.cookieValues) {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ module.exports = {
['/performance/ssr-cache', 'SSR cache']
]
},
{
title: 'Security',
collapsable: true,
children: [
['/security/headers-security', 'HTTP Headers security'],
['/security/api-url', 'Server Middleware URL']
]
},
{
title: 'Reference',
collapsable: true,
Expand Down
15 changes: 0 additions & 15 deletions packages/core/docs/getting-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ Every Vue Storefront project must contain two configuration files described belo

You can learn more about this file and available configuration options on the [Nuxt configuration file](https://nuxtjs.org/docs/directory-structure/nuxt-config/) page.

The only thing you have to configure when setting up a new project is the `middlewareUrl` property which defines the URL to the Server Middleware. Usually, it's your application domain followed by the `/api` path.

Example:

```javascript
// nuxt.config.js
export default {
publicRuntimeConfig: {
middlewareUrl: 'https://yourdomain.com/api/'
}
}
```

For the local development, set it to `http://localhost:3000/api/`.

### `middleware.config.js`

The `middleware.config.js` file is as essential as `nuxt.config.js`, but much simpler and likely smaller. It configures the Server Middleware used for communication with e-commerce platforms and contains sensitive credentials, custom endpoints, queries, etc.
Expand Down
5 changes: 3 additions & 2 deletions packages/core/docs/reference/changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## 2.5.8 (deprecated)
## 2.5.12

- **[BREAKING]** `middlewareUrl` is no longer required from the global config, however it should be provided to handle integration specific API calls
- Middleware and Nuxt Middleware [Helmet](https://github.com/helmetjs/helmet) added. ([6688](https://github.com/vuestorefront/vue-storefront/pull/6688)) [Heitor Ramon](https://github.com/bloodf)
- Revert the breaking change introduced in `2.5.7`, with the default behavior and the possibility to use Nuxt `middlewareUrl` as the endpoint definition. ([6688](https://github.com/vuestorefront/vue-storefront/pull/6688)) [Heitor Ramon](https://github.com/bloodf)

## 2.5.7

Expand Down
40 changes: 40 additions & 0 deletions packages/core/docs/reference/migrate/2.5.12/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Migrating projects to 2.5.12

## Update `nuxt.config.js`

In this release, we've added the optional `middlewareUrl` property to define the URL to the Server Middleware. Open the `nuxt.config.js` file and add the `middlewareUrl` property to the `publicRuntimeConfig` object as shown below.
bloodf marked this conversation as resolved.
Show resolved Hide resolved

:::warning
Make sure to pass the whole URL with protocol, port (if applicable), and suffix it with `/api/`.
:::

```javascript
// nuxt.config.js
export default {
publicRuntimeConfig: {
middlewareUrl: 'https://example.com/api/' // For the local development, set it to `http://localhost:3000/api/`.
lukasborawski marked this conversation as resolved.
Show resolved Hide resolved
}
}
```

If you don't want to hardcode the URL in the configuration file, you can use environmental variables.

Example:

```javascript
// nuxt.config.js
export default {
publicRuntimeConfig: {
middlewareUrl: process.env.API_BASE_URL
}
}
```

Then add an entry in the `.env` file or use any other method for passing environmental variables that suits your needs.

Example:

```text
// .env
API_BASE_URL=https://example.com/api/
```
39 changes: 0 additions & 39 deletions packages/core/docs/reference/migrate/2.5.7/overview.md

This file was deleted.

4 changes: 2 additions & 2 deletions packages/core/docs/reference/migrate/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Migration guides

## 2.5.7
- [Overview](./2.5.7/overview.md)
## 2.5.12
- [Overview](./2.5.12/overview.md)

## 2.5.0
- [Overview](./2.5.0/overview.md)
Expand Down
40 changes: 40 additions & 0 deletions packages/core/docs/security/api-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Server Middleware URL

Internally we use Nuxt environment properties to get the URL of Server Middleware. However, you can change it by defining the `middlewareUrl` property in the `publicRuntimeConfig` object inside the `nuxt.config.js` file.

:::warning
Make sure to pass the whole URL with protocol, port (if applicable), and suffix it with `/api/`.
:::

```javascript
// nuxt.config.js
export default {
publicRuntimeConfig: {
middlewareUrl: process.env.NODE_ENV === 'production'
? `https://${process.env.API_BASE_URL}/api/`
: 'http://localhost:3000/api/'
lukasborawski marked this conversation as resolved.
Show resolved Hide resolved
}
}
```
filipsobol marked this conversation as resolved.
Show resolved Hide resolved

If you don't want to hardcode the URL in the configuration file, you can use environmental variables.

Example:

```javascript
// nuxt.config.js
export default {
publicRuntimeConfig: {
middlewareUrl: process.env.API_BASE_URL
}
}
```

Then add an entry in the `.env` file or use any other method for passing environmental variables that suits your needs.

Example:

```text
// .env
API_BASE_URL=https://example.com/api/
```
81 changes: 81 additions & 0 deletions packages/core/docs/security/headers-security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# HTTP Headers security

To improve the security of Vue Storefront applications, we preinstall the [Helmet](https://helmetjs.github.io/) security extension by default for Nuxt application and the [Server Middleware](/architecture/server-middleware.html).

In this document we show how to change the default configuration in both applications. For a list of all available options, see the [Helmet documentation](https://helmetjs.github.io/docs/).

## Configuring Helmet in Nuxt

**Helmet** is disabled by default, you can enable it by passing `helmet` property set to `true` or as a configuration object like this.
lukasborawski marked this conversation as resolved.
Show resolved Hide resolved

```javascript
// nuxt.config.js
export default {
modules: [
['@vue-storefront/middleware/nuxt', {
helmet: true
// or
helmet: {
// ...configuration
}
}]
]
}
```

To change the default configuration of Helmet in Nuxt application, use the `helmet` object in the configuration of the `@vue-storefront/middleware/nuxt` module.
lukasborawski marked this conversation as resolved.
Show resolved Hide resolved

```javascript
// nuxt.config.js
export default {
modules: [
['@vue-storefront/middleware/nuxt', {
helmet: {
// default configuration
crossOriginOpenerPolicy: false,
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
permittedCrossDomainPolicies: {
permittedPolicies: 'none'
}
}
}]
]
}
```

## Configuring Helmet in VSF Server Middleware

**Helmet** is disabled by default, you can enable it by passing `helmet` property set to `true` or as a configuration object like this.

```javascript
// nuxt.config.js
export default {
modules: [
['@vue-storefront/middleware/nuxt', {
helmet: true
// or
helmet: {
// ...configuration
}
}]
]
}
```

To change the default configuration of Helmet in Server Middleware, use the `helmet` object in the `middleware.config.js` file.
lukasborawski marked this conversation as resolved.
Show resolved Hide resolved

```javascript
// middleware.config.js
module.exports = {
helmet: {
// default configuration
crossOriginOpenerPolicy: false,
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
permittedCrossDomainPolicies: {
permittedPolicies: 'none'
}
}
};
```
2 changes: 1 addition & 1 deletion packages/core/health-check/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vue-storefront/health-check",
"version": "2.5.7",
"version": "2.5.12",
"description": "",
"main": "lib/module.js",
"scripts": {
Expand Down
Loading