Upgrading from 7 to 8
For those who previously upgraded from 6 to 7, be sure to complete preparing for the future. The future is now.
Upgrading can be done gradually as all changes were initially released in /next
.
Upgrade to the latest v7:
npm install --save @rest-hooks/[email protected]
Incrementally move to new versions by importing from
/next
import { useController } from '@rest-hooks/react/next';
Upgrade to v8.
npm install --save @rest-hooks/[email protected]
Imports can be updated incrementally after upgrade.
/next
exports the same as top-level.import { useController } from '@rest-hooks/react';
Changes
Controller.fetch(): 2545 Controller.fetch() returns denormalized form when Endpoint has a Schema
const handleChange = async e => {
const todo = await ctrl.fetch(
TodoResource.partialUpdate,
{ id: todo.id },
{ completed: e.currentTarget.checked },
);
// todo is Todo, we can use all its members and be type-safe
console.log(todo.pk(), todo.title);
};NetworkManager: NetworkManager interface changed to only support new actions 2690
SubscriptionManager/PollingSubscription interfaces simplified based on new actions 2690
Removals of deprecated items
- 2691: Remove DispatchContext, DenormalizeCacheContext
Deprecations
- controller.receive, controller.receiveError 2690
- RECEIVE_TYPE 2690
- MiddlewareAPI.controller (MiddlewareAPI is just controller itself) 2690
({controller}) => {}
->(controller) => {}
Rest @7
Upgrading can be done gradually as all changes were initially released in /next
.
Upgrade to the latest v6:
npm install --save @rest-hooks/[email protected]
Incrementally move to new versions by importing from
/next
import {
RestEndpoint,
createResource,
GetEndpoint,
MutateEndpoint,
} from '@rest-hooks/rest/next';See the migrations of the /examples directory as an example
If you have a base
RestEndpoint
and/orcreateResource
function you can simply create two versions, which each extend from@rest-hooks/rest
and@rest-hooks/rest/next
Inheritance migration example
EndpointBase.tsimport { RestEndpoint, RestGenerics } from '@rest-hooks/rest';
export default class MyEndpoint<O extends RestGenerics = any> extends RestEndpoint<O> {
urlPrefix = 'https://api.github.com';
getHeaders(headers: HeadersInit): HeadersInit {
return {
...headers,
'Access-Token': getAuth(),
};
}
}NextEndpointBase.tsimport { RestEndpoint, RestGenerics } from '@rest-hooks/rest/next';
export default class NextMyEndpoint<O extends RestGenerics = any> extends RestEndpoint<O> {
urlPrefix = 'https://api.github.com';
getHeaders(headers: HeadersInit): Promise<HeadersInit> {
return {
...headers,
'Access-Token': await getAuth(),
};
}
}Upgrade to v7
npm install --save @rest-hooks/[email protected]
Imports can be updated incrementally after upgrade.
/next
exports the same as top-level.import { RestEndpoint, createResource } from '@rest-hooks/rest';
Changes
RestEndpoint's getRequestInit and getHeaders optionally return a promise 2542
import { RestEndpoint } from '@rest-hooks/rest/next';
export default class AuthdEndpoint<
O extends RestGenerics = any,
> extends RestEndpoint<O> {
declare static accessToken?: string;
async getHeaders(headers: HeadersInit) {
return {
...headers,
'Access-Token': await getOrFetchToken(),
} as HeadersInit;
}
}
export const TodoResource = createResource({
urlPrefix: 'https://jsonplaceholder.typicode.com',
path: '/todos/:id',
schema: Todo,
Endpoint: AuthdEndpoint,
});createResource().getList uses a Collection, which .create appends to 2593
Resource.create
will automatically add to the listResource.getList.push
is identical toResource.create
- Remove any Endpoint.update as it is not necessary and will not work
const createUser = new RestEndpoint({
path: '/user',
method: 'POST',
schema: User,
// delete the following:
update: (newUserId: string) => ({
[userList.key()]: (users = []) => [newUserId, ...users],
}),
});
GetEndpoint
andMutateEndpoint
parameters changed to whatNewGetEndpoint
,NewMutateEndpoint
was.createResource() generics changed to
O extends ResourceGenerics
This allows customizing the Resource type with body and searchParams 2593createGithubResource<U extends string, S extends Schema>
->createGithubResource<O extends ResourceGenerics>
Removals of deprecated items
- 2690: Removed deprecated
Endpoint.optimisticUpdate
-> useEndpoint.getOptimisticResponse
- 2688 Remove
FetchShape
compatibility. This removes support for the legacy hooks in 'rest-hooks' like useResource()
Support
As usual, if you have any troubles or questions, feel free to join our or file a bug