From 83d2b661c82bbd99844dd7731c5ffe549457ff70 Mon Sep 17 00:00:00 2001 From: Azri Kahar <42867097+azrikahar@users.noreply.github.com> Date: Fri, 24 Sep 2021 04:15:18 +0800 Subject: [PATCH] Allow environment syntax prefix per item within an array (#8257) * Updating environment syntax prefix to identify prefix per item * Updating environment syntax prefix documentation to show how to add prefix to array value * Add additional example Co-authored-by: Juan Carlos Blanco Delgado Co-authored-by: rijkvanzanten --- api/src/env.ts | 17 +++++++++++++++-- docs/reference/environment-variables.md | 12 ++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/api/src/env.ts b/api/src/env.ts index c6822fe823..99a6d01460 100644 --- a/api/src/env.ts +++ b/api/src/env.ts @@ -163,6 +163,15 @@ function getEnvVariableValue(variableValue: string, variableType: string) { return variableValue.split(`${variableType}:`)[1]; } +function getEnvironmentValueWithPrefix(envArray: Array): Array { + return envArray.map((item: string) => { + if (isEnvSyntaxPrefixPresent(item)) { + return getEnvironmentValueByType(item); + } + return item; + }); +} + function getEnvironmentValueByType(envVariableString: string) { const variableType = getVariableType(envVariableString); const envVariableValue = getEnvVariableValue(envVariableString, variableType); @@ -171,7 +180,7 @@ function getEnvironmentValueByType(envVariableString: string) { case 'number': return toNumber(envVariableValue); case 'array': - return toArray(envVariableValue); + return getEnvironmentValueWithPrefix(toArray(envVariableValue)); case 'regex': return new RegExp(envVariableValue); case 'string': @@ -181,6 +190,10 @@ function getEnvironmentValueByType(envVariableString: string) { } } +function isEnvSyntaxPrefixPresent(value: string): boolean { + return acceptedEnvTypes.some((envType) => value.includes(`${envType}:`)); +} + function processValues(env: Record) { env = clone(env); @@ -205,7 +218,7 @@ function processValues(env: Record) { // Convert values with a type prefix // (see https://docs.directus.io/reference/environment-variables/#environment-syntax-prefix) - if (typeof value === 'string' && acceptedEnvTypes.some((envType) => value.includes(`${envType}:`))) { + if (typeof value === 'string' && isEnvSyntaxPrefixPresent(value)) { env[key] = getEnvironmentValueByType(value); continue; } diff --git a/docs/reference/environment-variables.md b/docs/reference/environment-variables.md index e6e236b4d0..6c7f086aa9 100644 --- a/docs/reference/environment-variables.md +++ b/docs/reference/environment-variables.md @@ -429,12 +429,12 @@ Directus will attempt to automatically type cast environment variables based on ([see above](#type-casting-and-nesting)). If you have a specific need for a given type, you can tell Directus what type to use for the given value by prefixing the value with `{type}:`. The following types are available: -| Syntax Prefix | Example | Output | -| ------------- | ------------------------------------------------ | ------------------------------------------------ | -| `string` | `string:value` | `"value"` | -| `number` | `number:3306` | `3306` | -| `regex` | `regex:/\.example\.com$/` | `/\.example\.com$/` | -| `array` | `array:https://example.com,https://example2.com` | `["https://example.com","https://example2.com"]` | +| Syntax Prefix | Example | Output | +| ------------- | ----------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | +| `string` | `string:value` | `"value"` | +| `number` | `number:3306` | `3306` | +| `regex` | `regex:/\.example\.com$/` | `/\.example\.com$/` | +| `array` | `array:https://example.com,https://example2.com`
`array:string:https://example.com,regex:/\.example3\.com$/` | `["https://example.com", "https://example2.com"]`
`["https://example.com", "https://example2.com", /\.example3\.com$/]` | ## File Based Environment Variables (Docker Secrets)