UNPKG

@dataset.sh/client

Version:

TypeScript client library for dataset.sh - A powerful dataset management system supporting both local and remote storage with seamless transfer capabilities.

221 lines 9.32 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.RemoteClient = void 0; const RemoteDataset_1 = require("./RemoteDataset"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const os = __importStar(require("os")); class RemoteClient { constructor(config) { this.maxRedirects = 5; if (typeof config === 'string') { const profileConfig = this.loadProfile(config); this.host = profileConfig.host; this.accessKey = profileConfig.accessKey; } else { this.host = config.host; this.accessKey = config.accessKey; } this.host = this.host.replace(/\/$/, ''); this.headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', }; if (this.accessKey) { this.headers['X-DATASET-SH-ACCESS-KEY'] = this.accessKey; } // Initialize endpoints object this.endpoints = { serverVersion: () => this.getUrl('/api/version'), serverInfo: () => this.getUrl('/api/info'), listNamespaceDatasets: (namespace, page) => { const url = `/api/namespace/${namespace}/dataset`; return page !== undefined ? this.getUrl(`${url}?page=${page}`) : this.getUrl(url); }, listVersions: (namespace, datasetName, page, limit) => { let url = `/api/dataset/${namespace}/${datasetName}/version`; const params = new URLSearchParams(); if (page !== undefined) params.append('page', page.toString()); if (limit !== undefined) params.append('limit', limit.toString()); const queryString = params.toString(); return queryString ? this.getUrl(`${url}?${queryString}`) : this.getUrl(url); }, downloadVersion: (namespace, datasetName, version) => this.getUrl(`/api/dataset/${namespace}/${datasetName}/version/${version}/file`), listTags: (namespace, datasetName) => this.getUrl(`/api/dataset/${namespace}/${datasetName}/tag`), resolveTag: (namespace, datasetName, tag) => this.getUrl(`/api/dataset/${namespace}/${datasetName}/tag/${tag}`), setTag: (namespace, datasetName, tag) => this.getUrl(`/api/dataset/${namespace}/${datasetName}/tag/${tag}`), downloadByTag: (namespace, datasetName, tag) => this.getUrl(`/api/dataset/${namespace}/${datasetName}/tag/${tag}/file`), readme: (namespace, datasetName, version) => this.getUrl(`/api/dataset/${namespace}/${datasetName}/version/${version}/readme`), updateReadme: (namespace, datasetName, version) => this.getUrl(`/api/dataset/${namespace}/${datasetName}/version/${version}/readme`), }; } loadProfile(profileName) { const configPath = path.join(os.homedir(), '.dataset-sh', 'profiles.json'); if (!fs.existsSync(configPath)) { throw new Error(`Profile configuration not found at ${configPath}`); } const configContent = fs.readFileSync(configPath, 'utf-8'); const config = JSON.parse(configContent); if (!config.profiles || !config.profiles[profileName]) { throw new Error(`Profile '${profileName}' not found`); } return config.profiles[profileName]; } async getRemoteVersion() { const response = await fetch(this.endpoints.serverVersion(), { method: 'GET', headers: this.headers, }); if (!response.ok) { throw new Error(`Failed to get server version: ${response.status} ${response.statusText}`); } const data = await response.json(); return data.version; } async testConnection() { try { const response = await fetch(this.endpoints.serverInfo(), { method: 'GET', headers: this.headers, }); return response.ok; } catch (error) { return false; } } dataset(name) { const parts = name.split('/'); if (parts.length !== 2) { throw new Error(`Invalid dataset name format: ${name}. Expected: namespace/dataset`); } const [namespace, datasetName] = parts; return new RemoteDataset_1.RemoteDataset(this, namespace, datasetName); } async listNamespaceDatasets(namespace, page) { const response = await fetch(this.endpoints.listNamespaceDatasets(namespace, page), { method: 'GET', headers: this.headers, }); if (!response.ok) { throw new Error(`Failed to list namespace datasets: ${response.status} ${response.statusText}`); } return await response.json(); } async listVersions(namespace, datasetName, page, limit) { const response = await fetch(this.endpoints.listVersions(namespace, datasetName, page, limit), { method: 'GET', headers: this.headers, }); if (!response.ok) { throw new Error(`Failed to list versions: ${response.status} ${response.statusText}`); } return await response.json(); } async listTags(namespace, datasetName) { const response = await fetch(this.endpoints.listTags(namespace, datasetName), { method: 'GET', headers: this.headers, }); if (!response.ok) { throw new Error(`Failed to list tags: ${response.status} ${response.statusText}`); } return await response.json(); } async resolveTag(namespace, datasetName, tag) { const response = await fetch(this.endpoints.resolveTag(namespace, datasetName, tag), { method: 'GET', headers: this.headers, }); if (!response.ok) { if (response.status === 404) { return { version: '', exists: false }; } throw new Error(`Failed to resolve tag: ${response.status} ${response.statusText}`); } return await response.json(); } async setTag(namespace, datasetName, tag, version) { const response = await fetch(this.endpoints.setTag(namespace, datasetName, tag), { method: 'POST', headers: this.headers, body: JSON.stringify({ version }), }); if (!response.ok) { throw new Error(`Failed to set tag: ${response.status} ${response.statusText}`); } } async getReadme(namespace, datasetName, version) { const response = await fetch(this.endpoints.readme(namespace, datasetName, version), { method: 'GET', headers: this.headers, }); if (!response.ok) { if (response.status === 404) { return ''; } throw new Error(`Failed to get readme: ${response.status} ${response.statusText}`); } const data = await response.json(); return data.readme || ''; } async updateReadme(namespace, datasetName, version, readme) { const response = await fetch(this.endpoints.updateReadme(namespace, datasetName, version), { method: 'POST', headers: this.headers, body: JSON.stringify({ readme }), }); if (!response.ok) { throw new Error(`Failed to update readme: ${response.status} ${response.statusText}`); } } getHost() { return this.host; } getHeaders() { return { ...this.headers }; } getUrl(path) { // Ensure path starts with / const normalizedPath = path.startsWith('/') ? path : `/${path}`; return `${this.host}${normalizedPath}`; } } exports.RemoteClient = RemoteClient; //# sourceMappingURL=RemoteClient.js.map