9.4.1 • Published 1 year ago

@rest-hooks/experimental v9.4.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

Experimental extensions for Rest Hooks

CircleCI Coverage Status npm downloads bundle size npm version PRs Welcome

📖Read The Docs

Motivation

Field application of designs help smooth edges of a theoretical design. New designs can be iterated on here, breaking freely without worry of legacy support plans.

Usage

useController()

const controller = useController();

return (
  <form onSubmit={() => controller.fetch(User.create(), userPayload)}>
    <button onClick={() => controller.fetch(User.list())}>Refresh list</button>
    <button onClick={() => controller.invalidate(User.list())}>Suspend list</button>
  </form>
);

Endpoint.update

const createUser = new Endpoint(postToUserFunction, {
  schema: User,
  update: (newUserId: string) => ({
    [userList.key({})]: (users = []) => [newUserId, ...users],
  }),
});
const createUser = new Endpoint(postToUserFunction, {
  schema: User,
  update: (newUserId: string, newUser: User)  => {
    const updates = {
      [userList.key()]: (users = []) => [newUserId, ...users],
      [userList.key({ sortBy: 'createdAt' })]: (users = [], { sortBy }) => {
        const ret = [newUserId, ...users];
        ret.sortBy(sortBy);
        return ret;
      },
    ];
    if (newUser.isAdmin) {
      updates[userList.key({ admin: true })] = (users = []) => [newUserId, ...users];
    }
    return updates;
  },
});

Entity, EntityRecord, Resource, BaseResource

  • Normalizes to pojo
  • Faster
  • Entity has no defined key lookups - but EntityRecord does.
  • BaseResource is missing predefined endpoints (list, detail, etc), but has everything else

Resource.list() declarative pagination

Addition of paginated().

class NewsResource extends Resource {
  static listPage<T extends typeof NewsResource>(this: T) {
    return this.list().paginated(({ cursor, ...rest }) => [rest]);
  }
}
import { useSuspense, useController } from '@rest-hooks/react';
import NewsResource from 'resources/NewsResource';

function NewsList() {
  const { results, cursor } = useSuspense(NewsResource.list(), {});
  const curRef = useRef(cursor);
  curRef.current = cursor;
  const controller = useController();
  const getNextPage = useCallback(
    () => controller.fetch(NewsResource.listPage(), { cursor: curRef.current }),
    []
  );

  return (
    <Pagination onPaginate={getNextPage} nextCursor={cursor}>
      <NewsList data={results} />
    </Pagination>
  );
}
9.4.1

1 year ago

9.4.0

1 year ago

9.3.7

1 year ago

9.3.6

1 year ago

9.3.5

1 year ago

9.3.8

1 year ago

9.3.4

1 year ago

9.3.3

1 year ago

9.3.2

1 year ago

9.3.1

1 year ago

9.2.5

1 year ago

9.2.4

1 year ago

9.2.3

1 year ago

9.2.2

1 year ago

9.3.0

1 year ago

9.2.1

1 year ago

9.1.0

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

8.0.2

2 years ago

9.2.0

2 years ago

9.0.1

2 years ago

9.0.0

2 years ago

6.0.0

2 years ago

7.1.0-beta.0

2 years ago

7.1.0-beta.2

2 years ago

7.1.0-beta.1

2 years ago

7.0.0

2 years ago

7.1.0

2 years ago

5.0.6

2 years ago

5.0.5

2 years ago

5.0.4

2 years ago

5.0.3

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

3.3.2

2 years ago

4.0.0

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.2.0

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.0.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.0-beta.4

3 years ago

1.0.0-beta.3

3 years ago

1.0.0-beta.2

3 years ago

0.9.1

3 years ago

1.0.0-beta.1

3 years ago

1.0.0-beta.0

3 years ago

0.9.0

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.3.0

3 years ago

0.4.0

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago