Adding endpoints as a plugin does not work?

default discord avatar
Alessio 🍣
7 months ago
10
https://github.com/AlessioGr/payload-plugin-lexical/blob/master/src/index.ts#L17

Is it not possible to add an endpoint to payload like that, in a plugin? Running the plugin demo, it works perfectly. But when actually installing the plugin, the new endpoint I added for some reason is not available, even though the code gets executed. ("Cannot GET /api/openai-completion")



Should it not be possible for plugins to add endpoints like that? Is it better to do that using express directly?



Or is there some field/function I can use to find out which endpoints are currently "registered" in express?

  • default discord avatar
    thisisnotchris
    7 months ago

    @Alessio 🍣 app._router.stack, or router.stack should list all registered routes



    depending on how your express setup looks

  • default discord avatar
    Teun
    7 months ago

    It should work. Are you sure you set

    pluginOptions?.ai?.openai_key

    ? (your env file is in your repo and it's not in there and also not set in your script.)



    I did same thing in my

    payload.swagger

    plugin and it works just fine. I did spread the endpoints, to keep the function pure, but that doesn't explain why it doesn't work for you.

  • default discord avatar
    Alessio 🍣
    7 months ago

    Thank you, that worked! For anyone needing this, I used this function to display all routes & endpoints in a pretty way:


    app._router.stack.forEach((r) => {
          if (r.route && r.route.path) {
            console.log(r.route.path);
          } else if (r.name === "router" && r.handle.stack) {
            r.handle.stack.forEach((r2) => {
              if (r2.route && r2.route.path) {
                console.log(r2.route.path);
              }
            });
          }
        });


    thank you, I did actually! Just figured out why it didn't work. And damn it's stupid...



    I had a different plugin which did this



    So it overrode all my endpoints as I forgot to use the spread syntax there 😅

    Code_2023-04-13_at_14.29.202x.jpg
  • default discord avatar
    thisisnotchris
    7 months ago

    Ah good catch, glad it is working!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.