Skip to content

Commit 8035633

Browse files
committed
feat: renterd-js library
1 parent 06a0c9c commit 8035633

17 files changed

+1092
-0
lines changed

.changeset/calm-sloths-collect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@siafoundation/renterd-js': minor
3+
---
4+
5+
Introduced a new renterd-js library. Closes https://github.com/SiaFoundation/web/issues/585

libs/renterd-js/.babelrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": [
3+
[
4+
"@nx/react/babel",
5+
{
6+
"runtime": "automatic",
7+
"useBuiltIns": "usage"
8+
}
9+
]
10+
],
11+
"plugins": []
12+
}

libs/renterd-js/.eslintrc.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"extends": ["plugin:@nx/react", "../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"rules": {
5+
"@nx/dependency-checks": [
6+
"error",
7+
{
8+
"ignoredFiles": ["libs/renterd-js/rollup.config.js"]
9+
}
10+
]
11+
},
12+
"overrides": [
13+
{
14+
"files": ["*.json"],
15+
"parser": "jsonc-eslint-parser",
16+
"rules": {
17+
"@nx/dependency-checks": "error"
18+
}
19+
}
20+
]
21+
}

libs/renterd-js/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# renterd-js
2+
3+
SDK for interacting with `renterd`.
4+
5+
## installation
6+
7+
```sh
8+
npm install @siafoundation/renterd-js
9+
```
10+
11+
## usage
12+
13+
```js
14+
import { Bus } from './bus'
15+
import { Autopilot } from './autopilot'
16+
import { Worker } from './worker'
17+
18+
export async function example() {
19+
const bus = Bus({
20+
api: 'http://localhost:9980',
21+
password: 'password1337',
22+
})
23+
const autopilot = Autopilot({
24+
api: 'http://localhost:9980',
25+
password: 'password1337',
26+
})
27+
const worker = Worker({
28+
api: 'http://worker:4444',
29+
password: 'password1337',
30+
})
31+
32+
const buckets = await bus.buckets()
33+
34+
buckets.data.forEach((bucket) => {
35+
console.log(bucket.name, bucket.createdAt, bucket.policy)
36+
})
37+
38+
bus.bucketCreate({
39+
data: { name: 'my-bucket' },
40+
})
41+
42+
bus.bucketPolicyUpdate({
43+
params: {
44+
name: 'my-bucket',
45+
},
46+
data: {
47+
policy: {
48+
publicReadAccess: true,
49+
},
50+
},
51+
})
52+
53+
const hosts = await autopilot.hostsSearch({
54+
data: {
55+
filterMode: 'allowed',
56+
usabilityMode: 'usable',
57+
addressContains: 'example.com',
58+
keyIn: ['key1', 'key2'],
59+
offset: 0,
60+
limit: 50,
61+
},
62+
})
63+
64+
hosts.data.forEach((host) => {
65+
console.log(host.host.publicKey, host.host.priceTable)
66+
})
67+
68+
worker.objectUpload({
69+
params: {
70+
key: 'path/to/file.txt',
71+
bucket: 'my-bucket',
72+
},
73+
data: new File(['file contents'], 'file.txt'),
74+
config: {
75+
onUploadProgress: (progress) => {
76+
console.log(progress.loaded / progress.total)
77+
},
78+
},
79+
})
80+
81+
worker.objectDownload({
82+
params: {
83+
key: 'path/to/file.txt',
84+
bucket: 'my-bucket',
85+
},
86+
config: {
87+
onDownloadProgress: (progress) => {
88+
console.log(progress.loaded / progress.total)
89+
},
90+
},
91+
})
92+
}
93+
```

libs/renterd-js/jest.config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: 'renterd-js',
4+
preset: '../../jest.preset.js',
5+
transform: {
6+
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
7+
'^.+\\.[tj]sx?$': [
8+
'babel-jest',
9+
{
10+
presets: ['@nx/next/babel'],
11+
plugins: ['@babel/plugin-transform-private-methods'],
12+
},
13+
],
14+
},
15+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
16+
coverageDirectory: '../../coverage/libs/renterd-js',
17+
}

libs/renterd-js/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@siafoundation/renterd-js",
3+
"description": "SDK for interacting with `renterd`.",
4+
"version": "0.0.0",
5+
"license": "MIT",
6+
"dependencies": {
7+
"@siafoundation/renterd-types": "0.0.0",
8+
"@siafoundation/request": "0.0.0"
9+
},
10+
"types": "./src/index.d.ts"
11+
}

