1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _search_string = Joi.string().min(1);
|
6 |
|
7 |
|
8 | module.exports = (options = {}) => {
|
9 | const { error } = validate(options);
|
10 | if (error) throw new Error(error.details[0].message);
|
11 |
|
12 | const { url, headers } = prepare(options);
|
13 |
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | return {
|
21 | list: (options = {}) => {
|
22 | const { error } = Joi.object({
|
23 | search_string: _search_string.required()
|
24 | }).validate(options);
|
25 | if (error) throw new Error(error.details[0].message);
|
26 |
|
27 | const { search_string } = options;
|
28 | return {
|
29 | method: 'GET',
|
30 | url: `${url}/api/v2/search.json?query=${search_string}`,
|
31 | headers
|
32 | };
|
33 | },
|
34 |
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | count: (options = {}) => {
|
42 | const { error } = Joi.object({
|
43 | search_string: _search_string.required()
|
44 | }).validate(options);
|
45 | if (error) throw new Error(error.details[0].message);
|
46 |
|
47 | const { search_string } = options;
|
48 | return {
|
49 | method: 'GET',
|
50 | url: `${url}/api/v2/search/count.json?query=${search_string}`,
|
51 | headers
|
52 | };
|
53 | }
|
54 | };
|
55 | };
|