Typescript support
See original GitHub issueWhat is Expected: No Error
What is happening
I wanted to use this module with nuxt, typescript and vue composition api but there are no type declarations for it, so I started writing my own, But still there is an error telling me that there is no $strapi on context when I want to use $strapi in a middleware. The exact error: { “resource”: “/project/middleware/auth.ts”, “owner”: “typescript”, “code”: “2339”, “severity”: 8, “message”: “Property ‘$strapi’ does not exist on type ‘Context’.”, “source”: “ts”, “startLineNumber”: 4, “startColumn”: 29, “endLineNumber”: 4, “endColumn”: 36 }
I want to be able to use this module in a nuxt-typescript project without errors.
What I think would fix the error
I think rewriting the module in typescript or just providing @types folder would fix the issue. I myself would be interested in helping.
My code
import { Middleware } from '@nuxt/types'
const auth: Middleware = ({ $strapi, redirect, route }) => {
if (!$strapi.user) {
return redirect(`/login?next=${route.path}`)
}
}
export default auth
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top GitHub Comments
You can add typescript support manually, You still need to add strapi to modules in nuxt.config.js but this is a solution that works without to much work. Hope this helps.
Hi @austinbakker , yesterday I made a quick tryout and notice that I was using
this.$strapi.centers.find()
, when looking at your code (I only copy&paste directly) it’s obsvious that’s not going to work. When I changed it tothis.$strapi.find('centers')
it work straight forward.Another thing missing was the
login
function. I added it to your code like this:Login works great when added that.
Right now, I’m only getting this error (~maybe related to using SSR~ tried with a new SPA nuxt app and the error keeps popping up, same happens when using a javascript nuxt app):
Any idea about this error?
If someone needs the plugin file with the
login
function, mine looks like this (only adding that line):