Skip to content

Commit

Permalink
refactor(store): export storeMdw as collection
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Jan 10, 2024
1 parent 2bf9e4c commit f43c20f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
6 changes: 3 additions & 3 deletions query/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ it(tests, "createApi with hash key on a large post", async () => {
const { store, schema } = testStore();
const query = createApi();
query.use(mdw.api());
query.use(storeMdw(schema.db));
query.use(storeMdw.store(schema.db));
query.use(query.routes());
query.use(function* fetchApi(ctx, next) {
const data = {
Expand Down Expand Up @@ -308,7 +308,7 @@ it(tests, "createApi - two identical endpoints", async () => {
const { store, schema } = testStore();
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(mdw.nameParser);
api.use(api.routes());

Expand Down Expand Up @@ -462,7 +462,7 @@ it(tests, "should bubble up error", () => {
}
});
api.use(mdw.queryCtx);
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());

const fetchUser = api.get(
Expand Down
24 changes: 12 additions & 12 deletions query/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ it(
const { store, schema } = testStore();
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(fetchMdw.headers);
api.use(mdw.fetch({ baseUrl }));
Expand Down Expand Up @@ -91,7 +91,7 @@ it(
const { store, schema } = testStore();
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(mdw.fetch({ baseUrl }));

Expand Down Expand Up @@ -127,7 +127,7 @@ it(tests, "error handling", async () => {
const { schema, store } = testStore();
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(mdw.fetch({ baseUrl }));

Expand Down Expand Up @@ -163,7 +163,7 @@ it(tests, "status 204", async () => {
const { schema, store } = testStore();
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(function* (ctx, next) {
const url = ctx.req().url;
Expand Down Expand Up @@ -203,7 +203,7 @@ it(tests, "malformed json", async () => {
const { schema, store } = testStore();
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(function* (ctx, next) {
const url = ctx.req().url;
Expand Down Expand Up @@ -248,7 +248,7 @@ it(tests, "POST", async () => {
const { schema, store } = testStore();
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(fetchMdw.headers);
api.use(mdw.fetch({ baseUrl }));
Expand Down Expand Up @@ -291,7 +291,7 @@ it(tests, "POST multiple endpoints with same uri", async () => {
const { store, schema } = testStore();
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(fetchMdw.headers);
api.use(mdw.fetch({ baseUrl }));
Expand Down Expand Up @@ -353,7 +353,7 @@ it(
const { store, schema } = testStore();
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(mdw.fetch({ baseUrl }));

Expand Down Expand Up @@ -402,7 +402,7 @@ it(
const { schema, store } = testStore();
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(mdw.fetch({ baseUrl }));

Expand Down Expand Up @@ -448,7 +448,7 @@ it(
let actual = null;
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(mdw.fetch({ baseUrl }));

Expand Down Expand Up @@ -479,7 +479,7 @@ it(
let actual = null;
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(mdw.fetch({ baseUrl }));

Expand All @@ -504,7 +504,7 @@ it(tests, "should use dynamic mdw to mock response", async () => {
let actual = null;
const api = createApi();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(mdw.fetch({ baseUrl }));

Expand Down
15 changes: 7 additions & 8 deletions query/mdw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createApi, createKey, mdw } from "../query/mod.ts";
import type { ApiCtx, Next, ThunkCtx } from "../query/mod.ts";
import { createQueryState } from "../action.ts";
import { sleep } from "../test.ts";

import {
configureStore,
createSchema,
Expand Down Expand Up @@ -110,7 +109,7 @@ it(tests, "with loader", () => {
const { schema, store } = testStore();
const api = createApi<ApiCtx>();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(function* fetchApi(ctx, next) {
ctx.response = new Response(jsonBlob(mockUser), { status: 200 });
Expand Down Expand Up @@ -153,7 +152,7 @@ it(tests, "with item loader", () => {
const { store, schema } = testStore();
const api = createApi<ApiCtx>();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(function* fetchApi(ctx, next) {
ctx.response = new Response(jsonBlob(mockUser), { status: 200 });
Expand Down Expand Up @@ -250,7 +249,7 @@ it(tests, "simpleCache", () => {
const { store, schema } = testStore();
const api = createApi<ApiCtx>();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(function* fetchApi(ctx, next) {
const data = { users: [mockUser] };
Expand Down Expand Up @@ -280,7 +279,7 @@ it(tests, "overriding default loader behavior", () => {
const { store, schema } = testStore();
const api = createApi<ApiCtx>();
api.use(mdw.api());
api.use(storeMdw(schema.db));
api.use(storeMdw.store(schema.db));
api.use(api.routes());
api.use(function* fetchApi(ctx, next) {
const data = { users: [mockUser] };
Expand Down Expand Up @@ -337,7 +336,7 @@ it(tests, "mdw.api() - error handler", () => {
const { schema, store } = testStore();
const query = createApi<ApiCtx>();
query.use(mdw.api());
query.use(storeMdw(schema.db));
query.use(storeMdw.store(schema.db));
query.use(query.routes());
query.use(function* () {
throw new Error("something happened");
Expand All @@ -353,7 +352,7 @@ it(tests, "createApi with own key", async () => {
const { schema, store } = testStore();
const query = createApi();
query.use(mdw.api());
query.use(storeMdw(schema.db));
query.use(storeMdw.store(schema.db));
query.use(query.routes());
query.use(mdw.customKey);
query.use(function* fetchApi(ctx, next) {
Expand Down Expand Up @@ -424,7 +423,7 @@ it(tests, "createApi with custom key but no payload", async () => {
const { store, schema } = testStore();
const query = createApi();
query.use(mdw.api());
query.use(storeMdw(schema.db));
query.use(storeMdw.store(schema.db));
query.use(query.routes());
query.use(mdw.customKey);
query.use(function* fetchApi(ctx, next) {
Expand Down
3 changes: 2 additions & 1 deletion store/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ export * from "./context.ts";
export * from "./fx.ts";
export * from "./store.ts";
export * from "./types.ts";
export * from "./query.ts";
export * from "./supervisor.ts";
export { createSelector } from "../deps.ts";
export * from "./slice/mod.ts";
export * from "./schema.ts";
export * from "./batch.ts";
export * from "./persist.ts";
import * as storeMdw from "./query.ts";
export { storeMdw };
16 changes: 8 additions & 8 deletions store/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ import { put, select, updateStore } from "./fx.ts";
import { LoaderOutput } from "./slice/loader.ts";
import { TableOutput } from "./slice/table.ts";

export function storeMdw<
export function store<
Ctx extends ApiCtx = ApiCtx,
M extends AnyState = AnyState,
>({ cache, loaders, errorFn }: {
>(props: {
loaders: LoaderOutput<M, AnyState>;
cache: TableOutput<any, AnyState>;
errorFn?: (ctx: Ctx) => string;
}) {
return compose<Ctx>([
dispatchActions,
loadingMonitor(loaders, errorFn),
simpleCache(cache),
actions,
loaderApi(props.loaders, props.errorFn),
cache(props.cache),
]);
}

/**
* This middleware will automatically cache any data found inside `ctx.json`
* which is where we store JSON data from the {@link mdw.fetch} middleware.
*/
export function simpleCache<Ctx extends ApiCtx = ApiCtx>(
export function cache<Ctx extends ApiCtx = ApiCtx>(
dataSchema: TableOutput<any, AnyState>,
) {
return function* (
Expand Down Expand Up @@ -54,7 +54,7 @@ export function simpleCache<Ctx extends ApiCtx = ApiCtx>(
* within the pipeline of the middleware and instead of dispatching them serially this
* improves performance by only hitting the reducers once.
*/
export function* dispatchActions(ctx: { actions: AnyAction[] }, next: Next) {
export function* actions(ctx: { actions: AnyAction[] }, next: Next) {
if (!ctx.actions) ctx.actions = [];
yield* next();
if (ctx.actions.length === 0) return;
Expand Down Expand Up @@ -100,7 +100,7 @@ export function loader<
/**
* This middleware will track the status of a fetch request.
*/
export function loadingMonitor<
export function loaderApi<
Ctx extends ApiCtx = ApiCtx,
M extends AnyState = AnyState,
>(
Expand Down

0 comments on commit f43c20f

Please sign in to comment.