UNPKG

@pocinnovation/alumi

Version:

Dynamic pulumi provider for Aleph

164 lines (163 loc) 7.36 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Post = void 0; const pulumi = __importStar(require("@pulumi/pulumi")); const client_1 = require("@aleph-sdk/client"); const utils_1 = require("./utils"); const propContent = 'content'; const propPostType = 'postType'; const propChannel = 'channel'; const propStorageEngine = 'storageEngine'; const propRef = 'ref'; const propAccountEnvName = 'accountEnvName'; const PostProvider = { diff(id, olds, news) { return __awaiter(this, void 0, void 0, function* () { const replaces = []; const changes = []; if (JSON.stringify(olds[propContent]) !== JSON.stringify(news[propContent])) { changes.push(propContent); } if (olds[propPostType] !== news[propPostType]) { replaces.push(propPostType); } if (olds[propChannel] !== news[propChannel]) { replaces.push(propChannel); } if (olds[propStorageEngine] !== news[propStorageEngine]) { replaces.push(propStorageEngine); } if (replaces.length > 0) { return { replaces: replaces }; } else if (changes.length > 0) { return { changes: true }; } else { return { changes: false }; } }); }, update(id, olds, news) { return __awaiter(this, void 0, void 0, function* () { const inputs = { content: news[propContent], postType: 'amend', channel: news[propChannel], accountEnvName: news[propAccountEnvName], storageEngine: news[propStorageEngine], ref: olds.item_hash, }; return yield this.create(inputs); }); }, delete(id, props) { return __awaiter(this, void 0, void 0, function* () { const account = yield (0, utils_1.getAccount)(props[propAccountEnvName]); const client = new client_1.AuthenticatedAlephHttpClient(account); yield client.forget({ channel: props[propChannel], hashes: [props.item_hash], sync: true, }); }); }, create(inputs) { return __awaiter(this, void 0, void 0, function* () { const account = yield (0, utils_1.getAccount)(inputs[propAccountEnvName]); const client = new client_1.AuthenticatedAlephHttpClient(account); const ref = inputs[propRef] == '' ? undefined : inputs[propRef]; const res = yield client.createPost({ content: inputs[propContent], postType: inputs[propPostType], channel: inputs[propChannel], storageEngine: inputs[propStorageEngine], ref: ref, sync: true, }); const out = { // inputs content: inputs[propContent], postType: inputs[propPostType], storageEngine: inputs[propStorageEngine], accountEnvName: inputs[propAccountEnvName], // outputs chain: res.chain, sender: res.sender, type: `${res.type}`, channel: res.channel || '', signature: res.signature, time: res.time, item_type: res.item_type, item_hash: res.item_hash, // Created content_address: res.content.address, content_type: res.content.type, content_content: res.content.content !== undefined ? res.content.content : {}, content_time: res.content.time, aleph_explorer_url: (0, utils_1.getAlephExplorerUrl)(res.chain, res.sender, 'POST', res.item_hash), }; return { id: `${account.address}-${res.item_hash}`, outs: out, }; }); }, read(id, props) { return __awaiter(this, void 0, void 0, function* () { const account = yield (0, utils_1.getAccount)(props[propAccountEnvName]); const client = new client_1.AuthenticatedAlephHttpClient(account); const res = yield client.getPost({ types: `${props.postType}`, hashes: [props.item_hash], }); const out = Object.assign(Object.assign({}, props), { chain: res.chain, sender: res.sender, type: `${res.type}`, channel: res.channel || '', signature: res.signature, time: res.time, item_type: res.item_type, item_hash: res.item_hash, content_address: res.content.address, content_type: res.content.type, content_content: res.content.content, content_time: res.content.time, aleph_explorer_url: (0, utils_1.getAlephExplorerUrl)(res.chain, res.sender, 'POST', res.item_hash) }); return { id: id, outs: out, }; }); }, }; class Post extends pulumi.dynamic.Resource { constructor(name, props, opts) { super(PostProvider, name, Object.assign({ // outputs chain: undefined, sender: undefined, type: undefined, signature: undefined, time: undefined, item_type: undefined, item_hash: undefined, // Created content_address: undefined, content_type: undefined, content_content: undefined, content_time: undefined, aleph_explorer_url: undefined }, props), opts); } } exports.Post = Post;