Skip to content

Commit

Permalink
docs: Move resource-lifetime into expiry-policy page (#1978)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed May 2, 2022
1 parent 07e7f68 commit b84239d
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 79 deletions.
37 changes: 32 additions & 5 deletions docs/README.md
Expand Up @@ -321,7 +321,11 @@ Rest Hooks what response it _expects_ to receive from the mutation call, Rest Ho
can **immediately** update **all** components using the relevant entity.

```typescript
const getOptimisticResponse = (snap: SnapshotInterface, params: Params, body: FormData) => ({
const getOptimisticResponse = (
snap: SnapshotInterface,
params: Params,
body: FormData,
) => ({
id: params.id,
...body,
});
Expand Down Expand Up @@ -355,7 +359,11 @@ const todoUpdate = new Endpoint(fetchTodoUpdate, {
getOptimisticResponse,
});

const getOptimisticResponse = (snap: SnapshotInterface, params: Params, body: FormData) => ({
const getOptimisticResponse = (
snap: SnapshotInterface,
params: Params,
body: FormData,
) => ({
id: params.id,
...body,
});
Expand Down Expand Up @@ -479,7 +487,6 @@ export const getPhoto = new Endpoint(async ({ userId }: { userId: string }) => {
[Suspense](./getting-started/data-dependency.md#async-fallbacks) as well as the [render as you fetch](./guides/render-as-you-fetch.md)
pattern for improved user experience.


## Debugging

<img src={require('@site/static/img/redux-devtools-logo.jpg').default} width="75" height="75" alt="redux-devtools" style={{ float: 'left', "marginRight": "var(--ifm-paragraph-margin-bottom)" }} />
Expand Down Expand Up @@ -564,6 +571,26 @@ const todoDetail404Fixture: FixtureEndpoint = {

## Demo

See this all in action in [examples/todo-app](https://stackblitz.com/github/coinbase/rest-hooks/tree/master/examples/todo-app?file=src%2Fpages%2FHome%2Findex.tsx)
### Todo Demo

<iframe
src="https://stackblitz.com/github/coinbase/rest-hooks/tree/master/examples/todo-app?embed=1&file=src%2Fpages%2FHome%2FTodoListComponent.tsx&hidedevtools=1&view=both&ctl=1"
width="100%"
height="500"
></iframe>
[Open demo in own tab](https://stackblitz.com/github/coinbase/rest-hooks/tree/master/examples/todo-app?file=src%2Fpages%2FHome%2FTodoListComponent.tsx)

[Explore on github](https://github.com/coinbase/rest-hooks/tree/master/examples/todo-app)

### Github Demo

<iframe
src="https://stackblitz.com/github/coinbase/rest-hooks/tree/master/examples/github-app?embed=1&file=src%2Fpages%2FIssueList.tsx&hidedevtools=1&view=preview&ctl=1"
width="100%"
height="500"
></iframe>
[Open demo in own tab](https://stackblitz.com/github/coinbase/rest-hooks/tree/master/examples/github-app?file=src%2Fpages%2FIssueList.tsx)

Or a [github api demo](https://stackblitz.com/github/coinbase/rest-hooks/tree/master/examples/github-app?file=src%2Fpages%2FIssueList.tsx)
[Explore on github](https://github.com/coinbase/rest-hooks/tree/master/examples/github-app)
7 changes: 6 additions & 1 deletion docs/getting-started/endpoint.md
@@ -1,7 +1,12 @@
---
title: Endpoint
title: Define API Endpoint
sidebar_label: Endpoint
---

<head>
<title>TypeScript Standard Endpoints</title>
</head>

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import LanguageTabs from '@site/src/components/LanguageTabs';
Expand Down
44 changes: 44 additions & 0 deletions docs/getting-started/expiry-policy.md
Expand Up @@ -96,6 +96,50 @@ render(<Navigator />);

</HooksPlayground>

<details><summary><b>@rest-hooks/rest</b></summary>

## Examples

To apply to all of a [Resource's endpoints](../api/resource#detail), use [getEndpointExtra](../api/resource#getEndpointExtra)

### Long cache lifetime

`LongLivingResource.ts`

```typescript
import { Resource } from '@rest-hooks/rest';

// We can now extend LongLivingResource to get a resource that will be cached for one hour
abstract class LongLivingResource extends Resource {
static getEndpointExtra() {
return {
...super.getEndpointExtra(),
dataExpiryLength: 60 * 60 * 1000, // one hour
};
}
}
```

### Never retry on error

`NoRetryResource.ts`

```typescript
import { Resource } from '@rest-hooks/rest';

// We can now extend NoRetryResource to get a resource that will never retry on network error
abstract class NoRetryResource extends Resource {
static getEndpointExtra() {
return {
...super.getEndpointExtra(),
errorExpiryLength: Infinity,
};
}
}
```

</details>

### Endpoint.invalidIfStale

[Endpoint.invalidIfStale](../api/Endpoint#invalidifstale) eliminates the `stale` status, making data
Expand Down
3 changes: 2 additions & 1 deletion docs/getting-started/validation.md
@@ -1,5 +1,6 @@
---
title: Validation
title: Entity Validation
sidebar_label: Validation
---

import HooksPlayground from '@site/src/components/HooksPlayground';
Expand Down
13 changes: 5 additions & 8 deletions docs/guides/custom-networking.md
Expand Up @@ -3,7 +3,7 @@ title: Using a custom networking library
sidebar_label: Custom networking library
---
import CodeBlock from '@theme/CodeBlock';
import ResourceSource from '!!raw-loader!@site/../packages/rest/src/Resource.ts';
import ResourceSource from '!!raw-loader!@site/../packages/rest/src/BaseResource.ts';

`Resource.fetch()` wraps the standard [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
One key customization is ensuring every network related error thrown has a
Expand All @@ -29,17 +29,14 @@ The [whatwg-fetch](https://github.com/github/fetch#installation) polyfill will b
useful in environments that don't support it, like node and older browsers
(Internet Explorer). Be sure to include it in any bundles that need it.

This implementation is provided as a useful reference for building your own.
For the most up-to-date implementation, see the [source on master](https://github.com/coinbase/rest-hooks/blob/master/packages/rest-hooks/src/resource/Resource.ts)

<CodeBlock className="language-typescript">{ResourceSource}</CodeBlock>

## Superagent

[Superagent](https://visionmedia.github.io/superagent/)

```typescript
import { SimpleResource, Method } from '@rest-hooks/rest';
import { Resource, Method } from '@rest-hooks/rest';
import type { SuperAgentRequest } from 'superagent';

const ResourceError = `JSON expected but not returned from API`;
Expand All @@ -48,7 +45,7 @@ const ResourceError = `JSON expected but not returned from API`;
* Represents an entity to be retrieved from a server.
* Typically 1:1 with a url endpoint.
*/
export default abstract class Resource extends SimpleResource {
export default abstract class SuperResource extends Resource {
/** A function to mutate all requests for fetch */
static fetchPlugin?: (request: SuperAgentRequest) => SuperAgentRequest;

Expand Down Expand Up @@ -81,10 +78,10 @@ export const isInvalidResponse = (res: request.Response): boolean => {
[Axios](https://github.com/axios/axios)

```typescript
import { SimpleResource, Method } from '@rest-hooks/rest';
import { Resource, Method } from '@rest-hooks/rest';
import axios from 'axios';

export default abstract class AxiosResource extends SimpleResource {
export default abstract class AxiosResource extends Resource {
/** Perform network request and resolve with json body */
static async fetch(
input: RequestInfo, init: RequestInit
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/nested-response.md
Expand Up @@ -90,7 +90,7 @@ export default class ArticleResource extends Resource {
}
static urlRoot = 'http://test.com/article/';

static schema = {
static schema: { [k: string]: Schema } = {
author: UserResource,
contributors: [UserResource],
};
Expand Down
41 changes: 0 additions & 41 deletions docs/guides/resource-lifetime.md

This file was deleted.

4 changes: 2 additions & 2 deletions examples/todo-app/src/pages/Home/NewTodo.tsx
Expand Up @@ -43,8 +43,8 @@ const TodoBox = styled.div`
const TitleInput = styled.input`
flex: 1 1 auto;
width: 100%;
background: #aaaacc;
background: #e1e1e1;
&:focus {
background: aaf;
background: eeeeee;
}
`;
2 changes: 1 addition & 1 deletion examples/todo-app/src/pages/Home/index.tsx
Expand Up @@ -21,7 +21,7 @@ const Title = styled.nav`
top: 0;
width: 100%;
padding: 18px;
background: #4516f0;
background: white;
z-index: 100;
`;

Expand Down
4 changes: 2 additions & 2 deletions examples/todo-app/src/style/main.scss
Expand Up @@ -9,6 +9,6 @@
body {
font-family: 'Graphik', 'Avenir Next', 'Helvetica Nueue', Helvetica,
sans-serif;
background-color: #4516f0;
color: white;
background-color: white;
color: #111;
}
4 changes: 4 additions & 0 deletions website/docusaurus.config.js
Expand Up @@ -148,6 +148,10 @@ module.exports = {
to: '/docs/api/useDLE',
from: ['/docs/guides/no-suspense'],
},
{
to: '/docs/getting-started/expiry-policy',
from: ['/docs/guides/resource-lifetime'],
},
],
},
],
Expand Down
23 changes: 6 additions & 17 deletions website/sidebars.json
Expand Up @@ -75,17 +75,9 @@
{
"type": "doc",
"id": "guides/mocking-unfinished"
},
{
"type": "doc",
"id": "guides/resource-lifetime"
}
]
},
{
"type": "doc",
"id": "guides/computed-properties"
},
{
"type": "category",
"collapsible": false,
Expand All @@ -102,15 +94,12 @@
]
},
{
"type": "category",
"collapsible": false,
"label": "Advanced",
"items": [
{
"type": "doc",
"id": "guides/custom-networking"
}
]
"type": "doc",
"id": "guides/computed-properties"
},
{
"type": "doc",
"id": "guides/custom-networking"
}
]
},
Expand Down

0 comments on commit b84239d

Please sign in to comment.