-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for files API endpoints (#184)
* Add support for files API endpoints * Apply suggestions from code review Co-authored-by: Aron Carroll <aron@replicate.com> * Remove prepareInputs in favor of FileEncodingStrategy This allows the user to determine if file uploads, falling back to base64 should be used or just sticking to one approach. The tests have been updated to validate the file upload payload and to ensure the url is correctly passed to the prediction create method. * Remove replicate.files API * Tell Biome to ignore .wrangler directory --------- Co-authored-by: Aron Carroll <aron@replicate.com>
- Loading branch information
Showing
8 changed files
with
244 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* Create a file | ||
* | ||
* @param {object} file - Required. The file object. | ||
* @param {object} metadata - Optional. User-provided metadata associated with the file. | ||
* @returns {Promise<object>} - Resolves with the file data | ||
*/ | ||
async function createFile(file, metadata = {}) { | ||
const form = new FormData(); | ||
|
||
let filename; | ||
let blob; | ||
if (file instanceof Blob) { | ||
filename = file.name || `blob_${Date.now()}`; | ||
blob = file; | ||
} else if (Buffer.isBuffer(file)) { | ||
filename = `buffer_${Date.now()}`; | ||
blob = new Blob(file, { type: "application/octet-stream" }); | ||
} else { | ||
throw new Error("Invalid file argument, must be a Blob, File or Buffer"); | ||
} | ||
|
||
form.append("content", blob, filename); | ||
form.append( | ||
"metadata", | ||
new Blob([JSON.stringify(metadata)], { type: "application/json" }) | ||
); | ||
|
||
const response = await this.request("/files", { | ||
method: "POST", | ||
data: form, | ||
headers: { | ||
"Content-Type": "multipart/form-data", | ||
}, | ||
}); | ||
|
||
return response.json(); | ||
} | ||
|
||
/** | ||
* List all files | ||
* | ||
* @returns {Promise<object>} - Resolves with the files data | ||
*/ | ||
async function listFiles() { | ||
const response = await this.request("/files", { | ||
method: "GET", | ||
}); | ||
|
||
return response.json(); | ||
} | ||
|
||
/** | ||
* Get a file | ||
* | ||
* @param {string} file_id - Required. The ID of the file. | ||
* @returns {Promise<object>} - Resolves with the file data | ||
*/ | ||
async function getFile(file_id) { | ||
const response = await this.request(`/files/${file_id}`, { | ||
method: "GET", | ||
}); | ||
|
||
return response.json(); | ||
} | ||
|
||
/** | ||
* Delete a file | ||
* | ||
* @param {string} file_id - Required. The ID of the file. | ||
* @returns {Promise<object>} - Resolves with the deletion confirmation | ||
*/ | ||
async function deleteFile(file_id) { | ||
const response = await this.request(`/files/${file_id}`, { | ||
method: "DELETE", | ||
}); | ||
|
||
return response.json(); | ||
} | ||
|
||
module.exports = { | ||
create: createFile, | ||
list: listFiles, | ||
get: getFile, | ||
delete: deleteFile, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters