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

feat(insights): introduce insights middleware (1/4) #4446

Merged
merged 35 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3963956
feat: update userToken automatically
eunjae-lee Jul 15, 2020
4331d24
chore: fix bug where it gets token before rendering the custom widget
eunjae-lee Jul 15, 2020
77996c9
chore: add access modifier
eunjae-lee Jul 15, 2020
467a648
chore: use custom widget and helper
eunjae-lee Jul 16, 2020
ec3f7bb
Update src/lib/InstantSearch.ts
Jul 16, 2020
3ab96a2
chore: update token to helper directly
eunjae-lee Jul 16, 2020
020381e
chore: move setupUserTokenUpdater to start()
eunjae-lee Jul 16, 2020
a0cc96f
chore: move to middleware
eunjae-lee Jul 23, 2020
c642234
Update src/middleware/insights.ts
Jul 23, 2020
03e52c7
export middleware
eunjae-lee Jul 23, 2020
0ae09e2
fix lint error
eunjae-lee Jul 23, 2020
2417c50
export for umd build
eunjae-lee Jul 23, 2020
090fe28
Update src/middleware/insights.ts
Jul 23, 2020
c262700
Merge branch 'master' into feat/automatic-user-token
Jul 23, 2020
2a49909
update token automatically
eunjae-lee Jul 23, 2020
20254cf
chore: update error message
eunjae-lee Jul 24, 2020
10ce292
inline functions
eunjae-lee Jul 24, 2020
5a1fc34
initialize insightsClient earlier
eunjae-lee Jul 24, 2020
963d305
add warning message if userToken is set before creating the middleware
eunjae-lee Jul 27, 2020
daca533
accept `false` for `insightsClient`
eunjae-lee Jul 27, 2020
000d708
clean up types
eunjae-lee Jul 29, 2020
602037a
fix wrong import
eunjae-lee Jul 29, 2020
8fd4698
Update src/middleware/insights.ts
Aug 24, 2020
b0b7add
accept null as insightsClient
eunjae-lee Aug 24, 2020
e2a2743
Merge branch 'master' into feat/automatic-user-token
Aug 24, 2020
21faadb
Update src/types/insights.ts
Aug 24, 2020
7f49f25
add test for getAppIdAndApiKey
eunjae-lee Aug 24, 2020
94e394b
Update src/types/insights.ts
Aug 24, 2020
ad0dafb
bring back exports
eunjae-lee Aug 24, 2020
f34170d
rename middleware to middlewares
eunjae-lee Aug 24, 2020
ef95a87
Merge branch 'master' into feat/automatic-user-token
eunjae-lee Aug 27, 2020
183b1ca
chore: rename files for better alignment
eunjae-lee Aug 31, 2020
e7c7e58
chore: export all types related to insights middleware
eunjae-lee Aug 31, 2020
60af9e0
feat(insights): send events from hits and refinementList (2/4) (#4456)
Sep 2, 2020
5067ec7
Merge branch 'master' into feat/automatic-user-token
Sep 2, 2020
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
2 changes: 2 additions & 0 deletions src/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import version from './version';
import * as connectors from '../connectors/index';
import * as widgets from '../widgets/index';
import * as helpers from '../helpers/index';
import * as middleware from '../middleware/index';

import * as routers from './routers/index';
import * as stateMappings from './stateMappings/index';
Expand Down Expand Up @@ -39,5 +40,6 @@ instantsearch.createInfiniteHitsSessionStorageCache = createInfiniteHitsSessionS
instantsearch.highlight = helpers.highlight;
instantsearch.snippet = helpers.snippet;
instantsearch.insights = helpers.insights;
instantsearch.middleware = middleware;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

middleware vs middlewares

Copy link
Member

Choose a reason for hiding this comment

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

"middlewares" is not proper English.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://en.wiktionary.org/wiki/middleware
image

I don't think I've seen s at the end of middleware before, though.
So you agree it's natural to have middleware although we have routers, stateMappings, connectors, widgets, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it should be middlewares, at least for the object containing all possible middlewares. Interestingly my spell-checker only accepts singular "middleware" as correct, and not "middlewares"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

https://www.wordhippo.com/what-is/the-plural-of/middleware.html

While middleware is more common, it's also safe to use middlewares if we want to explicitly indicate it's a collection.

Copy link
Contributor Author

Choose a reason for hiding this comment

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


export default instantsearch;
1 change: 1 addition & 0 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export type Middleware = ({
}) => MiddlewareDefinition;

export { createRouter, RouterProps } from './createRouter';
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if anyone uses this, but this changes the API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added them back ad0dafb
but not like before because of this
(tl;dr -> rollup failed to resolve RouterProps type from ./createRouter. It didn't happen before because this was not exported at src/lib/main.ts, but not it is.)

export { createInsightsMiddleware } from './insights';
40 changes: 40 additions & 0 deletions src/middleware/insights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Middleware } from '.';
import { InsightsClient } from '../types';

export type InsightsProps = {
insightsClient: InsightsClient;
};

export type CreateInsightsMiddleware = (props: InsightsProps) => Middleware;

export const createInsightsMiddleware: CreateInsightsMiddleware = props => {
const { insightsClient } = props;
if (!insightsClient) {
eunjae-lee marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
'passing insightsClient to instantsearch is required for insightsMiddleware'
);
}
return ({ instantSearchInstance }) => {
eunjae-lee marked this conversation as resolved.
Show resolved Hide resolved
return {
onStateChange() {},
subscribe() {
instantSearchInstance.mainIndex
.getHelper()
.setQueryParameter('clickAnalytics', true);

insightsClient(
'onUserTokenChange',
userToken => {
instantSearchInstance.mainIndex
.getHelper()
.setQueryParameter('userToken', userToken);
},
{ immediate: true }
);
},
unsubscribe() {
insightsClient('onUserTokenChange', () => {});
eunjae-lee marked this conversation as resolved.
Show resolved Hide resolved
},
};
};
};
12 changes: 11 additions & 1 deletion src/types/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ export type InsightsClientPayload = {
positions?: number[];
};

export type InsightsClient = (
export type InsightsSendEvent = (
eunjae-lee marked this conversation as resolved.
Show resolved Hide resolved
method: InsightsClientMethod,
payload: InsightsClientPayload
) => void;

export type InsightsOnUserTokenChangeMethod = 'onUserTokenChange';

export type InsightsOnUserTokenChange = (
method: InsightsOnUserTokenChangeMethod,
eunjae-lee marked this conversation as resolved.
Show resolved Hide resolved
callback: (userToken: string) => void,
options?: { immediate?: boolean }
) => void;

export type InsightsClient = InsightsSendEvent & InsightsOnUserTokenChange;

export type InsightsClientWrapper = (
method: InsightsClientMethod,
payload: Partial<InsightsClientPayload>
Expand Down