From 2f42d898d4bbe8227aff140407eb64dd230e924c Mon Sep 17 00:00:00 2001 From: Jacob Bolda Date: Wed, 28 Aug 2024 10:43:38 -0500 Subject: [PATCH] increment calls --- packages/foundation/src/index.ts | 2 ++ packages/foundation/src/store/index.ts | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/foundation/src/index.ts b/packages/foundation/src/index.ts index c80f9f46..3726fabb 100644 --- a/packages/foundation/src/index.ts +++ b/packages/foundation/src/index.ts @@ -104,6 +104,7 @@ export function createFoundationSimulationServer< let simulationStore = createSimulationStore(extendStore); app.use((req, res, next) => { + // add each response to the internal log simulationStore.store.dispatch( simulationStore.actions.simulationLog({ method: req.method, @@ -278,6 +279,7 @@ export function createFoundationSimulationServer< } } + // return simulation helper page app.get("/", (req, res) => { let routes = simulationStore.schema.simulationRoutes.selectTableAsList( simulationStore.store.getState() diff --git a/packages/foundation/src/store/index.ts b/packages/foundation/src/store/index.ts index 283ae2d1..a7a09ca1 100644 --- a/packages/foundation/src/store/index.ts +++ b/packages/foundation/src/store/index.ts @@ -1,7 +1,7 @@ import { generateSchemaWithInputSlices } from "./schema"; import type { ExtendSimulationSchemaInput } from "./schema"; import type { AnyState, StoreUpdater, Callable } from "starfx"; -import { parallel, take, createStore, createSelector } from "starfx"; +import { parallel, take, select, createStore, createSelector } from "starfx"; import { updateStore, createThunks, mdw } from "starfx"; type StoreThunks = ReturnType; @@ -82,6 +82,7 @@ export function createSimulationStore< }>("simulationLog", function* (ctx, next) { const { method, url, query, body } = ctx.payload; const timestamp = Date.now(); + yield* schema.update( schema.simulationLogs.add({ [timestamp]: { @@ -92,6 +93,17 @@ export function createSimulationStore< }, }) ); + + // attempt to increment `route.calls` + const id = `${method.toLowerCase()}:${url}`; + const route = yield* select(schema.simulationRoutes.selectById, { + id, + }); + if (route.url !== "") + yield* schema.update( + schema.simulationRoutes.merge({ [id]: { calls: route.calls + 1 } }) + ); + yield* next(); });