@multicloud/sls-aws
Version:
Amazon AWS specific middleware and components for Serverless @multicloud.
67 lines (66 loc) • 2.62 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const sls_core_1 = require("@multicloud/sls-core");
const inversify_1 = require("inversify");
/**
* Implementation of Cloud Response for AWS Lambda
*/
let AwsResponse = class AwsResponse {
/**
* Initialize new AWS Response, injecting Cloud Context
* @param context Current CloudContext
*/
constructor(context) {
/** The HTTP response status code */
this.status = 200;
/** Headers of HTTP Response */
this.headers = new sls_core_1.StringParams();
this.headers.set(sls_core_1.CloudProviderResponseHeader, sls_core_1.ProviderType.AWS);
this.callback = context.runtime.callback;
}
/**
* Send HTTP response via provided callback
* @param body Body of HTTP response
* @param status Status code of HTTP response
* @param callback Callback function to call with response
*/
send(body, status = 200) {
const responseBody = typeof (body) !== "string"
? JSON.stringify(body)
: body;
this.body = responseBody;
this.status = status;
if (!body) {
return;
}
const bodyType = body.constructor.name;
if (["Object", "Array"].includes(bodyType)) {
this.headers.set("Content-Type", "application/json");
}
if (["String"].includes(bodyType)) {
this.headers.set("Content-Type", "text/html");
}
}
flush() {
this.callback(null, {
headers: this.headers.toJSON(),
body: this.body,
statusCode: this.status || 200,
});
}
};
AwsResponse = __decorate([
inversify_1.injectable(),
__param(0, inversify_1.inject(sls_core_1.ComponentType.CloudContext))
], AwsResponse);
exports.AwsResponse = AwsResponse;