strapi icon indicating copy to clipboard operation
strapi copied to clipboard

How to use "useStrapi4" from /server/api?

Open niklasfjeldberg opened this issue 2 years ago • 9 comments

How can you use Strapi from /server/api/name.post.js?

I get the error [nuxt] [request error] useStrapi4 is not defined if i do not import strapi, and if i try to import useStrapi4 from @nuxtjs/strapi I get the following error [worker reload] [worker init] The requested module 'file://C:/Users/...../node_modules/@nuxtjs/strapi/dist/module.mjs' does not provide an export named 'useStrapi4'.

niklasfjeldberg avatar May 31 '22 21:05 niklasfjeldberg

Got exactly the same problem

paulvonber avatar Jul 15 '22 16:07 paulvonber

How can you use Strapi from /server/api/name.post.js?

I get the error [nuxt] [request error] useStrapi4 is not defined if i do not import strapi, and if i try to import useStrapi4 from @nuxtjs/strapi I get the following error [worker reload] [worker init] The requested module 'file://C:/Users/...../node_modules/@nuxtjs/strapi/dist/module.mjs' does not provide an export named 'useStrapi4'.

Did you find a solution?

tiYusupov avatar Aug 10 '22 09:08 tiYusupov

@tiYusupov no, I have not.

niklasfjeldberg avatar Aug 10 '22 18:08 niklasfjeldberg

Hello,

Did you find a solution ?

If yes : have you an example ?

Thanks.

titoff002 avatar Aug 12 '22 23:08 titoff002

I did like this: Maybe it's wrong. But working for me. ( Nuxt 3 [3.0.0-rc.7], Strapi 4 )

At frontend

const token = useStrapiToken()

await $fetch('/api/check', {
    method: 'post',
    body: {
        jwt: token.value
    }
  })

At /server/api/test.post.ts

import { defineEventHandler, readBody } from 'h3'
const runtimeConfig = useRuntimeConfig()

export default defineEventHandler(async (event) => {
    const body = await readBody(event)
    return await $fetch(`${runtimeConfig.strapi.url}/api/users/me`, {
            headers: {
                'Content-type': 'application/json',
                'Authorization': `Bearer ${body.jwt}`,
            },
        })
})

tiYusupov avatar Aug 13 '22 11:08 tiYusupov

Frustrating, I had the same problem

selvinzr avatar Feb 24 '23 02:02 selvinzr

Frustrating, I had the same problem

You can replace nuxt server API logic to strapi API, then request on nuxt page to strapi via client() or something. It's better than use Strapi on Nuxt server API

tiYusupov avatar Feb 24 '23 06:02 tiYusupov

The same problem. Sometimes it is needed to fetch the data from API that is not publically available, but accessible by read-only token. And also where no user authentication. In this case, exposing the sensitive data (token) on the client side is not an option, so server-side usage is required. So, it would be really good if this module can support data fetching on the server side out of the box as well.

maxdzin avatar Aug 22 '23 20:08 maxdzin

As far as I understood, the Strapi module is accessible from the client-side only. I wanted to use it as well from Nitro, but we do not have access to the composables from server/api. I wanted to protect my Strapi API with the API Token and keep the server URL private, but I had to write everything from scratch. I used Strapi's recommended way of using qs to build my queries alongside $fetch requests. Bummer.

Maybe as a Nitro plugin?

cantkillemall avatar Oct 01 '23 10:10 cantkillemall