UNPKG

@eclipse-ditto/ditto-javascript-client-node

Version:

Node.js(r) implementation of Eclipse Ditto JavaScript API.

105 lines 3.98 kB
"use strict"; /*! * Copyright (c) 2019 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0 * * SPDX-License-Identifier: EPL-2.0 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultSearchHandle = void 0; const response_1 = require("../../model/response"); const request_options_1 = require("../../options/request.options"); const content_type_1 = require("../constants/content-type"); const header_1 = require("../constants/header"); const http_verb_1 = require("../constants/http-verb"); /** * Handle to send Search requests. */ class DefaultSearchHandle { constructor(requestFactory) { this.requestFactory = requestFactory; } /** * returns an instance of SearchHandle using the provided RequestSender. * * @param builder - The builder for the RequestSender to work with. * @returns The SearchHandle */ static getInstance(builder) { return new DefaultSearchHandle(builder.buildInstance('search/things')); } /** * Searches for Things that match the restrictions set in options. * * @param options - Options to use for the search. * @returns The search result */ search(options) { return this.requestFactory.fetchJsonRequest({ verb: http_verb_1.HttpVerb.GET, parser: response_1.SearchThingsResponse.fromObject, requestOptions: options }); } /** * Searches for Things that match the restrictions set in options using a http post request. * * @param options - Options to use for the search. * @returns The search result */ postSearch(options) { return this.requestFactory.fetchFormRequest({ verb: http_verb_1.HttpVerb.POST, parser: response_1.SearchThingsResponse.fromObject, payload: options === null || options === void 0 ? void 0 : options.getOptions(), requestOptions: this.getFormRequestOptions(options) }); } /** * Counts the Things that match the restrictions set in options. * * @param options - Options to use for the search. * @returns The count */ count(options) { return this.requestFactory.fetchJsonRequest({ verb: http_verb_1.HttpVerb.GET, parser: Number, path: 'count', requestOptions: options }); } /** * Counts the Things that match the restrictions set in options using a http post request. * * @param options - Options to use for the search. * @returns The count */ postCount(options) { return this.requestFactory.fetchFormRequest({ verb: http_verb_1.HttpVerb.POST, parser: Number, path: 'count', payload: options === null || options === void 0 ? void 0 : options.getOptions(), requestOptions: this.getFormRequestOptions(options) }); } getFormRequestOptions(options) { const requestOptions = request_options_1.DefaultSearchOptions.getInstance(); options === null || options === void 0 ? void 0 : options.getHeaders().forEach((value, key) => { requestOptions.addHeader(key, value); }); if (!(requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.getHeaders().has(header_1.Header.CONTENT_TYPE))) { requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.addHeader(header_1.Header.CONTENT_TYPE, content_type_1.ContentType.FORM_URLENCODED); } return requestOptions; } } exports.DefaultSearchHandle = DefaultSearchHandle; //# sourceMappingURL=search.js.map