|
| 1 | +--- |
| 2 | +pcx_content_type: reference |
| 3 | +title: Methods |
| 4 | +sidebar: |
| 5 | + order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +The Flagship binding provides the following methods for evaluating feature flags. All methods are asynchronous and return a `Promise`. Evaluation methods never throw — they always return a value, falling back to the `defaultValue` you provide on errors. |
| 9 | + |
| 10 | +Refer to the [types reference](/flagship/binding/types/) for the definitions of `FlagshipEvaluationContext` and `FlagshipEvaluationDetails`. |
| 11 | + |
| 12 | +## `get()` |
| 13 | + |
| 14 | +Returns the raw flag value without type checking. Use this method when the flag type is not known at compile time. |
| 15 | + |
| 16 | +```ts |
| 17 | +get(flagKey: string, defaultValue?: unknown, context?: FlagshipEvaluationContext): Promise<unknown> |
| 18 | +``` |
| 19 | + |
| 20 | +| Parameter | Type | Required | Description | |
| 21 | +| -------------- | --------------------------- | -------- | ------------------------------------------------------------------------- | |
| 22 | +| `flagKey` | `string` | Yes | The key of the flag to evaluate. | |
| 23 | +| `defaultValue` | `unknown` | No | The fallback value returned if evaluation fails or the flag is not found. | |
| 24 | +| `context` | `FlagshipEvaluationContext` | No | Key-value attributes for targeting rules. | |
| 25 | + |
| 26 | +```ts |
| 27 | +const value = await env.FLAGS.get("checkout-flow", "v1", { |
| 28 | + userId: "user-42", |
| 29 | +}); |
| 30 | +``` |
| 31 | + |
| 32 | +## `getBooleanValue()` |
| 33 | + |
| 34 | +Returns the flag value as a `boolean`. |
| 35 | + |
| 36 | +```ts |
| 37 | +getBooleanValue(flagKey: string, defaultValue: boolean, context?: FlagshipEvaluationContext): Promise<boolean> |
| 38 | +``` |
| 39 | + |
| 40 | +| Parameter | Type | Required | Description | |
| 41 | +| -------------- | --------------------------- | -------- | ------------------------------------------------------------------------- | |
| 42 | +| `flagKey` | `string` | Yes | The key of the flag to evaluate. | |
| 43 | +| `defaultValue` | `boolean` | Yes | The fallback value returned if evaluation fails or the flag is not found. | |
| 44 | +| `context` | `FlagshipEvaluationContext` | No | Key-value attributes for targeting rules. | |
| 45 | + |
| 46 | +```ts |
| 47 | +const enabled = await env.FLAGS.getBooleanValue("dark-mode", false, { |
| 48 | + userId: "user-42", |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +## `getStringValue()` |
| 53 | + |
| 54 | +Returns the flag value as a `string`. |
| 55 | + |
| 56 | +```ts |
| 57 | +getStringValue(flagKey: string, defaultValue: string, context?: FlagshipEvaluationContext): Promise<string> |
| 58 | +``` |
| 59 | + |
| 60 | +| Parameter | Type | Required | Description | |
| 61 | +| -------------- | --------------------------- | -------- | ------------------------------------------------------------------------- | |
| 62 | +| `flagKey` | `string` | Yes | The key of the flag to evaluate. | |
| 63 | +| `defaultValue` | `string` | Yes | The fallback value returned if evaluation fails or the flag is not found. | |
| 64 | +| `context` | `FlagshipEvaluationContext` | No | Key-value attributes for targeting rules. | |
| 65 | + |
| 66 | +```ts |
| 67 | +const variant = await env.FLAGS.getStringValue("checkout-flow", "v1", { |
| 68 | + userId: "user-42", |
| 69 | + country: "US", |
| 70 | +}); |
| 71 | +``` |
| 72 | + |
| 73 | +## `getNumberValue()` |
| 74 | + |
| 75 | +Returns the flag value as a `number`. |
| 76 | + |
| 77 | +```ts |
| 78 | +getNumberValue(flagKey: string, defaultValue: number, context?: FlagshipEvaluationContext): Promise<number> |
| 79 | +``` |
| 80 | + |
| 81 | +| Parameter | Type | Required | Description | |
| 82 | +| -------------- | --------------------------- | -------- | ------------------------------------------------------------------------- | |
| 83 | +| `flagKey` | `string` | Yes | The key of the flag to evaluate. | |
| 84 | +| `defaultValue` | `number` | Yes | The fallback value returned if evaluation fails or the flag is not found. | |
| 85 | +| `context` | `FlagshipEvaluationContext` | No | Key-value attributes for targeting rules. | |
| 86 | + |
| 87 | +```ts |
| 88 | +const maxRetries = await env.FLAGS.getNumberValue("max-retries", 3, { |
| 89 | + plan: "enterprise", |
| 90 | +}); |
| 91 | +``` |
| 92 | + |
| 93 | +## `getObjectValue()` |
| 94 | + |
| 95 | +Returns the flag value as a typed object. Use the generic parameter `T` to specify the expected shape. |
| 96 | + |
| 97 | +```ts |
| 98 | +getObjectValue<T extends object>(flagKey: string, defaultValue: T, context?: FlagshipEvaluationContext): Promise<T> |
| 99 | +``` |
| 100 | +
|
| 101 | +| Parameter | Type | Required | Description | |
| 102 | +| -------------- | --------------------------- | -------- | ------------------------------------------------------------------------- | |
| 103 | +| `flagKey` | `string` | Yes | The key of the flag to evaluate. | |
| 104 | +| `defaultValue` | `T` | Yes | The fallback value returned if evaluation fails or the flag is not found. | |
| 105 | +| `context` | `FlagshipEvaluationContext` | No | Key-value attributes for targeting rules. | |
| 106 | +
|
| 107 | +```ts |
| 108 | +interface ThemeConfig { |
| 109 | + primaryColor: string; |
| 110 | + fontSize: number; |
| 111 | +} |
| 112 | + |
| 113 | +const theme = await env.FLAGS.getObjectValue<ThemeConfig>( |
| 114 | + "theme-config", |
| 115 | + { primaryColor: "#000", fontSize: 14 }, |
| 116 | + { userId: "user-42" }, |
| 117 | +); |
| 118 | +``` |
| 119 | + |
| 120 | +## `getBooleanDetails()` |
| 121 | + |
| 122 | +Returns the flag value as a `boolean` with evaluation metadata. |
| 123 | + |
| 124 | +```ts |
| 125 | +getBooleanDetails(flagKey: string, defaultValue: boolean, context?: FlagshipEvaluationContext): Promise<FlagshipEvaluationDetails<boolean>> |
| 126 | +``` |
| 127 | + |
| 128 | +| Parameter | Type | Required | Description | |
| 129 | +| -------------- | --------------------------- | -------- | ------------------------------------------------------------------------- | |
| 130 | +| `flagKey` | `string` | Yes | The key of the flag to evaluate. | |
| 131 | +| `defaultValue` | `boolean` | Yes | The fallback value returned if evaluation fails or the flag is not found. | |
| 132 | +| `context` | `FlagshipEvaluationContext` | No | Key-value attributes for targeting rules. | |
| 133 | + |
| 134 | +```ts |
| 135 | +const details = await env.FLAGS.getBooleanDetails("dark-mode", false, { |
| 136 | + userId: "user-42", |
| 137 | +}); |
| 138 | +console.log(details.value); // true |
| 139 | +console.log(details.reason); // "TARGETING_MATCH" |
| 140 | +``` |
| 141 | + |
| 142 | +## `getStringDetails()` |
| 143 | + |
| 144 | +Returns the flag value as a `string` with evaluation metadata. |
| 145 | + |
| 146 | +```ts |
| 147 | +getStringDetails(flagKey: string, defaultValue: string, context?: FlagshipEvaluationContext): Promise<FlagshipEvaluationDetails<string>> |
| 148 | +``` |
| 149 | + |
| 150 | +| Parameter | Type | Required | Description | |
| 151 | +| -------------- | --------------------------- | -------- | ------------------------------------------------------------------------- | |
| 152 | +| `flagKey` | `string` | Yes | The key of the flag to evaluate. | |
| 153 | +| `defaultValue` | `string` | Yes | The fallback value returned if evaluation fails or the flag is not found. | |
| 154 | +| `context` | `FlagshipEvaluationContext` | No | Key-value attributes for targeting rules. | |
| 155 | + |
| 156 | +```ts |
| 157 | +const details = await env.FLAGS.getStringDetails("checkout-flow", "v1", { |
| 158 | + userId: "user-42", |
| 159 | +}); |
| 160 | +console.log(details.value); // "v2" |
| 161 | +console.log(details.variant); // "new" |
| 162 | +console.log(details.reason); // "TARGETING_MATCH" |
| 163 | +``` |
| 164 | + |
| 165 | +## `getNumberDetails()` |
| 166 | + |
| 167 | +Returns the flag value as a `number` with evaluation metadata. |
| 168 | + |
| 169 | +```ts |
| 170 | +getNumberDetails(flagKey: string, defaultValue: number, context?: FlagshipEvaluationContext): Promise<FlagshipEvaluationDetails<number>> |
| 171 | +``` |
| 172 | + |
| 173 | +| Parameter | Type | Required | Description | |
| 174 | +| -------------- | --------------------------- | -------- | ------------------------------------------------------------------------- | |
| 175 | +| `flagKey` | `string` | Yes | The key of the flag to evaluate. | |
| 176 | +| `defaultValue` | `number` | Yes | The fallback value returned if evaluation fails or the flag is not found. | |
| 177 | +| `context` | `FlagshipEvaluationContext` | No | Key-value attributes for targeting rules. | |
| 178 | + |
| 179 | +```ts |
| 180 | +const details = await env.FLAGS.getNumberDetails("max-retries", 3, { |
| 181 | + plan: "enterprise", |
| 182 | +}); |
| 183 | +console.log(details.value); // 5 |
| 184 | +console.log(details.reason); // "TARGETING_MATCH" |
| 185 | +``` |
| 186 | + |
| 187 | +## `getObjectDetails()` |
| 188 | + |
| 189 | +Returns the flag value as a typed object with evaluation metadata. Use the generic parameter `T` to specify the expected shape. |
| 190 | + |
| 191 | +```ts |
| 192 | +getObjectDetails<T extends object>(flagKey: string, defaultValue: T, context?: FlagshipEvaluationContext): Promise<FlagshipEvaluationDetails<T>> |
| 193 | +``` |
| 194 | +
|
| 195 | +| Parameter | Type | Required | Description | |
| 196 | +| -------------- | --------------------------- | -------- | ------------------------------------------------------------------------- | |
| 197 | +| `flagKey` | `string` | Yes | The key of the flag to evaluate. | |
| 198 | +| `defaultValue` | `T` | Yes | The fallback value returned if evaluation fails or the flag is not found. | |
| 199 | +| `context` | `FlagshipEvaluationContext` | No | Key-value attributes for targeting rules. | |
| 200 | +
|
| 201 | +```ts |
| 202 | +interface ThemeConfig { |
| 203 | + primaryColor: string; |
| 204 | + fontSize: number; |
| 205 | +} |
| 206 | + |
| 207 | +const details = await env.FLAGS.getObjectDetails<ThemeConfig>( |
| 208 | + "theme-config", |
| 209 | + { primaryColor: "#000", fontSize: 14 }, |
| 210 | + { userId: "user-42" }, |
| 211 | +); |
| 212 | +console.log(details.value); // { primaryColor: "#0051FF", fontSize: 16 } |
| 213 | +console.log(details.variant); // "brand-refresh" |
| 214 | +``` |
| 215 | + |
| 216 | +## Error handling |
| 217 | + |
| 218 | +Evaluation methods never throw. They always return a value. When an error occurs, the method returns the `defaultValue` you provided. Use the `*Details` variants to inspect what went wrong. |
| 219 | + |
| 220 | +### Type mismatch |
| 221 | + |
| 222 | +If you call a typed method on a flag with a different type (for example, `getBooleanValue` on a string flag), the method returns the default value. The `*Details` variants set `errorCode` to `"TYPE_MISMATCH"`. |
| 223 | + |
| 224 | +```ts |
| 225 | +// Flag "checkout-flow" is a string flag, but you call getBooleanDetails. |
| 226 | +const details = await env.FLAGS.getBooleanDetails("checkout-flow", false); |
| 227 | +console.log(details.value); // false (the default value) |
| 228 | +console.log(details.errorCode); // "TYPE_MISMATCH" |
| 229 | +``` |
| 230 | + |
| 231 | +### Evaluation failure |
| 232 | + |
| 233 | +If evaluation fails for any other reason (for example, a network error or missing flag), the method returns the default value. The `*Details` variants set `errorCode` to `"GENERAL"`. |
| 234 | + |
| 235 | +```ts |
| 236 | +const details = await env.FLAGS.getStringDetails( |
| 237 | + "nonexistent-flag", |
| 238 | + "fallback", |
| 239 | +); |
| 240 | +console.log(details.value); // "fallback" |
| 241 | +console.log(details.errorCode); // "GENERAL" |
| 242 | +``` |
| 243 | + |
| 244 | +## Parameters reference |
| 245 | + |
| 246 | +The following table summarizes the parameters shared across all evaluation methods. |
| 247 | + |
| 248 | +| Parameter | Type | Required | Description | |
| 249 | +| -------------- | --------------------------- | ------------------ | ----------------------------------------------------------------------------------------------- | |
| 250 | +| `flagKey` | `string` | Yes | The key of the flag to evaluate. | |
| 251 | +| `defaultValue` | varies | Yes (except `get`) | The fallback value returned if evaluation fails or the flag is not found. | |
| 252 | +| `context` | `FlagshipEvaluationContext` | No | Key-value attributes for targeting rules (for example, `{ userId: "user-42", country: "US" }`). | |
0 commit comments