s5-js/src/client.ts

97 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-12-10 20:03:56 +00:00
import axios, {
AxiosError,
AxiosProgressEvent,
AxiosRequestConfig,
} from "axios";
import type { AxiosResponse, ResponseType, Method } from "axios";
import {
uploadFile,
uploadLargeFile,
uploadDirectory,
uploadDirectoryRequest,
uploadSmallFile,
uploadSmallFileRequest,
uploadLargeFileRequest,
2023-12-12 02:53:10 +00:00
uploadWebapp,
uploadWebappRequest,
} from "./methods/upload.js";
2023-12-11 04:33:55 +00:00
import {
downloadData,
downloadFile,
getCidUrl,
getMetadata,
} from "./methods/download.js";
2023-12-10 20:03:56 +00:00
import { ensureUrl } from "./utils/url.js";
2023-12-10 20:03:56 +00:00
2023-12-12 03:44:43 +00:00
import {
createEntry,
getEntry,
2023-12-12 03:44:43 +00:00
publishEntry,
subscribeToEntry,
} from "./methods/registry.js";
import { CustomClientOptions } from "#utils/options.js";
import { throwValidationError } from "#utils/validation.js";
2023-12-10 20:03:56 +00:00
/**
* The S5 Client which can be used to access S5-net.
*/
export class S5Client {
// Upload
uploadFile = uploadFile;
uploadDirectory = uploadDirectory;
// Set methods (defined in other files).
2023-12-12 02:53:10 +00:00
uploadWebapp = uploadWebapp;
2023-12-10 20:03:56 +00:00
downloadFile = downloadFile;
2023-12-11 04:33:55 +00:00
downloadData = downloadData;
2023-12-10 20:03:56 +00:00
getCidUrl = getCidUrl;
getMetadata = getMetadata;
2023-12-11 02:06:43 +00:00
// Registry
subscribeToEntry = subscribeToEntry;
2023-12-12 03:26:42 +00:00
publishEntry = publishEntry;
2023-12-12 03:44:43 +00:00
createEntry = createEntry;
getEntry = getEntry;
// Download
protected uploadSmallFile = uploadSmallFile;
protected uploadSmallFileRequest = uploadSmallFileRequest;
protected uploadLargeFile = uploadLargeFile;
protected uploadLargeFileRequest = uploadLargeFileRequest;
protected uploadDirectoryRequest = uploadDirectoryRequest;
protected uploadWebappRequest = uploadWebappRequest;
2023-12-11 02:06:43 +00:00
2023-12-10 20:03:56 +00:00
/**
* The S5 Client which can be used to access S5-net.
*
* @class
* @param [portalUrl] The initial portal URL to use to access S5, if specified. A request will be made to this URL to get the actual portal URL. To use the default portal while passing custom options, pass "".
2023-12-10 20:03:56 +00:00
* @param [customOptions] Configuration for the client.
*/
constructor(portalUrl: string, customOptions: CustomClientOptions = {}) {
if (!portalUrl) {
throwValidationError("portalUrl", portalUrl, "parameter", "string");
2023-12-10 20:03:56 +00:00
}
this._portalUrl = ensureUrl(portalUrl);
this._customOptions = customOptions;
2023-12-10 20:03:56 +00:00
}
private _customOptions: CustomClientOptions;
2023-12-10 20:03:56 +00:00
get customOptions(): CustomClientOptions {
return this._customOptions;
2023-12-10 20:03:56 +00:00
}
private _portalUrl: string;
2023-12-10 20:03:56 +00:00
get portalUrl(): string {
return this._portalUrl;
2023-12-10 20:03:56 +00:00
}
public static create(
portalUrl: string,
customOptions: CustomClientOptions = {},
) {
return new S5Client(portalUrl, customOptions);
2023-12-10 20:03:56 +00:00
}
}