Skip to content

Commit

Permalink
v4: typescript first
Browse files Browse the repository at this point in the history
  • Loading branch information
madimp committed Nov 7, 2024
1 parent 99b0873 commit 040a5f2
Show file tree
Hide file tree
Showing 18 changed files with 317 additions and 997 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.vscode
node_modules
*.swp
tests/server.key
Expand Down
19 changes: 16 additions & 3 deletions docs/typescript-examples/func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@ de.run(block2, {

const block3 = block2.extend({
options: {
after: ({ result }) => {
params: ({ params }: { params: { param2: number } }) => {
return {
param: params.param2,
};
},
after: ({ result, params }) => {
console.log(result);

return {
res: result,
x: params.param,
};
},
},
Expand All @@ -79,21 +85,28 @@ const block3 = block2.extend({

de.run(block3, {
params: {
param: 2,
param2: 2,
},
})
.then((result) => {
console.log(result);
});


const objBlock = de.object({
block: {
foo: block1,
boo: block2,
},
});

const block4 = de.func({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
block: ({ params }) => {
//TODO не выводится params.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
//const x = params.id;
return block1;
return objBlock;
},
options: {
after: ({ result, params }) => {
Expand Down
30 changes: 30 additions & 0 deletions docs/typescript-examples/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,33 @@ de.run(block2, {
.then((result) => {
console.log(result.c);
});

const block3 = de.http({
block: {},
options: {},
});

const block4 = block3.extend({
block: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
headers: ({ params }) => {
return {
'x-header': String(params.param2),
};
},
},
options: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
params: ({ params }: { params: { param: number } }) => {
return {
param2: params.param,
};
},
},
});

de.run(block4, {
params: {
param: 1,
},
});
129 changes: 129 additions & 0 deletions docs/typescript-examples/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ de.run(block2, {
const block3 = de.http({
block: {},
options: {
//TODO как вывести In из Out?
//params: ({ params }: { params: ParamsIn }) => params,
before: ({ params }: { params: ParamsIn }) => {
if (!params.id) {
return 'foo';
Expand All @@ -143,3 +145,130 @@ de.run(block3, {

return result;
});

const block4 = de.http({
block: {
agent: { maxSockets: 16 },
maxRetries: 1,
timeout: 1000,
body: ({ params }: { params: { p4: number } }) => params,
},
options: {
after: ({ result }: { result: de.DescriptHttpBlockResult<{ r: number }> }) => result.result,
},
});

de.run(block4, {
params: {
p4: 1,
},
});

const block5 = block4.extend({
block: {
body: ({ params }) => params,
},

options: {
after: ({ params }) => params,
},
});

const block6 = de.object({
block: {
block4: block5.extend({
options: {
params: ({ params }) => {
return {
p4: params.p4,
};
},
//after: ({ result }) => result,
},
}),
block3: block3.extend({}),
},

options: {
after: ({ result }) => {
return result.block4;
},
},
});

de.run(block6, {
params: {
p4: 8,
id: '1',
},
});

const block7 = de.object({
block: {
block6: block6.extend({
options: {
params: ({ params }) => {
return {
p4: params.p4,
id: '1',
x: 1,
};
},
//after: ({ result }) => result,
},
}),
block3: block3.extend({}),
},
options: {
after: ({ result }) => {
return result.block6;
},
},
});


de.run(block7, {
params: {
p4: 8,
id: '1',
},
});

const block8 = de.http({
block: {
agent: { maxSockets: 16 },
maxRetries: 1,
timeout: 1000,
body: ({ params }: { params: { p1: number } }) => params,
},
options: {
after: ({ result }: { result: de.DescriptHttpBlockResult<{ r: number }> }) => result.result,
},
});

const block9 = block8.extend({
block: {
body: ({ params }: { params: { p4: number; p1: number } }) => params,
},

options: {
after: ({ params }) => params,
},
});


de.run(block9, {
params: {
p1: 8,
},
});

const block10 = de.func({
block: () => {
return block8;
},
});

de.run(block10, {
params: {},
});
27 changes: 6 additions & 21 deletions lib/arrayBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,21 @@ class ArrayBlock<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ExtendedResultOut extends
BlockResultOut<ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut>,
ExtendedParamsOut = Params,
ExtendedParamsOut extends Params = Params,
ExtendedParams = Params,
ExtendedBlockResult = ResultOut,
ExtendedBeforeResultOut = undefined,
ExtendedAfterResultOut = undefined,
ExtendedErrorResultOut = undefined,
>({ options }: {
options: DescriptBlockOptions<
Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
>;
}) {
return this.extendClass<
ArrayBlock<
Context,
Block,
ExtendedResultOut,
Params & ExtendedParamsOut,
ExtendedBlockResult,
ExtendedBeforeResultOut,
ExtendedAfterResultOut,
ExtendedErrorResultOut,
ExtendedParams
>,
ExtendedBlockResult,
ExtendedParamsOut,
ExtendedParams,
ExtendedBeforeResultOut,
ExtendedAfterResultOut,
ExtendedErrorResultOut
>({ options });
return new ArrayBlock({
block: this.extendBlock(this.block),
options: this.extendOptions(this.options, options) as typeof options,
});
}

protected initBlock(array: ArrayBlockDefinition<Block>) {
Expand Down
44 changes: 23 additions & 21 deletions lib/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract class BaseBlock<
BeforeResultOut = undefined,
AfterResultOut = undefined,
ErrorResultOut = undefined,
Params = ParamsOut,
Params = ParamsOut
> {
protected block: CustomBlock;
protected options: BlockOptions<Context, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>;
Expand All @@ -84,49 +84,51 @@ abstract class BaseBlock<
protected extendClass<
ClassType,
ExtendedBlockResult,
ExtendedParamsOut = Params,
ExtendedParamsOut extends Params = Params,
ExtendedParams = Params,
ExtendedBeforeResultOut = undefined,
ExtendedAfterResultOut = undefined,
ExtendedErrorResultOut = undefined,
ExtendedBeforeResultOut = void,
ExtendedAfterResultOut = void,
ExtendedErrorResultOut = void,
ExtendedCustomBlock extends CustomBlock = CustomBlock,
>({ block, options }: {
block?: CustomBlock;
block?: ExtendedCustomBlock;
options?:
DescriptBlockOptions<
Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
>;
}): ClassType {

return new (<BlockConstructor<
Context,
Params & ExtendedParamsOut,
ExtendedParamsOut,
ExtendedBlockResult,
ExtendedBeforeResultOut,
ExtendedAfterResultOut,
ExtendedErrorResultOut,
ExtendedParams,
ClassType,
CustomBlock
ExtendedCustomBlock
>> this.constructor)({
block: this.extendBlock(block),
block: this.extendBlock(block) as ExtendedCustomBlock,
options: this.extendOptions(this.options, options),
});
}

abstract extend<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ExtendedResultOut extends
BlockResultOut<ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut>,
ExtendedParamsOut = Params,
//ExtendedResultOut extends
//BlockResultOut<ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut>,
ExtendedParamsOut extends Params = Params,
ExtendedParams = Params,
//ExtendedCustomBlock = CustomBlock,
ExtendedBlockResult = ResultOut,
ExtendedBeforeResultOut = undefined,
ExtendedAfterResultOut = undefined,
ExtendedErrorResultOut = undefined,
ExtendedBeforeResultOut = void,
ExtendedAfterResultOut = void,
ExtendedErrorResultOut = void,
>({ block, options }: {
block?: CustomBlock;
options?: DescriptBlockOptions<
Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
Context, ParamsOut & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
>;
}): unknown

Expand All @@ -144,7 +146,7 @@ abstract class BaseBlock<
}

//eslint-disable-next-line @typescript-eslint/no-unused-vars
protected extendBlock(block?: CustomBlock) {
protected extendBlock<ExtendedCustomBlock extends CustomBlock>(block?: ExtendedCustomBlock) {
return this.block;
}

Expand Down Expand Up @@ -201,9 +203,9 @@ abstract class BaseBlock<
private extendLifecycle<
ExtendedParamsOut,
ExtendedBlockResult,
ExtendedBeforeResultOut = undefined,
ExtendedAfterResultOut = undefined,
ExtendedErrorResultOut = undefined,
ExtendedBeforeResultOut = void,
ExtendedAfterResultOut = void,
ExtendedErrorResultOut = void,
ExtendedParams = ExtendedParamsOut,
// eslint-disable-next-line max-len
W extends BlockOptions<Context, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params> =
Expand Down
2 changes: 1 addition & 1 deletion lib/compositeBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class CompositeBlock<
BeforeResultOut = undefined,
AfterResultOut = undefined,
ErrorResultOut = undefined,
Params = ParamsOut,
Params = ParamsOut
> extends BaseBlock<
Context,
CustomBlock,
Expand Down
4 changes: 3 additions & 1 deletion lib/depsDomain.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//TODO как это типизировать any этот?
export type DescriptBlockDeps = Record<DescriptBlockId, any>;
export type DescriptBlockId = symbol;
export type GenerateId = (label?: string) => DescriptBlockId;

class DepsDomain {
ids: Record<DescriptBlockId, boolean>;
constructor(parent: any) {
this.ids = (parent instanceof DepsDomain) ? Object.create(parent.ids) : {};

}

generateId = (label?: string): DescriptBlockId => {
generateId: GenerateId = (label?: string): DescriptBlockId => {
const id = Symbol(label);
this.ids[ id ] = true;
return id;
Expand Down
Loading

0 comments on commit 040a5f2

Please sign in to comment.