Skip to main content
Best answer

Return a File object

  • July 25, 2023
  • 1 reply
  • 526 views

craibuc
Forum|alt.badge.img

I’m trying to return a file that can be used by other actions.

This code converts the base-64 string that’s returned by an API into a File:

const xml = require('pixl-xml');

const perform = async (z, bundle) => {

  const xml = 'XML HERE'
  var body = xml.stringify( 'Document', xml );

  const options = {
    url: 'https://www.domain.tld/get_file',
    method: 'POST',
    headers: {
      'Content-Type': 'application/xml',
    },
    body: body
  };

  return z.request(options).then((response) => {

    response.throwForStatus();
    response.data = xml.parse(response.content);

    if ( response.data.FILE ) {

      const buffer = Buffer.from(response.data.FILE`;
      const file = new File(buffer,'somefile.pdf', { type: 'application/octet-stream' });

      return {
        file: file
      }

    }

  });
};

Unit and integration tests work as expected, but I get an error when I try to use this in a Zap:

File is not defined What happened (You are seeing this because you are an admin): Starting POST request to https://www.domain.tld/get_file Received 200 code from https://www.domain.tld/get_file after 941ms Received content "[REDACTED]" File is not defined Console logs: Stack trace: ReferenceError: File is not defined at Object.<anonymous> (/var/task/creates/get_report.js:44:20) at bound (node:domain:433:15) at Object.runBound (node:domain:444:12) at Object.tryCatcher (/var/task/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/var/task/node_modules/bluebird/js/release/promise.js:547:31) at Promise._settlePromise (/var/task/node_modules/bluebird/js/release/promise.js:604:18) at Promise._settlePromise0 (/var/task/node_modules/bluebird/js/release/promise.js:649:10) at Promise._settlePromises (/var/task/node_modules/bluebird/js/release/promise.js:729:18) at _drainQueueStep (/var/task/node_modules/bluebird/js/release/async.js:93:12) at _drainQueue (/var/task/node_modules/bluebird/js/release/async.js:86:9) at Async._drainQueues (/var/task/node_modules/bluebird/js/release/async.js:102:5) at Immediate.Async.drainQueues [as _onImmediate] (/var/task/node_modules/bluebird/js/release/async.js:15:14) at processImmediate (node:internal/timers:466:21) at process.topLevelDomainCallback (node:domain:161:15) at process.callbackTrampoline (node:internal/async_hooks:128:24)

 

Is the File object not valid when the integration is pushed to the server?

Is there another way to do this?

Best answer by craibuc

Found a solution:

const response = await z.request(options);

response.data = xml.parse(response.content);

const base64EncodedFile = response.data.FILE;
const decodedFile = Buffer.from(base64EncodedFile, 'base64');
const filename = `${response.data.FILENO}.pdf`;
const contentType = 'application/pdf'

const url = await z.stashFile(decodedFile, decodedFile.length, filename, contentType);

return {
  url: url, 
  filetype: contentType, 
  filename: filename
}

At some point, I’ll refactor this to be a dehydrator.

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

craibuc
Forum|alt.badge.img
  • Author
  • Beginner
  • 8 replies
  • Answer
  • July 27, 2023

Found a solution:

const response = await z.request(options);

response.data = xml.parse(response.content);

const base64EncodedFile = response.data.FILE;
const decodedFile = Buffer.from(base64EncodedFile, 'base64');
const filename = `${response.data.FILENO}.pdf`;
const contentType = 'application/pdf'

const url = await z.stashFile(decodedFile, decodedFile.length, filename, contentType);

return {
  url: url, 
  filetype: contentType, 
  filename: filename
}

At some point, I’ll refactor this to be a dehydrator.