Skip to content

Commit

Permalink
feat(utils): export getParams function (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
smackfu committed Apr 24, 2023
1 parent 363d2a5 commit dbcbcd5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/parrot-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,35 @@ Sets the mock at `index` for scenario with matching `name`.
- `name` (_String_): Scenario name.
- `index` (_Number_): Mock index.
- `mock` (_Object_): Mock object.

## Utility Methods

### `getParams(path, route)`

Extracts the route parameters from a given `path`, using the specified `route`.

This is useful when using the manual `match` function.

#### Arguments

- `path` (_String_): Requested URL path.
- `route` (_String_): [Express-style](https://expressjs.com/en/guide/routing.html) route with route parameters.

#### Example Usage

```javascript
const { getParams } = require('parrot-core');

const getBook = ({ url }, match) => {
const path = '/books/:bookId';
if (match({ path, method: 'GET' })) {
const { bookId } = getParams(url, path);
const requestedBook = books.find(book => book.bookId === bookId);
if (!requestedBook) {
return { status: 404 };
}
return { status: 200, body: requestedBook };
}
return null;
};
```
1 change: 1 addition & 0 deletions packages/parrot-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
*/

export { default } from './Parrot';
export getParams from './utils/getParams';

0 comments on commit dbcbcd5

Please sign in to comment.