Skip to content

Commit

Permalink
fix for blocks chaining types (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
madimp authored Mar 28, 2024
1 parent fdfa02c commit 2ef9582
Show file tree
Hide file tree
Showing 9 changed files with 632 additions and 69 deletions.
3 changes: 1 addition & 2 deletions docs/typescript-examples/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const block1 = de.http( {
return de.request.DEFAULT_OPTIONS.is_error(error, request_options);
},
is_retry_allowed: (error, request_options) => {
// POST-запросы по умолчанию не ретраются
const method = request_options.http_options.method;
if ( method === 'POST' ) {
const id = error.error.id;
Expand Down Expand Up @@ -130,5 +129,5 @@ de.run( block2, {
}
} )
.then( ( result ) => {
console.log( result );
console.log( result.c );
} );
104 changes: 97 additions & 7 deletions docs/typescript-examples/object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {DescriptBlockParams} from '../../lib';
import {
inferBlockTypes
} from '../../lib';
import * as de from '../../lib';

// --------------------------------------------------------------------------------------------------------------- //
Expand Down Expand Up @@ -30,6 +32,20 @@ const block_1 = de.http( {
},
} );


de.run( block_1, {
params: {
id_1: '67890',
},
} )
.then( ( result ) => {
console.log( result );
return {
foo: result.a,
bar: undefined,
};
} );

// --------------------------------------------------------------------------------------------------------------- //

interface ParamsIn2 {
Expand All @@ -55,18 +71,64 @@ const block_2 = de.http( {
},
} );

const block_2_func = de.func({
block: (x) => block_2,
options: {
after: ({result}) => {
console.log(result.b);
return {
b: result.b + '2',
c: 1
};
}
}
});

de.run( block_2_func, {
params: {
id_2: 67890,
},
} )
.then( ( result ) => {
console.log( result );
return {
foo: result.c,
bar: result.b,
};
} );


// --------------------------------------------------------------------------------------------------------------- //

const block_3 = de.object( {
block: {
foo: block_1,
bar: block_2,
foo: inferBlockTypes(de.http( {
block: {},
options: {
params: ( { params }: { params: ParamsIn1, context: Context } ) => {
return {
s1: params.id_1,
};
},

after: ( { params, context } ) => {
return {
a: params.s1,
};
},
},
} )),
bar: block_2_func,
},
options: {
after: ( { result } ) => {
params: ({params}) => params,
after: ( { result, params } ) => {
return {
foo: result.foo.a,
bar: result.bar.b,
foo: {
...result.foo,
c: 1,
},
bar: result.bar,
};
},
},
Expand All @@ -77,6 +139,34 @@ de.run( block_3, {
id_1: '12345',
id_2: 67890,
},
} )
.then( ( result ) => {
console.log( result.foo.a, result.bar.b );
return {
foo: result.foo.a,
bar: result.bar.b,
};
} );

const block_3_func = de.func( {
block: () => {
return block_3;
},
options: {
after: ({ result }) => {
return {
foo: result.foo.a + '1',
bar: result.bar.b + '1',
}
}
}
});

de.run( block_3_func, {
params: {
id_1: '12345',
id_2: 67890,
},
} )
.then( ( result ) => {
console.log( result.foo, result.bar );
Expand All @@ -85,7 +175,7 @@ de.run( block_3, {
const block_4 = block_3( {
options: {
after: ( { result } ) => {
return result.foo + result.bar;
return result.foo.a + result.bar.b;
},
},
} );
Expand Down
14 changes: 8 additions & 6 deletions docs/typescript-examples/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ const block1 = de.http( {
// Если мы здесь хотим использовать params, то нам приходится прописать тип явно.
// Typescript не позволяет частично задавать тип при destructure.
//
after: ( { params, result }: { params: ParamsOut, result: ResultRaw }) => {
after: ( { params, result }) => {
// Тип для обработанного результата нам в принципе не нужен.
// Он выведется из того, что мы вернули.
//
return result.a;
return result;
},
},
} );
Expand Down Expand Up @@ -105,8 +105,8 @@ const block2 = de.http( {
}
},

after: ( { result }: { result: ResultRaw } ) => {
return result.a;
after: ( { result } ) => {
return result;
},
},
} );
Expand All @@ -129,7 +129,7 @@ de.run( block2, {
const block3 = de.http( {
block: {},
options: {
before: ( { params } ) => {
before: ( { params }: { params: ParamsIn } ) => {
if ( !params.id ) {
return 'foo';
}
Expand All @@ -138,7 +138,7 @@ const block3 = de.http( {
// Где-то нужно объявить тип входящих params.
// Например, в after. Или же в before. В зависимости от того, что есть.
//
after: ( { params, result }: { params: ParamsIn, result?: string } ) => {
after: ( { params, result } ) => {
return result;
},
},
Expand All @@ -151,4 +151,6 @@ de.run( block3, {
} )
.then( ( result ) => {
console.log( result );

return result;
} );
Loading

0 comments on commit 2ef9582

Please sign in to comment.