repos / starfx

a micro-mvc framework for react apps
git clone https://github.com/neurosnap/starfx.git

starfx / examples / tests-rtl / src
Eric Bower  ·  2025-06-06

api.ts

 1import { createApi, createSchema, mdw, slice } from "starfx";
 2
 3const emptyUser = { id: "", name: "" };
 4export const [schema, initialState] = createSchema({
 5  users: slice.table({ empty: emptyUser }),
 6  cache: slice.table(),
 7  loaders: slice.loaders(),
 8});
 9
10export const api = createApi();
11api.use(mdw.api({ schema }));
12api.use(api.routes());
13api.use(mdw.fetch({ baseUrl: "https://jsonplaceholder.typicode.com" }));
14
15export const fetchUsers = api.get(
16  "/users",
17  function* (ctx, next) {
18    yield* next();
19
20    if (!ctx.json.ok) {
21      return;
22    }
23
24    const users = ctx.json.value.reduce((acc, user) => {
25      acc[user.id] = user;
26      return acc;
27    }, {});
28
29    yield* schema.update(schema.users.add(users));
30  },
31);