mirror of
https://github.com/logos-storage/logos-storage-js.git
synced 2026-01-02 13:33:07 +00:00
Refactoring
This commit is contained in:
parent
c2a2a1b49b
commit
7b8fcc449b
10
README.md
10
README.md
@ -8,7 +8,7 @@ The SDK is currently under early development and the API can change at any time.
|
||||
|
||||
## Breaking changes
|
||||
|
||||
- Version 0.1.0 introduce download strategy to support browser and Node JS.
|
||||
- Version 0.1.0 introduce upload strategy to support browser and Node JS.
|
||||
|
||||
## How to use
|
||||
|
||||
@ -278,7 +278,7 @@ Upload a file in a streaming manner
|
||||
|
||||
## Browser
|
||||
|
||||
- stategy [BrowserDownloadStategy](./src/data/browser-download.ts#L5)
|
||||
- stategy [BrowserUploadStategy](./src/data/browser-upload.ts#L5)
|
||||
- returns [UploadResponse](./src/data/types.ts#L80)
|
||||
|
||||
Example:
|
||||
@ -292,7 +292,7 @@ const onProgress = (loaded, total) => {
|
||||
|
||||
const metadata = { filename: "foo.xt", mimetype: "text/plain" };
|
||||
|
||||
const stategy = new BrowserDownloadStategy(file, onProgress, metadata);
|
||||
const stategy = new BrowserUploadStategy(file, onProgress, metadata);
|
||||
|
||||
const uploadResponse = data.upload(stategy);
|
||||
|
||||
@ -308,13 +308,13 @@ console.info("CID is", res.data);
|
||||
|
||||
## Node
|
||||
|
||||
- stategy [NodeDownloadStategy](./src/data/node-download.ts#L8)
|
||||
- stategy [NodeUploadStategy](./src/data/node-download.ts#L8)
|
||||
- returns [UploadResponse](./src/data/types.ts#L80)
|
||||
|
||||
Example:
|
||||
|
||||
```js
|
||||
const stategy = new NodeDownloadStategy("Hello World !");
|
||||
const stategy = new NodeUploadStategy("Hello World !");
|
||||
const uploadResponse = data.upload(stategy);
|
||||
|
||||
const res = await uploadResponse.result;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { Codex } from "@codex-storage/sdk-js";
|
||||
import { BrowserDownloadStategy } from "@codex-storage/sdk-js/browser";
|
||||
import { BrowserUploadStategy } from "@codex-storage/sdk-js/browser";
|
||||
|
||||
async function main() {
|
||||
const codex = new Codex(process.env.CODEX_NODE_URL);
|
||||
@ -19,7 +19,7 @@ async function main() {
|
||||
mimetype: "text/plain",
|
||||
};
|
||||
|
||||
const stategy = new BrowserDownloadStategy(file, onProgress, metadata);
|
||||
const stategy = new BrowserUploadStategy(file, onProgress, metadata);
|
||||
|
||||
const uploadResponse = data.upload(stategy);
|
||||
|
||||
|
||||
@ -1,21 +1,23 @@
|
||||
const { Codex } = require("@codex-storage/sdk-js");
|
||||
const { NodeDownloadStategy } = require("@codex-storage/sdk-js/node");
|
||||
const { Codex } = require("@codex-storage/sdk-js");
|
||||
const { NodeUploadStategy } = require("@codex-storage/sdk-js/node");
|
||||
|
||||
async function main() {
|
||||
const codex = new Codex(process.env.CODEX_NODE_URL || "http://localhost:8080");
|
||||
const data = codex.data
|
||||
const codex = new Codex(
|
||||
process.env.CODEX_NODE_URL || "http://localhost:8080"
|
||||
);
|
||||
const data = codex.data;
|
||||
|
||||
const stategy = new NodeDownloadStategy("Hello World !")
|
||||
const stategy = new NodeUploadStategy("Hello World !");
|
||||
const uploadResponse = data.upload(stategy);
|
||||
|
||||
const res = await uploadResponse.result
|
||||
const res = await uploadResponse.result;
|
||||
|
||||
if (res.error) {
|
||||
console.error(res.data)
|
||||
return
|
||||
console.error(res.data);
|
||||
return;
|
||||
}
|
||||
|
||||
console.info("CID is", res.data)
|
||||
console.info("CID is", res.data);
|
||||
}
|
||||
|
||||
main()
|
||||
main();
|
||||
|
||||
@ -1 +1 @@
|
||||
export * from "./data/browser-download";
|
||||
export * from "./data/browser-upload";
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
import { CodexError } from "../errors/errors";
|
||||
import type { SafeValue } from "../values/values";
|
||||
import type { DownloadStategy } from "./types";
|
||||
import type { UploadStategy } from "./types";
|
||||
|
||||
export class BrowserDownloadStategy implements DownloadStategy {
|
||||
export class BrowserUploadStategy implements UploadStategy {
|
||||
private readonly file: Document | XMLHttpRequestBodyInit;
|
||||
private readonly onProgress:
|
||||
| ((loaded: number, total: number) => void)
|
||||
@ -5,7 +5,7 @@ import type {
|
||||
CodexDataResponse,
|
||||
CodexManifest,
|
||||
CodexNodeSpace,
|
||||
DownloadStategy,
|
||||
UploadStategy,
|
||||
NetworkDownloadResponse,
|
||||
UploadResponse,
|
||||
} from "./types";
|
||||
@ -50,7 +50,7 @@ export class CodexData {
|
||||
* XMLHttpRequest is used instead of fetch for this case, to obtain progress information.
|
||||
* A callback onProgress can be passed to receive upload progress data information.
|
||||
*/
|
||||
upload(stategy: DownloadStategy): UploadResponse {
|
||||
upload(stategy: UploadStategy): UploadResponse {
|
||||
const url = this.url + Api.config.prefix + "/data";
|
||||
|
||||
return {
|
||||
|
||||
@ -3,9 +3,9 @@ import { CodexError } from "../errors/errors";
|
||||
import type { SafeValue } from "../values/values";
|
||||
import Undici from "undici";
|
||||
import { type FormData } from "undici";
|
||||
import type { DownloadStategy } from "./types";
|
||||
import type { UploadStategy } from "./types";
|
||||
|
||||
export class NodeDownloadStategy implements DownloadStategy {
|
||||
export class NodeUploadStategy implements UploadStategy {
|
||||
private readonly body:
|
||||
| string
|
||||
| Buffer
|
||||
@ -82,7 +82,7 @@ export type UploadResponse = {
|
||||
|
||||
export type NetworkDownloadResponse = { cid: string; manifest: CodexManifest };
|
||||
|
||||
export interface DownloadStategy {
|
||||
export interface UploadStategy {
|
||||
download(url: string): Promise<SafeValue<string>>;
|
||||
abort(): void;
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
export * from "./data/node-download";
|
||||
export * from "./data/node-upload";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user