Skip to content

Commit 6c49728

Browse files
committed
docs: RestEndpoint and resource() docs improvements
1 parent 2d45da3 commit 6c49728

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

docs/rest/api/RestEndpoint.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,34 @@ must also contain a [Collection](./Collection.md).
563563

564564
Prepends this to the compiled [path](#path)
565565

566+
#### Inheritance defaults
567+
568+
```typescript
569+
export class MyEndpoint<
570+
O extends RestGenerics = any,
571+
> extends RestEndpoint<O> {
572+
// this allows us to override the prefix in production environments, with a dev fallback
573+
urlPrefix = process.env.API_SERVER ?? 'http://localhost:8000';
574+
}
575+
```
576+
577+
[Learn more about inheritance patterns](#inheritance) for RestEndpoint
578+
579+
#### Instance overrides
580+
581+
```typescript
582+
export const getTicker = new RestEndpoint({
583+
urlPrefix: 'https://api.exchange.coinbase.com',
584+
path: '/products/:product_id/ticker',
585+
schema: Ticker,
586+
});
587+
```
588+
589+
#### Dynamic prefix
590+
566591
:::tip
567592

568-
For a dynamic prefix, try overriding the url() method:
593+
For a dynamic prefix, try overriding the url() method instead:
569594

570595
```ts
571596
const getTodo = new RestEndpoint({

docs/rest/api/resource.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,9 @@ export const IssueResource= resource({
657657
658658
## Function Inheritance Patterns
659659
660-
To reuse code around `Resource` design, you can create your own function that calls resource().
661-
This has similar effects as class-based inheritance.
660+
To reuse code related to `Resource` definitions, you can create your own function that calls resource().
661+
This has similar effects as class-based inheritance, with the added benefit of allowing for complete
662+
typing overrides.
662663
663664
```typescript
664665
import {
@@ -673,6 +674,8 @@ import {
673674
export class AuthdEndpoint<
674675
O extends RestGenerics = any,
675676
> extends RestEndpoint<O> {
677+
urlPrefix = process.env.API_SERVER ?? 'http://localhost:8000';
678+
676679
async getRequestInit(body: any): Promise<RequestInit> {
677680
return {
678681
...(await super.getRequestInit(body)),
@@ -681,7 +684,7 @@ export class AuthdEndpoint<
681684
}
682685
}
683686

684-
export function createMyResource<O extends ResourceGenerics = any>({
687+
export function myResource<O extends ResourceGenerics = any>({
685688
schema,
686689
Endpoint = AuthdEndpoint,
687690
...extraOptions

0 commit comments

Comments
 (0)