Customized variant handlers for Mocks Server to return response by selectors.
Extended official json
and middleware
variant handlers to allow only returning response when given request selectors are satisfied.
- Developed by TypeScript
- Testing converge > 90%
- Supports ESM and CJS
- Developed following official instruction
// npm
npm install @yukun-han/selectable-response
// yarn
yarn add @yukun-han/selectable-response
Extended variant handlers now have: extended-json
, extended-middleware
.
Selector Conditions now support:
- header:
headerKey
- path:
pathVariable
- query:
queryParams
- body:
bodyJsonPath
- jsonpath is used to parse body value
Comparing operators now support:
equals
: tests if condition equals given value byObject.is
matchReg
: tests if condition could pass given Regex
Take extended-json
with headerKey
as an example.
-
Registered in config
{ variantHandlers: { register: [ExtendedJsonHandler], } }
-
Use in routes
{ id: 'ext-json', url: '/api/ext-json/header-key', method: 'GET', variants: [ { id: 'header-key', type: 'extended-json', // declare type as `extended-json` options: { selectors: [{ headerKey: 'x-test-key', equals: 'need' }], // declare header selector condition status: 200 // will only return if request condition satisfied body: 'OK' }, }, ] }
-
Send request and verify
// satisfy const response = await fetch('/api/ext-json/header-key', { headers: { 'x-test-key': 'need' }, }); response.status // => 200 // could not satisfy const response = await fetch('/api/ext-json/header-key'); response.status // => 404, could not found this endpoint
Thanks Javier Brea and Mocks Server. 🍻