UNPKG

liveperson-functions-cli

Version:
57 lines 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwiftFileReplacer = void 0; const httpClient_1 = require("../http-client/httpClient"); /** * Implementation a FileReplacer. Replaces files on Swift. */ class SwiftFileReplacer { constructor(replacementFile, { shouldReplace, authStrategy }, csdsClient) { this.replacementFile = replacementFile; this.csdsClient = csdsClient; this.shouldReplace = shouldReplace; this.authStrategy = authStrategy; } async replaceFiles(conversation) { if (!conversation) { throw new Error('Please provide conversation object.'); } const { conversationHistoryRecords = [] } = conversation; const transcript = conversationHistoryRecords .map((record) => record.messageRecords) .reduce((acc, messages) => acc.concat(messages), []); const paths = this.getFilePaths(transcript); await Promise.all(paths.map(this.replaceFile, this)); return paths; } getFilePaths(transcript) { return transcript .filter((m) => m.type === 'HOSTED_FILE') .map((m) => m.messageData.file.relativePath) .filter(this.shouldReplace); } async replaceFile(path) { try { const host = await this.getHost(); const authHeaders = await this.authStrategy.getAuthHeaders(host); await (0, httpClient_1.httpClient)(`https://${host}${path}`, { method: 'PUT', simple: true, resolveWithFullResponse: false, headers: { 'content-type': this.replacementFile.contentType, ...authHeaders, }, body: this.replacementFile.body, }); } catch (error) { throw new Error(`Unable to replace image ${path}: ${error.message}`); } } getHost() { return this.csdsClient.get('swift'); } } exports.SwiftFileReplacer = SwiftFileReplacer; //# sourceMappingURL=SwiftFileReplacer.js.map