Skip to content

Commit

Permalink
fix: Dataflow fetch inputs compatible type
Browse files Browse the repository at this point in the history
  • Loading branch information
Ni55aN committed Dec 27, 2024
1 parent 16eb88d commit 3d76c70
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/dataflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export type DataflowNodeSetup<
data(fetchInputs: () => Promise<Partial<{ [key in keyof I]: I[key][] }>>): Promise<O> | O
}

type DefaultInputs = null
type Inputs = Partial<Record<string, any[]>> | DefaultInputs
type FetchInputs<T> = T extends DefaultInputs ? Record<string, any> : Partial<T>

/**
* Dataflow is a class that allows to process nodes in a graph using Dataflow approach.
* @priority 8
Expand Down Expand Up @@ -55,7 +59,7 @@ export class Dataflow<Schemes extends ClassicScheme> {
* @param nodeId Node id
* @returns Object with inputs
*/
public async fetchInputs<T extends Partial<Record<string, any[]>>>(nodeId: NodeId): Promise<Partial<T>> {
public async fetchInputs<T extends Inputs = DefaultInputs>(nodeId: NodeId): Promise<FetchInputs<T>> {
const result = this.setups.get(nodeId)

if (!result) throw new Error('node is not initialized')
Expand All @@ -66,7 +70,7 @@ export class Dataflow<Schemes extends ClassicScheme> {
return c.target === nodeId && inputKeys.includes(c.targetInput)
})

const inputs = {} as T
const inputs = {} as FetchInputs<T>
const consWithSourceData = await Promise.all(cons.map(async c => {
return {
c,
Expand Down
2 changes: 1 addition & 1 deletion test/types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Dataflow types', () => {
it('returns default Record type for node inputs', () => {
const node = new ClassicPreset.Node('label')

expectType<Promise<Partial<Record<string, any[]>>>>(dataflow.fetchInputs(node.id))
expectType<Promise<Record<string, any>>>(dataflow.fetchInputs(node.id))
})

it('accepts fetch data type as generic parameter', () => {
Expand Down

0 comments on commit 3d76c70

Please sign in to comment.