Skip to content

Params CompareExpression .thenElse does not support RegExp as value, therefore disallows the use of RegExp values for CORS options #1943

Description

@sceee

Related issues

#1787
#1406
#1903

Actually I am trying to do this #1903 (comment) - however it turned out that #1903 unfortunately does not support setting regex CORS options directly from the .env files.

[REQUIRED] Version info

node: v24.18.0

firebase-functions: v7.3.0

firebase-tools: v15.23.0

firebase-admin: v13.10.0

[REQUIRED] Test case

const environmentVariable_Some = defineBoolean('SOME')
const environmentVariable_Staging = defineBoolean('IS_STAGING')

const corsOptionsLocal = [/^http:\/\/localhost:808[0|1]$/]
const corsOptionsStaging = [/^https:\/\/(staging|otherstaging)\.example\.com$/]
const corsOptionsProduction = [/^https:\/\/(some|other)\.example\.com$/]

const corsOptions = environmentVariable_Some
  .equals(true)
  .thenElse(corsOptionsLocal, environmentVariable_Staging.equals(true).thenElse(corsOptionsStaging, corsOptionsProduction))

export const myFn = onCall(
  {
    cors: corsOptions
  },
  async () => {
    ...
  }

[REQUIRED] Steps to reproduce

  1. Run npx tsc

[REQUIRED] Expected behavior

I can set dynamic (RegExp-based) allowed CORS hosts dynamically depending on the environment.
The onCall cors option allows the following types:

(property) HttpsOptions.cors?: string | boolean | RegExp | Expression<string> | Expression<string[]> | (string | RegExp)[] | undefined

...so it should also be possible to create a Expression<RegExp> using the .equals(xx).thenElse(...) CompareExpression helper.

[REQUIRED] Actual behavior

Argument of type 'RegExp[]' is not assignable to parameter of type 'string | number | boolean | string[] | Expression<string | number | boolean | string[]>'.
  Type 'RegExp[]' is not assignable to type 'string[]'.
    Type 'RegExp' is not assignable to type 'string'.

Were you able to successfully deploy your functions?

No

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions