UNPKG

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

Version:

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

218 lines 7.3 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 { Thing } from '../../model/things.model'; import { DefaultGetThingsOptions, MatchOptionsHelper } from '../../options/request.options'; import { DittoAction } from '../constants/ditto-actions'; import { HttpVerb } from '../constants/http-verb'; /** * Handle to send Things requests. */ export class DefaultThingsHandle { constructor(requestFactory) { this.requestFactory = requestFactory; } /** * returns an instance of DefaultThingsHandle using the provided RequestSender. * * @param builder - The builder for the RequestSender to work with. * @returns The DefaultThingsHandle */ static getInstance(builder) { return new DefaultThingsHandle(builder.buildInstance('things')); } getThing(thingId, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: Thing.fromObject, id: thingId, requestOptions: options }); } getAttributes(thingId, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => o, id: thingId, path: 'attributes', requestOptions: options }); } getAttribute(thingId, attributePath, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => o, id: thingId, path: `attributes/${attributePath}`, requestOptions: options }); } getPolicyId(thingId, options) { return this.getStringAtPath(thingId, 'policyId', options); } getDefinition(thingId, options) { return this.getStringAtPath(thingId, 'definition', options); } deleteThing(thingId, options) { return this.requestFactory.fetchRequest({ verb: HttpVerb.DELETE, id: thingId, requestOptions: options }); } deleteAttributes(thingId, options) { return this.deleteItemAtPath(thingId, 'attributes', options); } deleteAttribute(thingId, attributePath, options) { return this.deleteItemAtPath(thingId, `attributes/${attributePath}`, options); } deleteDefinition(thingId, options) { return this.deleteItemAtPath(thingId, 'definition', options); } getThings(thingIds, options) { let actualOptions; if (options === undefined) { actualOptions = DefaultGetThingsOptions.getInstance().setThingIds(thingIds); } else { actualOptions = options.setThingIds(thingIds); } return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => Object(o).map((obj) => Thing.fromObject(obj)), requestOptions: actualOptions }); } postThing(thingWithoutId) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.POST, parser: Thing.fromObject, payload: thingWithoutId }); } putThing(thing, options) { return this.changeThing(HttpVerb.PUT, thing, options); } createThing(thing, options) { return this.changeThing(DittoAction.CREATE, thing, options); } patchThing(thing, options) { return this.changeThing(HttpVerb.PATCH, thing, MatchOptionsHelper.getWithMergeHeader(options)); } putAttributes(thingId, attributes, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: o => o, id: thingId, path: 'attributes', requestOptions: options, payload: attributes }); } putAttribute(thingId, attributePath, attributeValue, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: o => o, id: thingId, path: `attributes/${attributePath}`, requestOptions: options, payload: attributeValue }); } patchAttributes(thingId, attributes, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PATCH, parser: o => o, id: thingId, path: 'attributes', requestOptions: MatchOptionsHelper.getWithMergeHeader(options), payload: attributes }); } patchAttribute(thingId, attributePath, attributeValue, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PATCH, parser: o => o, id: thingId, path: `attributes/${attributePath}`, requestOptions: MatchOptionsHelper.getWithMergeHeader(options), payload: attributeValue }); } putPolicyId(thingId, policyId, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: String, id: thingId, path: 'policyId', requestOptions: options, payload: policyId }); } patchPolicyId(thingId, policyId, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PATCH, parser: String, id: thingId, path: 'policyId', requestOptions: MatchOptionsHelper.getWithMergeHeader(options), payload: policyId }); } putDefinition(thingId, definition, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: String, id: thingId, path: 'definition', requestOptions: options, payload: definition }); } patchDefinition(thingId, definition, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PATCH, parser: String, id: thingId, path: 'definition', requestOptions: MatchOptionsHelper.getWithMergeHeader(options), payload: definition }); } changeThing(verb, thing, options) { return this.requestFactory.fetchPutRequest({ verb, parser: Thing.fromObject, id: thing.thingId, requestOptions: options, payload: thing.toObject() }); } getStringAtPath(thingId, path, options) { return this.requestFactory.fetchJsonRequest({ path, verb: HttpVerb.GET, parser: String, id: thingId, requestOptions: options }); } deleteItemAtPath(thingId, path, options) { return this.requestFactory.fetchRequest({ path, verb: HttpVerb.DELETE, id: thingId, requestOptions: options }); } } //# sourceMappingURL=things.js.map