Highlights
Deprecated networking definitions were removed from rest-hooks
exports, and moved to @rest-hooks/legacy.
FlatEntity, SimpleRecord, NestedEntity, schemas, isEntity, Entity, Resource, SimpleResource, SchemaDetail, SchemaList, Method
These are still supported! They are simply moved to @rest-hooks/legacy. This allows smooth incremental migrations.
yarn add @rest-hooks/legacy@2.2.0
- has all of these, and is compatible with both
rest-hooks
5 and 6.
- Upgrade
rest-hooks
&@rest-hooks/legacy
to 6. - Gradually migrate to @rest-hooks/rest
@rest-hooks/endpoint changes
SimpleRecord -> [Object](https://resthooks.io/docs/api/Object)
SimpleRecord was removed (though available in @rest-hooks/legacy)
- before
- after
export class Address extends SimpleRecord {
readonly street: string = '';
readonly suite: string = '';
readonly city: string = '';
readonly zipcode: string = '';
readonly createdAt: Date = new Date(0);
static schema = {
createdAt: Date,
};
}
export const Address = {
street: '',
suite: '',
city: '',
zipcode: '',
date: Date,
};
@rest-hooks/rest changes from 2 -> 3
These add on to the existing changes of @rest-hooks/rest from @rest-hooks/legacy
If
Resource.fromJS()
was used to customize normalization process, useprocess()
instead.class MyResource extends Resource {
static process(input: any, parent: any, key: string | undefined): any {
return {
...input,
extraThing: 5,
};
}
}
New default error behavior
- To keep existing:
class MyResource extends Resource {
static getEndpointExtra(): EndpointExtraOptions | undefined {
return {
errorPolicy: error => 'soft' as const,
};
}
}
- To keep existing:
Full list of changes
@rest-hooks/endpoint
Entity
- fromJS() -> process() to customize init
- normalize results in POJO rather than instances
- This is only meaningful for those inspecting the rest hooks state directly
- FlatEntity, SimpleRecord removed (use @rest-hooks/legacy)
@rest-hooks/rest
- peerDep @rest-hooks/endpoint > 2
@rest-hooks/core
buildInferredResult -> inferResults
Error behavior
useError() will no longer create synthetic errors for missing entities
useError() errorPolicy
EndpointExtraOptions
interface EndpointExtraOptions {
//...rest
errorPolicy?: (error: any) => 'soft' | undefined;
}'soft' vs
undefined
- 'soft' avoids errors if existing results are still available (even if stale)
undefined
(hard error) means any error always falls
@rest-hooks/rest
New default policy: 5xx are soft, else hard.
@rest-hooks/rest
is where errors have 'status' members. This concept does not exist in base Endpoints.static getEndpointExtra(): EndpointExtraOptions | undefined {
return;
return {
errorPolicy: error =>
error.status >= 500 ? ('soft' as const) : undefined,
};
}PollingSubscription
// never break when data already exists
errorPolicy: () => 'soft' as const,@rest-hooks/legacy - Resource
Existing policy was to always be 'soft' no matter what. This maintains that behavior.
/** @deprecated */
/** Get the request options for this SimpleResource */
static getFetchOptions(): FetchOptions | undefined {
return {
errorPolicy: () => 'soft' as const,
};
}polled fetch errors are always 'soft'
@rest-hooks/rest
- 5xx: 'soft'
- 4xx, 3xx, etc: 'hard'
peerDep @rest-hooks/endpoint > 2
rest-hooks
Removed exports from 'rest-hooks': NestedEntity, schemas, isEntity, Entity, Resource, SimpleResource, SchemaDetail, SchemaList, Method
- use @rest-hooks/legacy, or @rest-hooks/rest instead
@rest-hooks/legacy
- peerDep @rest-hooks/endpoint > 2