UNPKG

1.52 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _search_string = Joi.string().min(1);
6
7// Initialize Endpoint
8module.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 * List Search Results
16 *
17 * GET /api/v2/search.json?query={search_string}
18 * https://developer.zendesk.com/rest_api/docs/support/search#list-search-results
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 * Show Results Count
37 *
38 * GET /api/v2/search/count.json?query={search_string}
39 * https://developer.zendesk.com/rest_api/docs/support/search#show-results-count
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};