Skip to content

Commit

Permalink
feat: renterd-js library
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Apr 15, 2024
1 parent 833c8f9 commit 5288041
Show file tree
Hide file tree
Showing 18 changed files with 1,100 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-sloths-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@siafoundation/renterd-js': minor
---

Introduced a new renterd-js library. Closes https://github.com/SiaFoundation/web/issues/585
12 changes: 12 additions & 0 deletions libs/renterd-js/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@nx/react/babel",
{
"runtime": "automatic",
"useBuiltIns": "usage"
}
]
],
"plugins": []
}
21 changes: 21 additions & 0 deletions libs/renterd-js/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": ["plugin:@nx/react", "../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"rules": {
"@nx/dependency-checks": [
"error",
{
"ignoredFiles": ["libs/renterd-js/rollup.config.js"]
}
]
},
"overrides": [
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
93 changes: 93 additions & 0 deletions libs/renterd-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# renterd-js

SDK for interacting with `renterd`.

## installation

```sh
npm install @siafoundation/renterd-js
```

## usage

```js
import { Bus } from './bus'
import { Autopilot } from './autopilot'
import { Worker } from './worker'

export async function example() {
const bus = Bus({
api: 'http://localhost:9980',
password: 'password1337',
})
const autopilot = Autopilot({
api: 'http://localhost:9980',
password: 'password1337',
})
const worker = Worker({
api: 'http://worker:4444',
password: 'password1337',
})

const buckets = await bus.buckets()

buckets.data.forEach((bucket) => {
console.log(bucket.name, bucket.createdAt, bucket.policy)
})

bus.bucketCreate({
data: { name: 'my-bucket' },
})

bus.bucketPolicyUpdate({
params: {
name: 'my-bucket',
},
data: {
policy: {
publicReadAccess: true,
},
},
})

const hosts = await autopilot.hostsSearch({
data: {
filterMode: 'allowed',
usabilityMode: 'usable',
addressContains: 'example.com',
keyIn: ['key1', 'key2'],
offset: 0,
limit: 50,
},
})

hosts.data.forEach((host) => {
console.log(host.host.publicKey, host.host.priceTable)
})

worker.objectUpload({
params: {
key: 'path/to/file.txt',
bucket: 'my-bucket',
},
data: new File(['file contents'], 'file.txt'),
config: {
onUploadProgress: (progress) => {
console.log(progress.loaded / progress.total)
},
},
})

worker.objectDownload({
params: {
key: 'path/to/file.txt',
bucket: 'my-bucket',
},
config: {
onDownloadProgress: (progress) => {
console.log(progress.loaded / progress.total)
},
},
})
}
```
17 changes: 17 additions & 0 deletions libs/renterd-js/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */
export default {
displayName: 'renterd-js',
preset: '../../jest.preset.js',
transform: {
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
'^.+\\.[tj]sx?$': [
'babel-jest',
{
presets: ['@nx/next/babel'],
plugins: ['@babel/plugin-transform-private-methods'],
},
],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/renterd-js',
}
11 changes: 11 additions & 0 deletions libs/renterd-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@siafoundation/renterd-js",
"description": "SDK for interacting with `renterd`.",
"version": "0.0.0",
"license": "MIT",
"dependencies": {
"@siafoundation/renterd-types": "0.0.0",
"@siafoundation/request": "0.0.0"
},
"types": "./src/index.d.ts"
}
42 changes: 42 additions & 0 deletions libs/renterd-js/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "renterd-js",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/renterd-js/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nx/rollup:rollup",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/renterd-js",
"tsConfig": "libs/renterd-js/tsconfig.lib.json",
"project": "libs/renterd-js/package.json",
"entryFile": "libs/renterd-js/src/index.ts",
"external": ["react/jsx-runtime"],
"compiler": "tsc",
"outputFileName": "index.js",
"rollupConfig": "libs/renterd-js/rollup.config.js",
"assets": [
{
"glob": "libs/renterd-js/*.md",
"input": ".",
"output": "."
}
]
},
"configurations": {}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"]
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/libs/renterd-js"],
"options": {
"jestConfig": "libs/renterd-js/jest.config.ts"
}
}
}
}
18 changes: 18 additions & 0 deletions libs/renterd-js/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const preserveDirectives = require('rollup-plugin-preserve-directives')

// https://github.com/rollup/rollup/issues/4699#issuecomment-1465302665
function getRollupOptions(options) {
return {
...options,
output: {
...options.output,
preserveModules: true,
format: 'esm',
sourcemap: true,
},
plugins: options.plugins.concat(preserveDirectives.default()),
}
}

module.exports = getRollupOptions
59 changes: 59 additions & 0 deletions libs/renterd-js/src/autopilot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
AutopilotConfigParams,
AutopilotConfigPayload,
AutopilotConfigResponse,
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse,
AutopilotHostsSearchParams,
AutopilotHostsSearchPayload,
AutopilotHostsSearchResponse,
AutopilotStateParams,
AutopilotStatePayload,
AutopilotStateResponse,
AutopilotTriggerParams,
AutopilotTriggerPayload,
AutopilotTriggerResponse,
autopilotConfigRoute,
autopilotHostsRoute,
autopilotStateRoute,
autopilotTriggerRoute,
} from '@siafoundation/renterd-types'
import { buildRequestHandler, initAxios } from '@siafoundation/request'

export function Autopilot({
api,
password,
}: {
api: string
password?: string
}) {
const axios = initAxios(api, password)
return {
state: buildRequestHandler<
AutopilotStateParams,
AutopilotStatePayload,
AutopilotStateResponse
>(axios, 'get', autopilotStateRoute),
confg: buildRequestHandler<
AutopilotConfigParams,
AutopilotConfigPayload,
AutopilotConfigResponse
>(axios, 'get', autopilotConfigRoute),
configUpdate: buildRequestHandler<
AutopilotConfigUpdateParams,
AutopilotConfigUpdatePayload,
AutopilotConfigUpdateResponse
>(axios, 'put', autopilotConfigRoute),
hostsSearch: buildRequestHandler<
AutopilotHostsSearchParams,
AutopilotHostsSearchPayload,
AutopilotHostsSearchResponse
>(axios, 'post', autopilotHostsRoute),
trigger: buildRequestHandler<
AutopilotTriggerParams,
AutopilotTriggerPayload,
AutopilotTriggerResponse
>(axios, 'post', autopilotTriggerRoute),
}
}
Loading

0 comments on commit 5288041

Please sign in to comment.