Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server modules not getting auto-imported #2

Closed
juni0r opened this issue Nov 21, 2023 · 10 comments
Closed

Server modules not getting auto-imported #2

juni0r opened this issue Nov 21, 2023 · 10 comments

Comments

@juni0r
Copy link
Contributor

juni0r commented Nov 21, 2023

When using auth api handlers (such as login) fails due to server modules not being auto-imported. Reproduction repo.

[nuxt] [request error] [unhandled] [500] useEdgeDbPKCE is not defined
  at <anonymous> (./node_modules/nuxt-edgedb-module/dist/runtime/api/auth/login.mjs:3:16)  
  at Object.handler (./node_modules/h3/dist/index.mjs:1851:28)  
  at Object.handler (./node_modules/h3/dist/index.mjs:1675:31)  
  at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:1885:7)

export default defineEventHandler(async (req) => {
const pkce = useEdgeDbPKCE();
const { authBaseUrl } = useEdgeDbEnv();

The modules in src/runtime/server should be auto-imported since the module adds them to Nitro's import paths, but apparently something goes wrong. I'd love to investigate further but I'm not too familiar with Nuxt 3 modules yet. I suspect it's to do with the import folder containing .mjs files instead of .ts.

@Tahul
Copy link
Owner

Tahul commented Nov 21, 2023

Hey @juni0r ; thanks for reporting the issue!

I just published 0.0.9 that uses raw imports in runtime/ instead of relying on auto-imports.

That should avoid this kind of issue!

Would you mind keeping me updated on this, I'm very happy you are planning on using the module!

@juni0r
Copy link
Contributor Author

juni0r commented Nov 22, 2023

Hey @Tahul, thanks again for the swift release! I'm afraid you've missed a few instances, like this one:

export default defineEventHandler(async (req) => {
const pkce = useEdgeDbPKCE();

I'm also getting an 'unknown path' error from the auth server when using api/auth/login and I need to investigate if it's an issue with my setup or the module. I'll keep you posted.

@Tahul
Copy link
Owner

Tahul commented Nov 22, 2023

Hey @juni0r ; I should have fixed the issue with useEdgeDbPKCE() on all occurrences, sorry for the mismatch.

The fix is released under 0.0.11.

@Tahul
Copy link
Owner

Tahul commented Nov 22, 2023

nuxt-edgedb/src/module.ts

Lines 450 to 453 in f3b4fd5

addServerHandler({
handler: resolveLocal('./runtime/api/auth/login'),
route: '/api/auth/login',
})

Here is the part of the. code that injects /api/auth/login.

I don't see why, if options.auth set to true, these routes would not be available.

If needed, you can try to debug it by injecting the routes on your own by doing that:

import { createResolver, addServerHandler } from '@nuxt/kit'

const { resolve } = createResolver(import.meta.url)

export default defineNuxtConfig({
	modules: [
		function (nuxt) {
			addServerHandler({
				handler: resolve('./node_modules/nuxt-edgedb-module/dist/runtime/api/login'),
				route: '/api/auth/login',
			})
		}
	]
})

That should inject the route and let you debug it.

You can also copy the content of that route to another file and map it the same way to go further.

Let me know if I can help any further, and do not hesitate to provide a reproduction, I would happily go through it!

@juni0r
Copy link
Contributor Author

juni0r commented Nov 22, 2023

I don't see why, if options.auth set to true, these routes would not be available.

The server route is available. It contacts the edgedb auth server which returns 'path not found'.

I tried to investigate further, but I'm still struggling to run the module playground. From a quick glance at the code, I noticed that the auth server URL is built differently from what's in the docs:

http[s]://{edgedb_host}[:port]/db/{db_name}/ext/auth/

The /db/ part is missing when building the base URL:

nuxt-edgedb/src/module.ts

Lines 433 to 434 in f3b4fd5

const { host, port, database } = JSON.parse(dbCredentials.stdout)
process.env.NUXT_EDGEDB_AUTH_BASE_URL = `http://${host}:${port}/${database}/ext/auth/`

However, the default value for the auth URL does include /db/

NUXT_EDGEDB_AUTH_BASE_URL: authBaseUrl = 'http://localhost:10702/db/edgedb/ext/auth/',

@Tahul
Copy link
Owner

Tahul commented Nov 22, 2023

Damn, good catch @juni0r

I just fixed it in 0.0.12, thanks!

@juni0r
Copy link
Contributor Author

juni0r commented Nov 22, 2023

I got the playground running but I had to install a few packages in the playground project and even one in the root project. These were all dependencies of @nuxt/ui and I'm still getting errors occasionally.

It might be a consideration to remove @nuxt/ui as a dependency and go with plain css. It could be an obstacle to contributors when setting up their local dev project. The playground should focus on the module being tested and not have too many moving parts. Nuxt UI with Tailwind is quite a heavy dependency and in my case just caused trouble.

I also looked at the import paths again and I would suggest to use a path alias for the server module, such as #edgedb/server and use that consistently.

If you would like to have a look at my changes, I'd be happy to send you a PR.

@Tahul
Copy link
Owner

Tahul commented Nov 22, 2023

I would love to see your PR.

I just updated the aliases paths to be consistent (0.0.13).

Totally agree with you on the @nuxt/ui part, I would love a PR that removes it and switches to plain CSS.

I first built the playground using @nuxt/ui just for the sake of doing a demo and also trying myself that UI kit, then included it into the repository.

I think we would be better with a very minimal playground, built on top of PicoCSS or any other 1-liner CSS framework.

@juni0r
Copy link
Contributor Author

juni0r commented Nov 23, 2023

After doing a clean install of the playground, it suddenly worked without issues. Must have been a glitch in my installation. So it turns out @nuxt/ui wasn't the culprit after all.

I submitted the PR yesterday. It's nice how it fits with the aliases you've added in the meantime.

@Tahul
Copy link
Owner

Tahul commented Nov 23, 2023

Hey @juni0r ; could you take a look at 0.0.15 and let me know if it fixes your issue?

Thank you so much for both PRs, I left a comment on #5, you might want to answer here. :)

I'm happy to count you as first contributor on this module, let's prove out the potential of EdgeDB x Nuxt together!

@juni0r juni0r closed this as completed Nov 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants