var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var _a; import { S3Client, ListObjectsV2Command, GetObjectCommand, PutObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3'; const S3 = new S3Client({}); const bucketName = (_a = process.env.BUCKET) !== null && _a !== void 0 ? _a : "widget-default-bucket"; export const handler = function (event, context) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { try { const method = event.httpMethod; const widgetName = event.path.startsWith('/') ? event.path.substring(1) : event.path; if (method === "GET") { if (event.path === "/") { const data = yield S3.send(new ListObjectsV2Command({ Bucket: bucketName })); const body = { widgets: (_a = data === null || data === void 0 ? void 0 : data.Contents) === null || _a === void 0 ? void 0 : _a.map(function (e) { return e.Key; }) }; return { statusCode: 200, headers: {}, body: JSON.stringify(body) }; } if (widgetName) { const data = yield S3.send(new GetObjectCommand({ Bucket: bucketName, Key: widgetName })); const body = (_b = data === null || data === void 0 ? void 0 : data.Body) === null || _b === void 0 ? void 0 : _b.toString(); //'utf-8'); return { statusCode: 200, headers: {}, body: JSON.stringify(body) }; } } if (method === "POST") { if (!widgetName) { return { statusCode: 400, headers: {}, body: "Widget name missing" }; } const now = new Date(); const data = widgetName + " created: " + now; const base64data = Buffer.from(data, 'binary'); yield S3.send(new PutObjectCommand({ Bucket: bucketName, Key: widgetName, Body: base64data, ContentType: 'application/json' })); return { statusCode: 200, headers: {}, body: data }; } if (method === "DELETE") { if (!widgetName) { return { statusCode: 400, headers: {}, body: "Widget name missing" }; } yield S3.send(new DeleteObjectCommand({ Bucket: bucketName, Key: widgetName })); return { statusCode: 200, headers: {}, body: "Successfully deleted widget " + widgetName }; } return { statusCode: 400, headers: {}, body: "We only accept GET, POST, and DELETE, not " + method }; } catch (error) { var body = error.stack || JSON.stringify(error, null, 2); return { statusCode: 400, headers: {}, body: body }; } }); }; //# sourceMappingURL=widgets.js.map