UNPKG

@inkwell.ar/sdk

Version:

SDK for interacting with the Inkwell Blog CRUD AO process using aoconnect

599 lines 21.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InkwellBlogSDK = void 0; const aoconnect_1 = require("@permaweb/aoconnect"); const ao_deploy_1 = require("ao-deploy"); const validation_1 = require("../utils/validation"); class InkwellBlogSDK { constructor(config) { (0, validation_1.validateSDKConfig)(config); this.processId = config.processId; this.aoconnect = config.aoconnect || (0, aoconnect_1.connect)({ MODE: 'legacy' }); } /** * Get the appropriate signer for the wallet */ getSigner(wallet) { if (wallet) { return (0, aoconnect_1.createDataItemSigner)(wallet); } // Check for browser wallet if (typeof globalThis !== 'undefined' && globalThis.arweaveWallet) { return (0, aoconnect_1.createDataItemSigner)(globalThis.arweaveWallet); } throw new Error('No wallet provided and no browser wallet available. Please provide a wallet or connect a browser wallet.'); } /** * Get the result of a message using its message ID */ async getMessageResult(messageId) { try { const resultData = await (0, aoconnect_1.result)({ message: messageId, process: this.processId, }); return resultData; } catch (error) { throw new Error(`Failed to get message result: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Deploy a new Inkwell Blog process */ static async deploy(options = {}) { try { const defaultOptions = { name: options.name || 'inkwell-blog', contractPath: options.contractPath || './lua-process/inkwell_blog.lua', luaPath: options.luaPath || './lua-process/?.lua', tags: [ { name: 'App-Name', value: 'Inkwell-Blog' }, { name: 'App-Version', value: '1.0.0' }, ...(options.tags || []), ], retry: { count: options.retry?.count || 10, delay: options.retry?.delay || 3000, }, minify: options.minify !== false, onBoot: options.onBoot || false, silent: options.silent || false, forceSpawn: options.forceSpawn || false, }; const result = await (0, ao_deploy_1.deployContract)({ ...defaultOptions, wallet: options.wallet, }); return { processId: result.processId, messageId: result.messageId, }; } catch (error) { throw new Error(`Failed to deploy Inkwell Blog process: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Get blog information */ async getInfo() { try { const result = await this.aoconnect.dryrun({ process: this.processId, tags: [{ name: 'Action', value: 'Info' }], }); return this.parseInfoResponse(result); } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Get all posts from the blog */ async getAllPosts(options = {}) { try { const tags = [{ name: 'Action', value: 'Get-All-Posts' }]; if (options.ordered !== undefined) { tags.push({ name: 'Ordered', value: options.ordered.toString() }); } const result = await this.aoconnect.dryrun({ process: this.processId, tags, }); return this.parseDryrunResponse(result); } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Get a specific post by ID */ async getPost(options) { try { (0, validation_1.validatePostId)(options.id); const result = await this.aoconnect.dryrun({ process: this.processId, tags: [ { name: 'Action', value: 'Get-Post' }, { name: 'Id', value: options.id.toString() }, ], }); return this.parseDryrunResponse(result); } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Get user roles for the current wallet */ async getUserRoles(walletAddress) { try { const result = await this.aoconnect.dryrun({ process: this.processId, tags: [ { name: 'Action', value: 'Get-User-Roles' }, { name: 'User-Address', value: walletAddress }, ], }); return this.parseMessageResponse(result); } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Create a new post (Editor role required) */ async createPost(options) { try { (0, validation_1.validateCreatePostData)(options.data); const messageId = await this.aoconnect.message({ process: this.processId, tags: [{ name: 'Action', value: 'Create-Post' }], data: JSON.stringify(options.data), signer: this.getSigner(options.wallet), }); if (!messageId) { return { success: false, data: 'Create-Post message failed', }; } // Try to get the result of the message try { const resultData = await this.getMessageResult(messageId); return this.parseMessageResponse(resultData); } catch (resultError) { // If we can't get the result, return a success message with the message ID return { success: true, data: `Post created successfully. Message ID: ${messageId}`, }; } } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Update an existing post (Editor role required) */ async updatePost(options) { try { (0, validation_1.validatePostId)(options.id); (0, validation_1.validateUpdatePostData)(options.data); const messageId = await this.aoconnect.message({ process: this.processId, tags: [ { name: 'Action', value: 'Update-Post' }, { name: 'Id', value: options.id.toString() }, ], data: JSON.stringify(options.data), signer: this.getSigner(options.wallet), }); if (!messageId) { return { success: false, data: 'Update-Post message failed', }; } // Try to get the result of the message try { const resultData = await this.getMessageResult(messageId); return this.parseMessageResponse(resultData); } catch (resultError) { return { success: true, data: `Post updated successfully. Message ID: ${messageId}`, }; } } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Delete a post (Editor role required) */ async deletePost(options) { try { (0, validation_1.validatePostId)(options.id); const messageId = await this.aoconnect.message({ process: this.processId, tags: [ { name: 'Action', value: 'Delete-Post' }, { name: 'Id', value: options.id.toString() }, ], signer: this.getSigner(options.wallet), }); if (!messageId) { return { success: false, data: 'Delete-Post message failed', }; } // Try to get the result of the message try { const resultData = await this.getMessageResult(messageId); return this.parseMessageResponse(resultData); } catch (resultError) { return { success: true, data: `Post ${options.id} deleted successfully. Message ID: ${messageId}`, }; } } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Add editors to the blog (Admin role required) */ async addEditors(options) { try { (0, validation_1.validateRoleManagementOptions)(options); const messageId = await this.aoconnect.message({ process: this.processId, tags: [{ name: 'Action', value: 'Add-Editors' }], data: JSON.stringify({ accounts: options.accounts }), signer: this.getSigner(options.wallet), }); if (!messageId) { return { success: false, data: 'Add-Editors message failed', }; } // Try to get the result of the message try { const resultData = await this.getMessageResult(messageId); return this.parseMessageResponse(resultData); } catch (resultError) { return { success: true, data: `Editors ${options.accounts.join(', ')} added successfully. Message ID: ${messageId}`, }; } } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Remove editors from the blog (Admin role required) */ async removeEditors(options) { try { (0, validation_1.validateRoleManagementOptions)(options); const messageId = await this.aoconnect.message({ process: this.processId, tags: [{ name: 'Action', value: 'Remove-Editors' }], data: JSON.stringify({ accounts: options.accounts }), signer: this.getSigner(options.wallet), }); if (!messageId) { return { success: false, data: 'Remove-Editors message failed', }; } // Try to get the result of the message try { const resultData = await this.getMessageResult(messageId); return this.parseMessageResponse(resultData); } catch (resultError) { return { success: true, data: `Editors ${options.accounts.join(', ')} removed successfully. Message ID: ${messageId}`, }; } } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Add admins to the blog (Admin role required) */ async addAdmins(options) { try { (0, validation_1.validateRoleManagementOptions)(options); const messageId = await this.aoconnect.message({ process: this.processId, tags: [{ name: 'Action', value: 'Add-Admins' }], data: JSON.stringify({ accounts: options.accounts }), signer: this.getSigner(options.wallet), }); if (!messageId) { return { success: false, data: 'Add-Admins message failed', }; } // Try to get the result of the message try { const resultData = await this.getMessageResult(messageId); return this.parseMessageResponse(resultData); } catch (resultError) { return { success: true, data: `Admins ${options.accounts.join(', ')} added successfully. Message ID: ${messageId}`, }; } } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Remove admins from the blog (Admin role required) */ async removeAdmins(options) { try { (0, validation_1.validateRoleManagementOptions)(options); const messageId = await this.aoconnect.message({ process: this.processId, tags: [{ name: 'Action', value: 'Remove-Admins' }], data: JSON.stringify({ accounts: options.accounts }), signer: this.getSigner(options.wallet), }); if (!messageId) { return { success: false, data: 'Remove-Admins message failed', }; } // Try to get the result of the message try { const resultData = await this.getMessageResult(messageId); return this.parseMessageResponse(resultData); } catch (resultError) { return { success: true, data: `Admins ${options.accounts.join(', ')} removed successfully. Message ID: ${messageId}`, }; } } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Get all editors (Admin role required) */ async getEditors() { try { const result = await this.aoconnect.dryrun({ process: this.processId, tags: [{ name: 'Action', value: 'Get-Editors' }], }); return this.parseMessageResponse(result); } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Get all admins (Admin role required) */ async getAdmins() { try { const result = await this.aoconnect.dryrun({ process: this.processId, tags: [{ name: 'Action', value: 'Get-Admins' }], }); return this.parseMessageResponse(result); } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Set blog details (Admin role required) */ async setBlogDetails(options) { try { (0, validation_1.validateBlogDetailsData)(options.data); const messageId = await this.aoconnect.message({ process: this.processId, tags: [{ name: 'Action', value: 'Set-Blog-Details' }], data: JSON.stringify(options.data), signer: this.getSigner(options.wallet), }); if (!messageId) { return { success: false, data: 'Set-Blog-Details message failed', }; } // Try to get the result of the message try { const resultData = await this.getMessageResult(messageId); return this.parseDryrunResponse(resultData); } catch (resultError) { return { success: true, data: `Blog details updated successfully. Message ID: ${messageId}`, }; } } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Unknown error occurred', }; } } /** * Parse the response */ parseResponse(result, recursiveParse = false) { try { if (result && result.Messages && result.Messages.length > 0) { const message = result.Messages[0]; if (message.Data) { const parsed = JSON.parse(message.Data); if (recursiveParse && typeof parsed.data === 'string') { parsed.data = JSON.parse(parsed.data); } return { success: parsed.success, data: parsed.data || parsed, }; } } return { success: false, data: 'Invalid response format from process', }; } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Failed to parse response', }; } } /** * Parse the response from dryrun operations */ parseDryrunResponse(result) { return this.parseResponse(result); } /** * Parse the response from message result operations */ parseMessageResponse(result) { return this.parseResponse(result, true); } /** * Parse the Info response from the AO process */ parseInfoResponse(result) { try { if (result && result.Messages && result.Messages.length > 0) { const message = result.Messages[0]; // Info handler returns data in message tags and Data field const info = { name: message.Tags?.Name || '', author: message.Tags?.Author || '', blogTitle: message.Tags?.['Blog-Title'] || '', blogDescription: message.Tags?.['Blog-Description'] || '', blogLogo: message.Tags?.['Blog-Logo'] || '', details: { title: message.Tags?.['Blog-Title'] || '', description: message.Tags?.['Blog-Description'] || '', logo: message.Tags?.['Blog-Logo'] || '', }, }; // Also try to parse the Data field for additional details if (message.Data) { try { const parsedData = JSON.parse(message.Data); if (parsedData.success && parsedData.data) { info.details = { title: parsedData.data.title || info.details.title, description: parsedData.data.description || info.details.description, logo: parsedData.data.logo || info.details.logo, }; } } catch (parseError) { // If Data parsing fails, continue with tag-based info } } return { success: true, data: info, }; } return { success: false, data: 'Invalid response format from process', }; } catch (error) { return { success: false, data: error instanceof Error ? error.message : 'Failed to parse Info response', }; } } } exports.InkwellBlogSDK = InkwellBlogSDK; //# sourceMappingURL=blog-sdk.js.map