Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions docs/typescript-examples/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const block2 = de.http({

const block2Func = de.func({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
block: ({ params }: { params: InferParamsInFromBlock<typeof block1> & { p1: number } }) => block2,
block: ({ params }: { params: InferParamsInFromBlock<typeof block2> & { p1: number } }) => block2,
// block: () => block2,
options: {
after: ({ result }) => {
Expand All @@ -111,11 +111,8 @@ const block2Func = de.func({

de.run(block2Func, {
params: {
id1: '67890',
p1: 1,
payload: {
card: {},
},
id2: 578923,
},
})
.then((result) => {
Expand Down Expand Up @@ -166,12 +163,8 @@ const block3 = de.object({

de.run(block3, {
params: {
id1: '12345',
id2: 67890,
p1: 1,
payload: {
card: {},
},
},
})
.then((result) => {
Expand Down Expand Up @@ -230,12 +223,8 @@ const block5 = block3.extend({

de.run(block4, {
params: {
id1: '12345',
id2: 67890,
p1: 1,
payload: {
card: {},
},
},
})
.then((result) => {
Expand All @@ -244,12 +233,8 @@ de.run(block4, {

de.run(block5, {
params: {
id1: '12345',
id2: 67890,
p1: 1,
payload: {
card: {},
},
},
})
.then((result) => {
Expand Down
15 changes: 13 additions & 2 deletions lib/functionBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ import BaseBlock from './block';
import type { DepAccessor, DescriptBlockDeps } from './depsDomain';
import DepsDomain, { createDepAccessor } from './depsDomain';
import { createError, ERROR_ID } from './error';
import type { BlockResultOut, DescriptBlockOptions, InferParamsOutFromBlock } from './types';
import type { BlockResultOut, DescriptBlockOptions, DescriptParamsError, InferParamsOutFromBlock } from './types';
import type ContextClass from './context';
import type Cancel from './cancel';

type NestedBlockParamsConstraint<T, Params> =
[ unknown ] extends [ Params ]
? T
: T extends BaseBlock<any, any, any, any, any, any, any, any, any, infer BParams>
? unknown extends BParams
? T
: [ Params ] extends [ BParams ]
? T
: DescriptParamsError<BParams, Params>
: T;

export type FunctionBlockDefinition<
Context,
Params,
Expand All @@ -18,7 +29,7 @@ export type FunctionBlockDefinition<
generateId: DepsDomain['generateId'];
cancel: Cancel;
blockCancel: Cancel;
}) => Promise<BlockResult> | BlockResult;
}) => Promise<NestedBlockParamsConstraint<BlockResult, Params>> | NestedBlockParamsConstraint<BlockResult, Params>;

class FunctionBlock<
Context,
Expand Down
8 changes: 4 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const array = function<
Params = GetArrayBlockParams<Block>,
>({ block, options }: {
block: ArrayBlockDefinition<Block>;
options?: DescriptBlockOptions<Context, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>;
options?: DescriptBlockOptions<Context, NoInfer<ParamsOut>, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>;
}) {
return new ArrayBlock<Context, Block, ResultOut, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>({ block, options });
};
Expand All @@ -85,7 +85,7 @@ const object = function<
Params = GetObjectBlockParams<Blocks>,
>({ block, options }: {
block?: ObjectBlockDefinition<Blocks>;
options?: DescriptBlockOptions<Context, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>;
options?: DescriptBlockOptions<Context, NoInfer<ParamsOut>, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>;
} = {}) {
return new ObjectBlock<Context, Blocks, ResultOut, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>({ block, options });
};
Expand Down Expand Up @@ -121,7 +121,7 @@ const first = function<
Params = GetFirstBlockParams<Block>,
>({ block, options }: {
block: FirstBlockDefinition<Block>;
options?: DescriptBlockOptions<Context, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>;
options?: DescriptBlockOptions<Context, NoInfer<ParamsOut>, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>;
}) {
return new FirstBlock<Context, Block, ResultOut, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>({ block, options });
};
Expand All @@ -138,7 +138,7 @@ const pipe = function<
Params = GetPipeBlockParams<Block>,
>({ block, options }: {
block: PipeBlockDefinition<Block>;
options?: DescriptBlockOptions<Context, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>;
options?: DescriptBlockOptions<Context, NoInfer<ParamsOut>, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>;
}) {
return new PipeBlock<Context, Block, ResultOut, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>({ block, options });
};
Expand Down
7 changes: 7 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export type NonNullableObject<T extends Record<string, unknown>> = {
[P in keyof T]: Exclude<T[P], undefined>;
};

export type DescriptParamsError<Required, Available> = {
readonly __descriptError: 'NESTED_BLOCK_PARAMS_INCOMPATIBLE';
readonly __requiredParams: Required;
readonly __availableParams: Available;
readonly __fix: 'Add options.params to transform parent params into the required shape';
};

export type DescriptJSON =
boolean |
number |
Expand Down
Loading