dashport icon indicating copy to clipboard operation
dashport copied to clipboard

Using Dashport as a middleware leads to compile error

Open finlaydotb opened this issue 3 years ago • 12 comments

Hi there 👋

First all, thanks for making this library. Looks like the perfect thing I need for my use case. The only problem now is that I am following the documentation and it does not seem to work. In the documentation you see the following lines:

Dashport is now ready to authenticate. Dashport's authenticate method acts as middleware, so it can be used like so

import { dashport, googStrat, serializerA, deserializerA } from './dashportConfig.ts';

const xx = async (ctx: any, next: any) => {
    ctx.response.body = 'This is a private page!';
}

router.get('/privatepage', dashport.authenticate(googStrat, serializerA, deserializerA), xx)

But if I do so, I get the following compilation error

  Overload 1 of 2, '(name: string, path: string, ...middleware: RouterMiddleware<RouteParams, Record<string, any>>[]): Router<RouteParams, Record<string, any>>', gave the following error.
    Argument of type '(ctx: any, next: any) => Promise<void>' is not assignable to parameter of type 'string'.  Overload 2 of 2, '(path: string, ...middleware: RouterMiddleware<RouteParams, Record<string, any>>[]): Router<RouteParams, Record<string, any>>', gave the following error.
    Argument of type 'Function' is not assignable to parameter of type 'RouterMiddleware<RouteParams, Record<string, any>>'.
      Type 'Function' provides no match for the signature '(context: RouterContext<RouteParams, Record<string, any>>, next: () => Promise<void>): void | Promise<void>'.
router.get('/privatepage', xx, dashport.authenticate(googStrat, serializerA, deserializerA))

Any ideas what could be going wrong? Thanks!

finlaydotb avatar Apr 03 '21 14:04 finlaydotb

Hey, sorry this response is so delayed, things can get hectic. I am not sure if this remedies your issue, but I looked into it and seemed to have issues with deno.land not serving the most recent version if you do not specify. I would recommend trying to just import V1.2.0 and see if you have any progress. if not I am happy to take a closer look at your code and figure out is triggering this bug with dashport.

-Alex

BAlexanderNance avatar Apr 14 '21 17:04 BAlexanderNance

Unfortunately the issue still persist:

error: TS2769 [ERROR]: No overload matches this call.
  Overload 1 of 2, '(name: string, path: string, ...middleware: RouterMiddleware<RouteParams, Record<string, any>>[]): Router<RouteParams, Record<string, any>>', gave the following error.
    Argument of type 'Function' is not assignable to parameter of type 'string'.  Overload 2 of 2, '(path: string, ...middleware: RouterMiddleware<RouteParams, Record<string, any>>[]): Router<RouteParams, Record<string, any>>', gave the following error.
    Argument of type 'Function' is not assignable to parameter of type 'RouterMiddleware<RouteParams, Record<string, any>>'.
      Type 'Function' provides no match for the signature '(context: RouterContext<RouteParams, Record<string, any>>, next: () => Promise<void>): void | Promise<void>'.
    dashport.authenticate(googStrat, serializerA, deserializerA),

The imports I am using are:

import { DashportOak } from 'https://deno.land/x/[email protected]/mod.ts';
import GoogleStrategy from 'https://deno.land/x/[email protected]/mod.ts'

finlaydotb avatar Apr 19 '21 19:04 finlaydotb

@finlaydotb I had the same issue, but it looks like it's a typescript definitions mismatch between what Oak wants and what Dashport gives. I can't dig too much deeper but the following worked for me:

import {
  Application,
  Router,
  RouterMiddleware,
} from "https://deno.land/x/oak/mod.ts";
...

router.get(
  "/auth/google",
  dashport.authenticate(
    googleStrat,
    serializer,
    deserializer
  ) as RouterMiddleware
);

royletron avatar Apr 20 '21 15:04 royletron

Hey Finlay! Sorry for the delay. I'm having trouble replicating your specific issue. What other info could you give me about your setup? I did hit a different error because the version of Oak I was using was out of date, so I'd check that if you haven't already. If triple checking that your versions are all up to date doesn't work, please reach back out.

sportelance avatar Apr 21 '21 17:04 sportelance

Hey @sportelance sorry also for the late response. Strangely enough @royletron suggestion to type cast to RouterMiddleware seems to have fixed the compilation error. I am not sure why this is the case, but right now at least introducing Dashport does not cause the project to stop compiling...next is to see if it actually does what it should do :)

finlaydotb avatar May 06 '21 08:05 finlaydotb

I'll keep this issue open as I am not sure manually casting to RouterMiddleware is the way to go

finlaydotb avatar May 07 '21 13:05 finlaydotb

Okay, upgraded my version of deno ( 1.9.2) and oak (7.4.0), and the as RouterMiddleware type cast trick is no longer working :(

Any insight on this issue? and is there a recommended version of oak to use with each corresponding dashport version? Could not see any version matrix in the readme.

Update: oak version 6.5.0 seems to be the oldest version in which the as RouterMiddleware trick works

finlaydotb avatar May 11 '21 10:05 finlaydotb

I am also facing the same issue. Getting error: TS2769 [ERROR]: No overload matches this call compilation failure.

dadepo avatar May 18 '21 20:05 dadepo

Hey guys, 6.5.0 is the latest version of Oak that the module is currently working with. Oak has made many, many changes since our initial release and every type change they make affects the functionality of the module. This is clearly a problem in need of a solution and we're working on it. In the meantime, I think Dashport, unfortunately, will need to be run with Oak 6.5.0

sportelance avatar May 19 '21 19:05 sportelance

@sportelance thanks for the update. I'll stick with Oak 6.5.0 even though I still need to have the as RouterMiddleware type assertion to get compilation to work. Better than not having a working application at all :)

This is clearly a problem in need of a solution and we're working on it.

Godspeed!

Thanks!

finlaydotb avatar May 20 '21 07:05 finlaydotb

I'm now getting

error: TS2345 [ERROR]: Argument of type 'import("https://deno.land/x/[email protected]/application.ts").Application<Record<string, any>>' is not assignable to parameter of type 'import("https://deno.land/x/[email protected]/application.ts").Application<Record<string, any>>'.
  Type 'Application<Record<string, any>>' is missing the following properties from type 'Application<Record<string, any>>': #contextState, #middleware, #serverConstructor, #getComposed, and 3 more.
const dashport = new DashportOak(app)

which I /think/ means that DashportOak is expecting the latest version of Oak, even though it does not work with it? (Apologies if that's wrong, I'm pretty new to Deno)

dannyob avatar Nov 18 '21 08:11 dannyob

dashport.authenticate(googStrat, serializerA, deserializerA) as any works for me with the newest oak and dashport.

robertheessels avatar Jan 15 '22 20:01 robertheessels