Fix documentation and types

This commit is contained in:
Arnaud 2024-08-30 12:24:00 +02:00
parent 19d798e7dc
commit fca1bd7259
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
8 changed files with 113 additions and 31 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules
dist
dist
*.tgz

View File

@ -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<string>
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<string>
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();
```

View File

@ -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",

View File

@ -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<SafeValue<string>>;
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: () => {

View File

@ -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<SafeValue<string>>;
abort: () => void;
};

View File

@ -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<SafeValue<Response>> {
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 =

View File

@ -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;

View File

@ -13,6 +13,7 @@
"outDir": "./dist",
"module": "ESNext",
"moduleResolution": "Bundler",
"verbatimModuleSyntax": true
"verbatimModuleSyntax": true,
"sourceMap": true
}
}