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

Allow intercepting evaluations #58

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Allow intercepting evaluations #58

wants to merge 1 commit into from

Conversation

dferber90
Copy link
Collaborator

This is an exploration of adding an intercept mechanism to flags. It is not guaranteed to be shipped.

Adds a concept called intercept would allow modifying or skipping the decide() function or adapter().decide() function call.

import { flag } from '@vercel/flags/next';

export const exampleFlag = flag<boolean>({
  key: 'example-flag',
  async intercept(options, next) {
    return next(options)
  },
  decide() {
    return false;
  },
});

Interception happens after overrides and after the identify call. This means modifications made within intercept will not be cached under the originally provided identify.

Usage examples

Add your own error handling

This is powerful in case you want full control over error handling and report errors to your own systems, even when the flag itself falls back to a defaultValue.

import { flag } from '@vercel/flags/next';

export const exampleFlag = flag<boolean>({
  key: 'example-flag',
  async intercept(options, next) {
    return next(options).catch(error => {
      /* handle errors here or re-throw to use the default error handling */
      reportError(error) // report error to your observability tooling
      throw error // re-throw error so the defaultValue can be applied
    })
  },
  defaultValue: false,
  decide() {/* skipped */},
});

Time flag evaluations

import { flag } from '@vercel/flags/next';

export const exampleFlag = flag<boolean>({
  key: 'example-flag',
  async intercept(options, next) {
    const before = Date.now()
    const value = await next(options)
    console.log(`evaluation took ${Date.now() - before}ms`);
    return value
  },
  decide() {/* skipped */},
});

Modify options

There isn't usually a use case for this, and you should rely on identify instead, as the entities determined by intercept will not become part of the cache key. The evaluation will instead be cached for the duration of the request under the entities returned by identify.

import { flag } from '@vercel/flags/next';

export const exampleFlag = flag<boolean>({
  key: 'example-flag',
  async intercept(options, next) {
    return next({ ...options, entities: {/* custom entities */} })
  },
  decide() {/* skipped */},
});

Copy link

vercel bot commented Feb 5, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
flags-sdk-snippets ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 5, 2025 0:51am
summer-sale ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 5, 2025 0:51am

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.

1 participant