Skip to content

Commit

Permalink
fix for blocks chaining types
Browse files Browse the repository at this point in the history
  • Loading branch information
madimp committed May 20, 2024
1 parent aae7a2d commit 740fbc6
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 64 deletions.
29 changes: 24 additions & 5 deletions docs/typescript-examples/func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ const block_1 = de.func( {
};
},
options: {
error: () => {
return {
x: 1
}
},
params: ( { params }: { params: ParamsIn1 } ) => {
return {
s1: params.id,
};
},

// Важно!!!
// Необходимо задать тип входящего result'а именно здесь.
// Почему-то этот тип не берется из результата функции из block.
//
after: ( { params, result }) => {
console.log( params );
return {
Expand All @@ -64,7 +65,7 @@ const block_2 = de.func({
return Promise.resolve(result);
},
options: {
after: ({ result }): string => {
after: ({ result }) => {
return result.foo;
},
},
Expand All @@ -74,3 +75,21 @@ de.run( block_2, {} )
.then( ( result ) => {
console.log( result );
});

const block3 = block_2({
options: {
after: ({ result }) => {
console.log(result);

return {
res: result,
}
},
}
})


de.run( block3, {} )
.then( ( result ) => {
console.log( result );
});
10 changes: 10 additions & 0 deletions docs/typescript-examples/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ const block1 = de.http( {
},

options: ({
before: () => {
return {
d: 1
}
},
error: () => {
return {
x: 's'
}
},
params: ( { params }: { params: ParamsIn1 }) => {
return {
s1: params.id_1,
Expand Down
58 changes: 53 additions & 5 deletions docs/typescript-examples/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,49 @@ interface Context {
}

// --------------------------------------------------------------------------------------------------------------- //
export interface CreateCardRequest {
added_manually?: boolean;
added_by_identifier?: string;
// card: Card;
card: Record<string, string>;
}

interface ParamsIn1 {
id_1: string;
payload: CreateCardRequest;
}
interface ParamsOut1 {
s1: string;
}
interface Result1 {
r: number;
}

const block_1 = de.http( {
block: {},
block: {
body: ({ params }) => params.payload,
},
options: {
params: ( { params }: { params: ParamsIn1, context: Context } ) => {
return {
s1: params.id_1,
payload: params.payload
};
},

after: ( { params, context } ) => {
return {
a: params.s1,
after: ( { params, context, result }: { params: ParamsOut1, context: Context, result: Result1 } ) => {
const a = {
a: 1,
};
const b = {
b: params.s1,
}

if (params.s1 === 'lol') {
return a;
}

return b;
},
},
} );
Expand All @@ -36,12 +61,15 @@ const block_1 = de.http( {
de.run( block_1, {
params: {
id_1: '67890',
payload: {
card: {}
}
},
} )
.then( ( result ) => {
console.log( result );
return {
foo: result.a,
foo: 'a' in result ? result.a : result.b,
bar: undefined,
};
} );
Expand Down Expand Up @@ -138,6 +166,9 @@ de.run( block_3, {
params: {
id_1: '12345',
id_2: 67890,
payload: {
card: {}
}
},
} )
.then( ( result ) => {
Expand Down Expand Up @@ -166,6 +197,9 @@ de.run( block_3_func, {
params: {
id_1: '12345',
id_2: 67890,
payload: {
card: {}
}
},
} )
.then( ( result ) => {
Expand All @@ -180,10 +214,24 @@ const block_4 = block_3( {
},
} );

const block_5 = block_3( {
options: {
error: ({ cancel, error }) => {
if (error.error) {
throw error;
}
},
after: ( { result } ) => result,
},
} );

de.run( block_4, {
params: {
id_1: '12345',
id_2: 67890,
payload: {
card: {}
}
},
} )
.then( ( result ) => {
Expand Down
1 change: 1 addition & 0 deletions docs/typescript-examples/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const sessionMethod = de.func({
},

after: ({context, params, result}) => {
const x = result;
const session = result.session as Session & Record<string, unknown>;

return session;
Expand Down
Loading

0 comments on commit 740fbc6

Please sign in to comment.