libs/renterd-js/project.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "renterd-js",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "libs/renterd-js/src",
5+
"projectType": "library",
6+
"tags": [],
7+
"targets": {
8+
"build": {
9+
"executor": "@nx/rollup:rollup",
10+
"outputs": ["{options.outputPath}"],
11+
"options": {
12+
"outputPath": "dist/libs/renterd-js",
13+
"tsConfig": "libs/renterd-js/tsconfig.lib.json",
14+
"project": "libs/renterd-js/package.json",
15+
"entryFile": "libs/renterd-js/src/index.ts",
16+
"external": ["react/jsx-runtime"],
17+
"compiler": "tsc",
18+
"outputFileName": "index.js",
19+
"rollupConfig": "libs/renterd-js/rollup.config.js",
20+
"assets": [
21+
{
22+
"glob": "libs/renterd-js/*.md",
23+
"input": ".",
24+
"output": "."
25+
}
26+
]
27+
},
28+
"configurations": {}
29+
},
30+
"lint": {
31+
"executor": "@nx/eslint:lint",
32+
"outputs": ["{options.outputFile}"]
33+
},
34+
"test": {
35+
"executor": "@nx/jest:jest",
36+
"outputs": ["{workspaceRoot}/coverage/libs/renterd-js"],
37+
"options": {
38+
"jestConfig": "libs/renterd-js/jest.config.ts"
39+
}
40+
}
41+
}
42+
}

libs/renterd-js/rollup.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// eslint-disable-next-line @typescript-eslint/no-var-requires
2+
const preserveDirectives = require('rollup-plugin-preserve-directives')
3+
4+
// https://github.com/rollup/rollup/issues/4699#issuecomment-1465302665
5+
function getRollupOptions(options) {
6+
return {
7+
...options,
8+
output: {
9+
...options.output,
10+
preserveModules: true,
11+
format: 'esm',
12+
sourcemap: true,
13+
},
14+
plugins: options.plugins.concat(preserveDirectives.default()),
15+
}
16+
}
17+
18+
module.exports = getRollupOptions

libs/renterd-js/src/autopilot.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {
2+
AutopilotConfigParams,
3+
AutopilotConfigPayload,
4+
AutopilotConfigResponse,
5+
AutopilotConfigUpdateParams,
6+
AutopilotConfigUpdatePayload,
7+
AutopilotConfigUpdateResponse,
8+
AutopilotHostsSearchParams,
9+
AutopilotHostsSearchPayload,
10+
AutopilotHostsSearchResponse,
11+
AutopilotStateParams,
12+
AutopilotStatePayload,
13+
AutopilotStateResponse,
14+
AutopilotTriggerParams,
15+
AutopilotTriggerPayload,
16+
AutopilotTriggerResponse,
17+
autopilotConfigRoute,
18+
autopilotHostsRoute,
19+
autopilotStateRoute,
20+
autopilotTriggerRoute,
21+
} from '@siafoundation/renterd-types'
22+
import { buildRequestHandler, initAxios } from '@siafoundation/request'
23+
24+
export function Autopilot({
25+
api,
26+
password,
27+
}: {
28+
api: string
29+
password?: string
30+
}) {
31+
const axios = initAxios(api, password)
32+
return {
33+
state: buildRequestHandler<
34+
AutopilotStateParams,
35+
AutopilotStatePayload,
36+
AutopilotStateResponse
37+
>(axios, 'get', autopilotStateRoute),
38+
confg: buildRequestHandler<
39+
AutopilotConfigParams,
40+
AutopilotConfigPayload,
41+
AutopilotConfigResponse
42+
>(axios, 'get', autopilotConfigRoute),
43+
configUpdate: buildRequestHandler<
44+
AutopilotConfigUpdateParams,
45+
AutopilotConfigUpdatePayload,
46+
AutopilotConfigUpdateResponse
47+
>(axios, 'put', autopilotConfigRoute),
48+
hostsSearch: buildRequestHandler<
49+
AutopilotHostsSearchParams,
50+
AutopilotHostsSearchPayload,
51+
AutopilotHostsSearchResponse
52+
>(axios, 'post', autopilotHostsRoute),
53+
trigger: buildRequestHandler<
54+
AutopilotTriggerParams,
55+
AutopilotTriggerPayload,
56+
AutopilotTriggerResponse
57+
>(axios, 'post', autopilotTriggerRoute),
58+
}
59+
}

0 commit comments

Comments
 (0)