Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Adding endpoints as a plugin does not work?

discord user avatar
alessiogr
2 years ago
4
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
    notchr2 years ago
    @360823574644129795

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



    depending on how your express setup looks

  • default discord avatar
    teun45402 years 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.

  • discord user avatar
    alessiogr
    2 years 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 😅

  • default discord avatar
    notchr2 years ago

    Ah good catch, glad it is working!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.