@lightbend/akkaserverless-javascript-sdk
Version:
Akka Serverless JavaScript SDK
246 lines • 7.15 kB
JavaScript
"use strict";
/*
* Copyright 2021 Lightbend Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Failure = exports.noReply = exports.failure = exports.forward = exports.message = exports.Reply = exports.Effect = void 0;
const akkaserverless_1 = require("./akkaserverless");
// Note that there is a parallel reply.jsdoc that needs to be changed for API changes here to be visible
/**
* Side effect for a reply.
*/
class Effect {
/**
* @param method - the entity service method to invoke
* @param message - the message to send to that service
* @param synchronous - whether the effect should be execute synchronously or not, default is false
* @param metadata - metadata to send with the effect
*/
constructor(method, message, synchronous = false, metadata) {
this.method = method;
this.message = message;
this.synchronous = synchronous;
this.metadata = metadata;
}
}
exports.Effect = Effect;
/**
* A return type to allow returning forwards or failures, and attaching effects to messages.
*/
class Reply {
constructor(method, message, metadata, forward, failure, effects = []) {
this.method = method;
this.message = message;
this.metadata = metadata;
this.forward = forward;
this.failure = failure;
this.effects = effects;
}
/**
* @returns the protobuf method for a forwarding reply
*/
getMethod() {
return this.method;
}
/**
* Set the protobuf method for a forwarding reply.
*
* @param method - the protobuf method
* @returns the updated reply
*/
setMethod(method) {
this.method = method;
return this;
}
/**
* @returns the reply message
*/
getMessage() {
return this.message;
}
/**
* Set the message for this reply.
* @param message - the reply message
* @returns the updated reply
*/
setMessage(message) {
this.message = message;
return this;
}
/**
* @returns the metadata attached to the reply
*/
getMetadata() {
return this.metadata;
}
/**
* Attach metadata to this reply.
*
* @param metadata - metadata to send with the reply
* @returns the updated reply
*/
setMetadata(metadata) {
this.metadata = metadata;
return this;
}
/**
* @returns the forwarding reply
*/
getForward() {
return this.forward;
}
/**
* Make this a forwarding reply.
*
* @param forward - the forward reply
* @returns the updated reply
*/
setForward(forward) {
this.forward = forward;
return this;
}
/**
* @returns the failure
*/
getFailure() {
return this.failure;
}
/**
* Make this a failure reply.
*
* @param description - the failure description
* @param status - the status code to fail with, defaults to Unknown.
* @returns the updated reply
*/
setFailure(description, status) {
this.failure = new Failure(description, status);
return this;
}
/**
* @returns the side effects for this reply
*/
getEffects() {
return this.effects;
}
/**
* Attach the given effect to this reply.
*
* @param method - the entity service method to invoke
* @param message - the message to send to that service
* @param synchronous - whether the effect should be execute synchronously or not, default is false
* @param metadata - metadata to send with the effect
* @returns this reply after adding the effect
*/
addEffect(method, message, synchronous = false, metadata = undefined) {
this.addEffects([new Effect(method, message, synchronous, metadata)]);
return this;
}
/**
* Attach the given effects to this reply.
*
* @param effects - service calls to execute as side effects
* @returns this reply after adding the effects
*/
addEffects(effects) {
if (this.effects)
this.effects.push(...effects);
else
this.effects = effects;
return this;
}
/**
* Whether this reply is empty: does not have a message, forward, or failure.
*
* @returns whether the reply is empty
*/
isEmpty() {
return !this.message && !this.forward && !this.failure;
}
}
exports.Reply = Reply;
/**
* Create a message reply.
*
* @param message - the message to reply with
* @param metadata - optional metadata to pass with the reply
* @returns a message reply
*/
function message(message, metadata = undefined) {
const reply = new Reply().setMessage(message).setMetadata(metadata);
return reply;
}
exports.message = message;
/**
* Create a forward reply.
*
* @param method - the service call representing the forward
* @param message - the message to forward
* @param metadata - optional metadata to pass with the forwarded message
* @returns a forward reply
*/
function forward(method, message, metadata = undefined) {
const forward = new Reply()
.setMethod(method)
.setMessage(message)
.setMetadata(metadata);
const reply = new Reply().setForward(forward);
return reply;
}
exports.forward = forward;
/**
* Create a failure reply.
*
* @param description - description of the failure
* @param status - the GRPC status, defaults to Unknown
* @return a failure reply
*/
function failure(description, status) {
const reply = new Reply().setFailure(description, status);
return reply;
}
exports.failure = failure;
/**
* Create a reply that contains neither a message nor a forward nor a failure.
*
* This may be useful for emitting effects without sending a message.
*
* @returns an empty reply
*/
function noReply() {
return new Reply();
}
exports.noReply = noReply;
class Failure {
constructor(description, status) {
this.description = description;
this.status = status;
if (status !== undefined) {
if (status === akkaserverless_1.GrpcStatus.Ok) {
throw new Error('gRPC failure status code must not be OK');
}
if (status < 0 || status > 16) {
throw new Error('Invalid gRPC status code: ' + status);
}
}
}
getDescription() {
return this.description;
}
getStatus() {
return this.status;
}
}
exports.Failure = Failure;
//# sourceMappingURL=reply.js.map