NuxtN
Nuxt3y ago
nilssso

Writing a module, how to import template into composable?

I'm trying my hand at writing a module and so far so good.

  1. It fetches an OpenAPI schema
  2. Builds it using
    openapi-typescript
  3. Writes it to the project build directory via
    @nuxt/kit
    addTemplate
  4. And adds a composable via
    @nuxt/kit
    addImports
The
setup
function is this

setup(options, nuxt) {
  const { resolve } = createResolver(import.meta.url)
  const url = options.apiUrl
  if (!url) {
    throw new Error('API_URL not set')
  }
  addTemplate({
    filename: 'api.d.ts',
    write: true,
    getContents: () => openapiTS(url)
  })
  addImports({
    name: 'useApi',
    as: 'useApi',
    from: resolve('runtime/composables/useApi')
  })
}

My question now is, how can I import the written template
api.d.ts
into my added composable? I assume this should be possible since this is all done a build time?
Was this page helpful?