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

Conversation

eunjae-lee
Copy link
Contributor

@eunjae-lee eunjae-lee commented Jul 15, 2020

Summary

This PR introduces insights middleware.
It is to automatically setup userToken across search calls and Insights api calls.
Once this middleware is added to instantSearchInstance, when user calls

window.aa('setUserToken', userToken);
// or 
insightsClient('setUserToken', userToken);

the middleware will automatically apply the userToken to the searchParameters. Then the next search call will include this userToken.

Stacked PRs

  1. introduce insights middleware ← current
  2. send events from hits and refinementList
  3. implement sendEvent in all the connectors
  4. add tests

examples

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 15, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 5067ec7:

Sandbox Source
InstantSearch.js Configuration
InstantSearch.js PR
InstantSearch.js PR
InstantSearch.js PR
InstantSearch.js PR

src/lib/InstantSearch.ts Outdated Show resolved Hide resolved
@eunjae-lee eunjae-lee force-pushed the feat/automatic-user-token branch from 053783e to 467a648 Compare July 16, 2020 08:54
src/lib/InstantSearch.ts Outdated Show resolved Hide resolved
src/lib/InstantSearch.ts Outdated Show resolved Hide resolved
Co-authored-by: Haroen Viaene <[email protected]>
@eunjae-lee eunjae-lee force-pushed the feat/automatic-user-token branch from 2386a91 to 3ab96a2 Compare July 16, 2020 10:13
@eunjae-lee
Copy link
Contributor Author

src/middleware/insights.ts Outdated Show resolved Hide resolved
src/lib/main.ts Outdated
@@ -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.

Co-authored-by: François Chalifour <[email protected]>
@eunjae-lee eunjae-lee changed the title feat: update userToken automatically feat: introduce insights middleware Jul 23, 2020
src/types/insights.ts Outdated Show resolved Hide resolved
src/middleware/insights.ts Outdated Show resolved Hide resolved
src/middleware/insights.ts Outdated Show resolved Hide resolved
@eunjae-lee eunjae-lee changed the title feat(insights): introduce insights middleware (1/3) feat(insights): introduce insights middleware (1/n) Jul 29, 2020
import { warning, noop } from '../lib/utils';

export type InsightsProps = {
insightsClient: false | InsightsClient;
Copy link
Member

Choose a reason for hiding this comment

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

Accepting false for the Insights client is confusing because it can't be true. Why don't we use null as we're doing with other options?

Copy link
Contributor

Choose a reason for hiding this comment

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

null is a fair option, I suggested false, since that's what I did with Vue: https://github.com/algolia/vue-instantsearch/blob/master/src/mixins/widget.js#L47-L55

EDIT: it's actually true there, but null would make more sense (maybe a next major)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, null sounds good.
b0b7add

src/middleware/insights.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

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

lgtm if open comments are resolved 👍

};
};

function getAppIdAndApiKey(searchClient) {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add a unit test here. Since we run the tests with algoliasearch v3 and v4 separately, you should be able to cover both branches.

Other case we can add a dev dependency for "algoliasearch-v3": "npm:algoliasearch@3" and import algoliasearch from 'algoliasearc-v3'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL: "npm:algoliasearch@3" syntax. Nice! 7f49f25

src/types/insights.ts Show resolved Hide resolved
src/types/insights.ts Outdated Show resolved Hide resolved
instantSearchInstance: InstantSearch,
}) => 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.)

@eunjae-lee eunjae-lee marked this pull request as ready for review August 24, 2020 14:16
Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

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

I think it's good now, assuming that nobody imported instantsearch.js/es/middleware, since that will be broken now. Since middleware is experimental, I think it's ok

@@ -0,0 +1,2 @@
export { createInsightsMiddleware } from './insights';
Copy link
Member

Choose a reason for hiding this comment

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

To be consistent with the file naming, we can rename the file createInsightsMiddleware (and perhaps createRouterMiddleware?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

183b1ca good call!

@@ -0,0 +1,2 @@
export { createInsightsMiddleware } from './insights';
export * from './createRouter';
Copy link
Member

Choose a reason for hiding this comment

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

We exports the types from the router middleware. Any opinions on aligning the two? (either exporting the types or not exporting the types)

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 think it's good to export all of them e7c7e58

src/types/index.ts Show resolved Hide resolved
@eunjae-lee eunjae-lee changed the title feat(insights): introduce insights middleware (1/n) feat(insights): introduce insights middleware (1/4) Sep 2, 2020
Eunjae Lee and others added 2 commits September 2, 2020 14:29
* chore: dummy commit

* add sendEvent method

* send event from refinementList

* accept custom payload at sendEvent

* delegate to onEvent or actually send event

* store _insightsMiddleware in instantSearchInstance

* add clickedFilters to InsightsClientMethod

* rename $$type of middlewares

* chore: remove !

* add sendEvent for hits

* update for consistency

* send view event when rendering hits

* expose bindEvent to templates

* handle insightsEvent from templates

* allow single hit for bind/sendEventForHits

* assign sendEventToInsights by the middleware

* Update src/lib/insights/listener.tsx

Co-authored-by: Haroen Viaene <[email protected]>

* require eventName for click and conversion in hits

* fix type errors

* fix test cases

* reuse $$type from connectors

* remove $$type from middleware

* Apply suggestions from code review

Co-authored-by: François Chalifour <[email protected]>

* fix: allow only one way for sending events

* feat(insights): implement sendEvent in all the connectors (3/4) (#4463)

* update infiniteHits

* modify createSendEventForHits to accept index instead of helper

* add sendEvent to connectAutocomplete

* add sendEvent to connectGeoSearch

* add sendEvent to connectHierarchicalMenu

* add sendEvent to connectBreadcrumb

* add sendEvent to connectMenu

* add sendEvent to connectNumericMenu

* extract getRefinedState in connectRange

* add sendEvent to connectRange

* add sendEvent to connectRatingMenu

* add sendEvent to connectToggleRefinement

* fix test error

* fix export

* fix type error

* use $$type instead of hard-code

* remove sendEvent from connectBreadcrumb

* moved createSendEvent to the top level in the files

* feat(insights): add tests (4/4) (#4464)

* add tests for userToken

* fix import paths

* fix test cases to accept null instead of false as insightsClient

* update bundlesize

* fix lint errors

* test sendEventToInsights

* use $$type instead of hard-code

* remove sendEvent from connectBreadcrumb

* moved createSendEvent to the top level in the files

* log insights event from storybook

* add test for connectors

* add tests for createSendEvent helpers

* add integration tests for hits and infinite-hits widgets with bindEvent in templates

* update bundlesize

* Update src/connectors/hits/__tests__/connectHits-test.ts

Co-authored-by: Haroen Viaene <[email protected]>

* use factory function instead of globally exposed variables

* clean up

* update comment

* use runAllMicroTasks instead of nextTick

* fix: type errors

* fix wrong import paths

* fix: pass insightsClient to onEvent as the second parameter

* update titles of describe blocks

Co-authored-by: Haroen Viaene <[email protected]>

Co-authored-by: Haroen Viaene <[email protected]>

Co-authored-by: Haroen Viaene <[email protected]>
Co-authored-by: François Chalifour <[email protected]>
@eunjae-lee eunjae-lee merged commit 9bc6359 into master Sep 2, 2020
@eunjae-lee eunjae-lee deleted the feat/automatic-user-token branch September 2, 2020 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants