Skip to content

Commit cdbaf64

Browse files
refactor: reverse execution order of interceptors in the after interceptor (#244)
# Description This PR reverses the execution order of interceptors in the interceptAfter method. It ensures symmetry with interceptBefore, meaning the last interceptor called during the 'before' phase is the first one called during the 'after' phase. --------- Co-authored-by: Ivan Shymko <vana.shimko@gmail.com>
1 parent 9706438 commit cdbaf64

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/client/multitransport-client.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,8 @@ export class Client {
382382
args: AfterArgs<K>,
383383
interceptors?: CallInterceptor[]
384384
): Promise<void> {
385-
if (!this.config?.interceptors || this.config.interceptors.length === 0) {
386-
return;
387-
}
388-
for (const interceptor of interceptors ?? this.config.interceptors) {
385+
const reversedInterceptors = [...(interceptors ?? this.config?.interceptors ?? [])].reverse();
386+
for (const interceptor of reversedInterceptors) {
389387
await interceptor.after(args);
390388
if (args.earlyReturn) {
391389
return;

test/client/multitransport-client.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,15 +569,15 @@ describe('Client', () => {
569569
{
570570
before: async () => {},
571571
after: async (args) => {
572-
args.earlyReturn = true;
572+
if (args.result.method === 'getTask') {
573+
args.result.value = { ...args.result.value, metadata: { foo: 'bar' } };
574+
}
573575
},
574576
},
575577
{
576578
before: async () => {},
577579
after: async (args) => {
578-
if (args.result.method === 'getTask') {
579-
args.result.value = { ...args.result.value, metadata: { foo: 'bar' } };
580-
}
580+
args.earlyReturn = true;
581581
},
582582
},
583583
],

0 commit comments

Comments
 (0)