Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"I want to pass in a user-defined function, but runtimeConfig cannot pass in functions and arrays." #27384

Closed
devcui opened this issue May 28, 2024 · 4 comments

Comments

@devcui
Copy link

devcui commented May 28, 2024

Summary (generated):

Here is a brief summary of the text in English:

Operating system: Windows_NT. Node version is v20.13.1, Nuxt version is 3.11.2, and CLI version is 3.11.1. The problem is related to passing a user-defined function in runtimeConfig, which cannot handle functions and arrays.


Environment


  • Operating System: Windows_NT
  • Node Version: v20.13.1
  • Nuxt Version: 3.11.2
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.6
  • Package Manager: npm@10.5.0
  • Builder: -
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://github.com/copyfrom/nuxt-http/blob/chore/src/runtime/core/options.ts

Describe the bug

I want to pass in a user-defined function, but runtimeConfig cannot pass in functions and arrays.

Additional context

No response

Logs

No response

@nuxtbot nuxtbot changed the title I want to pass in a user-defined function, but runtimeConfig cannot pass in functions and arrays. "I want to pass in a user-defined function, but runtimeConfig cannot pass in functions and arrays." May 28, 2024
@devcui
Copy link
Author

devcui commented May 28, 2024

If I use addTemplate to set a config file to the user directory, how should I write it? Are there any simple examples?
and i have read #20933 #18762
Still no solution found

@manniL
Copy link
Member

manniL commented May 28, 2024

That is intended as runtimeConfig (and the whole nuxt config) should be serializable ☺️

Check e.g. how nuxt-graphql-client adds templates

@manniL manniL closed this as not planned Won't fix, can't repro, duplicate, stale May 28, 2024
@devcui
Copy link
Author

devcui commented May 29, 2024

I think I have found the answer: Use addPluginTemplate to provide settings to nuxtapp

import { defineNuxtPlugin } from '#imports'

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.provide("auth", {
baseURL: '<%= options.baseURL %>',
<% if (options.http) { %>
http: {
<% if (options.http.handlers) { %>
handlers:{
<% if (options.http.handlers['401']) { %>
401: <%= options.http.handlers['401'] %>
<% } %>
}
<% } %>
}
<% } %>
})
})
 addPluginTemplate({
    filename: name,
    src: resolve(`../runtime/templates`, tempalte),
    options: {
      ...options,
    },
  })
<template>
  <div>Nuxt module playground!</div>
</template>

<script setup lang="ts">
const { $auth } = useNuxtApp()
console.log($auth)
$auth.http.handlers['401']('yahahahah')
</script>

Then I can call the function configured from nuxt.config

@devcui
Copy link
Author

devcui commented May 29, 2024

But I don't think this is the best way to do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants