From ad1723920cef23da34e81b55dcac2b1adb2f4c1b Mon Sep 17 00:00:00 2001 From: Nathaniel Tucker Date: Wed, 17 Jan 2024 12:24:32 +0000 Subject: [PATCH] docs: Add config sharing section to restendpoint docs --- docs/rest/api/Endpoint.md | 15 ++++++++++++--- docs/rest/api/RestEndpoint.md | 21 +++++++++++++-------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/docs/rest/api/Endpoint.md b/docs/rest/api/Endpoint.md index a7b0cfe92737..50ab3bb9d527 100644 --- a/docs/rest/api/Endpoint.md +++ b/docs/rest/api/Endpoint.md @@ -148,6 +148,15 @@ render(); +### Configuration sharing + +Use [Endpoint.extend()](#extend) instead of `{...getTodo}` (spread) + +```ts +const getTodoNormalized = getTodo.extend({ schema: Todo }); +const getTodoUpdatingEveryFiveSeconds = getTodo.extend({ pollFrequency: 5000 }); +``` + ## Lifecycle ### Success @@ -231,7 +240,7 @@ class User extends Entity { pk() { return this.id;} } -const UserDetail = new Endpoint( +const getUser = new Endpoint( ({ id }) ⇒ fetch(`/users/${id}`), { schema: User } ); @@ -242,10 +251,10 @@ const UserDetail = new Endpoint( Can be used to further customize the endpoint definition ```typescript -const UserDetail = new Endpoint(({ id }) ⇒ fetch(`/users/${id}`)); +const getUser = new Endpoint(({ id }) ⇒ fetch(`/users/${id}`)); -const UserDetailNormalized = UserDetail.extend({ schema: User }); +const getUserNormalized = getUser.extend({ schema: User }); ``` In addition to the members, `fetch` can be sent to override the fetch function. diff --git a/docs/rest/api/RestEndpoint.md b/docs/rest/api/RestEndpoint.md index e5c9bc959068..0b67e275e96f 100644 --- a/docs/rest/api/RestEndpoint.md +++ b/docs/rest/api/RestEndpoint.md @@ -113,6 +113,14 @@ const getTodo = new RestEndpoint({ }); ``` +### Configuration sharing + +Use [RestEndpoint.extend()](#extend) instead of `{...getTodo}` (spread) + +```ts +const updateTodo = getTodo.extend({ method: 'PUT' }); +``` + ### Managing state @@ -127,17 +135,12 @@ export class Todo extends Entity { } } -const getTodo = new RestEndpoint({ - urlPrefix: 'https://jsonplaceholder.typicode.com', - path: '/todos/:id', - schema: Todo, -}); -const updateTodo = new RestEndpoint({ +export const getTodo = new RestEndpoint({ urlPrefix: 'https://jsonplaceholder.typicode.com', path: '/todos/:id', - method: 'PUT', schema: Todo, }); +export const updateTodo = getTodo.extend({ method: 'PUT' }); ``` @@ -419,7 +422,9 @@ class QSEndpoint extends RestEndpoint { import { RestEndpoint, RestGenerics } from '@data-client/rest'; import qs from 'qs'; -export default class QSEndpoint extends RestEndpoint { +export default class QSEndpoint< + O extends RestGenerics = any, +> extends RestEndpoint { searchToString(searchParams) { return qs.stringify(searchParams); }