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 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 ## Import
```js ```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") 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 ### 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. 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: Example:
```js ```js
const slots = await codex.marketplace.activeSlots(); const slots = marketplace.activeSlots();
if (slots.error) { if (slots.error) {
// Do something to handle the error in slots.data // Do something to handle the error in slots.data
@ -49,6 +57,13 @@ if (slots.error) {
### Marketplace ### 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() #### activeSlots()
Returns active slots. Returns active slots.
@ -58,7 +73,7 @@ Returns active slots.
Example: Example:
```js ```js
const slots = await codex.marketplace.activeSlots(); const slots = await marketplace.activeSlots();
``` ```
#### activeSlot(slotId) #### activeSlot(slotId)
@ -72,7 +87,7 @@ Example:
```js ```js
const slotId= "AB9........" 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: Example:
```js ```js
const availabilities = await codex.marketplace.availabilities(); const availabilities = await marketplace.availabilities();
``` ```
#### createAvailability #### createAvailability
@ -98,7 +113,7 @@ Offers storage for sale.
Example: Example:
```js ```js
const response = await codex.marketplace.createAvailability({ const response = await marketplace.createAvailability({
maxCollateral: 1, maxCollateral: 1,
totalSize: 3000, totalSize: 3000,
minPrice: 100, minPrice: 100,
@ -116,7 +131,7 @@ Return list of reservations for ongoing Storage Requests that the node hosts.
Example: Example:
```js ```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: Example:
```js ```js
const request = await codex.marketplace.createStorageRequest({ const request = await marketplace.createStorageRequest({
duration: 3000, duration: 3000,
reward: 100, reward: 100,
proofProbability: 1, proofProbability: 1,
@ -152,7 +167,7 @@ Returns list of purchase IDs
Example: Example:
```js ```js
const ids = await codex.marketplace.purchaseIds(); const ids = await marketplace.purchaseIds();
``` ```
#### purchaseDetail #### purchaseDetail
@ -166,5 +181,5 @@ Example:
```js ```js
const purchaseId = "Ox........" 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", "name": "@codex/sdk-js",
"version": "0.0.1", "version": "0.0.1",
"description": "Codex SDK to interact with the Codex decentralized storage network.", "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": { "scripts": {
"prepack": "npm run build", "prepack": "npm run build",
"prebuild": "rm -Rf dist/*", "prebuild": "rm -Rf dist/*",
"build": "tsc", "build": "tsup tsup src/index.ts --format esm,cjs --dts --minify",
"compile": "tsc --noEmit", "compile": "tsc --noEmit",
"pretest": "npm run build", "pretest": "npm run build",
"test": "node --test", "test": "node --test",
@ -18,25 +21,37 @@
"SDK", "SDK",
"storage" "storage"
], ],
"files": [ "main": "./dist/index.js",
"dist/**" "types": "./dist/index.d.ts",
],
"types": "dist/index.d.ts",
"exports": { "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", "author": "Codex team",
"readme": "README.md", "readme": "README.md",
"license": "MIT", "license": "MIT",
"engines": { "engines": {
"node": ">=20" "node": ">=18"
}, },
"devDependencies": { "devDependencies": {
"@faker-js/faker": "^8.4.1", "@faker-js/faker": "^8.4.1",
"@tsconfig/strictest": "^2.0.5", "@tsconfig/strictest": "^2.0.5",
"tsup": "^8.2.3",
"typescript": "^5.5.4" "typescript": "^5.5.4"
}, },
"dependencies": { "dependencies": {
"valibot": "^0.36.0" "valibot": "^0.36.0"
} }
} }

View File

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

View File

@ -1,4 +1,4 @@
import { SafeValue } from "../values/values" import { type SafeValue } from "../values/values"
export const Fetch = { export const Fetch = {
async safe<T extends Object>(url: string, init: RequestInit): Promise<SafeValue<T>> { 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 { export class Codex {
readonly url: string readonly url: string
readonly marketplace: Marketplace private _marketplace: Marketplace | null
readonly disk: Disk readonly disk: Disk
constructor(url: string) { constructor(url: string) {
this.url = url this.url = url
this.marketplace = new Marketplace(url) this._marketplace = null
this.disk = new Disk(url) 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 { Api } from "../api/config"
import { CodexValibotIssuesMap } from '../errors/errors' import { CodexValibotIssuesMap } from '../errors/errors'
import { Fetch } from "../fetch-safe/fetch-safe" import { Fetch } from "../fetch-safe/fetch-safe"
import { SafeValue } from "../values/values" import type { SafeValue } from "../values/values"
import { CodexAvailability, CodexAvailabilityCreateResponse, CodexCreateAvailabilityInput, CodexCreateStorageRequestInput, CodexCreateStorageRequestResponse, CodexPurchase, CodexReservation, CodexSlot, CodexUpdateAvailabilityInput } from "./types" import { type CodexAvailability, type CodexAvailabilityCreateResponse, CodexCreateAvailabilityInput, CodexCreateStorageRequestInput, type CodexCreateStorageRequestResponse, type CodexPurchase, type CodexReservation, type CodexSlot, CodexUpdateAvailabilityInput } from "./types"
export class Marketplace { export class Marketplace {
readonly url: string 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. * SafeValue is a type used for error handling instead of throwing errors.

View File

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