johannschopplich / nuxt-api-party

🐬 Securely connect to any API with a server proxy and generated composables
https://nuxt-api-party.byjohann.dev
MIT License
260 stars 10 forks source link

Merge multiple `endpoint` configurations into a single one #80

Closed PolanskiPol closed 2 months ago

PolanskiPol commented 2 months ago

Describe the feature

Issue

We are using Nuxt Api Party in the company that I'm working at, but we've found a limitation when installing it programatically using installModule('nuxt-api-party', { ... }) inside of modules instead of installing it directly in the nuxt.config.ts file of a project.

Right now, if I add more than one module to nuxt.config.ts that installs Nuxt Api Party using Nuxt's await installModule('nuxt-api-party', { ... }), only the first module in-line options will be applied, and only the first module composables will be correctly auto-imported to the project.

This is affecting our workflow as we intended to modularize the installation of various Api Party endpoints into the same project by separating them in different Nuxt modules.

Minimal example

// Nuxt module 1
export default defineNuxtModule({
  meta: {
    name: 'module1',
    configKey: 'configKey',
  },
  defaults: {},
  async setup(options, nuxt) {
    await installModule('nuxt-api-party', {
      endpoints : {
        endpoint1 : { // endpoint1 from module 1 
          url: "",
          query: {},
          headers: {},
        }
      }
    });
  },
});
// Nuxt module 2
export default defineNuxtModule({
  meta: {
    name: 'module2',
    configKey: 'configKey',
  },
  defaults: {},
  async setup(options, nuxt) {
    await installModule('nuxt-api-party', {
      endpoints : {
        endpoint2 : { // endpoint2 from module 2
          url: "",
          query: {},
          headers: {},
        }
      }
    });
  },
});
// Nuxt project config
export default defineNuxtConfig({
  ...
  modules : [
    "module1", // This module will be installed and its endpoints and composables will be available
    "module2", // This module will be installed but its endpoints and composables will not be available
  ],
  ...
});

Doing the previous example, Nuxt Api Party will be correctly installed in the project, and both modules will be installed too. However, only the configuration from module1 will be applied, and only the composables $module1 and useModule1Data will be auto-imported into the project.

Proposal

We propose to allow Nuxt Api Party to be installed from different modules at the same time, such as the example given allow, joining all the in-line configurations into a single one that implements the endpoints from all modules and auto-imports the composables from all modules.

Thank you for your time! 😊

Additional information

Final checks

johannschopplich commented 2 months ago

Hi there, isn't this a problem you will have with all plugins that you install more than one time?

I don't see how the Nuxt API Party module should detect multiple usages. I'd rather recommend you move your configuration from the module options to the runtime config. So instead of passing the endpoints to Nuxt API Party, add it to your runtime config:

// Nuxt module 2
export default defineNuxtModule({
  meta: {
    name: 'module2',
    configKey: 'configKey',
  },
  defaults: {},
  async setup(options, nuxt) {
    await installModule('nuxt-api-party');
  },
});

// Nuxt project config
export default defineNuxtConfig({
  ...
  modules : [
    "module1", // This module will be installed and its endpoints and composables will be available
    "module2", // This module will be installed but its endpoints and composables will not be available
  ],
  runtimeConfig: {
    apiParty: {
      endpoints: {}
    }
  }
});

Or you can add new items to the endpoints from inside your modules, which merges the configs:

await installModule('nuxt-api-party')

nuxt.options.runtimeConfig.apiParty = defu(
  nuxt.options.runtimeConfig.apiParty,
  {
    endpoints: {
      newApi: {}
    }
  },
)
PolanskiPol commented 2 months ago

Hello,

isn't this a problem you will have with all plugins that you install more than one time?

Yeah, this could be the case in the future now that you mention it, but as of now, it has only happened to us using Nuxt Api Party, and I'm not aware of any other modules being used the same way as we're using this one.

I'd rather recommend you move your configuration from the module options to the runtime config

We've found a similar solution for the problem that seems to fit our needs, many thanks for your recommendation!

Thank you for your time once again! 😊

johannschopplich commented 2 months ago

Glad you've found a viable solution. 🙂