Skip to content
44 changes: 44 additions & 0 deletions contract-tests/TestHook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import got from 'got';

export default class TestHook {
constructor(name, endpoint, data) {
this._name = name;
this._endpoint = endpoint;
this._data = data;
}

async _safePost(body) {
try {
await got.post(this._endpoint, { json: body });
} catch {
// The test could move on before the post, so we are ignoring
// failed posts.
}
}

getMetadata() {
return {
name: 'LaunchDarkly Tracing Hook',
};
}

beforeEvaluation(hookContext, data) {
this._safePost({
evaluationSeriesContext: hookContext,
evaluationSeriesData: data,
stage: 'beforeEvaluation',
});
return { ...data, ...(this._data?.['beforeEvaluation'] || {}) };
}

afterEvaluation(hookContext, data, detail) {
this._safePost({
evaluationSeriesContext: hookContext,
evaluationSeriesData: data,
stage: 'afterEvaluation',
evaluationDetail: detail,
});

return { ...data, ...(this._data?.['afterEvaluation'] || {}) };
}
}
1 change: 1 addition & 0 deletions contract-tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ app.get('/', (req, res) => {
'polling-gzip',
'inline-context',
'anonymous-redaction',
'evaluation-hooks',
],
});
});
Expand Down
7 changes: 7 additions & 0 deletions contract-tests/sdkClientEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ld, {

import BigSegmentTestStore from './BigSegmentTestStore.js';
import { Log, sdkLogger } from './log.js';
import TestHook from './TestHook.js';

const badCommandError = new Error('unsupported command');
export { badCommandError };
Expand All @@ -19,6 +20,7 @@ export function makeSdkConfig(options, tag) {
logger: sdkLogger(tag),
diagnosticOptOut: true,
};

const maybeTime = (seconds) =>
seconds === undefined || seconds === null ? undefined : seconds / 1000;
if (options.streaming) {
Expand Down Expand Up @@ -60,6 +62,11 @@ export function makeSdkConfig(options, tag) {
: undefined,
};
}
if (options.hooks) {
cf.hooks = options.hooks.hooks.map(
(hook) => new TestHook(hook.name, hook.callbackUri, hook.data),
);
}
return cf;
}

Expand Down
Loading