Skip to content

feat(runtime-config): experimental env expansion support#2043

Merged
pi0 merged 9 commits intonitrojs:mainfrom
Vespand:main
Jan 4, 2024
Merged

feat(runtime-config): experimental env expansion support#2043
pi0 merged 9 commits intonitrojs:mainfrom
Vespand:main

Conversation

@Vespand
Copy link
Copy Markdown
Contributor

@Vespand Vespand commented Dec 30, 2023

🔗 Linked issue

There no linked issue, because this is feature

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Feature add additional check env for used in runtime config templates like: {{MAIL_HOST}}
For example instead of (actual small part of runtime config app):

export default defineNuxtConfig({
    runtimeConfig: {
        public: {
            auth: {
                strategies: {
                    some: {
                        endpoints: {
                            authorization: '', // NUXT_PUBLIC_AUTH_STRATEGIES_SOME_ENDPOINTS_AUTHORIZATION
                            userInfo: '', // NUXT_PUBLIC_AUTH_STRATEGIES_SOME_ENDPOINTS_USER_INFO
                            token: '', // NUXT_PUBLIC_AUTH_STRATEGIES_SOME_ENDPOINTS_TOKEN
                            logout: '' // NUXT_PUBLIC_AUTH_STRATEGIES_SOME_ENDPOINTS_LOGOUT
                        },
                        clientId: '' // NUXT_PUBLIC_AUTH_STRATEGIES_SOME_CLIENT_ID
                    }
                }
            }
        }
    }
});

We can use (with envExpansion: true):

export default defineNuxtConfig({
    runtimeConfig: {
        public: {
            auth: {
                strategies: {
                    some: {
                        endpoints: {
                            authorization: '{{HOST_SCHEMA}}://{{AUTH_HOST}}/authorization',
                            userInfo: '{{HOST_SCHEMA}}://{{AUTH_HOST}}/userInfo',
                            token: '{{HOST_SCHEMA}}://{{AUTH_HOST}}/token',
                            logout: '{{HOST_SCHEMA}}://{{AUTH_HOST}}/logout'
                        },
                        clientId: '{{AUTH_CLIENT_ID}}'
                    }
                }
            }
        }
    }
});

This feature targeting developer experience with best practices from other frameworks and programming languages
By default it is off.

Now working with envs in Nuxt is pain, because:

  • Envs can't be reused (need to set a lot of variables)
  • Long env names (developer can get typo error when adding long-name env)
  • Hardcode and no flexability with env name in Nuxt and env name in environment

Case of this is setting up environment env for dynamic environments (including production), where you have different backends, sso paths, clients and others integrations and can't reuse same env for others (like HOST_SCHEMA)

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Looking for feedback about this feature :)
If it's ok, will add docs
For now added example and tests with some cases

@pi0 pi0 changed the title feat(envExpansion): Add env expansion by template in runtime config feat: support env expansion for runtime config Jan 4, 2024
@pi0 pi0 changed the title feat: support env expansion for runtime config feat(runtime-config): experimental env expansion support Jan 4, 2024
@pi0
Copy link
Copy Markdown
Member

pi0 commented Jan 4, 2024

Thanks so much for this it is gonna be really useful! I expect at some point we enable this behavior by default. With latest changes, users can opt-in using experimental.envExpansion config (nitro.experimental.envExpansion for nuxt)

@pi0 pi0 merged commit c8e8549 into nitrojs:main Jan 4, 2024
@TheAlexLichter
Copy link
Copy Markdown
Member

@pi0 I wonder, will the replacement also work during runtime? 🤔
Mainly referring to because of the 1:1 env replacements (see e.g. what I've shown in my video / this docs PR)

@pi0
Copy link
Copy Markdown
Member

pi0 commented Jan 5, 2024

Yes it works!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants