Skip to content

Instantly share code, notes, and snippets.

@S-codes14
Created July 23, 2022 02:47
Show Gist options
  • Save S-codes14/ab3542a3f2337447faa3e8197e871586 to your computer and use it in GitHub Desktop.
Save S-codes14/ab3542a3f2337447faa3e8197e871586 to your computer and use it in GitHub Desktop.
Coda template for a pack
import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();
pack.addNetworkDomain("codahosted.io");
pack.addFormula({
name: "FileSize",
description: "Gets the file size of a file attachment, in bytes.",
parameters: [
coda.makeParameter({
type: coda.ParameterType.File,
name: "file",
description:
"The file to operate on.",
}),
],
resultType: coda.ValueType.Number,
execute: async function ([fileUrl], context) {
// Fetch the file content.
let response = await context.fetcher.fetch({
method: "GET",
url: fileUrl,
isBinaryResponse: true, // Required when fetching binary content.
});
// The binary content of the response is returned as a Node.js Buffer.
// See: https://nodejs.org/api/buffer.html
let buffer = response.body as Buffer;
// Return the length, in bytes.
return buffer.length;
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment