A TypeScript library for interacting with the ShareFile API. This package provides a simple and intuitive interface to perform operations on ShareFile items such as files and folders.
npm install @jc-rowland/sharefile-client-api
import ShareFileAPI from '@jc-rowland/sharefile-client-api';
// Create an API instance
const shareFileAPI = new ShareFileAPI({
subdomain: 'your-subdomain',
username: 'your-username',
password: 'your-password',
clientId: 'your-client-id',
clientSecret: 'your-client-secret'
});
// Authenticate
await shareFileAPI.connect();
// Now you're ready to use the API
- Easy authentication
- Retrieve items by ID or path
- Download files and folders
- Update item properties
- Move and rename items
- Upload files
- List folder contents
- Search for items
- Create and manage folders
- Handle file versions
The main entry point for interacting with the ShareFile API.
constructor(auth: SharefileApiAuth)
connect(): Promise<string>
getItem(idOrPath: string | ShareFileAPIModels.SpecialItemIDs): Promise<SharefileItem>
getFolderContents(folderId: string, queryParams?: Record<string, any>): Promise<SharefileItem[]>
createItem(parentId: string, itemType: string, itemData: Record<string, any>): Promise<SharefileItem>
deleteItem(itemId: string): Promise<void>
searchItems(query: string, searchParams?: Record<string, any>): Promise<SharefileSearchResults>
getHttpClient(): SharefileHTTP
Represents a file or folder in ShareFile.
download(redirect: boolean, includeAllVersions?: boolean, includeDeleted?: boolean): Promise<string | DownloadSpecification>
createFolder(folderName: string, description?: string, overwrite: boolean = false): Promise<SharefileItem>
updateItem(ops: SharefileNodeAPI.Items.UpdateItemOps_Body): Promise<SharefileItem>
rename(newValue: string): Promise<SharefileItem>
move(parentIdorPath: string): Promise<SharefileItem>
getParent(): Promise<SharefileItem>
children(includeDeleted = false): Promise<SharefileItem[]>
childBy(propName: keyof SharefileItem, propVal: any, includeDeleted = false): Promise<SharefileItem | undefined>
upload(contents: string | Buffer, filename: string): Promise<SharefileItem | undefined>
getStream(includeDeleted: boolean = false): Promise<ShareFileAPIModels.Item[]>
delete(singleversion: boolean = false, forceSync: boolean = false): Promise<true>
Handles the download process for ShareFile items.
checkStatus(): Promise<boolean>
download(): DownloadChain
waitAndDownload(maxAttempts: number = 10, interval: number = 2000): Promise<DownloadChain>
toBuffer(): Promise<Buffer>
toStream(): Promise<ReadableStream>
Handles the upload process for ShareFile items.
upload(contents: string | Buffer): Promise<string>
Represents the results of a search operation in ShareFile.
PartialResults: boolean
Results: SharefileItem[]
TimedOut: boolean
getNextPage(): Promise<SharefileSearchResults | null>
const homeFolder = await shareFileAPI.getItem('home');
console.log(`Home folder name: ${homeFolder.Name}`);
const folderContents = await homeFolder.children();
folderContents.forEach(item => {
console.log(`${item.Name} (${item.id})`);
});
const documentsFolder = await shareFileAPI.getItem('/Personal Folders/Documents');
const files = await documentsFolder.children();
const folder = await shareFileAPI.getItem('folder-id');
const fileContent = 'Hello, ShareFile!';
const newFile = await folder.upload(fileContent, 'hello.txt');
console.log(`File uploaded: ${newFile.Name}`);
const file = await shareFileAPI.getItem('file-id');
const downloadLink = await file.download(true);
console.log(`Download link: ${downloadLink}`);
// Or download as a buffer
const downloadSpec = await file.download(false);
const buffer = await downloadSpec.waitAndDownload().then(chain => chain.toBuffer());
const file = await shareFileAPI.getItem('file-id');
await file.updateItem({
Name: 'Updated File Name.txt',
Description: 'This file has been updated.'
});
const file = await shareFileAPI.getItem('file-id');
const newParentFolder = await shareFileAPI.getItem('new-folder-id');
await file.move(newParentFolder.id);
const file = await shareFileAPI.getItem('file-id');
await file.delete();
const parentFolder = await shareFileAPI.getItem('parent-folder-id');
const newFolder = await parentFolder.createFolder('New Folder', 'Description of the new folder');
const folder = await shareFileAPI.getItem('folder-id');
await folder.rename('New Folder Name');
const searchResultObj = await shareFileAPI.searchItems('budget report');
searchResultObj.Results.forEach(item => {
console.log(`Found: ${item.Name} (${item.id})`);
});
const folder = await shareFileAPI.getItem('folder-id');
const pdfFiles = await folder.children({
$filter: "endswith(Name, '.pdf') eq true"
});
const file = await shareFileAPI.getItem('file-id');
const shareLink = await file.createShareLink({
ExpirationDate: '2023-12-31',
RequireLogin: false
});
console.log(`Share link: ${shareLink.Uri}`);
const file = await shareFileAPI.getItem('file-id');
const versions = await file.getStream();
versions.forEach(version => {
console.log(`Version: ${version.CreationDate}`);
});
const folder = await shareFileAPI.getItem('folder-id');
const filesToUpload = [
{ name: 'file1.txt', content: 'Content 1' },
{ name: 'file2.txt', content: 'Content 2' }
];
for (const file of filesToUpload) {
await folder.upload(file.content, file.name);
}
const folder = await shareFileAPI.getItem('folder-id');
const files = await folder.children();
for (const file of files) {
const downloadSpec = await file.download(false);
const buffer = await downloadSpec.waitAndDownload().then(chain => chain.toBuffer());
// Process or save the buffer
}
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the GPL-3.0 License.