Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: encodeURIComponent filters #342

Merged
merged 5 commits into from
Dec 13, 2021
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
22 changes: 21 additions & 1 deletion lib/__tests__/_sendEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,27 @@ describe("sendEvents", () => {
expect(payload).toEqual({
events: [
expect.objectContaining({
filters: ["brand:Apple"]
filters: ["brand%3AApple"]
})
]
});
});

it("should uri-encodes filters", () => {
(analyticsInstance as any).sendEvents([
{
eventType: "click",
eventName: "my-event",
index: "my-index",
filters: ["brand:Cool Brand"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more interesting example could be filters: ["discount:20%"] because this would break with the current implementation, and is fixed with the changes.

}
]);
expect(XMLHttpRequest.send).toHaveBeenCalledTimes(1);
const payload = JSON.parse(XMLHttpRequest.send.mock.calls[0][0]);
expect(payload).toEqual({
events: [
expect.objectContaining({
filters: ["brand%3ACool%20Brand"]
})
]
});
Expand Down
18 changes: 13 additions & 5 deletions lib/_sendEvent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RequestFnType } from "./utils/request";
import { InsightsEvent } from "./types";
import { InsightsEvent } from './types'
import { isUndefined } from "./utils";

export function makeSendEvents(requestFn: RequestFnType) {
return function sendEvents(
Expand All @@ -14,10 +15,17 @@ export function makeSendEvents(requestFn: RequestFnType) {
);
}

const events: InsightsEvent[] = eventData.map(data => ({
...data,
userToken: data?.userToken ?? this._userToken
}))
const events: InsightsEvent[] = eventData.map(data => {
const {filters, ...rest} = data;
const payload: InsightsEvent = {
...rest,
userToken: data?.userToken ?? this._userToken
};
if (!isUndefined(filters)) {
payload.filters = filters.map(encodeURIComponent)
}
return payload;
})

return sendRequest(
requestFn,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"bundlesize": [
{
"path": "./dist/search-insights.min.js",
"maxSize": "2.4 kB"
"maxSize": "2.75 kB"
}
]
}