UNPKG

reduct-js

Version:

ReductStore Client SDK for Javascript/NodeJS/Typescript

172 lines (171 loc) 5.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Client = void 0; /** * Represents HTTP Client for ReductStore API * @class */ const ServerInfo_1 = require("./messages/ServerInfo"); const APIError_1 = require("./APIError"); const BucketInfo_1 = require("./messages/BucketInfo"); const BucketSettings_1 = require("./messages/BucketSettings"); const Bucket_1 = require("./Bucket"); const Token_1 = require("./messages/Token"); const ReplicationInfo_1 = require("./messages/ReplicationInfo"); const ReplicationSettings_1 = require("./messages/ReplicationSettings"); const HttpClient_1 = require("./http/HttpClient"); class Client { /** * HTTP Client for ReductStore * @param url URL to the storage * @param options */ constructor(url, options = {}) { this.httpClient = new HttpClient_1.HttpClient(url, options); } /** * Get server information * @async * @return {Promise<ServerInfo>} the data about the server */ async getInfo() { const { data } = await this.httpClient.get("/info"); return ServerInfo_1.ServerInfo.parse(data); } /** * Get list of buckets * @async * @return {BucketInfo[]} * @see BucketInfo */ async getBucketList() { const { data } = await this.httpClient.get("/list"); return data.buckets.map((bucket) => BucketInfo_1.BucketInfo.parse(bucket)); } /** * Create a new bucket * @param name name of the bucket * @param settings optional settings * @return {Promise<Bucket>} */ async createBucket(name, settings) { await this.httpClient.post(`/b/${name}`, settings ? BucketSettings_1.BucketSettings.serialize(settings) : undefined); return new Bucket_1.Bucket(name, this.httpClient); } /** * Get a bucket by name * @param name name of the bucket * @return {Promise<Bucket>} */ async getBucket(name) { await this.httpClient.get(`/b/${name}`); return new Bucket_1.Bucket(name, this.httpClient); } /** * Try to create a bucket and get it if it already exists * @param name name of the bucket * @param settings optional settings * @return {Promise<Bucket>} */ async getOrCreateBucket(name, settings) { try { return await this.createBucket(name, settings); } catch (error) { if (error instanceof APIError_1.APIError && error.status === 409) { return await this.getBucket(name); } throw error; // pass exception forward } } /** * Create a new access token * @param name name of the token * @param permissions permissions for the token * @return {Promise<string>} the token * * @example * const token = await client.createToken("my-token", {fullAccess: true}); * const client = new Client("https://play.storage-reduct.dev", {apiToken: token}); */ async createToken(name, permissions) { const { data } = await this.httpClient.post(`/tokens/${name}`, Token_1.TokenPermissions.serialize(permissions)); return data.value; } /** * Get a token by name * @param name name of the token * @return {Promise<Token>} the token */ async getToken(name) { const { data } = await this.httpClient.get(`/tokens/${name}`); return Token_1.Token.parse(data); } /** * List all tokens * @return {Promise<Token[]>} the list of tokens */ async getTokenList() { const { data } = await this.httpClient.get("/tokens"); return data.tokens.map((token) => Token_1.Token.parse(token)); } /** * Delete a token by name * @param name name of the token */ async deleteToken(name) { await this.httpClient.delete(`/tokens/${name}`); } /** * Get current API token and its permissions * @return {Promise<Token>} the token */ async me() { const { data } = await this.httpClient.get("/me"); return Token_1.Token.parse(data); } /** * Get the list of replications * @return {Promise<ReplicationInfo[]>} the list of replications */ async getReplicationList() { const { data } = await this.httpClient.get("/replications"); return data.replications.map((replication) => ReplicationInfo_1.ReplicationInfo.parse(replication)); } /** * Get full information about a replication * @param name name of the replication * @return {Promise<FullReplicationInfo>} the replication */ async getReplication(name) { const { data } = await this.httpClient.get(`/replications/${name}`); return ReplicationInfo_1.FullReplicationInfo.parse(data); } /** * Create a new replication * @param name name of the replication * @param settings settings of the replication * @return {Promise<void>} */ async createReplication(name, settings) { await this.httpClient.post(`/replications/${name}`, ReplicationSettings_1.ReplicationSettings.serialize(settings)); } /** * Update a replication * @param name name of the replication * @param settings settings of the replication * @return {Promise<void>} */ async updateReplication(name, settings) { await this.httpClient.put(`/replications/${name}`, ReplicationSettings_1.ReplicationSettings.serialize(settings)); } /** * Delete a replication * @param name name of the replication * @return {Promise<void>} */ async deleteReplication(name) { await this.httpClient.delete(`/replications/${name}`); } } exports.Client = Client;