-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get api host from middleware url (#6680)
* Apply suggestions from code review Co-authored-by: Filip Sobol <filipsobol@users.noreply.github.com> * changelog update Co-authored-by: Filip Sobol <filipsobol@users.noreply.github.com>
- Loading branch information
1 parent
9020e17
commit 43b92c9
Showing
8 changed files
with
161 additions
and
99 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
packages/core/core/__tests__/utils/nuxt/proxyUtils.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import * as utils from '../../../src/utils/nuxt/_proxyUtils'; | ||
|
||
describe('[CORE - utils] _proxyUtils', () => { | ||
it('returns proxy for defined api', () => { | ||
const givenApi = { | ||
getProduct: jest.fn() | ||
}; | ||
|
||
const client = { | ||
post: jest.fn(() => ({ then: jest.fn() })) | ||
}; | ||
|
||
const proxiedApi = utils.createProxiedApi({ givenApi, client, tag: 'ct' }); | ||
|
||
proxiedApi.getProduct({ product: 1 }); | ||
proxiedApi.getCategory({ category: 1 }); | ||
|
||
expect(givenApi.getProduct).toBeCalled(); | ||
expect(client.post).toBeCalledWith('/ct/getCategory', [{ category: 1 }]); | ||
}); | ||
|
||
it('reads cookies from incoming request', () => { | ||
expect(utils.getCookies(null)).toEqual(''); | ||
expect(utils.getCookies({} as any)).toEqual(''); | ||
expect(utils.getCookies({ req: { headers: {} } } as any)).toEqual(''); | ||
expect(utils.getCookies({ req: { headers: { cookie: { someCookie: 1 } } } } as any)).toEqual({ someCookie: 1 }); | ||
}); | ||
|
||
it('it combines config with the current one', () => { | ||
jest.spyOn(utils, 'getCookies').mockReturnValue(''); | ||
|
||
expect(utils.getIntegrationConfig( | ||
{ | ||
$config: { | ||
middlewareUrl: 'some-url' | ||
} | ||
} as any, | ||
{ someGivenOption: 1 } | ||
)).toEqual({ | ||
axios: { | ||
baseURL: 'some-url', | ||
headers: {} | ||
}, | ||
someGivenOption: 1 | ||
}); | ||
}); | ||
|
||
it('it combines config with the current one and adds a cookie', () => { | ||
jest.spyOn(utils, 'getCookies').mockReturnValue('xxx'); | ||
|
||
expect(utils.getIntegrationConfig( | ||
{ | ||
$config: { | ||
middlewareUrl: 'some-url' | ||
} | ||
} as any, | ||
{} | ||
)).toEqual({ | ||
axios: { | ||
baseURL: 'some-url', | ||
headers: { | ||
cookie: 'xxx' | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
it('it throws error when no middlewareUrl is passed in config', () => { | ||
jest.spyOn(utils, 'getCookies').mockReturnValue(''); | ||
|
||
expect(() => { | ||
utils.getIntegrationConfig( | ||
{ | ||
$config: { | ||
middlewareUrl: '' | ||
} | ||
} as any, | ||
{} | ||
); | ||
}).toThrowError(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = { | ||
description: 'get api host from middleware url', | ||
link: 'https://github.com/vuestorefront/vue-storefront/pull/6680', | ||
isBreaking: true, | ||
breakingChanges: [ | ||
{ | ||
module: '@vue-storefront/core', | ||
before: 'The `middlewareUrl` property was optional', | ||
after: 'The `middlewareUrl` is required', | ||
comment: 'The `middlewareUrl` property in the `nuxt.config.js` file is now required. Please follow the instruction in the Migration Guide.' | ||
} | ||
], | ||
author: 'Dawid Ziobro', | ||
linkToGitHubAccount: 'https://github.com/dawid-ziobro' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Migrating projects to 2.5.7 | ||
|
||
## Update `nuxt.config.js` | ||
|
||
In this release, we made the `middlewareUrl` property required for security reasons. Open the `nuxt.config.js` file and add the `middlewareUrl` property like shown below: | ||
|
||
```javascript | ||
// nuxt.config.js | ||
export default { | ||
publicRuntimeConfig: { | ||
middlewareUrl: 'https://yourdomain.com/api/' // For the local development, set it to `http://localhost:3000/api/`. | ||
} | ||
} | ||
``` | ||
|
||
:::warning | ||
Make sure to pass whole url with protocol and/or port and suffix it with `/api/`. | ||
::: | ||
|
||
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: | ||
``` | ||
// .env | ||
API_BASE_URL=https://yourdomain.com/api/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Migration guides | ||
|
||
## 2.5.7 | ||
- [Overview](./2.5.7/overview.md) | ||
|
||
## 2.5.0 | ||
- [Overview](./2.5.0/overview.md) | ||
|
||
|