UNPKG

@eclipse-ditto/ditto-javascript-client-dom

Version:

DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.

101 lines 3.7 kB
/*! * 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 */ import { SearchThingsResponse } from '../../model/response'; import { DefaultSearchOptions } from '../../options/request.options'; import { ContentType } from '../constants/content-type'; import { Header } from '../constants/header'; import { HttpVerb } from '../constants/http-verb'; /** * Handle to send Search requests. */ export 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: HttpVerb.GET, parser: 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: HttpVerb.POST, parser: 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: 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: HttpVerb.POST, parser: Number, path: 'count', payload: options === null || options === void 0 ? void 0 : options.getOptions(), requestOptions: this.getFormRequestOptions(options) }); } getFormRequestOptions(options) { const requestOptions = 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.CONTENT_TYPE))) { requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.addHeader(Header.CONTENT_TYPE, ContentType.FORM_URLENCODED); } return requestOptions; } } //# sourceMappingURL=search.js.map