@flags-sdk/bucket
Version:
Bucket.co provider for the Flags SDK
63 lines (60 loc) • 2.27 kB
text/typescript
import { Adapter, ProviderData } from 'flags';
import { ClientOptions, ContextWithTracking, Context, BucketClient } from '@bucketco/node-sdk';
export { Context } from '@bucketco/node-sdk';
type AdapterOptions = Pick<ContextWithTracking, 'enableTracking' | 'meta'>;
type AdapterResponse = {
featureIsEnabled: (options?: AdapterOptions) => Adapter<boolean, Context>;
/** The Bucket client instance used by the adapter. */
bucketClient: () => Promise<BucketClient>;
};
declare function createBucketAdapter(clientOptions: ClientOptions): AdapterResponse;
/**
* The default Bucket adapter.
*
* This is a convenience object that pre-initializes the Bucket SDK and provides
* the adapter function for usage with the Flags SDK.
*
* This is the recommended way to use the Bucket adapter.
*
* ```ts
* // flags.ts
* import { flag } from 'flags/next';
* import { bucketAdapter, type Context } from '@flags-sdk/bucket';
*
* const flag = flag<boolean, Context>({
* key: 'my-flag',
* defaultValue: false,
* identify: () => ({ key: "user-123" }),
* adapter: bucketAdapter.featureIsEnabled(),
* });
* ```
*/
declare const bucketAdapter: AdapterResponse;
/**
* Get the provider data for the Bucket adapter.
*
* This function is used the the [Flags API endpoint](https://vercel.com/docs/workflow-collaboration/feature-flags/implement-flags-in-toolbar#creating-the-flags-api-endpoint) to load and emit your Bucket data.
*
* ```ts
* // .well-known/vercel/flags/route.ts
* import { NextResponse, type NextRequest } from 'next/server';
* import { verifyAccess, type ApiData } from 'flags';
* import { bucketAdapter, getProviderData } from '@flags-sdk/bucket';
*
* export async function GET(request: NextRequest) {
* const access = await verifyAccess(request.headers.get('Authorization'));
* if (!access) return NextResponse.json(null, { status: 401 });
*
* return NextResponse.json<ApiData>(
* await getProviderData({ bucketClient: await bucketAdapter.bucketClient() }),
* );
* }
* ```
*/
declare function getProviderData({ bucketClient, }?: {
/**
* The BucketClient instance.
*/
bucketClient?: BucketClient;
}): Promise<ProviderData>;
export { bucketAdapter, createBucketAdapter, getProviderData };