Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions migrations/feature-flags_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- up
-- Supports the optional enabled filter used by GET /api/feature-flags.
-- Keeping this as a standalone migration makes the plan easy to verify with
-- EXPLAIN and lets operators roll it back independently.
CREATE INDEX IF NOT EXISTS feature_flags_enabled_idx
ON feature_flags (enabled);

-- down
DROP INDEX IF EXISTS feature_flags_enabled_idx;
9 changes: 6 additions & 3 deletions src/routes/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ featureFlagsRouter.use(
const featureFlagsQuerySchema = z.object({
cursor: z.string().optional(),
limit: z.coerce.number().int().positive().max(100).default(DEFAULT_PAGE_SIZE),
enabled: z.enum(["true", "false"]).transform((value) => value === "true").optional(),
});

featureFlagsRouter.get("/", async (req, res, next) => {
Expand All @@ -49,7 +50,7 @@ featureFlagsRouter.get("/", async (req, res, next) => {
throw parsed.error;
}

const { cursor, limit: rawLimit } = parsed.data;
const { cursor, limit: rawLimit, enabled } = parsed.data;
const limit = clampLimit(rawLimit, DEFAULT_PAGE_SIZE);

const flagsRecord = await abortableRace(
Expand All @@ -58,11 +59,13 @@ featureFlagsRouter.get("/", async (req, res, next) => {
);

// Convert the Record to a sorted array for pagination.
const flags = Object.entries(flagsRecord).map(([id, value]) => ({
const flags = Object.entries(flagsRecord)
.filter(([, value]) => enabled === undefined || value.enabled === enabled)
.map(([id, value]) => ({
id,
enabled: value.enabled,
variant: (value.metadata?.variant as string | undefined) ?? null,
}));
}));
const sorted = flags.sort((a, b) => b.id.localeCompare(a.id));

const page = paginate(
Expand Down
13 changes: 13 additions & 0 deletions tests/featureFlagsRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ describe('GET /feature-flags', () => {
expect(typeof res.body.next_cursor).toBe('string');
});

it('filters flags by enabled state before pagination', async () => {
const res = await request(app)
.get('/feature-flags')
.query({ enabled: true });

expect(res.status).toBe(200);
expect(res.body.items).toEqual([
{ id: 'NEW_MARKET_FLOW', enabled: true, variant: 'v2' },
]);
expect(res.body.total).toBe(1);
expect(res.body.next_cursor).toBeNull();
});

it('should return next_cursor as null on the last page', async () => {
const first = await request(app)
.get('/feature-flags')
Expand Down
Loading