UNPKG

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

Version:

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

220 lines 7.84 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 { Feature, Features } from '../../model/things.model'; import { MatchOptionsHelper } from '../../options/request.options'; import { HttpVerb } from '../constants/http-verb'; /** * Handle to send Feature requests. */ export class DefaultFeaturesHandle { constructor(requestFactory, thingId) { this.requestFactory = requestFactory; this.thingId = thingId; } /** * returns an instance of FeaturesHandle using the provided RequestSender. * * @param builder - The for the RequestSender to work with. * @param thingId - The ThingId to use. * @returns The FeaturesHandle */ static getInstance(builder, thingId) { return new DefaultFeaturesHandle(builder.buildInstance('things'), thingId); } getFeatures(options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: Features.fromObject, id: this.thingId, path: 'features', requestOptions: options }); } getFeature(featureId, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => Feature.fromObject(o, featureId), id: this.thingId, path: `features/${featureId}`, requestOptions: options }); } getDefinition(featureId, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => Object(o).map((obj) => String(obj)), id: this.thingId, path: `features/${featureId}/definition`, requestOptions: options }); } getProperties(featureId, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => o, id: this.thingId, path: `features/${featureId}/properties`, requestOptions: options }); } getProperty(featureId, propertyPath, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => o, id: this.thingId, path: `features/${featureId}/properties/${propertyPath}`, requestOptions: options }); } deleteFeatures(options) { return this.requestFactory.fetchRequest({ verb: HttpVerb.DELETE, id: this.thingId, path: 'features', requestOptions: options }); } deleteFeature(featureId, options) { return this.requestFactory.fetchRequest({ verb: HttpVerb.DELETE, id: this.thingId, path: `features/${featureId}`, requestOptions: options }); } deleteDefinition(featureId, options) { return this.requestFactory.fetchRequest({ verb: HttpVerb.DELETE, id: this.thingId, path: `features/${featureId}/definition`, requestOptions: options }); } deleteProperties(featureId, options) { return this.requestFactory.fetchRequest({ verb: HttpVerb.DELETE, id: this.thingId, path: `features/${featureId}/properties`, requestOptions: options }); } deleteProperty(featureId, propertyPath, options) { return this.requestFactory.fetchRequest({ verb: HttpVerb.DELETE, id: this.thingId, path: `features/${featureId}/properties/${propertyPath}`, requestOptions: options }); } putFeatures(features, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: Features.fromObject, id: this.thingId, path: 'features', requestOptions: options, payload: Features.toObject(features) }); } putFeature(feature, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: o => Feature.fromObject(o, feature.id), id: this.thingId, path: `features/${feature.id}`, requestOptions: options, payload: feature.toObject() }); } patchFeatures(features, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PATCH, parser: Features.fromObject, id: this.thingId, path: 'features', requestOptions: MatchOptionsHelper.getWithMergeHeader(options), payload: Features.toObject(features) }); } patchFeature(feature, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PATCH, parser: o => Feature.fromObject(o, feature.id), id: this.thingId, path: `features/${feature.id}`, requestOptions: MatchOptionsHelper.getWithMergeHeader(options), payload: feature.toObject() }); } putDefinition(featureId, definition, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: o => o !== undefined ? Object.values(o).map((obj) => String(obj)) : [], id: this.thingId, path: `features/${featureId}/definition`, requestOptions: options, payload: definition }); // `[${String(definition.map(s => `"${s}"`))}]` } patchDefinition(featureId, definition, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PATCH, parser: o => o !== undefined ? Object.values(o).map((obj) => String(obj)) : [], id: this.thingId, path: `features/${featureId}/definition`, requestOptions: MatchOptionsHelper.getWithMergeHeader(options), payload: definition }); } putProperties(featureId, properties, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: o => o, id: this.thingId, path: `features/${featureId}/properties`, requestOptions: options, payload: properties }); } putProperty(featureId, propertyPath, property, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: o => o, id: this.thingId, path: `features/${featureId}/properties/${propertyPath}`, requestOptions: options, payload: property }); } patchProperties(featureId, properties, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PATCH, parser: o => o, id: this.thingId, path: `features/${featureId}/properties`, requestOptions: MatchOptionsHelper.getWithMergeHeader(options), payload: properties }); } patchProperty(featureId, propertyPath, property, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PATCH, parser: o => o, id: this.thingId, path: `features/${featureId}/properties/${propertyPath}`, requestOptions: MatchOptionsHelper.getWithMergeHeader(options), payload: property }); } } //# sourceMappingURL=features.js.map