@eclipse-ditto/ditto-javascript-client-node
Version:
Node.js(r) implementation of Eclipse Ditto JavaScript API.
222 lines • 7.86 kB
JavaScript
;
/*!
* 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.DefaultThingsHandle = void 0;
const things_model_1 = require("../../model/things.model");
const request_options_1 = require("../../options/request.options");
const ditto_actions_1 = require("../constants/ditto-actions");
const http_verb_1 = require("../constants/http-verb");
/**
* Handle to send Things requests.
*/
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: http_verb_1.HttpVerb.GET,
parser: things_model_1.Thing.fromObject,
id: thingId,
requestOptions: options
});
}
getAttributes(thingId, options) {
return this.requestFactory.fetchJsonRequest({
verb: http_verb_1.HttpVerb.GET,
parser: o => o,
id: thingId,
path: 'attributes',
requestOptions: options
});
}
getAttribute(thingId, attributePath, options) {
return this.requestFactory.fetchJsonRequest({
verb: http_verb_1.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: http_verb_1.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 = request_options_1.DefaultGetThingsOptions.getInstance().setThingIds(thingIds);
}
else {
actualOptions = options.setThingIds(thingIds);
}
return this.requestFactory.fetchJsonRequest({
verb: http_verb_1.HttpVerb.GET,
parser: o => Object(o).map((obj) => things_model_1.Thing.fromObject(obj)),
requestOptions: actualOptions
});
}
postThing(thingWithoutId) {
return this.requestFactory.fetchJsonRequest({
verb: http_verb_1.HttpVerb.POST,
parser: things_model_1.Thing.fromObject,
payload: thingWithoutId
});
}
putThing(thing, options) {
return this.changeThing(http_verb_1.HttpVerb.PUT, thing, options);
}
createThing(thing, options) {
return this.changeThing(ditto_actions_1.DittoAction.CREATE, thing, options);
}
patchThing(thing, options) {
return this.changeThing(http_verb_1.HttpVerb.PATCH, thing, request_options_1.MatchOptionsHelper.getWithMergeHeader(options));
}
putAttributes(thingId, attributes, options) {
return this.requestFactory.fetchPutRequest({
verb: http_verb_1.HttpVerb.PUT,
parser: o => o,
id: thingId,
path: 'attributes',
requestOptions: options,
payload: attributes
});
}
putAttribute(thingId, attributePath, attributeValue, options) {
return this.requestFactory.fetchPutRequest({
verb: http_verb_1.HttpVerb.PUT,
parser: o => o,
id: thingId,
path: `attributes/${attributePath}`,
requestOptions: options,
payload: attributeValue
});
}
patchAttributes(thingId, attributes, options) {
return this.requestFactory.fetchPutRequest({
verb: http_verb_1.HttpVerb.PATCH,
parser: o => o,
id: thingId,
path: 'attributes',
requestOptions: request_options_1.MatchOptionsHelper.getWithMergeHeader(options),
payload: attributes
});
}
patchAttribute(thingId, attributePath, attributeValue, options) {
return this.requestFactory.fetchPutRequest({
verb: http_verb_1.HttpVerb.PATCH,
parser: o => o,
id: thingId,
path: `attributes/${attributePath}`,
requestOptions: request_options_1.MatchOptionsHelper.getWithMergeHeader(options),
payload: attributeValue
});
}
putPolicyId(thingId, policyId, options) {
return this.requestFactory.fetchPutRequest({
verb: http_verb_1.HttpVerb.PUT,
parser: String,
id: thingId,
path: 'policyId',
requestOptions: options,
payload: policyId
});
}
patchPolicyId(thingId, policyId, options) {
return this.requestFactory.fetchPutRequest({
verb: http_verb_1.HttpVerb.PATCH,
parser: String,
id: thingId,
path: 'policyId',
requestOptions: request_options_1.MatchOptionsHelper.getWithMergeHeader(options),
payload: policyId
});
}
putDefinition(thingId, definition, options) {
return this.requestFactory.fetchPutRequest({
verb: http_verb_1.HttpVerb.PUT,
parser: String,
id: thingId,
path: 'definition',
requestOptions: options,
payload: definition
});
}
patchDefinition(thingId, definition, options) {
return this.requestFactory.fetchPutRequest({
verb: http_verb_1.HttpVerb.PATCH,
parser: String,
id: thingId,
path: 'definition',
requestOptions: request_options_1.MatchOptionsHelper.getWithMergeHeader(options),
payload: definition
});
}
changeThing(verb, thing, options) {
return this.requestFactory.fetchPutRequest({
verb,
parser: things_model_1.Thing.fromObject,
id: thing.thingId,
requestOptions: options,
payload: thing.toObject()
});
}
getStringAtPath(thingId, path, options) {
return this.requestFactory.fetchJsonRequest({
path,
verb: http_verb_1.HttpVerb.GET,
parser: String,
id: thingId,
requestOptions: options
});
}
deleteItemAtPath(thingId, path, options) {
return this.requestFactory.fetchRequest({
path,
verb: http_verb_1.HttpVerb.DELETE,
id: thingId,
requestOptions: options
});
}
}
exports.DefaultThingsHandle = DefaultThingsHandle;
//# sourceMappingURL=things.js.map