@planet-a/affinity-node
Advanced tools
Comparing version 0.0.1-test.15 to 0.0.1-test.16
import * as dntShim from "../_dnt.shims.js"; | ||
import { assert } from '../deps/jsr.io/@std/assert/0.226.0/mod.js'; | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { defaultTransformers } from './axios_default_transformers.js'; | ||
@@ -116,15 +118,3 @@ import { createSearchIteratorFn } from './create_search_iterator_fn.js'; | ||
assert(files.length, 'At least one file must be provided'); | ||
if (files.length === 1) { | ||
// Append the file as 'file' if only one file is provided | ||
// it's a bit odd that the Affinity API expects the file to be sent in a different | ||
// parameter, but maybe there is an implementation detail that treats multiple files | ||
// differently to a single one, so we're complying with the API here | ||
const [file] = files; | ||
formData.append('file', file, file.name); | ||
} | ||
else { | ||
files.forEach((file) => { | ||
formData.append('files[]', file, file.name); | ||
}); | ||
} | ||
await Promise.all(files.map((file) => appendToFormData(formData, file))); | ||
if (params.person_id) { | ||
@@ -150,1 +140,29 @@ formData.append('person_id', params.person_id.toString()); | ||
} | ||
function isFile(file) { | ||
return file instanceof dntShim.File; | ||
} | ||
async function appendToFormData(formData, file) { | ||
if (typeof file === 'string') { | ||
formData.append('files[]', await openAsBlob(file), path.basename(file)); | ||
} | ||
else if (isFile(file)) { | ||
formData.append('files[]', file); | ||
} | ||
else { | ||
throw new Error('Unsupported file type'); | ||
} | ||
} | ||
// TODO(@joscha): replace with `import { openAsBlob } from "node:fs";` ASAP | ||
function openAsBlob(filePath) { | ||
return new Promise((resolve, reject) => { | ||
const fileStream = fs.createReadStream(filePath); | ||
const chunks = []; | ||
fileStream.on('data', (chunk) => { | ||
chunks.push(typeof chunk === 'string' ? chunk : chunk.buffer); | ||
}); | ||
fileStream.on('end', () => { | ||
resolve(new Blob(chunks)); | ||
}); | ||
fileStream.on('error', reject); | ||
}); | ||
} |
{ | ||
"name": "@planet-a/affinity-node", | ||
"version": "0.0.1-test.15", | ||
"version": "0.0.1-test.16", | ||
"description": "API wrapper for the affinity.co API", | ||
@@ -41,2 +41,5 @@ "keywords": [ | ||
}, | ||
"engines": { | ||
"node": ">=20" | ||
}, | ||
"dependencies": { | ||
@@ -43,0 +46,0 @@ "axios": "^1.7.2", |
@@ -54,4 +54,5 @@ /// <reference types="node" /> | ||
}>; | ||
export type SupportedFileType = dntShim.File | string; | ||
export type UploadEntityFileRequest = { | ||
files: dntShim.File[]; | ||
files: SupportedFileType[]; | ||
} & RequireOnlyOne<EntityRequestFilter, keyof EntityRequestFilter>; | ||
@@ -58,0 +59,0 @@ /** |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
245971
6743