From 87c30f669410df49314938348006b4cae5656239 Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:16:51 -0300 Subject: [PATCH] feat: improve typing --- packages/data-flow/src/data-loader/dataLoader.ts | 1 + .../src/data-loader/handlers/application.handlers.ts | 5 ++--- .../src/data-loader/handlers/project.handlers.ts | 10 ++-------- .../src/data-loader/handlers/round.handlers.ts | 12 ++---------- packages/repository/src/external.ts | 7 ++++++- packages/repository/src/types/changeset.types.ts | 6 +++--- 6 files changed, 16 insertions(+), 25 deletions(-) diff --git a/packages/data-flow/src/data-loader/dataLoader.ts b/packages/data-flow/src/data-loader/dataLoader.ts index 5aa6cfc..9c1098b 100644 --- a/packages/data-flow/src/data-loader/dataLoader.ts +++ b/packages/data-flow/src/data-loader/dataLoader.ts @@ -62,6 +62,7 @@ export class DataLoader implements IDataLoader { for (const changeset of changesets) { result.numExecuted++; try { + //TODO: inside each handler, we should add zod validation that the args match the expected type await this.handlers[changeset.type](changeset as never); result.changesets.push(changeset.type); result.numSuccessful++; diff --git a/packages/data-flow/src/data-loader/handlers/application.handlers.ts b/packages/data-flow/src/data-loader/handlers/application.handlers.ts index 5e1cbb6..20afd8a 100644 --- a/packages/data-flow/src/data-loader/handlers/application.handlers.ts +++ b/packages/data-flow/src/data-loader/handlers/application.handlers.ts @@ -1,4 +1,4 @@ -import { IApplicationRepository } from "@grants-stack-indexer/repository"; +import { ApplicationChangeset, IApplicationRepository } from "@grants-stack-indexer/repository"; import { ChangesetHandler } from "../types/index.js"; @@ -7,8 +7,7 @@ import { ChangesetHandler } from "../types/index.js"; * Each handler corresponds to a specific Application changeset type. */ export type ApplicationHandlers = { - InsertApplication: ChangesetHandler<"InsertApplication">; - UpdateApplication: ChangesetHandler<"UpdateApplication">; + [K in ApplicationChangeset["type"]]: ChangesetHandler; }; /** diff --git a/packages/data-flow/src/data-loader/handlers/project.handlers.ts b/packages/data-flow/src/data-loader/handlers/project.handlers.ts index 2acbaa2..b32a9db 100644 --- a/packages/data-flow/src/data-loader/handlers/project.handlers.ts +++ b/packages/data-flow/src/data-loader/handlers/project.handlers.ts @@ -1,4 +1,4 @@ -import { IProjectRepository } from "@grants-stack-indexer/repository"; +import { IProjectRepository, ProjectChangeset } from "@grants-stack-indexer/repository"; import { ChangesetHandler } from "../types/index.js"; @@ -7,13 +7,7 @@ import { ChangesetHandler } from "../types/index.js"; * Each handler corresponds to a specific Project changeset type. */ export type ProjectHandlers = { - InsertProject: ChangesetHandler<"InsertProject">; - UpdateProject: ChangesetHandler<"UpdateProject">; - InsertPendingProjectRole: ChangesetHandler<"InsertPendingProjectRole">; - DeletePendingProjectRoles: ChangesetHandler<"DeletePendingProjectRoles">; - InsertProjectRole: ChangesetHandler<"InsertProjectRole">; - DeleteAllProjectRolesByRole: ChangesetHandler<"DeleteAllProjectRolesByRole">; - DeleteAllProjectRolesByRoleAndAddress: ChangesetHandler<"DeleteAllProjectRolesByRoleAndAddress">; + [K in ProjectChangeset["type"]]: ChangesetHandler; }; /** diff --git a/packages/data-flow/src/data-loader/handlers/round.handlers.ts b/packages/data-flow/src/data-loader/handlers/round.handlers.ts index c9b0fde..cb6c831 100644 --- a/packages/data-flow/src/data-loader/handlers/round.handlers.ts +++ b/packages/data-flow/src/data-loader/handlers/round.handlers.ts @@ -1,4 +1,4 @@ -import { IRoundRepository } from "@grants-stack-indexer/repository"; +import { IRoundRepository, RoundChangeset } from "@grants-stack-indexer/repository"; import { ChangesetHandler } from "../types/index.js"; @@ -7,15 +7,7 @@ import { ChangesetHandler } from "../types/index.js"; * Each handler corresponds to a specific Round changeset type. */ export type RoundHandlers = { - InsertRound: ChangesetHandler<"InsertRound">; - UpdateRound: ChangesetHandler<"UpdateRound">; - UpdateRoundByStrategyAddress: ChangesetHandler<"UpdateRoundByStrategyAddress">; - IncrementRoundFundedAmount: ChangesetHandler<"IncrementRoundFundedAmount">; - IncrementRoundTotalDistributed: ChangesetHandler<"IncrementRoundTotalDistributed">; - InsertPendingRoundRole: ChangesetHandler<"InsertPendingRoundRole">; - DeletePendingRoundRoles: ChangesetHandler<"DeletePendingRoundRoles">; - InsertRoundRole: ChangesetHandler<"InsertRoundRole">; - DeleteAllRoundRolesByRoleAndAddress: ChangesetHandler<"DeleteAllRoundRolesByRoleAndAddress">; + [K in RoundChangeset["type"]]: ChangesetHandler; }; /** diff --git a/packages/repository/src/external.ts b/packages/repository/src/external.ts index 8dbf22a..c3cc4d8 100644 --- a/packages/repository/src/external.ts +++ b/packages/repository/src/external.ts @@ -35,7 +35,12 @@ export type { PartialApplication, } from "./types/application.types.js"; -export type { Changeset } from "./types/index.js"; +export type { + Changeset, + ProjectChangeset, + RoundChangeset, + ApplicationChangeset, +} from "./types/index.js"; export { KyselyRoundRepository, diff --git a/packages/repository/src/types/changeset.types.ts b/packages/repository/src/types/changeset.types.ts index 547f7d3..648d111 100644 --- a/packages/repository/src/types/changeset.types.ts +++ b/packages/repository/src/types/changeset.types.ts @@ -18,7 +18,7 @@ import { //TODO: see if in the future we move out of inline object types for changesets -type ProjectChangeset = +export type ProjectChangeset = | { type: "InsertProject"; args: { @@ -64,7 +64,7 @@ type ProjectChangeset = }; }; -type RoundChangeset = +export type RoundChangeset = | { type: "InsertRound"; args: { @@ -129,7 +129,7 @@ type RoundChangeset = }; }; -type ApplicationChangeset = +export type ApplicationChangeset = | { type: "InsertApplication"; args: NewApplication;