forked from pkp/ui-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkp/pkp-lib#10134 Add option to access ojs via storybook proxy to use…
… real API
- Loading branch information
1 parent
8276669
commit dd0e0a3
Showing
5 changed files
with
53 additions
and
9 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
const {createProxyMiddleware} = require('http-proxy-middleware'); | ||
|
||
module.exports = function expressMiddleware(router) { | ||
router.use( | ||
'/index.php', // This is the API prefix that you want to proxy | ||
createProxyMiddleware({ | ||
target: 'http://localhost:7003', // The backend server you want to proxy to | ||
changeOrigin: true, | ||
}), | ||
); | ||
if (process.env.STORYBOOK_APP_DOMAIN_URL) { | ||
router.use( | ||
'/index.php', // This is the API prefix that you want to proxy | ||
createProxyMiddleware({ | ||
target: process.env.STORYBOOK_APP_DOMAIN_URL, // The backend server you want to proxy to | ||
changeOrigin: true, | ||
//pathRewrite: { | ||
//'^/index.php': '', // Rewrites the URL removing /api prefix before sending to the backend server | ||
//}, | ||
}), | ||
); | ||
} | ||
}; |
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,23 @@ | ||
import {Meta} from '@storybook/blocks'; | ||
|
||
<Meta title="Guide/API Mocking" /> | ||
|
||
# API Mocking | ||
|
||
Everything that run in storybook runs indepdendently from our PHP server. Therefore any interactions with API needs to be mocked. Good example how this can be achieved with [MSW](https://mswjs.io) library can be found in `ExamplePage.stories.js` file. | ||
|
||
## API interactions with real PHP backend | ||
|
||
In case the component/page is implemented in isolation and new API is being created - its possible to point storybook to use this real API for testing purposes. | ||
|
||
To overcome CORS challenges its achieved by adding simple proxy to storybook (`./storybook/middleware.js`). | ||
|
||
Assuming that you run ojs/omp/ops via php development server (`php -S localhost:7001`), you can run storybook with alternative command: | ||
|
||
`npm run storybookWithProxy` | ||
|
||
Or if you want to customise the port, you can run it directly with following command | ||
|
||
`STORYBOOK_APP_DOMAIN_URL=http://localhost:7878 npm run storybook` | ||
|
||
As result you will be able to access your instance via storybook url (`http://localhost:6006/index.php/publicknowledge/en/login`) and for example to login to create new session, which will than be used by the page/component in storybook as well. |