@workos-inc/node 12.2.0 → 12.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -185,6 +185,136 @@ polling and also emits `failed`. Attach an `error` listener or a `logger` to see
185
185
  failures; with neither, the client warns once and keeps polling, and warns again
186
186
  if a 401 stops it.
187
187
 
188
+ ## Feature flag rule management
189
+
190
+ Create an empty rule, then add memberships using the rule's ID:
191
+
192
+ ```ts
193
+ const rule = await workos.featureFlags.createFlagRule({
194
+ flagSlug: 'new-checkout',
195
+ targetType: 'organization',
196
+ value: true,
197
+ });
198
+
199
+ const membership = await workos.featureFlags.createFlagTarget({
200
+ ruleId: rule.id,
201
+ targetId: 'org_...',
202
+ });
203
+
204
+ await workos.featureFlags.deleteFlagTarget(membership.id);
205
+ ```
206
+
207
+ The `flagSlug` option is sent as `flag_slug` to the API for rule creation and
208
+ listing. Membership creation sends only `rule_id` and `target_id`; the rule
209
+ supplies the flag, target type, and served value.
210
+
211
+ Use `getFlagTarget(membership.id)` to read a target or combine filters when listing:
212
+
213
+ ```ts
214
+ const members = await workos.featureFlags.listFlagTargets({
215
+ ruleId: rule.id,
216
+ flagSlug: 'new-checkout',
217
+ targetType: 'organization',
218
+ order: 'asc',
219
+ });
220
+ const allMembers = await members.autoPagination();
221
+ ```
222
+
223
+ Target lists also accept `targetId`, `limit`, `before`, and `after`. The SDK
224
+ defaults target lists to descending creation order, so `after` advances through
225
+ the results. Rule lists always follow evaluation order.
226
+
227
+ Rules are appended in evaluation order. `targetType` can be `organization`,
228
+ `user`, or a registered custom target type. A duplicate rule with the same
229
+ target type and value returns a conflict; it does not reuse the existing rule.
230
+ Use `listFlagRules({ flagSlug: 'new-checkout' })` to find existing rules,
231
+ `getFlagRule(rule.id)` to inspect one, or `deleteFlagRule(rule.id)` to delete a
232
+ rule and its memberships. Rule lists support `limit`, `before`, `after`, and
233
+ `autoPagination()` and always follow the server's evaluation order.
234
+
235
+ To create a rule and its initial members together:
236
+
237
+ ```ts
238
+ import { CreateRuleWithTargetsError } from '@workos-inc/node';
239
+
240
+ try {
241
+ const { rule, targets } = await workos.featureFlags.createRuleWithTargets({
242
+ flagSlug: 'new-checkout',
243
+ targetType: 'organization',
244
+ value: true,
245
+ targetIds: ['org_first', 'org_second'],
246
+ });
247
+ } catch (error) {
248
+ if (error instanceof CreateRuleWithTargetsError) {
249
+ // Save these IDs to reconcile or resume membership creation.
250
+ console.error(
251
+ error.rule.id,
252
+ error.targets,
253
+ error.failedTargetId,
254
+ error.cause,
255
+ );
256
+ }
257
+ throw error;
258
+ }
259
+ ```
260
+
261
+ This helper performs separate requests in order and stops at the first failure.
262
+ It does not roll back: the rule and confirmed memberships remain. A rule
263
+ creation error is returned unchanged; a membership error includes the created
264
+ rule, confirmed memberships, failed target ID, and original error as `cause`.
265
+ After an ambiguous network failure, the failed membership may also exist on
266
+ the server. Reconcile it before retrying; do not retry the whole helper, which
267
+ would try to create the rule again. An empty `targetIds` list creates an empty
268
+ rule.
269
+
270
+ The legacy `addFlagTarget({ slug, targetId })` and
271
+ `removeFlagTarget({ slug, targetId })` helpers remain supported on their alias
272
+ routes. Their deprecation annotations guide new integrations toward explicit
273
+ rules; existing integrations do not need to change.
274
+
275
+ `getFlagTarget()` and `listFlagTargets()` return `FlagTargetResource`: a
276
+ `FlagTarget` with `ruleId`, or a `LegacyFlagTarget` with `value` and
277
+ `valueType` while the environment still uses the legacy target contract. Narrow
278
+ using `'ruleId' in target` before accessing the contract-specific fields.
279
+
280
+ ## Feature flag events
281
+
282
+ The Events API and `webhooks.constructEvent` support the namespaced feature flag
283
+ lifecycle events:
284
+
285
+ - `feature_flags.flag.created`, `feature_flags.flag.updated`, `feature_flags.flag.deleted`
286
+ - `feature_flags.flag_rule.created`, `feature_flags.flag_rule.updated`, `feature_flags.flag_rule.deleted`
287
+ - `feature_flags.flag_target.created`, `feature_flags.flag_target.deleted`
288
+
289
+ Use these names in `events.listEvents({ events: [...] })`. The returned `Event`
290
+ union narrows by `event`; event data and context use camelCase fields.
291
+
292
+ ```ts
293
+ const { data: events } = await workos.events.listEvents({
294
+ events: [
295
+ 'feature_flags.flag_target.created',
296
+ 'feature_flags.flag_rule.updated',
297
+ ],
298
+ });
299
+
300
+ for (const event of events) {
301
+ if (event.event === 'feature_flags.flag_target.created') {
302
+ if ('ruleId' in event.data) {
303
+ console.log(event.data.ruleId, event.data.targetId);
304
+ } else {
305
+ console.log(event.data.valueType, event.data.value);
306
+ }
307
+ }
308
+ }
309
+ ```
310
+
311
+ Flag events include `environmentId`, nullable `description`, and a nullable
312
+ `owner` with `email`, `firstName`, and `lastName`. Updated flag context includes
313
+ optional `previousAttributes.data`, and updated rule context includes the rule's
314
+ previous `value` there. Rule context can omit `actor` for system changes. Target
315
+ event data is a `FlagTarget` with `ruleId`, or a `LegacyFlagTarget` with `value`
316
+ and `valueType`. The `flag.*` events keep their existing data shape.
317
+
188
318
  ## SDK Versioning
189
319
 
190
320
  For our SDKs WorkOS follows a Semantic Versioning ([SemVer](https://semver.org/)) process where all releases will have a version X.Y.Z (like 1.0.0) pattern wherein Z would be a bug fix (e.g., 1.0.1), Y would be a minor release (1.1.0) and X would be a major release (2.0.0). We permit any breaking changes to only be released in major versions and strongly recommend reading changelogs before making any major version upgrades.