Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit f035139

Browse files
committed
feature(core): add to the Apisearch client an events endoint, this is because events are working with the same Query model
1 parent 202e555 commit f035139

File tree

8 files changed

+211
-35
lines changed

8 files changed

+211
-35
lines changed

dist/apisearch.js

Lines changed: 45 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.node.js

Lines changed: 45 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.node.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.node.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/get-last-events.html

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<html>
2+
<head>
3+
<title>Apisearch Javascript example</title>
4+
<script src="../dist/apisearch.js"></script>
5+
</head>
6+
<body>
7+
<!-- Content -->
8+
<h1>Last 10 events:</h1>
9+
<div id="result"></div>
10+
<!-- /Content -->
11+
12+
<!-- Underscore template -->
13+
<script type="text/template" id="template">
14+
Total events: <b><%= total_hits %></b><br>
15+
<% if (events.length !== 0) { %>
16+
<table>
17+
<tbody>
18+
<tr>
19+
<th scope="col">Name</th>
20+
<th scope="col">Occurred on</th>
21+
</tr>
22+
<% events.forEach(function(event) { %>
23+
<tr>
24+
<td><%= event.name %></td>
25+
<td><%= event.occurred_on %></td>
26+
</tr>
27+
<% }) %>
28+
</tbody>
29+
</table>
30+
<% } else { %>
31+
<div>No results found.</div>
32+
<% } %>
33+
</script>
34+
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
35+
<!-- /Underscore templating -->
36+
37+
<!-- Apisearch -->
38+
<script>
39+
(function() {
40+
var searchInput = document.getElementById('search'),
41+
resultBlock = document.getElementById('result'),
42+
compiled = _.template(document.getElementById('template').innerHTML)
43+
;
44+
45+
// 1. Create the client
46+
var api = apisearch({
47+
appId: '54725861',
48+
indexId: '66777162',
49+
token: '456d1875-b940-4e7d-bce2-c164e3737a16'
50+
});
51+
var query;
52+
53+
// 2.- compose query
54+
query = api.query.create('', 20);
55+
56+
// 3.- Execute search
57+
api.events(query, function (result, error) {
58+
// Handle error
59+
if (error) {
60+
console.log(error);
61+
return;
62+
}
63+
64+
console.log(result)
65+
66+
// Render result
67+
resultBlock.innerHTML = compiled({
68+
total_hits: result.total_hits,
69+
events: result.events || []
70+
});
71+
});
72+
})();
73+
</script>
74+
<!-- /Apisearch -->
75+
</body>
76+
</html>

src/Apisearch.js

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ class Apisearch {
4242
/**
4343
* HttpClient
4444
*/
45-
this.repository = new HttpClient(
45+
this.client = new HttpClient(
4646
inMemoryCache ? new MemoryCache() : false
4747
);
4848
}
4949

5050
/**
51-
* Search entry point
51+
* Search items
5252
*
5353
* @param query
5454
* @param callback
@@ -58,8 +58,44 @@ class Apisearch {
5858
let encodedQuery = encodeURIComponent(
5959
JSON.stringify(query)
6060
);
61+
let url = `${this.endpoint}/${this.apiVersion}?app_id=${this.appId}&index=${this.indexId}&token=${this.token}&query=${encodedQuery}`;
62+
63+
return this
64+
.fetch(url)
65+
.then(response => callback(response, null))
66+
.catch(error => callback(null, error))
67+
;
68+
}
69+
70+
/**
71+
* Search events
72+
*
73+
* @param query
74+
* @param callback
75+
* @returns {Promise}
76+
*/
77+
events(query, callback) {
78+
let encodedQuery = encodeURIComponent(
79+
JSON.stringify(query)
80+
);
81+
let url = `${this.endpoint}/${this.apiVersion}/events?app_id=${this.appId}&index=${this.indexId}&token=${this.token}&query=${encodedQuery}`;
82+
83+
return this
84+
.fetch(url)
85+
.then(response => callback(response, null))
86+
.catch(error => callback(null, error))
87+
;
88+
}
89+
90+
/**
91+
* Fetch data
92+
*
93+
* @param url
94+
* @returns {Promise}
95+
*/
96+
fetch(url) {
6197
let composedQuery = {
62-
url: `${this.endpoint}/${this.apiVersion}?app_id=${this.appId}&index=${this.indexId}&token=${this.token}&query=${encodedQuery}`,
98+
url: url,
6399
options: {
64100
timeout: this.timeout
65101
}
@@ -69,21 +105,13 @@ class Apisearch {
69105
* Abort any previous existing request
70106
*/
71107
if (this.overrideQueries) {
72-
this.repository.abort();
108+
this.client.abort();
73109
}
74110

75111
/**
76112
* Start new request
77113
*/
78-
return this.repository
79-
.query(composedQuery)
80-
.then(
81-
response => callback(response, null)
82-
)
83-
.catch(
84-
error => callback(null, error)
85-
)
86-
;
114+
return this.client.query(composedQuery)
87115
}
88116
}
89117

0 commit comments

Comments
 (0)