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

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

Merged
merged 9 commits into from
Jan 4, 2024
Merged

Conversation

Vespand
Copy link
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
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 unjs:main Jan 4, 2024
4 checks passed
@manniL
Copy link
Member

manniL commented Jan 4, 2024

@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
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