Skip to content

Commit

Permalink
Update type and consider max. amount of flags
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyBitz committed May 22, 2024
1 parent 598a1de commit 469a282
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/web/src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function track(
name: string,
properties?: Record<string, AllowedPropertyValues>,
options?: {
flagKeys?: string[];
flags?: (string | Record<string, unknown>)[] | Record<string, unknown>;
}
): void {
if (!isBrowser()) {
Expand Down
20 changes: 16 additions & 4 deletions packages/web/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function isHeaders(headers?: AllowedHeaders): headers is Headers {
}

interface Options {
flagKeys?: string[];
flags?: (string | Record<string, unknown>)[] | Record<string, unknown>;
headers?: AllowedHeaders;
request?: { headers: AllowedHeaders };
}
Expand All @@ -28,6 +28,8 @@ interface RequestContext {
};
}

const MAX_FLAG_KEYS = 5;

const symbol = Symbol.for('@vercel/request-context');
const logPrefix = '[Vercel Web Analytics]';

Expand Down Expand Up @@ -99,11 +101,21 @@ export async function track(

const url = new URL(origin);

const flags = options?.flags;
const flagValuesToReport: Record<string, unknown> = {};
const allFlagValues = requestContext?.flags?.getValues();
const allFlagValues = requestContext?.flags?.getValues() ?? {};

if (flags) {
const list = Array.isArray(flags) ? flags : [flags];

const keys = list.flatMap((keyOrObject) => {
if (typeof keyOrObject === 'string') return keyOrObject;
Object.assign(allFlagValues, keyOrObject); // Add values to global list
return Object.keys(keyOrObject); // Retrieve the keys
});

if (options?.flagKeys && allFlagValues) {
options.flagKeys.forEach((key) => {
// Only report the first MAX_FLAG_KEYS flags
keys.slice(0, MAX_FLAG_KEYS).forEach((key) => {
flagValuesToReport[key] = allFlagValues[key];
});
}
Expand Down

0 comments on commit 469a282

Please sign in to comment.