diff --git a/.gitignore b/.gitignore index 76add87..c58c7db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -dist \ No newline at end of file +dist +*.tgz \ No newline at end of file diff --git a/README.md b/README.md index 1ba5ee7..19e8887 100644 --- a/README.md +++ b/README.md @@ -139,8 +139,8 @@ const reservations = await marketplace.reservations("Ox..."); Creates a new Request for storage -- input ([CodexCreateStorageRequestInput](./src/marketplace/types.ts#L186), required) -- returns Promise<[CodexCreateStorageRequestResponse](./src/marketplace/types.ts#L201)[]> +- input ([CodexCreateStorageRequestInput](./src/marketplace/types.ts#L188), required) +- returns Promise Example: @@ -181,3 +181,90 @@ Example: const purchaseId = "Ox........"; const purchase = await marketplace.purchaseDetail(purchaseId); ``` + +### Data + +The following API assume that you have already a data module loaded, example: + +```js +const codex = new Codex("http://localhost:3000"); +const data = await codex.data(); +``` + +#### cids + +Returns the manifest stored locally in node. + +- returns Promise<[CodexDataResponse](./src/data/types.ts#L57)[]> + +Example: + +```js +const cids = await data.cids(); +``` + +#### space + +Returns a summary of the storage space allocation of the node + +- returns Promise<[CodexNodeSpace](./src/data/types.ts#61)[]> + +Example: + +```js +const space = await data.space(); +``` + +#### upload + +Upload a file in a streaming manner + +- file (File, require) +- onProgress (onProgress: (loaded: number, total: number) => void, optional) +- returns Promise<[UploadResponse](./src/data/types.ts#85)[]> + +Example: + +```js +// Get file from previous event +const [file] = e.target.files + +const upload = await data.upload(file, (loaded: number, total: number) => { + // Use loaded and total so update a progress bar for example +}); +await upload.result(); +``` + +### Node + +The following API assume that you have already a node module loaded, example: + +```js +const codex = new Codex("http://localhost:3000"); +const data = await codex.node(); +``` + +#### setLogLevel + +Set log level at run time. + +- level ([CodexLogLevel](./src/debug/types.ts#L3), required) +- returns Promise + +Example: + +```js +await debug.setLogLevel("DEBUG"); +``` + +#### info + +Gets node information + +- returns Promise<[CodexDebugInfo](./src/debug/types.ts#L15)> + +Example: + +```js +const info = await debug.info(); +``` diff --git a/package.json b/package.json index 1fbb67b..c041e65 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@codex/sdk-js", + "name": "@codex-storage/sdk-js", "version": "0.0.1", "description": "Codex SDK to interact with the Codex decentralized storage network.", "repository": { @@ -7,9 +7,9 @@ "url": "https://github.com/codex-storage/codex-js" }, "scripts": { - "prepack": "tsup tsup src/index.ts --format esm,cjs --dts", + "prepack": "npm run buid", "prebuild": "rm -Rf dist/*", - "build": "tsc --p tsconfig.json", + "build": "tsup tsup src/index.ts --format esm,cjs --dts --sourcemap", "compile": "tsc --noEmit", "pretest": "npm run build", "pretest:only": "npm run build", diff --git a/src/data/data.ts b/src/data/data.ts index 99f0b9b..12f52f5 100644 --- a/src/data/data.ts +++ b/src/data/data.ts @@ -1,12 +1,11 @@ import { Api } from "../api/config"; import { Fetch } from "../fetch-safe/fetch-safe"; import type { SafeValue } from "../values/values"; -import type { CodexDataResponse, CodexNodeSpace } from "./types"; - -export type UploadResponse = { - result: Promise>; - abort: () => void; -}; +import type { + CodexDataResponse, + CodexNodeSpace, + UploadResponse, +} from "./types"; export class CodexData { readonly url: string; @@ -98,20 +97,6 @@ export class CodexData { }; }); - // const promise = Fetch.safe(url, { - // method: "POST", - // headers: { "Content-Type": "text/plain" }, - // body: file.stream(), - // // @ts-ignore - // duplex: "half", - // }) - // .then(async (res) => { - // console.info(res); - // return res.error - // ? res - // : { error: false as false, data: await res.data.text() }; - // }) - return { result: promise, abort: () => { diff --git a/src/data/types.ts b/src/data/types.ts index c35edd1..4f8c1ee 100644 --- a/src/data/types.ts +++ b/src/data/types.ts @@ -1,3 +1,5 @@ +import type { SafeValue } from "../values/values"; + export type CodexManifest = { /** * "Root hash of the content" @@ -79,3 +81,8 @@ export type CodexNodeSpace = { */ quotaReservedBytes: number; }; + +export type UploadResponse = { + result: Promise>; + abort: () => void; +}; diff --git a/src/debug/debug.ts b/src/debug/debug.ts index 405d599..833db9b 100644 --- a/src/debug/debug.ts +++ b/src/debug/debug.ts @@ -1,6 +1,7 @@ import { Api } from "../api/config"; import { CodexValibotIssuesMap } from "../errors/errors"; import { Fetch } from "../fetch-safe/fetch-safe"; +import type { SafeValue } from "../values/values"; import { CodexLogLevel, type CodexDebugInfo } from "./types"; import * as v from "valibot"; @@ -14,17 +15,17 @@ export class Debug { /** * Set log level at run time */ - setLogLevel(level: CodexLogLevel) { + setLogLevel(level: CodexLogLevel): Promise> { const result = v.safeParse(CodexLogLevel, level); if (!result.success) { - return { + return Promise.resolve({ error: true, data: { message: "Cannot validate the input", errors: CodexValibotIssuesMap(result.issues), }, - }; + }); } const url = diff --git a/src/index.ts b/src/index.ts index 48420a6..7636350 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,7 @@ export * from "./data/types"; export * from "./values/values"; export * from "./errors/errors"; -export { type CodexData, type UploadResponse } from "./data/data"; +export { type CodexData } from "./data/data"; export class Codex { readonly url: string; diff --git a/tsconfig.json b/tsconfig.json index 63a344c..ae764b4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,7 @@ "outDir": "./dist", "module": "ESNext", "moduleResolution": "Bundler", - "verbatimModuleSyntax": true + "verbatimModuleSyntax": true, + "sourceMap": true } } \ No newline at end of file