Skip to content
Merged
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
10 changes: 7 additions & 3 deletions lib/arrayBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
0: never;
1: [ InferArrayElement<First<T>> ];
2: [ InferArrayElement<First<T>>, ...GetArrayBlockResult<Tail<T>> ];
}[ T extends [] ? 0 : T extends ((readonly [ any ]) | [ any ]) ? 1 : 2 ];

Check warning on line 27 in lib/arrayBlock.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 27 in lib/arrayBlock.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

export type GetArrayBlockParamsUnion<T extends ReadonlyArray<unknown>> = {
0: never;
1: First<T>;
2: First<T> & GetArrayBlockParamsUnion<Tail<T>>;
}[ T extends [] ? 0 : T extends ((readonly [ any ]) | [ any ]) ? 1 : 2 ];

Check warning on line 33 in lib/arrayBlock.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 33 in lib/arrayBlock.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

type GetArrayBlockParamsMap<T extends ReadonlyArray<unknown>> = {
[ P in keyof T ]: InferParamsInFromBlock<T[ P ]>;
Expand Down Expand Up @@ -79,7 +79,6 @@
> {

extend<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ExtendedResultOut extends
BlockResultOut<ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut>,
ExtendedParamsOut extends Params = Params,
Expand All @@ -88,15 +87,20 @@
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
const ExtendedIsRequired extends boolean = IsRequired,
>({ options }: {
options: DescriptBlockOptions<
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
>;
> & { required?: ExtendedIsRequired };
}) {
return new ArrayBlock({
const result = new ArrayBlock({
block: this.extendBlock(this.block),
options: this.extendOptions(this.options, options) as typeof options,
});
return result as unknown as ArrayBlock<
Context, Block, ExtendedResultOut, ExtendedParamsOut, ExtendedBlockResult,
ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams, ExtendedIsRequired
>;
}

protected initBlock(array: ArrayBlockDefinition<Block>) {
Expand Down
3 changes: 2 additions & 1 deletion lib/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
const ExtendedIsRequired extends boolean = IsRequired,
>({ block, options }: {
block?: CustomBlock;
options?: DescriptBlockOptions<
Expand All @@ -88,7 +89,7 @@
ExtendedAfterResultOut,
ExtendedErrorResultOut,
ExtendedParams
>;
> & { required?: ExtendedIsRequired };
}): unknown;

protected initBlock(block: CustomBlock) {
Expand Down Expand Up @@ -444,7 +445,7 @@
blockCancel.throwIfCancelled();

if (typeof step.after === 'function') {
resultAfter = await step.after({ cancel, params, context, deps, dep, result: (resultBefore || resultBlock) as any });

Check warning on line 448 in lib/block.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
blockCancel.throwIfCancelled();

if (resultAfter instanceof BaseBlock) {
Expand Down
10 changes: 7 additions & 3 deletions lib/firstBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class FirstBlock<
> {

extend<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ExtendedResultOut extends
BlockResultOut<ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut>,
// ExtendedCustomBlock extends FirstBlockDefinition<Block>,
Expand All @@ -95,15 +94,20 @@ class FirstBlock<
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
const ExtendedIsRequired extends boolean = IsRequired,
>({ options }: {
options: DescriptBlockOptions<
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
>;
> & { required?: ExtendedIsRequired };
}) {
return new FirstBlock({
const result = new FirstBlock({
block: this.extendBlock(this.block),
options: this.extendOptions(this.options, options) as typeof options,
});
return result as unknown as FirstBlock<
Context, Block, ExtendedResultOut, ExtendedParamsOut, ExtendedBlockResult,
ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams, ExtendedIsRequired
>;
}

protected initBlock(block: FirstBlockDefinition<Block>) {
Expand Down
6 changes: 4 additions & 2 deletions lib/httpBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,12 @@ class HttpBlock<
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
const ExtendedIsRequired extends boolean = IsRequired,
>({ options, block }: {
block?: DescriptHttpBlockDescription<ParamsOut & ExtendedParamsOut, Context, HttpResult>;
options?: DescriptBlockOptions<
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
>;
> & { required?: ExtendedIsRequired };
}) {
const x = new HttpBlock<
Context,
Expand All @@ -229,7 +230,8 @@ class HttpBlock<
ExtendedBeforeResultOut,
ExtendedAfterResultOut,
ExtendedErrorResultOut,
ExtendedParams
ExtendedParams,
ExtendedIsRequired
>({
block: this.extendBlock(block),
options: this.extendOptions(this.options, options),
Expand Down
10 changes: 7 additions & 3 deletions lib/objectBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ class ObjectBlock<
}

extend<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ExtendedResultOut extends
BlockResultOut<ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut>,
// ExtendedCustomBlock extends ObjectBlockDefinition<Blocks>,
Expand All @@ -120,15 +119,20 @@ class ObjectBlock<
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
const ExtendedIsRequired extends boolean = IsRequired,
>({ options }: {
options: DescriptBlockOptions<
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
>;
> & { required?: ExtendedIsRequired };
}) {
return new ObjectBlock({
const result = new ObjectBlock({
block: this.extendBlock(this.block),
options: this.extendOptions(this.options, options) as typeof options,
});
return result as unknown as ObjectBlock<
Context, Blocks, ExtendedResultOut, ExtendedParamsOut, ExtendedBlockResult,
ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams, ExtendedIsRequired
>;
}

}
Expand Down
14 changes: 9 additions & 5 deletions lib/pipeBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,29 @@ class PipeBlock<
> {

extend<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// ExtendedResultOut extends
// BlockResultOut<ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut>,
ExtendedResultOut extends
BlockResultOut<ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut>,
// ExtendedCustomBlock extends PipeBlockDefinition<Block>,
ExtendedParamsOut extends Params = Params,
ExtendedParams = Params,
ExtendedBlockResult = ResultOut,
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
const ExtendedIsRequired extends boolean = IsRequired,
>({ options }: {
options: DescriptBlockOptions<
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
>;
> & { required?: ExtendedIsRequired };
}) {
return new PipeBlock({
const result = new PipeBlock({
block: this.extendBlock(this.block),
options: this.extendOptions(this.options, options) as typeof options,
});
return result as unknown as PipeBlock<
Context, Block, ExtendedResultOut, ExtendedParamsOut, ExtendedBlockResult,
ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams, ExtendedIsRequired
>;
}

protected initBlock(block: PipeBlockDefinition<Block>) {
Expand Down
166 changes: 166 additions & 0 deletions tests/options.required.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,170 @@ describe('options.required', () => {
Expect<TypesMatch<typeof result, { foo: number | DescriptError }>>,
];
});

it('types: func .extend({ required: true }) removes DescriptError from object block result', async() => {
const base = de.func({
block: () => 42,
});
const block = de.object({
block: {
foo: base.extend({
options: {
required: true,
},
}),
},
});

const result = await de.run(block, { params: {} });

expect(result.foo).toBe(42);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Tests = [
Expect<TypesMatch<typeof result, { foo: number }>>,
];
});

it('types: http .extend({ required: true }) removes DescriptError from object block result', async() => {
const path = getPath();
fake.add(path, {
statusCode: 200,
headers: { 'content-type': 'application/json' },
content: JSON.stringify({ foo: 'bar' }),
});

const base = de.http({
block: {
hostname: 'localhost',
port: PORT,
pathname: path,
},
options: {
after: ({ result }) => result.result as { foo: string },
},
});
const block = de.object({
block: {
data: base.extend({
options: {
required: true,
},
}),
},
});

const result = await de.run(block);

expect(result.data).toEqual({ foo: 'bar' });

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Tests = [
Expect<TypesMatch<typeof result, { data: { foo: string } }>>,
];
});

it('types: http .extend({ required: false }) keeps DescriptError in object block result', async() => {
const path = getPath();
fake.add(path, {
statusCode: 200,
headers: { 'content-type': 'application/json' },
content: JSON.stringify({ foo: 'bar' }),
});

const base = de.http({
block: {
hostname: 'localhost',
port: PORT,
pathname: path,
},
options: {
after: ({ result }) => result.result as { foo: string },
},
});
const block = de.object({
block: {
data: base.extend({
options: {
required: false,
},
}),
},
});

const result = await de.run(block);

expect(result.data).toEqual({ foo: 'bar' });

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Tests = [
Expect<TypesMatch<typeof result, { data: { foo: string } | DescriptError }>>,
];
});

it('types: http .extend() inherits required=true from parent', async() => {
const path = getPath();
fake.add(path, {
statusCode: 200,
headers: { 'content-type': 'application/json' },
content: JSON.stringify({ foo: 'bar' }),
});

const base = de.http({
block: {
hostname: 'localhost',
port: PORT,
pathname: path,
},
options: {
after: ({ result }) => result.result as { foo: string },
required: true,
},
});
const block = de.object({
block: {
data: base.extend({}),
},
});

const result = await de.run(block);

expect(result.data).toEqual({ foo: 'bar' });

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Tests = [
Expect<TypesMatch<typeof result, { data: { foo: string } }>>,
];
});

it('types: object .extend({ required: true }) removes DescriptError from parent object block result', async() => {
const inner = de.object({
block: {
foo: de.func({
block: () => 42,
options: {
required: true,
},
}),
},
});
const block = de.object({
block: {
data: inner.extend({
options: {
required: true,
},
}),
},
});

const result = await de.run(block, { params: {} });

expect(result.data).toEqual({ foo: 42 });

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Tests = [
Expect<TypesMatch<typeof result, { data: { foo: number } }>>,
];
});
});
Loading