diff --git a/.changeset/calm-sloths-collect.md b/.changeset/calm-sloths-collect.md new file mode 100644 index 000000000..2fbef32a7 --- /dev/null +++ b/.changeset/calm-sloths-collect.md @@ -0,0 +1,5 @@ +--- +'@siafoundation/renterd-js': minor +--- + +Introduced a new renterd-js library. Closes https://github.com/SiaFoundation/web/issues/585 diff --git a/libs/renterd-js/.babelrc b/libs/renterd-js/.babelrc new file mode 100644 index 000000000..1ea870ead --- /dev/null +++ b/libs/renterd-js/.babelrc @@ -0,0 +1,12 @@ +{ + "presets": [ + [ + "@nx/react/babel", + { + "runtime": "automatic", + "useBuiltIns": "usage" + } + ] + ], + "plugins": [] +} diff --git a/libs/renterd-js/.eslintrc.json b/libs/renterd-js/.eslintrc.json new file mode 100644 index 000000000..eaf3daf9f --- /dev/null +++ b/libs/renterd-js/.eslintrc.json @@ -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" + } + } + ] +} diff --git a/libs/renterd-js/README.md b/libs/renterd-js/README.md new file mode 100644 index 000000000..d8c550980 --- /dev/null +++ b/libs/renterd-js/README.md @@ -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) + }, + }, + }) +} +``` diff --git a/libs/renterd-js/jest.config.ts b/libs/renterd-js/jest.config.ts new file mode 100644 index 000000000..8214c352f --- /dev/null +++ b/libs/renterd-js/jest.config.ts @@ -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', +} diff --git a/libs/renterd-js/package.json b/libs/renterd-js/package.json new file mode 100644 index 000000000..e3477a9a8 --- /dev/null +++ b/libs/renterd-js/package.json @@ -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" +} diff --git a/libs/renterd-js/project.json b/libs/renterd-js/project.json new file mode 100644 index 000000000..9cacf2ab2 --- /dev/null +++ b/libs/renterd-js/project.json @@ -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" + } + } + } +} diff --git a/libs/renterd-js/rollup.config.js b/libs/renterd-js/rollup.config.js new file mode 100644 index 000000000..3f5e4e997 --- /dev/null +++ b/libs/renterd-js/rollup.config.js @@ -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 diff --git a/libs/renterd-js/src/autopilot.ts b/libs/renterd-js/src/autopilot.ts new file mode 100644 index 000000000..321c6f0e4 --- /dev/null +++ b/libs/renterd-js/src/autopilot.ts @@ -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), + } +} diff --git a/libs/renterd-js/src/bus.ts b/libs/renterd-js/src/bus.ts new file mode 100644 index 000000000..ae1b150c0 --- /dev/null +++ b/libs/renterd-js/src/bus.ts @@ -0,0 +1,612 @@ +import { + AccountResetDriftParams, + AccountResetDriftPayload, + AccountResetDriftResponse, + AlertsDismissParams, + AlertsDismissPayload, + AlertsDismissResponse, + AlertsParams, + AlertsPayload, + AlertsResponse, + BucketCreateParams, + BucketCreatePayload, + BucketCreateResponse, + BucketDeleteParams, + BucketDeletePayload, + BucketDeleteResponse, + BucketParams, + BucketPayload, + BucketPolicyUpdateParams, + BucketPolicyUpdatePayload, + BucketPolicyUpdateResponse, + BucketResponse, + BucketsParams, + BucketsPayload, + BucketsResponse, + BusStateParams, + BusStatePayload, + BusStateResponse, + ConsensusAcceptBlockParams, + ConsensusAcceptBlockPayload, + ConsensusAcceptBlockResponse, + ConsensusStateParams, + ConsensusStatePayload, + ConsensusStateResponse, + ContractAcquireParams, + ContractAcquirePayload, + ContractAcquireResponse, + ContractDeleteParams, + ContractDeletePayload, + ContractDeleteResponse, + ContractMetricsParams, + ContractMetricsPayload, + ContractMetricsResponse, + ContractParams, + ContractPayload, + ContractRenewedParams, + ContractRenewedPayload, + ContractRenewedResponse, + ContractResponse, + ContractSetChurnMetricsParams, + ContractSetChurnMetricsPayload, + ContractSetChurnMetricsResponse, + ContractSetMetricsParams, + ContractSetMetricsPayload, + ContractSetMetricsResponse, + ContractSetUpdateParams, + ContractSetUpdatePayload, + ContractSetUpdateResponse, + ContractSetsParams, + ContractSetsPayload, + ContractSetsResponse, + ContractsAddParams, + ContractsAddPayload, + ContractsAddResponse, + ContractsParams, + ContractsPayload, + ContractsReleaseParams, + ContractsReleasePayload, + ContractsReleaseResponse, + ContractsResponse, + HostInteractionParams, + HostInteractionPayload, + HostInteractionResponse, + HostParams, + HostPayload, + HostResetLostSectorCountParams, + HostResetLostSectorCountPayload, + HostResetLostSectorCountResponse, + HostResponse, + HostsAllowlistParams, + HostsAllowlistPayload, + HostsAllowlistResponse, + HostsAllowlistUpdateParams, + HostsAllowlistUpdatePayload, + HostsAllowlistUpdateResponse, + HostsBlocklistParams, + HostsBlocklistPayload, + HostsBlocklistResponse, + HostsBlocklistUpdateParams, + HostsBlocklistUpdatePayload, + HostsBlocklistUpdateResponse, + HostsParams, + HostsPayload, + HostsResponse, + HostsSearchParams, + HostsSearchPayload, + HostsSearchResponse, + MultipartUploadAbortParams, + MultipartUploadAbortPayload, + MultipartUploadAbortResponse, + MultipartUploadAddPartParams, + MultipartUploadAddPartPayload, + MultipartUploadAddPartResponse, + MultipartUploadCompleteParams, + MultipartUploadCompletePayload, + MultipartUploadCompleteResponse, + MultipartUploadCreateParams, + MultipartUploadCreatePayload, + MultipartUploadCreateResponse, + MultipartUploadListPartsParams, + MultipartUploadListPartsPayload, + MultipartUploadListPartsResponse, + MultipartUploadListUploadsParams, + MultipartUploadListUploadsPayload, + MultipartUploadListUploadsResponse, + ObjectAddParams, + ObjectAddPayload, + ObjectAddResponse, + ObjectDeleteParams, + ObjectDeletePayload, + ObjectDeleteResponse, + ObjectDirectoryParams, + ObjectDirectoryPayload, + ObjectDirectoryResponse, + ObjectListParams, + ObjectListPayload, + ObjectListResponse, + ObjectParams, + ObjectPayload, + ObjectRenameParams, + ObjectRenamePayload, + ObjectRenameResponse, + ObjectResponse, + ObjectSearchParams, + ObjectSearchPayload, + ObjectSearchResponse, + ObjectsStatsParams, + ObjectsStatsPayload, + ObjectsStatsResponse, + Setting, + SettingParams, + SettingPayload, + SettingResponse, + SettingUpdateParams, + SettingUpdatePayload, + SettingUpdateResponse, + SettingsParams, + SettingsPayload, + SettingsResponse, + SlabObjectsParams, + SlabObjectsPayload, + SlabObjectsResponse, + SyncerAddressParams, + SyncerAddressPayload, + SyncerAddressResponse, + SyncerConnectParams, + SyncerConnectPayload, + SyncerConnectResponse, + SyncerPeersParams, + SyncerPeersPayload, + SyncerPeersResponse, + TxPoolBroadcastParams, + TxPoolBroadcastPayload, + TxPoolBroadcastResponse, + TxPoolFeeParams, + TxPoolFeePayload, + TxPoolFeeResponse, + TxPoolTransactionsParams, + TxPoolTransactionsPayload, + TxPoolTransactionsResponse, + WalletAddressesParams, + WalletAddressesPayload, + WalletAddressesResponse, + WalletDiscardParams, + WalletDiscardPayload, + WalletDiscardResponse, + WalletFundParams, + WalletFundPayload, + WalletFundResponse, + WalletMetricsParams, + WalletMetricsPayload, + WalletMetricsResponse, + WalletParams, + WalletPayload, + WalletPendingParams, + WalletPendingPayload, + WalletPendingResponse, + WalletPrepareFormParams, + WalletPrepareFormPayload, + WalletPrepareFormResponse, + WalletPrepareRenewParams, + WalletPrepareRenewPayload, + WalletPrepareRenewResponse, + WalletRedistributeParams, + WalletRedistributePayload, + WalletRedistributeResponse, + WalletResponse, + WalletSignParams, + WalletSignPayload, + WalletSignResponse, + WalletTransactionsParams, + WalletTransactionsPayload, + WalletTransactionsResponse, + WalletUtxoParams, + WalletUtxoPayload, + WalletUtxoResponse, + busAccountIdResetdriftRoute, + busAlertsDismissRoute, + busAlertsRoute, + busBucketNamePolicyRoute, + busBucketNameRoute, + busBucketsRoute, + busConsensusAcceptblockRoute, + busConsensusStateRoute, + busContractIdAcquireRoute, + busContractIdNewRoute, + busContractIdReleaseRoute, + busContractIdRenewedRoute, + busContractIdRoute, + busContractsRoute, + busContractsSetsRoute, + busContractsSetsSetRoute, + busHostHostKeyRoute, + busHostPublicKeyResetlostsectorsRoute, + busHostsAllowlistRoute, + busHostsBlocklistRoute, + busHostsHostKeyRoute, + busHostsRoute, + busMetricChurnRoute, + busMetricContractRoute, + busMetricContractsetRoute, + busMetricWalletRoute, + busMultipartAbortRoute, + busMultipartCompleteRoute, + busMultipartCreateRoute, + busMultipartListpartsRoute, + busMultipartListuploadsRoute, + busMultipartPartRoute, + busObjectsKeyRoute, + busObjectsListRoute, + busObjectsRenameRoute, + busSearchHostsRoute, + busSearchObjectsRoute, + busSettingKeyRoute, + busSettingsRoute, + busSlabKeyObjectsRoute, + busStateRoute, + busStatsObjectsRoute, + busSyncerAddrRoute, + busSyncerConnectRoute, + busSyncerPeersRoute, + busTxpoolBroadcastRoute, + busTxpoolFeeRoute, + busTxpoolTransactionsRoute, + busWalletAddressesRoute, + busWalletDiscardRoute, + busWalletFundRoute, + busWalletOutputsRoute, + busWalletPendingRoute, + busWalletPrepareFormRoute, + busWalletRedistributeRoute, + busWalletRoute, + busWalletSignRoute, + busWalletTransactionsRoute, +} from '@siafoundation/renterd-types' +import { buildRequestHandler, initAxios } from '@siafoundation/request' + +export function Bus({ api, password }: { api: string; password?: string }) { + const axios = initAxios(api, password) + return { + busState: buildRequestHandler< + BusStateParams, + BusStatePayload, + BusStateResponse + >(axios, 'get', busStateRoute), + consensusState: buildRequestHandler< + ConsensusStateParams, + ConsensusStatePayload, + ConsensusStateResponse + >(axios, 'get', busConsensusStateRoute), + consensusAcceptBlock: buildRequestHandler< + ConsensusAcceptBlockParams, + ConsensusAcceptBlockPayload, + ConsensusAcceptBlockResponse + >(axios, 'post', busConsensusAcceptblockRoute), + syncerPeers: buildRequestHandler< + SyncerPeersParams, + SyncerPeersPayload, + SyncerPeersResponse + >(axios, 'get', busSyncerPeersRoute), + syncerConnect: buildRequestHandler< + SyncerConnectParams, + SyncerConnectPayload, + SyncerConnectResponse + >(axios, 'post', busSyncerConnectRoute), + syncerAddress: buildRequestHandler< + SyncerAddressParams, + SyncerAddressPayload, + SyncerAddressResponse + >(axios, 'get', busSyncerAddrRoute), + txPoolFee: buildRequestHandler< + TxPoolFeeParams, + TxPoolFeePayload, + TxPoolFeeResponse + >(axios, 'get', busTxpoolFeeRoute), + txPoolTransactions: buildRequestHandler< + TxPoolTransactionsParams, + TxPoolTransactionsPayload, + TxPoolTransactionsResponse + >(axios, 'get', busTxpoolTransactionsRoute), + txPoolBroadcast: buildRequestHandler< + TxPoolBroadcastParams, + TxPoolBroadcastPayload, + TxPoolBroadcastResponse + >(axios, 'post', busTxpoolBroadcastRoute), + wallet: buildRequestHandler( + axios, + 'get', + busWalletRoute + ), + walletAddresses: buildRequestHandler< + WalletAddressesParams, + WalletAddressesPayload, + WalletAddressesResponse + >(axios, 'get', busWalletAddressesRoute), + walletTransactions: buildRequestHandler< + WalletTransactionsParams, + WalletTransactionsPayload, + WalletTransactionsResponse + >(axios, 'get', busWalletTransactionsRoute), + walletUtxos: buildRequestHandler< + WalletUtxoParams, + WalletUtxoPayload, + WalletUtxoResponse + >(axios, 'get', busWalletOutputsRoute), + walletFund: buildRequestHandler< + WalletFundParams, + WalletFundPayload, + WalletFundResponse + >(axios, 'post', busWalletFundRoute), + walletSign: buildRequestHandler< + WalletSignParams, + WalletSignPayload, + WalletSignResponse + >(axios, 'post', busWalletSignRoute), + walletRedistribute: buildRequestHandler< + WalletRedistributeParams, + WalletRedistributePayload, + WalletRedistributeResponse + >(axios, 'post', busWalletRedistributeRoute), + walletDiscard: buildRequestHandler< + WalletDiscardParams, + WalletDiscardPayload, + WalletDiscardResponse + >(axios, 'post', busWalletDiscardRoute), + walletPrepareForm: buildRequestHandler< + WalletPrepareFormParams, + WalletPrepareFormPayload, + WalletPrepareFormResponse + >(axios, 'post', busWalletPrepareFormRoute), + walletPrepareRenew: buildRequestHandler< + WalletPrepareRenewParams, + WalletPrepareRenewPayload, + WalletPrepareRenewResponse + >(axios, 'post', '/bus/wallet/prepare/form'), + walletPending: buildRequestHandler< + WalletPendingParams, + WalletPendingPayload, + WalletPendingResponse + >(axios, 'get', busWalletPendingRoute), + hosts: buildRequestHandler( + axios, + 'get', + busHostsRoute + ), + hostsSearch: buildRequestHandler< + HostsSearchParams, + HostsSearchPayload, + HostsSearchResponse + >(axios, 'post', busSearchHostsRoute), + host: buildRequestHandler( + axios, + 'get', + busHostHostKeyRoute + ), + hostsInteractionAdd: buildRequestHandler< + HostInteractionParams, + HostInteractionPayload, + HostInteractionResponse + >(axios, 'post', busHostsHostKeyRoute), + hostsBlocklist: buildRequestHandler< + HostsBlocklistParams, + HostsBlocklistPayload, + HostsBlocklistResponse + >(axios, 'get', busHostsBlocklistRoute), + hostsAllowlist: buildRequestHandler< + HostsAllowlistParams, + HostsAllowlistPayload, + HostsAllowlistResponse + >(axios, 'get', busHostsAllowlistRoute), + hostsAllowlistUpdate: buildRequestHandler< + HostsAllowlistUpdateParams, + HostsAllowlistUpdatePayload, + HostsAllowlistUpdateResponse + >(axios, 'put', busHostsAllowlistRoute), + hostsBlocklistUpdate: buildRequestHandler< + HostsBlocklistUpdateParams, + HostsBlocklistUpdatePayload, + HostsBlocklistUpdateResponse + >(axios, 'put', busHostsBlocklistRoute), + hostResetLostSectorCount: buildRequestHandler< + HostResetLostSectorCountParams, + HostResetLostSectorCountPayload, + HostResetLostSectorCountResponse + >(axios, 'post', busHostPublicKeyResetlostsectorsRoute), + accountResetDrift: buildRequestHandler< + AccountResetDriftParams, + AccountResetDriftPayload, + AccountResetDriftResponse + >(axios, 'post', busAccountIdResetdriftRoute), + contracts: buildRequestHandler< + ContractsParams, + ContractsPayload, + ContractsResponse + >(axios, 'get', busContractsRoute), + contractsAcquire: buildRequestHandler< + ContractAcquireParams, + ContractAcquirePayload, + ContractAcquireResponse + >(axios, 'post', busContractIdAcquireRoute), + contractsRelease: buildRequestHandler< + ContractsReleaseParams, + ContractsReleasePayload, + ContractsReleaseResponse + >(axios, 'post', busContractIdReleaseRoute), + contract: buildRequestHandler< + ContractParams, + ContractPayload, + ContractResponse + >(axios, 'get', busContractIdRoute), + contractAdd: buildRequestHandler< + ContractsAddParams, + ContractsAddPayload, + ContractsAddResponse + >(axios, 'post', busContractIdNewRoute), + contractRenew: buildRequestHandler< + ContractRenewedParams, + ContractRenewedPayload, + ContractRenewedResponse + >(axios, 'post', busContractIdRenewedRoute), + contractDelete: buildRequestHandler< + ContractDeleteParams, + ContractDeletePayload, + ContractDeleteResponse + >(axios, 'delete', busContractIdRoute), + contractSets: buildRequestHandler< + ContractSetsParams, + ContractSetsPayload, + ContractSetsResponse + >(axios, 'get', busContractsSetsRoute), + contractSetUpdate: buildRequestHandler< + ContractSetUpdateParams, + ContractSetUpdatePayload, + ContractSetUpdateResponse + >(axios, 'put', busContractsSetsSetRoute), + buckets: buildRequestHandler< + BucketsParams, + BucketsPayload, + BucketsResponse + >(axios, 'get', busBucketsRoute), + bucket: buildRequestHandler( + axios, + 'get', + busBucketNameRoute + ), + bucketCreate: buildRequestHandler< + BucketCreateParams, + BucketCreatePayload, + BucketCreateResponse + >(axios, 'post', busBucketsRoute), + bucketPolicyUpdate: buildRequestHandler< + BucketPolicyUpdateParams, + BucketPolicyUpdatePayload, + BucketPolicyUpdateResponse + >(axios, 'put', busBucketNamePolicyRoute), + bucketDelete: buildRequestHandler< + BucketDeleteParams, + BucketDeletePayload, + BucketDeleteResponse + >(axios, 'delete', busBucketNameRoute), + objectDirectory: buildRequestHandler< + ObjectDirectoryParams, + ObjectDirectoryPayload, + ObjectDirectoryResponse + >(axios, 'get', busObjectsKeyRoute), + objectList: buildRequestHandler< + ObjectListParams, + ObjectListPayload, + ObjectListResponse + >(axios, 'post', busObjectsListRoute), + object: buildRequestHandler( + axios, + 'get', + busObjectsKeyRoute + ), + objectSearch: buildRequestHandler< + ObjectSearchParams, + ObjectSearchPayload, + ObjectSearchResponse + >(axios, 'get', busSearchObjectsRoute), + objectAdd: buildRequestHandler< + ObjectAddParams, + ObjectAddPayload, + ObjectAddResponse + >(axios, 'put', busObjectsKeyRoute), + objectRename: buildRequestHandler< + ObjectRenameParams, + ObjectRenamePayload, + ObjectRenameResponse + >(axios, 'post', busObjectsRenameRoute), + objectDelete: buildRequestHandler< + ObjectDeleteParams, + ObjectDeletePayload, + ObjectDeleteResponse + >(axios, 'delete', busObjectsKeyRoute), + objectStats: buildRequestHandler< + ObjectsStatsParams, + ObjectsStatsPayload, + ObjectsStatsResponse + >(axios, 'get', busStatsObjectsRoute), + settings: buildRequestHandler< + SettingsParams, + SettingsPayload, + SettingsResponse + >(axios, 'get', busSettingsRoute), + setting: buildRequestHandler< + SettingParams, + SettingPayload, + SettingResponse + >(axios, 'get', busSettingKeyRoute), + settingUpdate: buildRequestHandler< + SettingUpdateParams, + SettingUpdatePayload, + SettingUpdateResponse + >(axios, 'put', busSettingKeyRoute), + alerts: buildRequestHandler( + axios, + 'get', + busAlertsRoute + ), + alertsDismiss: buildRequestHandler< + AlertsDismissParams, + AlertsDismissPayload, + AlertsDismissResponse + >(axios, 'post', busAlertsDismissRoute), + slabObjects: buildRequestHandler< + SlabObjectsParams, + SlabObjectsPayload, + SlabObjectsResponse + >(axios, 'get', busSlabKeyObjectsRoute), + contractMetrics: buildRequestHandler< + ContractMetricsParams, + ContractMetricsPayload, + ContractMetricsResponse + >(axios, 'get', busMetricContractRoute), + contractSetMetrics: buildRequestHandler< + ContractSetMetricsParams, + ContractSetMetricsPayload, + ContractSetMetricsResponse + >(axios, 'get', busMetricContractsetRoute), + contractSetChurnMetrics: buildRequestHandler< + ContractSetChurnMetricsParams, + ContractSetChurnMetricsPayload, + ContractSetChurnMetricsResponse + >(axios, 'get', busMetricChurnRoute), + walletMetrics: buildRequestHandler< + WalletMetricsParams, + WalletMetricsPayload, + WalletMetricsResponse + >(axios, 'get', busMetricWalletRoute), + multipartUploadCreate: buildRequestHandler< + MultipartUploadCreateParams, + MultipartUploadCreatePayload, + MultipartUploadCreateResponse + >(axios, 'post', busMultipartCreateRoute), + multipartUploadComplete: buildRequestHandler< + MultipartUploadCompleteParams, + MultipartUploadCompletePayload, + MultipartUploadCompleteResponse + >(axios, 'post', busMultipartCompleteRoute), + multipartUploadAbort: buildRequestHandler< + MultipartUploadAbortParams, + MultipartUploadAbortPayload, + MultipartUploadAbortResponse + >(axios, 'post', busMultipartAbortRoute), + multipartUploadListParts: buildRequestHandler< + MultipartUploadListPartsParams, + MultipartUploadListPartsPayload, + MultipartUploadListPartsResponse + >(axios, 'post', busMultipartListpartsRoute), + multipartUploadListUploads: buildRequestHandler< + MultipartUploadListUploadsParams, + MultipartUploadListUploadsPayload, + MultipartUploadListUploadsResponse + >(axios, 'post', busMultipartListuploadsRoute), + multipartUploadAddPart: buildRequestHandler< + MultipartUploadAddPartParams, + MultipartUploadAddPartPayload, + MultipartUploadAddPartResponse + >(axios, 'post', busMultipartPartRoute), + } +} diff --git a/libs/renterd-js/src/example.ts b/libs/renterd-js/src/example.ts new file mode 100644 index 000000000..60767d4e7 --- /dev/null +++ b/libs/renterd-js/src/example.ts @@ -0,0 +1,79 @@ +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) + }, + }, + }) +} diff --git a/libs/renterd-js/src/index.ts b/libs/renterd-js/src/index.ts new file mode 100644 index 000000000..294ac1095 --- /dev/null +++ b/libs/renterd-js/src/index.ts @@ -0,0 +1,3 @@ +export * from './bus' +export * from './autopilot' +export * from './worker' diff --git a/libs/renterd-js/src/worker.ts b/libs/renterd-js/src/worker.ts new file mode 100644 index 000000000..57a0ef4a1 --- /dev/null +++ b/libs/renterd-js/src/worker.ts @@ -0,0 +1,53 @@ +import { + MultipartUploadPartParams, + MultipartUploadPartPayload, + MultipartUploadPartResponse, + ObjectDownloadParams, + ObjectDownloadPayload, + ObjectDownloadResponse, + ObjectUploadParams, + ObjectUploadPayload, + ObjectUploadResponse, + RhpScanParams, + RhpScanPayload, + RhpScanResponse, + WorkerStateParams, + WorkerStatePayload, + WorkerStateResponse, + workerMultipartKeyRoute, + workerObjectsKeyRoute, + workerRhpScanRoute, + workerStateRoute, +} from '@siafoundation/renterd-types' +import { buildRequestHandler, initAxios } from '@siafoundation/request' + +export function Worker({ api, password }: { api: string; password?: string }) { + const axios = initAxios(api, password) + return { + workerState: buildRequestHandler< + WorkerStateParams, + WorkerStatePayload, + WorkerStateResponse + >(axios, 'get', workerStateRoute), + objectDownload: buildRequestHandler< + ObjectDownloadParams, + ObjectDownloadPayload, + ObjectDownloadResponse + >(axios, 'get', workerObjectsKeyRoute), + objectUpload: buildRequestHandler< + ObjectUploadParams, + ObjectUploadPayload, + ObjectUploadResponse + >(axios, 'put', workerObjectsKeyRoute), + multipartUploadPart: buildRequestHandler< + MultipartUploadPartParams, + MultipartUploadPartPayload, + MultipartUploadPartResponse + >(axios, 'put', workerMultipartKeyRoute), + rhpScan: buildRequestHandler< + RhpScanParams, + RhpScanPayload, + RhpScanResponse + >(axios, 'post', workerRhpScanRoute), + } +} diff --git a/libs/renterd-js/tsconfig.json b/libs/renterd-js/tsconfig.json new file mode 100644 index 000000000..4c089585e --- /dev/null +++ b/libs/renterd-js/tsconfig.json @@ -0,0 +1,25 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "jsx": "react-jsx", + "allowJs": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/renterd-js/tsconfig.lib.json b/libs/renterd-js/tsconfig.lib.json new file mode 100644 index 000000000..d73537814 --- /dev/null +++ b/libs/renterd-js/tsconfig.lib.json @@ -0,0 +1,22 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [ + "node", + "@nx/react/typings/cssmodule.d.ts", + "@nx/react/typings/image.d.ts" + ] + }, + "exclude": [ + "**/*.spec.ts", + "**/*.test.ts", + "**/*.spec.tsx", + "**/*.test.tsx", + "**/*.spec.js", + "**/*.test.js", + "**/*.spec.jsx", + "**/*.test.jsx" + ], + "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"] +} diff --git a/libs/renterd-js/tsconfig.spec.json b/libs/renterd-js/tsconfig.spec.json new file mode 100644 index 000000000..503e9a83d --- /dev/null +++ b/libs/renterd-js/tsconfig.spec.json @@ -0,0 +1,19 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.test.tsx", + "src/**/*.spec.tsx", + "src/**/*.test.js", + "src/**/*.spec.js", + "src/**/*.test.jsx", + "src/**/*.spec.jsx", + "src/**/*.d.ts" + ] +} diff --git a/libs/request/src/index.ts b/libs/request/src/index.ts index 73e21e379..cbdf3f3af 100644 --- a/libs/request/src/index.ts +++ b/libs/request/src/index.ts @@ -36,7 +36,7 @@ export function buildRequestHandler< >(axios: Axios, method: Method, route: string) { type Args = Params extends void ? Data extends void - ? { config?: AxiosRequestConfig } + ? { config?: AxiosRequestConfig } | void : { data: Data; config?: AxiosRequestConfig } : Data extends void ? { @@ -51,7 +51,13 @@ export function buildRequestHandler< return (args: Args) => { // args is sometimes undefined - const a = args || ({} as Args) + const a = { + ...args, + } as { + params?: Params + data?: Data + config?: AxiosRequestConfig + } const paramRoute = 'params' in a ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion diff --git a/tsconfig.base.json b/tsconfig.base.json index 83f3665b6..98ce748ce 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -26,6 +26,7 @@ "@siafoundation/react-icons": ["libs/react-icons/src/index.ts"], "@siafoundation/renterd-types": ["libs/renterd-types/src/index.ts"], "@siafoundation/renterd-react": ["libs/renterd-react/src/index.ts"], + "@siafoundation/renterd-js": ["libs/renterd-js/src/index.ts"], "@siafoundation/walletd-types": ["libs/walletd-types/src/index.ts"], "@siafoundation/walletd-react": ["libs/walletd-react/src/index.ts"], // "buildLibsFromSource": false does not work with the next executor