Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

feat(search-client): Add support for Custom Search Clients #111

Merged
merged 16 commits into from
May 14, 2018

Conversation

francoischalifour
Copy link
Member

@francoischalifour francoischalifour commented May 3, 2018

This PR adds support for the searchClient parameter. This allows any Angular InstantSearch user to plug their own search client (including a custom Algolia backend).

Example

import * as algoliasearch from 'algoliasearch/lite';

// With the default Algolia search client
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  title = 'app';
  config = {
    indexName: 'concerts',
    searchClient: algoliasearch('appId', 'apiKey')
  };
}

// With a custom search client
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  title = 'app';
  config = {
    indexName: 'concerts',
    searchClient: {
      search(requests) {
        return fetchResultsFromRequests(requests);
      }
    }
  };
}

Related

@algobot
Copy link

algobot commented May 3, 2018

Deploy preview for angular-instantsearch ready!

Built with commit 512d6c7

https://deploy-preview-111--angular-instantsearch.netlify.com

@@ -28,6 +29,7 @@ export type InstantSearchConfig = {
appId: string,
apiKey: string
) => object;
searchClient?: object;
Copy link
Contributor

@samouss samouss May 3, 2018

Choose a reason for hiding this comment

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

Instead of rely on the object type we can create a new type that will better represent what we expect.

Copy link
Member Author

Choose a reason for hiding this comment

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

It wasn't done for searchParameters though, I was wondering if there was a reason, @iam4x?

Otherwise, I will add a more strict type for searchClient.

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, for the SearchParameters it's a bit more tedious because the data structure is way more larger. But we should add the correct types at some point.

Copy link
Contributor

Choose a reason for hiding this comment

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

searchParameters are what the Algolia API expect, it's hard to type since it can change at any time without our control.

Here you should create a type and use it 👍

Copy link
Member Author

@francoischalifour francoischalifour May 3, 2018

Choose a reason for hiding this comment

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

Makes sense. I'm working on the type according to the spec.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can import searchParameters from the typing of algoliasearch

@@ -60,10 +60,14 @@ export class AppComponent {}
> A hook that will be called each time a search needs to be done, with the helper as a parameter. It’s your responsibility to call `helper.search()`. This option allows you to avoid doing searches at page load for example.

`createAlgoliaClient?: (algoliasearch: Function, appId: string, apiKey: string) => CustomClient`
> _Deprecated in favor of `searchClient`._
Copy link
Contributor

Choose a reason for hiding this comment

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

We do not want to deprecate this yet, it is still used for the server side rendering strategy:

import {
  createSSRAlgoliaClient,
  parseServerRequest
} from "angular-instantsearch";

[...]

constructor(
  private httpClient: HttpClient,
  private transferState: TransferState,
  private injector: Injector,
  @Inject(PLATFORM_ID) private platformId: Object
) {
  const req = isPlatformServer(this.platformId)
    ? this.injector.get("request")
    : undefined;

  const searchParameters = parseServerRequest(req);

  this.instantSearchConfig = {
    searchParameters,
    appId: "latency",
    apiKey: "6be0576ff61c053d5f9a3225e2a90f76",
    indexName: "ikea",
    urlSync: true,
    createAlgoliaClient: createSSRAlgoliaClient({
      makeStateKey,
      HttpHeaders,
      transferState: this.transferState,
      httpClient: this.httpClient
    })
  };
}

If we want to get rid of createAlgoliaClient we have to support server side rendering as well with the new method.

I can help for this 👍

Copy link
Member Author

@francoischalifour francoischalifour May 3, 2018

Choose a reason for hiding this comment

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

I added createSSRSearchClient() and deprecated createSSRAlgoliaClient(). Hope this is how you envisioned it!

Copy link
Contributor

@iam4x iam4x left a comment

Choose a reason for hiding this comment

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

Thank you @francoischalifour

@iam4x iam4x merged commit 10b91dd into develop May 14, 2018
@iam4x iam4x deleted the feat/search-client branch May 14, 2018 06:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants