Koneksi TS SDK
A TypeScript SDK for interacting with Koneksi API services. This SDK provides a clean and intuitive interface for managing directories, files, and other resources in the Koneksi platform.
Installation
npm install koneksi-ts-sdk
Quick Start
import KoneksiSDK from "koneksi-ts-sdk";
const sdk = new KoneksiSDK({
client_id: "your_client_id",
client_secret: "your_client_secret",
});
// Check API health
const health = await sdk.health.check();
// Create a directory
const directory = await sdk.directory.create({
name: "My Documents",
directory_id: "parent_directory_id", // optional
});
// Upload a file
const uploadResult = await sdk.file.upload({
file: fileInput.files[0], // File object
directory_id: "target_directory_id", // optional
});
// Download a file
const fileBuffer = await sdk.file.download("file_id");
Configuration
The SDK supports multiple environments:
local
- Local developmentstaging
- Staging environmentuat
- User Acceptance Testingproduction
- Production environment
const sdk = new KoneksiSDK({
environment: "uat", // or "local", "staging", "production"
client_id: "your_client_id",
client_secret: "your_client_secret",
});
API Reference
Directory Operations
Create Directory
const directory = await sdk.directory.create({
name: "Directory Name",
directory_id: "parent_directory_id", // optional
});
// Returns: Directory object with id, name, size, createdAt, updatedAt
Read Directory
const directoryData = await sdk.directory.read("directory_id");
// Returns: DirectoryReadResponse with:
// {
// directory: Directory,
// files: FileInDirectory[],
// subdirectories: Subdirectory[]
// }
Update Directory
const message = await sdk.directory.update("directory_id", {
name: "New Name",
directory_id: "new_parent_directory_id", // optional
});
// Returns: Success message string
Delete Directory
const message = await sdk.directory.delete("directory_id");
// Returns: Success message string
File Operations
Upload File
const uploadResult = await sdk.file.upload({
file: fileInput.files[0], // File object or Buffer
directory_id: "target_directory_id", // optional
});
// Returns: UploadFileResponse with:
// {
// content_type: string,
// directory_id: string,
// file_id: string,
// hash: string,
// name: string,
// size: number
// }
Read File
const file = await sdk.file.read("file_id");
// Returns: FileData with:
// {
// id: string,
// content_type: string,
// directory_id: string,
// hash: string,
// name: string,
// size: number,
// is_shared: boolean,
// created_at: string,
// updated_at: string
// }
Update File
const message = await sdk.file.update("file_id", {
name: "New File Name",
directory_id: "new_directory_id", // optional
is_shared: false, // optional
});
// Returns: Success message string
Delete File
const message = await sdk.file.delete("file_id");
// Returns: Success message string
Download File
const fileBuffer = await sdk.file.download("file_id");
// Returns: Buffer containing file content
Health Operations
Check Health
const health = await sdk.health.check();
// Returns: HealthResponse with data, message, meta, status
Peers Operations
Fetch Peers
const peers = await sdk.peers.fetch();
// Returns: Array of Peer objects
Error Handling
The SDK throws descriptive errors for various failure scenarios:
try {
const directory = await sdk.directory.create({
name: "Test Directory",
});
} catch (error) {
console.error("Failed to create directory:", error.message);
}
TypeScript Support
The SDK is written in TypeScript and provides full type safety:
import {
Directory,
FileData,
UploadFileResponse,
DirectoryReadResponse,
CreateDirectoryRequest,
} from "koneksi-ts-sdk";
const createParams: CreateDirectoryRequest = {
name: "My Directory",
};
const directory: Directory = await sdk.directory.create(createParams);
const uploadResult: UploadFileResponse = await sdk.file.upload({
file: fileInput.files[0],
});
const directoryData: DirectoryReadResponse = await sdk.directory.read(
directory.id
);
Examples
Complete File Management Workflow
import KoneksiSDK from "koneksi-ts-sdk";
const sdk = new KoneksiSDK({
environment: "uat", // optional
client_id: "your_client_id",
client_secret: "your_client_secret",
});
async function manageFiles() {
try {
// 1. Create a directory
const documentsDir = await sdk.directory.create({
name: "Documents",
});
// 2. Upload a file to the directory
const uploadResult = await sdk.file.upload({
file: fileInput.files[0],
directory_id: documentsDir.id,
});
// 3. Read file details using the file_id from upload result
const fileDetails = await sdk.file.read(uploadResult.file_id);
// 4. Update file name
const updateMessage = await sdk.file.update(uploadResult.file_id, {
name: "Updated File Name",
});
// 5. Download the file
const fileBuffer = await sdk.file.download(uploadResult.file_id);
// 6. Read directory contents
const directoryData = await sdk.directory.read(documentsDir.id);
console.log("Files in directory:", directoryData.files);
console.log("Subdirectories:", directoryData.subdirectories);
// 7. Clean up
await sdk.file.delete(uploadResult.file_id);
await sdk.directory.delete(documentsDir.id);
} catch (error) {
console.error("Error:", error.message);
}
}
Health Monitoring
async function monitorHealth() {
try {
const health = await sdk.health.check();
console.log("API Health:", health);
} catch (error) {
console.error("Health check failed:", error.message);
}
}
Development
Building the SDK
npm run build
Running Tests
npm test
Linting
npm run lint
Formatting
npm run format
License
MIT