Skip to content

Commit

Permalink
fix middleware getting duplicated at api level (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
HomelessDinosaur authored Mar 7, 2023
2 parents f5fa80b + 7a34a50 commit 477d572
Showing 1 changed file with 7 additions and 30 deletions.
37 changes: 7 additions & 30 deletions src/resources/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export class Api<SecurityDefs extends string> extends Base<ApiDetails> {

const r = new Route(this, apiRoute, {
...options,
// join the api level middleware and route level (route options) middleware
middleware: [...this.middleware, ...routeMiddleware],
});
this.routes.push(r);
Expand All @@ -316,11 +317,7 @@ export class Api<SecurityDefs extends string> extends Base<ApiDetails> {
middleware: HttpMiddleware | HttpMiddleware[],
opts: MethodOptions<SecurityDefs> = {}
): Promise<void> {
const r = this.route(match, {
middleware: this.middleware ?? [],
});
const routeMiddleware = composeMiddleware(middleware);
return r.get(routeMiddleware, opts);
return this.route(match).get(composeMiddleware(middleware), opts);
}

/**
Expand All @@ -334,11 +331,7 @@ export class Api<SecurityDefs extends string> extends Base<ApiDetails> {
middleware: HttpMiddleware | HttpMiddleware[],
opts: MethodOptions<SecurityDefs> = {}
): Promise<void> {
const r = this.route(match, {
middleware: this.middleware ?? [],
});
const routeMiddleware = composeMiddleware(middleware);
return r.post(routeMiddleware, opts);
return this.route(match).post(composeMiddleware(middleware), opts);
}

/**
Expand All @@ -352,11 +345,7 @@ export class Api<SecurityDefs extends string> extends Base<ApiDetails> {
middleware: HttpMiddleware | HttpMiddleware[],
opts: MethodOptions<SecurityDefs> = {}
): Promise<void> {
const r = this.route(match, {
middleware: this.middleware ?? [],
});
const routeMiddleware = composeMiddleware(middleware);
return r.put(routeMiddleware, opts);
return this.route(match).put(composeMiddleware(middleware), opts);
}

/**
Expand All @@ -370,11 +359,7 @@ export class Api<SecurityDefs extends string> extends Base<ApiDetails> {
middleware: HttpMiddleware | HttpMiddleware[],
opts: MethodOptions<SecurityDefs> = {}
): Promise<void> {
const r = this.route(match, {
middleware: this.middleware ?? [],
});
const routeMiddleware = composeMiddleware(middleware);
return r.patch(routeMiddleware, opts);
return this.route(match).patch(composeMiddleware(middleware), opts);
}

/**
Expand All @@ -388,11 +373,7 @@ export class Api<SecurityDefs extends string> extends Base<ApiDetails> {
middleware: HttpMiddleware | HttpMiddleware[],
opts: MethodOptions<SecurityDefs> = {}
): Promise<void> {
const r = this.route(match, {
middleware: this.middleware ?? [],
});
const routeMiddleware = composeMiddleware(middleware);
return r.delete(routeMiddleware, opts);
return this.route(match).delete(composeMiddleware(middleware), opts);
}

/**
Expand All @@ -406,11 +387,7 @@ export class Api<SecurityDefs extends string> extends Base<ApiDetails> {
middleware: HttpMiddleware | HttpMiddleware[],
opts: MethodOptions<SecurityDefs> = {}
): Promise<void> {
const r = this.route(match, {
middleware: this.middleware ?? [],
});
const routeMiddleware = composeMiddleware(middleware);
return r.options(routeMiddleware, opts);
return this.route(match).options(composeMiddleware(middleware), opts);
}

/**
Expand Down

0 comments on commit 477d572

Please sign in to comment.