type Asana = {
token: string;
};
type Base64 = string;
/**
* Upload an attachment
* Upload an attachment.
*/
export async function main(
auth: Asana,
opt_pretty: string | undefined,
opt_fields: string | undefined,
body: {
connect_to_app?: boolean;
file?: {
base64: Base64;
type:
| "image/png"
| "image/jpeg"
| "image/gif"
| "application/pdf"
| "appication/json"
| "text/csv"
| "text/plain"
| "audio/mpeg"
| "audio/wav"
| "video/mp4";
name: string;
};
name?: string;
parent?: string;
resource_subtype?:
| "asana"
| "dropbox"
| "gdrive"
| "onedrive"
| "box"
| "vimeo"
| "external";
url?: string;
[k: string]: unknown;
}
) {
const url = new URL(`https://app.asana.com/api/1.0/attachments`);
for (const [k, v] of [
["opt_pretty", opt_pretty],
["opt_fields", opt_fields],
]) {
if (v !== undefined && v !== "") {
url.searchParams.append(k, v);
}
}
const formData = new FormData();
for (const [k, v] of Object.entries(body)) {
if (v !== undefined && v !== "") {
if (["file"].includes(k)) {
const { base64, type, name } = v as {
base64: Base64;
type: string;
name: string;
};
formData.append(
k,
new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
type,
}),
name
);
} else {
formData.append(k, String(v));
}
}
}
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: "Bearer " + auth.token,
},
body: formData,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 261 days ago
type Asana = {
token: string;
};
/**
* Upload an attachment
* Upload an attachment.
*/
export async function main(
auth: Asana,
opt_pretty: string | undefined,
opt_fields: string | undefined
) {
const url = new URL(`https://app.asana.com/api/1.0/attachments`);
for (const [k, v] of [
["opt_pretty", opt_pretty],
["opt_fields", opt_fields],
]) {
if (v !== undefined && v !== "") {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: "Bearer " + auth.token,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 388 days ago