reduct-js
Version:
ReductStore Client SDK for Javascript/NodeJS/Typescript
194 lines (193 loc) • 5.82 kB
JavaScript
import { BucketSettings } from "./messages/BucketSettings.js";
import { ServerInfo } from "./messages/ServerInfo.js";
import { APIError } from "./APIError.js";
import { BucketInfo } from "./messages/BucketInfo.js";
import { Bucket } from "./Bucket.js";
import { Token, TokenCreateRequest, TokenPermissions } from "./messages/Token.js";
import { ReplicationSettings } from "./messages/ReplicationSettings.js";
import { FullReplicationInfo, ReplicationInfo } from "./messages/ReplicationInfo.js";
import { HttpClient } from "./http/HttpClient.js";
//#region src/Client.ts
/**
* Represents HTTP Client for ReductStore API
* @class
*/
var Client = class {
/**
* HTTP Client for ReductStore
* @param url URL to the storage
* @param options
*/
constructor(url, options = {}) {
this.httpClient = new 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.parse(data);
}
/**
* Close underlying HTTP resources (Node.js only).
*/
async close() {
await this.httpClient.close();
}
/**
* Get list of buckets
* @async
* @return {BucketInfo[]}
* @see BucketInfo
*/
async getBucketList() {
const { data } = await this.httpClient.get("/list");
return data.buckets.map((bucket) => 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.serialize(settings) : void 0);
return new 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(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 && error.status === 409) return await this.getBucket(name);
throw error;
}
}
/**
* Create a new access token
* @param name name of the token
* @param permissionsOrRequest permissions or token create request
* @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, permissionsOrRequest) {
const request = "permissions" in permissionsOrRequest ? permissionsOrRequest : { permissions: permissionsOrRequest };
const body = this.httpClient.apiVersion && this.httpClient.apiVersion[1] < 19 ? TokenPermissions.serialize(request.permissions) : TokenCreateRequest.serialize(request);
const { data } = await this.httpClient.post(`/tokens/${name}`, body);
return data.value;
}
/**
* Rotate a token by name
* @param name name of the token
* @return {Promise<string>} new token value
*/
async rotateToken(name) {
const { data } = await this.httpClient.post(`/tokens/${name}/rotate`, {});
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.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.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.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.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 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.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.serialize(settings));
}
/**
* Update replication mode without changing settings
* @param name name of the replication
* @param mode new mode: enabled, paused, or disabled
* @return {Promise<void>}
*/
async setReplicationMode(name, mode) {
await this.httpClient.patch(`/replications/${name}/mode`, { mode });
}
/**
* Delete a replication
* @param name name of the replication
* @return {Promise<void>}
*/
async deleteReplication(name) {
await this.httpClient.delete(`/replications/${name}`);
}
};
//#endregion
export { Client };