curveball / core

The Curveball framework is a TypeScript framework for node.js with support for modern HTTP features.
https://curveballjs.org/
MIT License
525 stars 7 forks source link

Context not assignable to Middleware #206

Closed defrex closed 1 year ago

defrex commented 1 year ago

In @curveball/core@0.20.0, I can't use the exported Context to define a middleware, else they will be incompatible. Since controllers are middleware, this also makes controllers type-incompatible with route.

import { Context, Middleware } from '@curveball/core';

const exampleMiddleware: Middleware = async (ctx: Context, next) => null;

The above should work, but results in the following error.

Type '(ctx: Context<any, any>, next: () => Promise<void>) => Promise<null>' is not assignable to type 'Middleware'.
  Type '(ctx: Context<any, any>, next: () => Promise<void>) => Promise<null>' is not assignable to type 'MiddlewareFunction'.
    Types of parameters 'ctx' and 'ctx' are incompatible.
      Type 'Context<any, any>' is missing the following properties from type 'Context<any, any>': session, sessionId, getCsrf, validateCsrf, paramsts(2322)

The same error appears if both are imported from @curveball/kernel BTW, so that isn't a fix here.

Screen Shot 2022-09-27 at 3 44 40 PM
evert commented 1 year ago

Are all the packages up to date? Usually this only happens if you somehow have a mixed set of packages with different versions.

Might be helpful to see the output of npm ls -a if there's doubt.

defrex commented 1 year ago

Yup, I've updated them all to @latest

package.json

    "@badgateway/oauth2-client": "^2.0.16",
    "@curveball/bodyparser": "^0.5.0",
    "@curveball/browser": "^0.19.3",
    "@curveball/browser-to-bearer": "^0.3.5",
    "@curveball/controller": "^0.4.0",
    "@curveball/core": "^0.20.0",
    "@curveball/cors": "^0.2.0",
    "@curveball/http-errors": "^0.4.1",
    "@curveball/links": "^0.2.0",
    "@curveball/oauth2": "^0.4.2",
    "@curveball/problem": "^0.4.0",
    "@curveball/router": "^0.4.2",
    "@curveball/session": "^0.8.0",
    "@curveball/session-redis": "^0.5.0",
    "@curveball/validator": "^0.10.0",
╚╡npm ls -w apps/api -a | grep curveball
  ├─┬ @curveball/bodyparser@0.5.0
  │ └─┬ @curveball/kernel@0.20.0
  │   ├── @curveball/http-errors@0.4.1 deduped
  ├─┬ @curveball/browser-to-bearer@0.3.5
  │ ├── @curveball/core@0.20.0 deduped
  │ └── @curveball/session@0.8.0 deduped
  ├─┬ @curveball/browser@0.19.3
  │ ├── @curveball/kernel@0.20.0 deduped
  │ ├─┬ @curveball/static@0.3.0
  │ │ ├── @curveball/kernel@0.20.0 deduped
  ├─┬ @curveball/controller@0.4.0
  │ ├── @curveball/http-errors@0.4.1 deduped
  │ └── @curveball/kernel@0.20.0 deduped
  ├─┬ @curveball/core@0.20.0
  │ ├── @curveball/http-errors@0.4.1 deduped
  │ ├── @curveball/kernel@0.20.0 deduped
  ├─┬ @curveball/cors@0.2.0
  │ ├── @curveball/http-errors@0.4.1 deduped
  │ └── @curveball/kernel@0.20.0 deduped
  ├── @curveball/http-errors@0.4.1
  ├─┬ @curveball/links@0.2.0
  │ ├── @curveball/kernel@0.20.0 deduped
  ├─┬ @curveball/oauth2@0.4.2
  │ ├── @curveball/http-errors@0.4.1 deduped
  │ ├── @curveball/kernel@0.20.0 deduped
  ├─┬ @curveball/problem@0.4.0
  │ └── @curveball/kernel@0.20.0 deduped
  ├─┬ @curveball/router@0.4.2
  │ ├── @curveball/core@0.20.0 deduped
  ├─┬ @curveball/session-redis@0.5.0
  │ ├── @curveball/kernel@0.20.0 deduped
  ├─┬ @curveball/session@0.8.0
  │ ├── @curveball/kernel@0.20.0 deduped
  ├─┬ @curveball/validator@0.10.0
  │ ├── @curveball/controller@0.4.0 deduped
  │ ├── @curveball/http-errors@0.4.1 deduped
  │ ├── @curveball/kernel@0.20.0 deduped
  │ ├── @curveball/links@0.2.0 deduped
evert commented 1 year ago

Looking at this list, i think we're at least missing a npm release for browser-to-bearer. 0.4.0 is the latest, but the publish action failed:

https://github.com/curveball/browser-to-bearer/actions/runs/2986071165

Gonna fix this

evert commented 1 year ago

This has a new release:

https://github.com/curveball/browser-to-bearer/releases/tag/v0.4.1

evert commented 1 year ago

@curveball/router in this list is also old. This is correctly released and I would say very likely the actual culprit.

evert commented 1 year ago

I'm 90% sure upgrading router will fix this, so closing this ticket with that premise. If I'm wrong, we can re-open.

defrex commented 1 year ago

npm install @curveball/router@latest results in 0.4.2 somehow. Even if I npm i @curveball/router@0.5.0 or manually set it in package.json, the resulting entry in package-lock.json is.

    "apps/api/node_modules/@curveball/router": {
      "version": "0.4.2",
      "resolved": "https://registry.npmjs.org/@curveball/router/-/router-0.4.2.tgz",
      "integrity": "sha512-uEz5QDDBiLl8vHuq37RoGycQ69KcWSXeyQaIEielY/uc/AApSkOUyK28BQ58MAaa1I/BoXpoe471i6PROAE8gg==",
      "dependencies": {
        "path-to-regexp": "^6.2.0"
      },
      "engines": {
        "node": ">= 14"
      },
      "peerDependencies": {
        "@curveball/core": ">=0.19 <1"
      }
    },

Is it possible something is wrong with that release as well? NPM does seem to think there is a valid version, it just refuses to install.

evert commented 1 year ago

Pretty strange! You're not using some internal npm registry are you?

If you're willing to share package-lock, I could also take a look there. Also curious what is set in your own package.json for that dependency.