Mocking unfinished endpoints
To build against an endpoint the backend has not finished, mock it in the endpoint definition. When the endpoint is ready, your components do not change.
resources/Rating
Demo
import { Entity, resource } from '@data-client/rest'; import { Temporal } from 'temporal-polyfill'; export class Rating extends Entity { id = ''; rating = 4.6; author = ''; date = Temporal.Instant.fromEpochMilliseconds(0); static key = 'Rating'; static schema = { date: Temporal.Instant.from, }; } export const RatingResource = resource({ path: '/ratings/:id', schema: Rating, }).extend({ getList: { dataExpiryLength: Infinity, fetch() { return Promise.resolve( ['Morningstar', 'Seekingalpha', 'Morningstar', 'CNBC'].map(author => ({ id: `${Math.random()}`, rating: (2 + Math.random() * 3).toFixed(1), author, date: '1990-01-01T00:00:00Z', })), ); }, }, });
The fetch override on the RestEndpoint returns fake data. The rest of the code uses the
typed RatingResource as usual.
When the API is ready, remove the whole getList override. Both fetch and dataExpiryLength exist only for the mock.
dataExpiryLength: Infinity stops the data from expiring, so Data Client does not refetch it on its own and replace the random values.