|
1 | 1 | import Service from '@ember/service';
|
2 |
| -import algoliasearch from 'algoliasearch/dist/algoliasearchLite'; |
| 2 | +import algoliaSearch from 'algoliasearch/lite'; |
3 | 3 | import config from 'ember-api-docs/config/environment';
|
4 |
| -import { denodeify } from 'rsvp'; |
5 | 4 |
|
6 |
| -export default Service.extend({ |
7 |
| - _search(query, params, callback) { |
8 |
| - if (!callback) { |
9 |
| - callback = params; |
10 |
| - params = undefined; |
| 5 | +export default class AlgoliaService extends Service { |
| 6 | + constructor() { |
| 7 | + super(...arguments); |
| 8 | + this.indexes = {}; |
| 9 | + this.client = algoliaSearch(config.algolia.algoliaId, config.algolia.algoliaKey); |
| 10 | + } |
| 11 | + |
| 12 | + search(query, params = undefined) { |
| 13 | + if (!query) { |
| 14 | + return Promise.reject(`Could not search algolia for query "${query}"`); |
11 | 15 | }
|
12 |
| - if (query) { |
13 |
| - if (Array.isArray(query) && !params) { |
14 |
| - // if multiple indices |
15 |
| - this._client.search(query, callback); |
16 |
| - } else if (!params) { |
17 |
| - // if no params |
18 |
| - this.accessIndex(query.indexName).search(query.query, callback); |
19 |
| - } else { |
20 |
| - // if params and callback |
21 |
| - this.accessIndex(query.indexName).search(query.query, params, callback); |
22 |
| - } |
23 |
| - } else { |
24 |
| - callback(new Error(`Could not search algolia for query "${query}"`)); |
| 16 | + |
| 17 | + if (Array.isArray(query) && !params) { |
| 18 | + // if multiple indices |
| 19 | + return this.client.search(query); |
25 | 20 | }
|
26 |
| - }, |
27 | 21 |
|
28 |
| - accessIndex(IndexName) { |
29 |
| - if (!this._indices[IndexName]) { |
30 |
| - this._indices[IndexName] = this._client.initIndex(IndexName); |
| 22 | + if (!params) { |
| 23 | + return this.accessIndex(query.indexName).search(query.query); |
| 24 | + } |
| 25 | + |
| 26 | + return this.accessIndex(query.indexName).search(query.query, params); |
| 27 | + } |
| 28 | + |
| 29 | + accessIndex(indexName) { |
| 30 | + if (!this.indexes[indexName]) { |
| 31 | + this.indexes[indexName] = this.client.initIndex(indexName); |
31 | 32 | }
|
32 |
| - return this._indices[IndexName]; |
33 |
| - }, |
34 | 33 |
|
35 |
| - init() { |
36 |
| - this._super(...arguments); |
37 |
| - this._client = algoliasearch(config.algolia.algoliaId, config.algolia.algoliaKey); |
38 |
| - this._indices = {}; |
39 |
| - this.search = denodeify(this._search.bind(this)); |
| 34 | + return this.indexes[indexName]; |
40 | 35 | }
|
41 |
| -}); |
| 36 | +} |
0 commit comments