Baroshem / nuxt-medusa

🛍️ Medusa module for Nuxt
https://nuxt-medusa.vercel.app
MIT License
123 stars 9 forks source link

Server utility not finding import and also getting error "[nuxt] [request error] [unhandled] [500] Cannot read properties of undefined (reading 'request')" #16

Closed rasmuslian closed 1 year ago

rasmuslian commented 1 year ago

Version

nuxt-medusa ^0.3.2 nuxt: ^3.2.3

Steps to reproduce

Nuxt config:

export default defineNuxtConfig({
  modules: ["nuxt-medusa"],
  medusa: {
    server: true,
    global: true,
  },
});

What is Expected?

That serverMedusaClient can be used as in the documentation in server routes.

What is actually happening?

Using this code within 'server/api/products/index.ts'

import { serverMedusaClient } from '#medusa/server'
export default eventHandler(async (event) => {
  const client = serverMedusaClient(event)
  const { products } = await client.products.list()
  return { products }
})

Gives this result:

import { serverMedusaClient } from '#medusa/server' shows the error: "Cannot find module '#medusa/server' or its corresponding type declarations"

It seems that it cannot find the type declarations, but not only that, when using it (ignoring this error, since it should only by typescript related) I get the following error:

[nuxt] [request error] [unhandled] [500] Cannot read properties of undefined (reading 'request')                                                              15:52:07  
  at dispatchHttpRequest (/D:/Code/solsting/.nuxt/dev/index.mjs:2007:26)
  at new Promise (<anonymous>)
  at httpAdapter (/D:/Code/solsting/.nuxt/dev/index.mjs:1831:11)
  at dispatchRequest (/D:/Code/solsting/.nuxt/dev/index.mjs:2379:10)
  at Axios$1.request (/D:/Code/solsting/.nuxt/dev/index.mjs:2694:15)
  at C.wrap [as axiosClient] (/D:/Code/solsting/.nuxt/dev/index.mjs:655:15)
  at C.request (/D:/Code/solsting/.nuxt/dev/index.mjs:3904:2269)
  at Z.list (/D:/Code/solsting/.nuxt/dev/index.mjs:3904:9050)
  at /D:/Code/solsting/.nuxt/dev/index.mjs:3917:46
  at /D:/Code/solsting/node_modules/h3/dist/index.mjs:1138:14

Using useMedusaClient works like expected.

Baroshem commented 1 year ago

Hey hey,

The first problem you mentioned is TS related, dont need to worry about that :)

The second one I am more concerned.

Could you show me how are you using the event handler registered hereserver/api/products/index.ts?

Are you using useFetch or something else?

rasmuslian commented 1 year ago

Hey hey,

The first problem you mentioned is TS related, dont need to worry about that :)

The second one I am more concerned.

Could you show me how are you using the event handler registered hereserver/api/products/index.ts?

Are you using useFetch or something else?

Regarding the first problem, I know even though it's nothing tto worry about it would be nice with the types working so to use code completion inside VS code.

As to the second one, this is how I'm using it, inside a file called pages/shop/index.vue

<script setup lang="ts" scoped>
const { data: products } = await useFetch("/api/products");
</script>
Baroshem commented 1 year ago

The first problem wont be so easy to solve as this type of declaration is a recurring situation in several modules.

The second problem. Why are you using scoped on the script tag? I dont think it is needed :)

rasmuslian commented 1 year ago

The first problem wont be so easy to solve as this type of declaration is a recurring situation in several modules.

The second problem. Why are you using scoped on the script tag? I dont think it is needed :)

The scoped tag got added there accidentally, removed it now and still same error! :)

Baroshem commented 1 year ago

@rasmuslian

Could you create a stackblitz link with reproduction? This is really strange

rasmuslian commented 1 year ago

@rasmuslian

Could you create a stackblitz link with reproduction? This is really strange

Here is a stackblitz I created now: https://stackblitz.com/edit/nuxt-starter-ezqx4d (Obviously without an medusa backend within the Stack Blitz project)

Baroshem commented 1 year ago

I see.

Thanks for that. I know where the problem is. This is really strange one. I will publish a new version in the upcoming hours that should fix that :)

rasmuslian commented 1 year ago

I see.

Thanks for that. I know where the problem is. This is really strange one. I will publish a new version in the upcoming hours that should fix that :)

Awesome! Let me know also, would be interesting to know what caused this problem.

Baroshem commented 1 year ago

Hey, can you try the 0.3.4 version? It should be working there. I also need to correct the docs :)

rasmuslian commented 1 year ago

Hey, can you try the 0.3.4 version? It should be working there. I also need to correct the docs :)

It's working now with version 0.3.4! :) What was the issue?

Baroshem commented 1 year ago

The issue was really strange. It was caused by the transpilation of the axios dependency and as a result it was merging the medusa client with Axios.

With 0.3.4 you should also have the type delcaration for server utility.

I am closing the issue then. Thanks for the help! 💚

rasmuslian commented 1 year ago

The issue was really strange. It was caused by the transpilation of the axios dependency and as a result it was merging the medusa client with Axios.

With 0.3.4 you should also have the type delcaration for server utility.

I am closing the issue then. Thanks for the help! 💚

Very peculiar! Thank you for the elaboration, and not the least the updated module version! Keep up the good work 😄