Skip to content

Commit c18637e

Browse files
Code review resolutions
1 parent 5d5f643 commit c18637e

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

packages/optimizely-sdk/lib/plugins/odp/graphql_manager.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const EMPTY_JSON_RESPONSE = null;
3939
* Manager for communicating with the Optimizely Data Platform GraphQL endpoint
4040
*/
4141
export interface IGraphQLManager {
42-
fetchSegments(apiKey: string, apiHost: string, userKey: string, userValue: string, segmentsToCheck: string[]): Promise<string[]>;
42+
fetchSegments(apiKey: string, apiHost: string, userKey: string, userValue: string, segmentsToCheck: string[]): Promise<string[] | null>;
4343
}
4444

4545
/**
@@ -73,27 +73,34 @@ export class GraphqlManager implements IGraphQLManager {
7373
* @param userValue Associated value to query for the user key
7474
* @param segmentsToCheck Audience segments to check for experiment inclusion
7575
*/
76-
public async fetchSegments(apiKey: string, apiEndpoint: string, userKey: string, userValue: string, segmentsToCheck: string[]): Promise<string[]> {
77-
if (segmentsToCheck?.length === 0) {
78-
return EMPTY_SEGMENTS_COLLECTION;
76+
public async fetchSegments(apiKey: string, apiEndpoint: string, userKey: string, userValue: string, segmentsToCheck: string[]): Promise<string[] | null> {
77+
if (!apiEndpoint || !apiKey) {
78+
this._logger.log(LogLevel.ERROR, 'No ApiEndpoint or ApiKey set before attempting to fetch segments');
79+
return null;
7980
}
81+
8082
const parameters = new QuerySegmentsParameters({
8183
apiKey,
8284
apiEndpoint,
8385
userKey,
8486
userValue,
8587
segmentsToCheck,
8688
});
89+
8790
const segmentsResponse = await this._odpClient.querySegments(parameters);
8891
if (!segmentsResponse) {
8992
this._logger.log(LogLevel.ERROR, 'Audience segments fetch failed (network error)');
93+
return null;
94+
}
95+
96+
if (segmentsToCheck?.length === 0) {
9097
return EMPTY_SEGMENTS_COLLECTION;
9198
}
9299

93100
const parsedSegments = this.parseSegmentsResponseJson(segmentsResponse);
94101
if (!parsedSegments) {
95102
this._logger.log(LogLevel.ERROR, 'Audience segments fetch failed (decode error)');
96-
return EMPTY_SEGMENTS_COLLECTION;
103+
return null;
97104
}
98105

99106
if (parsedSegments.errors?.length > 0) {

packages/optimizely-sdk/lib/plugins/odp/odp_client.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ export class OdpClient implements IOdpClient {
6969
public async querySegments(parameters: QuerySegmentsParameters): Promise<string | null> {
7070
const { apiEndpoint, apiKey, httpVerb, userKey, userValue, segmentsToCheck } = parameters;
7171

72-
if (segmentsToCheck?.length === 0) {
73-
return '';
74-
}
75-
7672
if (!apiEndpoint || !apiKey) {
7773
this._logger.log(LogLevel.ERROR, 'No ApiHost or ApiKey set before querying segments');
7874
return null;
@@ -83,6 +79,10 @@ export class OdpClient implements IOdpClient {
8379
return null;
8480
}
8581

82+
if (segmentsToCheck?.length === 0) {
83+
return '';
84+
}
85+
8686
const method = httpVerb;
8787
const url = apiEndpoint;
8888
const headers = {
@@ -118,17 +118,17 @@ export class OdpClient implements IOdpClient {
118118
public async sendEvents(parameters: SendEventsParameters): Promise<number | null> {
119119
const { apiEndpoint, apiKey, httpVerb, events } = parameters;
120120

121-
if (events?.length === 0) {
121+
if (!apiEndpoint || !apiKey) {
122+
this._logger.log(LogLevel.ERROR, 'No ApiEndpoint or ApiKey set before attempting to send ODP events');
122123
return null;
123124
}
124125

125-
if (!apiEndpoint || !apiKey) {
126-
this._logger.log(LogLevel.ERROR, 'No ApiEndpoint or ApiKey set before attempting to send ODP events');
126+
if (events?.length === 0) {
127127
return null;
128128
}
129129

130130
const method = httpVerb;
131-
const url = apiEndpoint;
131+
const url = apiEndpoint ?? '';
132132
const headers = {
133133
'Content-Type': 'application/json',
134134
'x-api-key': apiKey,

packages/optimizely-sdk/tests/graphQLManager.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe('GraphQLManager', () => {
5151
resetCalls(mockOdpClient);
5252
});
5353

54+
5455
it('should parse a successful response', () => {
5556
const validJsonResponse = `{
5657
"data": {
@@ -187,7 +188,7 @@ describe('GraphQLManager', () => {
187188

188189
const segments = await manager.fetchSegments(VALID_ODP_PUBLIC_KEY, ODP_GRAPHQL_URL, FS_USER_ID, VALID_FS_USER_ID, SEGMENTS_TO_CHECK);
189190

190-
expect(segments).toHaveLength(0);
191+
expect(segments).toBeNull();
191192
verify(mockErrorHandler.handleError(anything())).never();
192193
verify(mockLogger.log(LogLevel.ERROR, 'Audience segments fetch failed (decode error)')).once();
193194
});
@@ -202,7 +203,7 @@ describe('GraphQLManager', () => {
202203

203204
const segments = await manager.fetchSegments(VALID_ODP_PUBLIC_KEY, ODP_GRAPHQL_URL, FS_USER_ID, VALID_FS_USER_ID, SEGMENTS_TO_CHECK);
204205

205-
expect(segments).toHaveLength(0);
206+
expect(segments).toBeNull();
206207
verify(mockErrorHandler.handleError(anything())).never();
207208
verify(mockLogger.log(anything(), anyString())).once();
208209
});
@@ -214,7 +215,7 @@ describe('GraphQLManager', () => {
214215

215216
const segments = await manager.fetchSegments(VALID_ODP_PUBLIC_KEY, ODP_GRAPHQL_URL, FS_USER_ID, VALID_FS_USER_ID, SEGMENTS_TO_CHECK);
216217

217-
expect(segments).toHaveLength(0);
218+
expect(segments).toBeNull();
218219
verify(mockErrorHandler.handleError(anything())).never();
219220
verify(mockLogger.log(LogLevel.ERROR, 'Audience segments fetch failed (decode error)')).once();
220221
});
@@ -225,7 +226,7 @@ describe('GraphQLManager', () => {
225226

226227
const segments = await manager.fetchSegments(VALID_ODP_PUBLIC_KEY, ODP_GRAPHQL_URL, FS_USER_ID, VALID_FS_USER_ID, SEGMENTS_TO_CHECK);
227228

228-
expect(segments).toHaveLength(0);
229+
expect(segments).toBeNull();
229230
verify(mockLogger.log(LogLevel.ERROR, 'Audience segments fetch failed (network error)')).once();
230231
});
231232
});

packages/optimizely-sdk/tests/odpSendEvents.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ describe('OdpClient Send Events', () => {
8080

8181
expect(statusReturned).toBeNull();
8282
verify(mockErrorHandler.handleError(anything())).never();
83-
verify(mockLogger.log(LogLevel.ERROR, 'No ApiEndpoint or ApiKey set before attempting to send ODP events')).once();
8483
});
8584

8685
it('should handle missing API Key', async () => {
87-
8886
const missingApiKey = new SendEventsParameters({
8987
apiKey: '',
9088
apiEndpoint: 'https://some.example.com/endpoint',
@@ -96,7 +94,6 @@ describe('OdpClient Send Events', () => {
9694

9795
expect(statusReturned).toBeNull();
9896
verify(mockErrorHandler.handleError(anything())).never();
99-
verify(mockLogger.log(LogLevel.ERROR, 'No ApiEndpoint or ApiKey set before attempting to send ODP events')).once();
10097
});
10198

10299
it('Browser: should send events successfully', async () => {

0 commit comments

Comments
 (0)