Setup tree shaking

This commit is contained in:
Arnaud 2024-08-15 12:08:41 +02:00
parent 7c40e4af5f
commit ddb98b794a
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
9 changed files with 2120 additions and 27 deletions

View File

@ -2,6 +2,8 @@
The Codex SDK provides an API for interacting with the Codex decentralized storage network.
The SDK has a small bundle size and support tree shaking.
## Import
```js
@ -22,6 +24,12 @@ To create a Codex instance, provide the REST API url to interact with the Codex
const codex = new Codex("http://localhost:3000")
```
To use a module, you need to use the await syntax. If the module is not loaded yet, it will be imported first and then cached in memory.
```js
const marketplace = await codex.marketplace()
```
### Error handling
The SDK provides a type called `SafeValue` for error handling instead of throwing errors. It is inspired by Go's "error as value" concept.
@ -37,7 +45,7 @@ The error type is a [CodexError](./src/errors/errors.ts#L15) which can be error
Example:
```js
const slots = await codex.marketplace.activeSlots();
const slots = marketplace.activeSlots();
if (slots.error) {
// Do something to handle the error in slots.data
@ -49,6 +57,13 @@ if (slots.error) {
### Marketplace
The following API assume that you have already a marketplace module loaded, example:
```js
const codex = new Codex("http://localhost:3000")
const marketplace = await codex.marketplace()
```
#### activeSlots()
Returns active slots.
@ -58,7 +73,7 @@ Returns active slots.
Example:
```js
const slots = await codex.marketplace.activeSlots();
const slots = await marketplace.activeSlots();
```
#### activeSlot(slotId)
@ -72,7 +87,7 @@ Example:
```js
const slotId= "AB9........"
const slot = await codex.marketplace.activeSlot(slotId);
const slot = await marketplace.activeSlot(slotId);
```
@ -85,7 +100,7 @@ Returns storage that is for sale.
Example:
```js
const availabilities = await codex.marketplace.availabilities();
const availabilities = await marketplace.availabilities();
```
#### createAvailability
@ -98,7 +113,7 @@ Offers storage for sale.
Example:
```js
const response = await codex.marketplace.createAvailability({
const response = await marketplace.createAvailability({
maxCollateral: 1,
totalSize: 3000,
minPrice: 100,
@ -116,7 +131,7 @@ Return list of reservations for ongoing Storage Requests that the node hosts.
Example:
```js
const reservations = await codex.marketplace.reservations("Ox...");
const reservations = await marketplace.reservations("Ox...");
```
@ -130,7 +145,7 @@ Creates a new Request for storage
Example:
```js
const request = await codex.marketplace.createStorageRequest({
const request = await marketplace.createStorageRequest({
duration: 3000,
reward: 100,
proofProbability: 1,
@ -152,7 +167,7 @@ Returns list of purchase IDs
Example:
```js
const ids = await codex.marketplace.purchaseIds();
const ids = await marketplace.purchaseIds();
```
#### purchaseDetail
@ -166,5 +181,5 @@ Example:
```js
const purchaseId = "Ox........"
const purchase = await codex.marketplace.purchaseDetail(purchaseId);
const purchase = await marketplace.purchaseDetail(purchaseId);
```

2050
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,11 +2,14 @@
"name": "@codex/sdk-js",
"version": "0.0.1",
"description": "Codex SDK to interact with the Codex decentralized storage network.",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/codex-storage/codex-js"
},
"scripts": {
"prepack": "npm run build",
"prebuild": "rm -Rf dist/*",
"build": "tsc",
"build": "tsup tsup src/index.ts --format esm,cjs --dts --minify",
"compile": "tsc --noEmit",
"pretest": "npm run build",
"test": "node --test",
@ -18,22 +21,34 @@
"SDK",
"storage"
],
"files": [
"dist/**"
],
"types": "dist/index.d.ts",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": "./dist/index.js"
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.js"
}
}
},
"sideEffects": false,
"files": [
"dist"
],
"author": "Codex team",
"readme": "README.md",
"license": "MIT",
"engines": {
"node": ">=20"
"node": ">=18"
},
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@tsconfig/strictest": "^2.0.5",
"tsup": "^8.2.3",
"typescript": "^5.5.4"
},
"dependencies": {

View File

@ -1,4 +1,4 @@
import { SafeValue } from "../values/values"
import type { SafeValue } from "../values/values"
export class Disk {
readonly url: string

View File

@ -1,4 +1,4 @@
import { SafeValue } from "../values/values"
import { type SafeValue } from "../values/values"
export const Fetch = {
async safe<T extends Object>(url: string, init: RequestInit): Promise<SafeValue<T>> {

View File

@ -6,13 +6,25 @@ export * from "./marketplace/types";
export class Codex {
readonly url: string
readonly marketplace: Marketplace
private _marketplace: Marketplace | null
readonly disk: Disk
constructor(url: string) {
this.url = url
this.marketplace = new Marketplace(url)
this._marketplace = null
this.disk = new Disk(url)
}
async marketplace() {
if (this._marketplace) {
return this._marketplace
}
const module = await import("./marketplace/marketplace")
this._marketplace = new module.Marketplace(this.url)
return module.Marketplace
}
}

View File

@ -2,8 +2,8 @@ import * as v from 'valibot'
import { Api } from "../api/config"
import { CodexValibotIssuesMap } from '../errors/errors'
import { Fetch } from "../fetch-safe/fetch-safe"
import { SafeValue } from "../values/values"
import { CodexAvailability, CodexAvailabilityCreateResponse, CodexCreateAvailabilityInput, CodexCreateStorageRequestInput, CodexCreateStorageRequestResponse, CodexPurchase, CodexReservation, CodexSlot, CodexUpdateAvailabilityInput } from "./types"
import type { SafeValue } from "../values/values"
import { type CodexAvailability, type CodexAvailabilityCreateResponse, CodexCreateAvailabilityInput, CodexCreateStorageRequestInput, type CodexCreateStorageRequestResponse, type CodexPurchase, type CodexReservation, type CodexSlot, CodexUpdateAvailabilityInput } from "./types"
export class Marketplace {
readonly url: string

View File

@ -1,4 +1,4 @@
import { CodexError } from "../errors/errors";
import { type CodexError } from "../errors/errors";
/**
* SafeValue is a type used for error handling instead of throwing errors.

View File

@ -11,8 +11,9 @@
"DOM",
],
"outDir": "./dist",
// "module": "ES2015",
// "moduleResolution": "Bundler"
"module": "ESNext",
"moduleResolution": "Bundler",
"verbatimModuleSyntax": true,
},
"buildOptions": {}
}