-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add throwable usage #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
2-towns
wants to merge
1
commit into
master
Choose a base branch
from
feat/throwing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Download example | ||
|
||
Small example to show how to download a file in the browser with Codex. | ||
|
||
## Install dependencies | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
## Run node | ||
|
||
```bash | ||
node index.js | ||
``` | ||
|
||
Note: You can define `CODEX_NODE_URL`, default value is "http://localhost:8080". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { Codex } = require("@codex-storage/sdk-js/throwable"); | ||
const { NodeUploadStrategy } = 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 strategy = new NodeUploadStrategy("Hello World !"); | ||
const uploadResponse = data.upload(strategy); | ||
|
||
const cid = await uploadResponse.result; | ||
|
||
console.info("CID is", cid); | ||
} | ||
|
||
main(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "@codex-storage/sdk-js-update-node-throwable-example", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": {}, | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"dependencies": { | ||
"@codex-storage/sdk-js": "../..", | ||
"undici": "^7.7.0" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^3.5.3" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { CodexData } from "./data"; | ||
import type { UploadStrategy } from "./types"; | ||
import { type FetchAuth } from "../fetch-safe/fetch-safe"; | ||
import { Throwable } from "../throwable/throwable"; | ||
|
||
type CodexDataThrowableOptions = { | ||
auth?: FetchAuth; | ||
}; | ||
|
||
export class CodexDataThrowable { | ||
readonly data: CodexData; | ||
|
||
constructor(url: string, options?: CodexDataThrowableOptions) { | ||
this.data = new CodexData(url, options); | ||
} | ||
|
||
cids = () => Throwable.from(this.data.cids()); | ||
space = () => Throwable.from(this.data.space()); | ||
upload = (strategy: UploadStrategy) => { | ||
const { result, abort } = this.data.upload(strategy); | ||
return { | ||
result: Throwable.from(result), | ||
abort, | ||
}; | ||
}; | ||
localDownload = (cid: string) => Throwable.from(this.data.localDownload(cid)); | ||
networkDownload = (cid: string) => | ||
Throwable.from(this.data.networkDownload(cid)); | ||
networkDownloadStream = (cid: string) => | ||
Throwable.from(this.data.networkDownloadStream(cid)); | ||
fetchManifest = (cid: string) => Throwable.from(this.data.fetchManifest(cid)); | ||
delete = (cid: string) => Throwable.from(this.data.delete(cid)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { CodexLogLevel } from "./types"; | ||
import { type FetchAuth } from "../fetch-safe/fetch-safe"; | ||
import { Throwable } from "../throwable/throwable"; | ||
import { CodexDebug } from "./debug"; | ||
|
||
type CodexDebugThrowableOptions = { | ||
auth?: FetchAuth; | ||
}; | ||
|
||
export class CodexDebugThrowable { | ||
readonly debug: CodexDebug; | ||
|
||
constructor(url: string, options?: CodexDebugThrowableOptions) { | ||
this.debug = new CodexDebug(url, options); | ||
} | ||
|
||
setLogLevel = (level: CodexLogLevel) => | ||
Throwable.from(this.debug.setLogLevel(level)); | ||
info = () => Throwable.from(this.debug.info()); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { type FetchAuth } from "../fetch-safe/fetch-safe"; | ||
import { Throwable } from "../throwable/throwable"; | ||
import { CodexMarketplace } from "./marketplace"; | ||
import type { | ||
CodexAvailabilityPatchInput, | ||
CodexCreateAvailabilityInput, | ||
CodexCreateStorageRequestInput, | ||
} from "./types"; | ||
|
||
type CodexMarketplaceThrowableOptions = { | ||
auth?: FetchAuth; | ||
}; | ||
|
||
export class CodexMarketplaceThrowable { | ||
readonly marketplace: CodexMarketplace; | ||
|
||
constructor(url: string, options?: CodexMarketplaceThrowableOptions) { | ||
this.marketplace = new CodexMarketplace(url, options); | ||
} | ||
|
||
activeSlots = () => Throwable.from(this.marketplace.activeSlots()); | ||
activeSlot = (slotId: string) => | ||
Throwable.from(this.marketplace.activeSlot(slotId)); | ||
availabilities = () => Throwable.from(this.marketplace.availabilities()); | ||
createAvailability = (input: CodexCreateAvailabilityInput) => | ||
Throwable.from(this.marketplace.createAvailability(input)); | ||
updateAvailability = (input: CodexAvailabilityPatchInput) => | ||
Throwable.from(this.marketplace.updateAvailability(input)); | ||
reservations = (availabilityId: string) => | ||
Throwable.from(this.marketplace.reservations(availabilityId)); | ||
purchaseIds = () => Throwable.from(this.marketplace.purchaseIds()); | ||
purchases = () => Throwable.from(this.marketplace.purchases()); | ||
purchaseDetail = (purchaseId: string) => | ||
Throwable.from(this.marketplace.purchaseDetail(purchaseId)); | ||
createStorageRequest = (input: CodexCreateStorageRequestInput) => | ||
Throwable.from(this.marketplace.createStorageRequest(input)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { CodexPeerIdContentType, CodexSprContentType } from "./types"; | ||
import { type FetchAuth } from "../fetch-safe/fetch-safe"; | ||
import { Throwable } from "../throwable/throwable"; | ||
import { CodexNode } from "./node"; | ||
|
||
type CodexNodeThrowableOptions = { | ||
auth?: FetchAuth; | ||
}; | ||
|
||
export class CodexNodeThrowable { | ||
readonly node: CodexNode; | ||
|
||
constructor(url: string, options?: CodexNodeThrowableOptions) { | ||
this.node = new CodexNode(url, options); | ||
} | ||
|
||
connect = (peerId: string, addrs: string[] = []) => | ||
Throwable.from(this.node.connect(peerId, addrs)); | ||
spr = (type: CodexSprContentType = "json") => | ||
Throwable.from(this.node.spr(type)); | ||
peerId = (type: CodexPeerIdContentType = "json") => | ||
Throwable.from(this.node.peerId(type)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { CodexDataThrowable } from "./data/data.throwable"; | ||
import { CodexNodeThrowable } from "./node/node.throwable"; | ||
import { CodexMarketplaceThrowable } from "./marketplace/marketplace.throwable"; | ||
import { CodexDebugThrowable } from "./debug/debug.throwable"; | ||
import type { FetchAuth } from "./fetch-safe/fetch-safe"; | ||
|
||
export * from "./fetch-safe/fetch-safe"; | ||
export * from "./marketplace/types"; | ||
export * from "./debug/types"; | ||
export * from "./data/types"; | ||
export * from "./values/values"; | ||
export * from "./errors/errors"; | ||
|
||
export { CodexDebugThrowable } from "./debug/debug.throwable"; | ||
export { CodexDataThrowable } from "./data/data.throwable"; | ||
export { CodexNodeThrowable } from "./node/node.throwable"; | ||
export { CodexMarketplaceThrowable } from "./marketplace/marketplace.throwable"; | ||
|
||
type CodexProps = { | ||
auth?: FetchAuth; | ||
}; | ||
|
||
export class Codex { | ||
readonly url: string; | ||
private _marketplace: CodexMarketplaceThrowable | null; | ||
private _data: CodexDataThrowable | null; | ||
private _node: CodexNodeThrowable | null; | ||
private _debug: CodexDebugThrowable | null; | ||
private readonly auth: FetchAuth = {}; | ||
|
||
constructor(url: string, options?: CodexProps) { | ||
this.url = url; | ||
this._marketplace = null; | ||
this._data = null; | ||
this._node = null; | ||
this._debug = null; | ||
|
||
if (options?.auth) { | ||
this.auth = options?.auth; | ||
} | ||
} | ||
|
||
get marketplace() { | ||
if (this._marketplace) { | ||
return this._marketplace; | ||
} | ||
|
||
this._marketplace = new CodexMarketplaceThrowable(this.url, { | ||
auth: this.auth, | ||
}); | ||
|
||
return this._marketplace; | ||
} | ||
|
||
get data() { | ||
if (this._data) { | ||
return this._data; | ||
} | ||
|
||
this._data = new CodexDataThrowable(this.url, { auth: this.auth }); | ||
|
||
return this._data; | ||
} | ||
|
||
get node() { | ||
if (this._node) { | ||
return this._node; | ||
} | ||
|
||
this._node = new CodexNodeThrowable(this.url, { auth: this.auth }); | ||
|
||
return this._node; | ||
} | ||
|
||
get debug() { | ||
if (this._debug) { | ||
return this._debug; | ||
} | ||
|
||
this._debug = new CodexDebugThrowable(this.url, { auth: this.auth }); | ||
|
||
return this._debug; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be like this?