Skip to content

Commit

Permalink
fix: Cast auth middleware to avoid tsc errors
Browse files Browse the repository at this point in the history
Fixes #1575
  • Loading branch information
WoH committed Mar 11, 2024
1 parent 679d977 commit bb40d2d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
11 changes: 8 additions & 3 deletions packages/cli/src/routeGeneration/templates/express.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const multer = require('multer');
const upload = multer({{{json multerOpts}}});
{{/if}}

{{#if authenticationModule}}
const expressAuthenticationRecasted = expressAuthentication as (req: ExRequest, securityName: string, scopes?: string[], res?: ExResponse) => Promise<any>;
{{/if}}


// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

const models: TsoaRoute.Models = {
Expand Down Expand Up @@ -121,7 +126,7 @@ export function RegisterRoutes(app: Router) {
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

function authenticateMiddleware(security: TsoaRoute.Security[] = []) {
return async function runAuthenticationMiddleware(request: any, response: any, next: any) {
return async function runexpressAuthenticationRecasted(request: any, response: any, next: any) {

// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

Expand All @@ -141,7 +146,7 @@ export function RegisterRoutes(app: Router) {

for (const name in secMethod) {
secMethodAndPromises.push(
expressAuthentication(request, name, secMethod[name], response)
expressAuthenticationRecasted(request, name, secMethod[name], response)
.catch(pushAndRethrow)
);
}
Expand All @@ -153,7 +158,7 @@ export function RegisterRoutes(app: Router) {
} else {
for (const name in secMethod) {
secMethodOrPromises.push(
expressAuthentication(request, name, secMethod[name], response)
expressAuthenticationRecasted(request, name, secMethod[name], response)
.catch(pushAndRethrow)
);
}
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/routeGeneration/templates/koa.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { koaAuthentication } from '{{authenticationModule}}';
import { iocContainer } from '{{iocModule}}';
import type { IocContainer, IocContainerFactory } from '@tsoa/runtime';
{{/if}}
import type { Context, Next, Middleware } from 'koa';
import type { Context, Next, Middleware, Request as KRequest, Response as KResponse } from 'koa';
import type * as KoaRouter from '@koa/router';
{{#if useFileUploads}}
{{#if esm}}
Expand All @@ -24,6 +24,10 @@ const multer = require('@koa/multer');
{{/if}}
const upload = multer({{{json multerOpts}}});
{{/if}}
{{#if authenticationModule}}
const koaAuthenticationRecasted = koaAuthentication as (req: KRequest, securityName: string, scopes?: string[], res?: KResponse) => Promise<any>;
{{/if}}


// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

Expand Down Expand Up @@ -120,7 +124,7 @@ export function RegisterRoutes(router: KoaRouter) {
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

function authenticateMiddleware(security: TsoaRoute.Security[] = []) {
return async function runAuthenticationMiddleware(context: any, next: any) {
return async function runkoaAuthenticationRecasted(context: any, next: any) {

// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

Expand All @@ -140,7 +144,7 @@ export function RegisterRoutes(router: KoaRouter) {

for (const name in secMethod) {
secMethodAndPromises.push(
koaAuthentication(context.request, name, secMethod[name], context.response)
koaAuthenticationRecasted(context.request, name, secMethod[name], context.response)
.catch(pushAndRethrow)
);
}
Expand All @@ -150,7 +154,7 @@ export function RegisterRoutes(router: KoaRouter) {
} else {
for (const name in secMethod) {
secMethodOrPromises.push(
koaAuthentication(context.request, name, secMethod[name], context.response)
koaAuthenticationRecasted(context.request, name, secMethod[name], context.response)
.catch(pushAndRethrow)
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/inversify-cpg/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as express from 'express';

export function expressAuthentication(req: express.Request, name: string, _scopes: string[] | undefined, res: express.Response): Promise<any> {
export function expressAuthentication(req: express.Request, name: string, _scopes: string[] | undefined): Promise<any> {
if (req.query && req.query.tsoa && req.query.tsoa === 'abc123456') {
return Promise.resolve({});
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/koaNoAdditional/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from 'koa';

export function koaAuthentication(request: Request, name: string, scopes: string[] | undefined, response: Response): Promise<any> {
export function koaAuthentication(request: Request, name: string, scopes: string[] | undefined): Promise<any> {
if (name === 'api_key') {
let token;
if (request.query && request.query.access_token) {
Expand Down

0 comments on commit bb40d2d

Please sign in to comment.