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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@absmartly/javascript-sdk",
"version": "1.13.4",
"version": "1.14.0-beta.0",
"description": "A/B Smartly Javascript SDK",
"homepage": "https://github.com/absmartly/javascript-sdk#README.md",
"bugs": "https://github.com/absmartly/javascript-sdk/issues",
Expand Down
71 changes: 31 additions & 40 deletions src/__tests__/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,22 +866,19 @@ describe("Client", () => {
})
.then((response) => {
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenLastCalledWith(
`${endpoint}/context?application=test_app&environment=test`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"X-API-Key": apiKey,
"X-Agent": "javascript-client",
"X-Environment": "test",
"X-Application": "test_app",
"X-Application-Version": 1000000,
},
keepalive: true,
signal: expect.any(Object),
}
);
expect(fetch).toHaveBeenLastCalledWith(`${endpoint}/context?application=test_app&environment=test`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"X-API-Key": apiKey,
"X-Agent": "javascript-client",
"X-Environment": "test",
"X-Application": "test_app",
"X-Application-Version": 1000000,
},
keepalive: true,
signal: expect.any(Object),
});

expect(response).toEqual(defaultMockResponse);

Expand All @@ -902,14 +899,11 @@ describe("Client", () => {
})
.then((response) => {
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenLastCalledWith(
`${endpoint}/context?application=website&environment=test`,
{
method: "GET",
keepalive: true,
signal: expect.any(Object),
}
);
expect(fetch).toHaveBeenLastCalledWith(`${endpoint}/context?application=website&environment=test`, {
method: "GET",
keepalive: true,
signal: expect.any(Object),
});

expect(response).toEqual(defaultMockResponse);

Expand All @@ -929,22 +923,19 @@ describe("Client", () => {
})
.then((response) => {
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenLastCalledWith(
`${endpoint}/context?application=test_app&environment=test`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"X-API-Key": apiKey,
"X-Agent": "javascript-client",
"X-Environment": "test",
"X-Application": "test_app",
"X-Application-Version": 1000000,
},
keepalive: true,
signal: expect.any(Object),
}
);
expect(fetch).toHaveBeenLastCalledWith(`${endpoint}/context?application=test_app&environment=test`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"X-API-Key": apiKey,
"X-Agent": "javascript-client",
"X-Environment": "test",
"X-Application": "test_app",
"X-Application-Version": 1000000,
},
keepalive: true,
signal: expect.any(Object),
});

expect(response).toEqual(defaultMockResponse);

Expand Down
10 changes: 6 additions & 4 deletions src/__tests__/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2400,10 +2400,12 @@ describe("Context", () => {
{
name: "US Internal",
type: "assign",
conditions: { and: [
{ eq: [{ var: "country" }, { value: "US" }] },
{ eq: [{ var: "user_type" }, { value: "internal" }] },
] },
conditions: {
and: [
{ eq: [{ var: "country" }, { value: "US" }] },
{ eq: [{ var: "user_type" }, { value: "internal" }] },
],
},
environments: [],
variant: 1,
},
Expand Down
30 changes: 20 additions & 10 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,13 @@ export default class Context {
}
}

private _computeRuleVariant(assignmentRules: string, variantCount: number, attrs: Record<string, unknown>): number | null {
private _computeRuleVariant(
assignmentRules: string,
variantCount: number,
attrs: Record<string, unknown>
): number | null {
const rawRuleVariant = this._audienceMatcher.evaluateRules(assignmentRules, this._environmentName, attrs);
return rawRuleVariant !== null && rawRuleVariant >= 0 && rawRuleVariant < variantCount
? rawRuleVariant
: null;
return rawRuleVariant !== null && rawRuleVariant >= 0 && rawRuleVariant < variantCount ? rawRuleVariant : null;
}

private _checkReady(expectNotFinalized?: boolean) {
Expand All @@ -463,7 +465,10 @@ export default class Context {
const app = client.getApplication();
attrs["application"] = app.name;
attrs["environment"] = client.getEnvironment() ?? null;
if ((typeof app.version === "string" && app.version.length > 0) || (typeof app.version === "number" && app.version > 0)) {
if (
(typeof app.version === "string" && app.version.length > 0) ||
(typeof app.version === "number" && app.version > 0)
) {
attrs["app_version"] = app.version;
}
}
Expand All @@ -485,9 +490,7 @@ export default class Context {
};

const audienceMatches = (experiment: ExperimentData, assignment: Assignment) => {
const ruleKey = experiment.assignmentRules
? `${experiment.assignmentRules}:${this._environmentName}`
: "";
const ruleKey = experiment.assignmentRules ? `${experiment.assignmentRules}:${this._environmentName}` : "";
const ruleKeyChanged = ruleKey !== (assignment.ruleKey ?? "");

if (ruleKeyChanged) {
Expand Down Expand Up @@ -584,7 +587,11 @@ export default class Context {
const attrs = this._getAttributesMap();

if (experiment.data.assignmentRules && experiment.data.assignmentRules.length > 0) {
ruleVariant = this._computeRuleVariant(experiment.data.assignmentRules, experiment.data.variants.length, attrs);
ruleVariant = this._computeRuleVariant(
experiment.data.assignmentRules,
experiment.data.variants.length,
attrs
);
}

assignment.ruleVariant = ruleVariant;
Expand Down Expand Up @@ -868,7 +875,10 @@ export default class Context {
{ name: "application", value: app.name, setAt: now },
{ name: "environment", value: client.getEnvironment() ?? null, setAt: now }
);
if ((typeof app.version === "string" && app.version.length > 0) || (typeof app.version === "number" && app.version > 0)) {
if (
(typeof app.version === "string" && app.version.length > 0) ||
(typeof app.version === "number" && app.version > 0)
) {
allAttributes.push({ name: "app_version", value: app.version, setAt: now });
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export class AudienceMatcher {
return null;
}

evaluateRules(assignmentRulesString: string, environmentName: string | null, vars: Record<string, unknown>): number | null {
evaluateRules(
assignmentRulesString: string,
environmentName: string | null,
vars: Record<string, unknown>
): number | null {
let assignmentRules;
try {
assignmentRules = JSON.parse(assignmentRulesString);
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const SDK_VERSION = "1.13.4";
export const SDK_VERSION = "1.14.0-beta.0";
Loading