@eclipse-ditto/ditto-javascript-client-node
Version:
Node.js(r) implementation of Eclipse Ditto JavaScript API.
144 lines • 6.38 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.DefaultHttpMessagesHandle = void 0;
/* tslint:disable:no-duplicate-string */
const request_options_1 = require("../../options/request.options");
const http_verb_1 = require("../constants/http-verb");
const header_1 = require("../constants/header");
const content_type_1 = require("../constants/content-type");
/**
* Handle to send Messages requests.
*/
class DefaultHttpMessagesHandle {
constructor(requestFactory) {
this.requestFactory = requestFactory;
}
/**
* returns an instance of HttpMessagesHandle based on the Context provided.
*
* @param factory - The factory to build with.
* @returns The HttpMessagesHandle
*/
static getInstance(factory) {
return new DefaultHttpMessagesHandle(factory.buildInstance('things'));
}
/**
* Initiates claiming the specified Thing.
*
* @param thingId - The ID of the Thing to claim.
* @param claimMessage - The message to be sent to the Thing for authentication.
* @param options - Options to use for the request.
* @returns A Promise for the server response
*/
claim(thingId, claimMessage, options) {
const messageOptions = options === undefined ? request_options_1.DefaultMessagesOptions.getInstance() : options;
messageOptions.addHeader(header_1.Header.CONTENT_TYPE, content_type_1.ContentType.JSON);
return this.requestFactory.fetchGenericJsonRequest({
verb: http_verb_1.HttpVerb.POST,
id: thingId,
path: 'inbox/claim',
requestOptions: messageOptions,
payload: claimMessage
});
}
/**
* Sends a message to a Thing.
*
* @param thingId - The ID of the Thing to message.
* @param messageSubject - The subject of the message to send.
* @param message - The message to send.
* @param contentType - The content type of the message.
* @param options?? - Options to use for the request.
* @returns A Promise for the server response
*/
messageToThing(thingId, messageSubject, message, contentType = content_type_1.ContentType.JSON, options) {
const messageOptions = options === undefined ? request_options_1.DefaultMessagesOptions.getInstance() : options;
messageOptions.addHeader(header_1.Header.CONTENT_TYPE, contentType);
return this.requestFactory.fetchGenericJsonRequest({
verb: http_verb_1.HttpVerb.POST,
id: thingId,
path: `inbox/messages/${messageSubject}`,
requestOptions: messageOptions,
payload: message
});
}
/**
* Sends a message from a Thing.
*
* @param thingId - The ID of the Thing to message from.
* @param messageSubject - The subject of the message to send.
* @param message - The message to send.
* @param contentType - The content type of the message.
* @param options?? - Options to use for the request.
* @returns A Promise for the server response
*/
messageFromThing(thingId, messageSubject, message, contentType = content_type_1.ContentType.JSON, options) {
const messageOptions = options === undefined ? request_options_1.DefaultMessagesOptions.getInstance() : options;
messageOptions.addHeader(header_1.Header.CONTENT_TYPE, contentType);
return this.requestFactory.fetchGenericJsonRequest({
verb: http_verb_1.HttpVerb.POST,
id: thingId,
path: `outbox/messages/${messageSubject}`,
requestOptions: messageOptions,
payload: message
});
}
/**
* Sends a message to a Feature.
*
* @param thingId - The ID of the Thing the Feature belongs to.
* @param featureId - The ID of the Feature to message.
* @param messageSubject - The subject of the message to send.
* @param message - The message to send.
* @param contentType - The content type of the message.
* @param options?? - Options to use for the request.
* @returns A Promise for the server response
*/
messageToFeature(thingId, featureId, messageSubject, message, contentType = content_type_1.ContentType.JSON, options) {
const messageOptions = options === undefined ? request_options_1.DefaultMessagesOptions.getInstance() : options;
messageOptions.addHeader(header_1.Header.CONTENT_TYPE, contentType);
return this.requestFactory.fetchGenericJsonRequest({
verb: http_verb_1.HttpVerb.POST,
id: thingId,
path: `features/${featureId}/inbox/messages/${messageSubject}`,
requestOptions: messageOptions,
payload: message
});
}
/**
* Sends a message from a Feature.
*
* @param thingId - The ID of the Thing the Feature belongs to.
* @param featureId - The ID of the Feature to message from.
* @param messageSubject - The subject of the message to send.
* @param message - The message to send.
* @param contentType - The content type of the message.
* @param options?? - Options to use for the request.
* @returns A Promise for the server response
*/
messageFromFeature(thingId, featureId, messageSubject, message, contentType = content_type_1.ContentType.JSON, options) {
const messageOptions = options === undefined ? request_options_1.DefaultMessagesOptions.getInstance() : options;
messageOptions.addHeader(header_1.Header.CONTENT_TYPE, contentType);
return this.requestFactory.fetchGenericJsonRequest({
verb: http_verb_1.HttpVerb.POST,
id: thingId,
path: `features/${featureId}/outbox/messages/${messageSubject}`,
requestOptions: messageOptions,
payload: message
});
}
}
exports.DefaultHttpMessagesHandle = DefaultHttpMessagesHandle;
//# sourceMappingURL=messages-http.js.map