medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.68k stars 2.56k forks source link

404 Cannot GET /admin/custom on custom API route #5519

Closed camfeghali closed 1 year ago

camfeghali commented 1 year ago

Bug report

Edit: This happens when I define functions in a file outside of the src/api/admin/custom folder, and import & execute them in the /route.ts file. Is there something to know about project level scope here?

Describe the bug

I am receiving a 404 Cannot GET /admin/custom when making a GET request to my custom API route even though I export a const AUTHENTICATION=false.

To clarify - when using Postman:

System information

Medusa packages:
    "@medusajs/admin": "7.1.5",
    "@medusajs/cache-inmemory": "^1.8.8",
    "@medusajs/cache-redis": "^1.8.8",
    "@medusajs/event-bus-local": "^1.9.6",
    "@medusajs/event-bus-redis": "^1.8.9",
    "@medusajs/file-local": "^1.0.2",
    "@medusajs/medusa": "^1.17.2",
    "@medusajs/ui": "^2.2.0",

Node.js version: 16.0.0 Database: postgresql Operating system: MAC

Steps to reproduce the behavior

  1. Create file src/api/admin/custom/route.ts
  2. Define a random function in a file outside the /custom dir. ex: export function log(){console.log("hello world")}
  3. in your route.ts file:
    
    import { MedusaRequest, MedusaResponse } from '@medusajs/medusa';
    import {log} from "../other-dir/index.ts";

export const GET = (req: MedusaRequest, res: MedusaResponse) => { log(); res.json({ message: '[GET] Hello world!', }); };

export const AUTHENTICATE = true;

5. Make a request from Postman (this is the CURL)

curl --location 'http://localhost:9000/admin/custom' \ --header 'x-medusa-access-token: ' \ --header 'Cookie: connect.sid=s%3AIuVcge8cY2Z-raq7E68lakY8NScmE2r4.b4LKiAa8uZOcTalLrQH%2By5%2FcWuZMfxmPT43vJ54ceWI; session=MTY5Njg3Njc1NXxEdi1CQkFFQ180SUFBUkFCRUFBQUhmLUNBQUVHYzNSeWFXNW5EQWdBQm5WelpYSkpSQU5wYm5RRUFnQU18fkpJ6zbUKrZzRy0-l_g8ZnV7uidAhQ6K_8EnLjRjbD8='


6. See error

### Expected behavior

I expect the server to respond with the json defined in the route handler.

Any help would be much appreciated.
kasperkristensen commented 1 year ago

Hi @camfeghali

My immediate thought is that it might be an issue with how you are importing your log function. You are importing it from "../other-dir/index.ts". You should just import it without the extension, eg. "../other-dir" (no need for /index since it's the index file of the folder). But if you want to use the ES import syntax then it needs to be "../other-dir/index.js" as that is the compiled file, and trying to import the uncompiled typescript file will fail.

camfeghali commented 1 year ago

hey @kasperkristensen , thanks for the help, it worked!

kasperkristensen commented 1 year ago

@camfeghali Glad to hear it! 💪