Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cast auth middleware to avoid tsc errors #1579

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 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 @@ -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
10 changes: 7 additions & 3 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 @@ -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
Loading