-
Notifications
You must be signed in to change notification settings - Fork 21
Mocking API Responses
Nick Watts edited this page Jul 11, 2022
·
7 revisions
API responses can be mocked by setting window.ajaxOverridesStore
.
The store should contain an array, where each item is an object with two properties:
-
filter
: Determines which URLs to apply the override to. This can be specified in two ways:- A function that accepts arguments to
fetch
and returns a boolean - An object with two keys:
-
url
: a regex matched against the request URL -
method
: (optional) a string compared against the request method
-
- A function that accepts arguments to
-
fn
: A function that wrapsfetch
(accepts afetch
function and returns a newfetch
function
For example:
window.ajaxOverridesStore.set([
{
filter: { url: /api\/billing\/v2$/ },
fn: () => () => Promise.resolve(new Response(JSON.stringify(projectListResult), { status: 200 }))
}
]
window.ajaxOverrideUtils
contains a few helpers for creating the fn
function.
-
mapJsonBody
: takes a function that transforms the original response body -
makeError
: takes an object with two keys:-
status
: status code for the mock response -
frequency
: (optional) number 0-1 that controls what proportion of responses are replaced with mock errors
-
-
makeSuccess
: takes an object to return as the response
Terra UI Wiki.
- Getting Started
- Contributor Guide
- Intro to UI Development
- Troubleshooting Build Failures
- Editor Configuration
- BEEs
- Pull Requests
- How to Find a PR Site
- Feature Flags
- Mixpanel
- Cobranding and White-Label Sites
- Using Terra UI packages in other projects