Skip to content

Commit

Permalink
feat: proxy now respects incoming currentTime property (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre authored Jan 26, 2024
1 parent ae471e5 commit de8de52
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/create-context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
/* eslint-disable prefer-object-spread */
import { Context } from 'unleash-client';

function tryParseDate(dateString: string | undefined): Date | undefined {
if (!dateString) {
return undefined;
}
const parsedDate = new Date(dateString);
if (!isNaN(parsedDate.getTime())) {
return parsedDate;
} else {
return undefined;
}
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function createContext(value: any): Context {
const {
Expand All @@ -10,6 +22,7 @@ export function createContext(value: any): Context {
sessionId,
remoteAddress,
properties,
currentTime,
...rest
} = value;

Expand All @@ -20,6 +33,7 @@ export function createContext(value: any): Context {
userId,
sessionId,
remoteAddress,
currentTime: tryParseDate(currentTime),
properties: Object.assign({}, rest, properties),
};

Expand Down
16 changes: 16 additions & 0 deletions src/test/__snapshots__/openapi.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,14 @@ Object {
"type": "string",
},
},
Object {
"description": "The current time in ISO 8601 format, representing the time at which the feature toggle is being resolved",
"in": "query",
"name": "currentTime",
"schema": Object {
"type": "string",
},
},
Object {
"description": "Additional (custom) context fields",
"example": Object {
Expand Down Expand Up @@ -868,6 +876,14 @@ However, using this endpoint will increase the payload size transmitted to your
"type": "string",
},
},
Object {
"description": "The current time in ISO 8601 format, representing the time at which the feature toggle is being resolved",
"in": "query",
"name": "currentTime",
"schema": Object {
"type": "string",
},
},
Object {
"description": "Additional (custom) context fields",
"example": Object {
Expand Down
25 changes: 25 additions & 0 deletions src/test/create-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@ test('will not blow up if properties is an array', () => {
expect(context).not.toHaveProperty('region');
});

test('accepts current time as a context value', () => {
const targetDate = new Date('2024-01-01T00:00:00.000Z');
const context = createContext({
currentTime: targetDate.toISOString(),
});

expect(context.currentTime).toStrictEqual(targetDate);
});

test('invalid time strings fall back to undefined currentTime', () => {
const context = createContext({
currentTime: 'its cute that you think this will parse',
});

expect(context).not.toHaveProperty('currentTime');
});

test('missing time current time falls back to undefined currentTime', () => {
const context = createContext({
userId: '123',
});

expect(context).not.toHaveProperty('currentTime');
});

test.skip('will not blow up if userId is an array', () => {
const context = createContext({
userId: ['123'],
Expand Down
4 changes: 4 additions & 0 deletions src/unleash-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export default class UnleashProxy {
userId: "The current user's ID",
sessionId: "The current session's ID",
remoteAddress: "Your application's IP address",
currentTime:
'The current time in ISO 8601 format, representing the time at which the feature toggle is being resolved',
}),
...createDeepObjectRequestParameters({
properties: {
Expand Down Expand Up @@ -117,6 +119,8 @@ export default class UnleashProxy {
userId: "The current user's ID",
sessionId: "The current session's ID",
remoteAddress: "Your application's IP address",
currentTime:
'The current time in ISO 8601 format, representing the time at which the feature toggle is being resolved',
}),
...createDeepObjectRequestParameters({
properties: {
Expand Down

0 comments on commit de8de52

Please sign in to comment.