diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 00000000..24002cb8 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,35 @@ +name: publish +on: + release: + types: [published] +jobs: + publish: + name: publish + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v1 + - name: Get latest tag + id: latesttag + uses: "WyriHaximus/github-action-get-previous-tag@master" + env: + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + - name: Set up Node + uses: actions/setup-node@v1 + with: + node-version: "12.x" + registry-url: "https://registry.npmjs.org" + - name: Get Dependencies + run: | + npm install + npm install -g json + npm install -g yaml-cli + - name: Build Library + run: | + npm run build + - name: Publish NPM Package + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} + run: | + json -I -f package.json -e 'this.version=("${{ steps.latesttag.outputs.tag }}").replace("v", "")' + npm publish --access=public diff --git a/README.md b/README.md index fe9b7ed6..6a01a8b9 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,9 @@ npm i @textile/powergate-client Start by creating an instance of the client. ```typescript -import { createPow } from '@textile/powergate-client' +import { createPow } from "@textile/powergate-client" -const host = 'http://0.0.0.0:6002' // or whatever powergate instance you want +const host = "http://0.0.0.0:6002" // or whatever powergate instance you want const pow = createPow({ host }) ``` @@ -74,13 +74,14 @@ pow.setToken(authToken) Now, the FFS API is available for you to use. ```typescript -import fs from 'fs' +import fs from "fs" +import { ffs } from "@textile/powergate-client" // get wallet addresses associated with your FFS instance const { addrsList } = await pow.ffs.addrs() // create a new address associated with your ffs instance -const { addr } = await pow.ffs.newAddr('my new addr') +const { addr } = await pow.ffs.newAddr("my new addr") // get general info about your ffs instance const { info } = await pow.ffs.info() @@ -94,12 +95,12 @@ const { jobId } = await pow.ffs.pushConfig(cid) // watch the FFS job status to see the storage process progressing const cancel = pow.ffs.watchJobs((job) => { - if (job.status === JobStatus.CANCELED) { - console.log('job canceled') - } else if (job.status === JobStatus.FAILED) { - console.log('job failed') - } else if (job.status === JobStatus.SUCCESS) { - console.log('job success!') + if (job.status === ffs.JobStatus.CANCELED) { + console.log("job canceled") + } else if (job.status === ffs.JobStatus.FAILED) { + console.log("job failed") + } else if (job.status === ffs.JobStatus.SUCCESS) { + console.log("job success!") } }, jobId) @@ -118,7 +119,7 @@ const { cidinfo } = await pow.ffs.show(cid) const bytes = await pow.ffs.get(cid) // senf FIL from an address managed by your FFS instance to any other address -await pow.ffs.sendFil(addrsList[0].addr, '', 1000) +await pow.ffs.sendFil(addrsList[0].addr, "", 1000) ``` There are also several useful examples included in the `*.spec.ts` files of this repo. diff --git a/package-lock.json b/package-lock.json index 74d5640c..ffc2ea66 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@textile/powergate-client", - "version": "0.1.0-beta.8", + "version": "0.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 18d6addb..c7e6db44 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@textile/powergate-client", - "version": "0.1.0-beta.8", + "version": "0.0.0", "description": "Client for Textile's Powergate", "main": "dist/index", "types": "dist/index", diff --git a/src/ffs/index.spec.ts b/src/ffs/index.spec.ts index b6e96bfb..20618213 100644 --- a/src/ffs/index.spec.ts +++ b/src/ffs/index.spec.ts @@ -1,28 +1,23 @@ -import { - AddrInfo, - CidConfig, - DefaultConfig, - JobStatus, -} from "@textile/grpc-powergate-client/dist/ffs/rpc/rpc_pb" import { expect } from "chai" import fs from "fs" import { createFFS, withConfig, withHistory, withOverrideConfig } from "." +import { ffs } from "../types" import { getTransport, host, useToken } from "../util" describe("ffs", () => { const { getMeta, setToken } = useToken("") - const ffs = createFFS({ host, transport: getTransport() }, getMeta) + const c = createFFS({ host, transport: getTransport() }, getMeta) let instanceId: string - let initialAddrs: AddrInfo.AsObject[] - let defaultConfig: DefaultConfig.AsObject + let initialAddrs: ffs.AddrInfo.AsObject[] + let defaultConfig: ffs.DefaultConfig.AsObject let cid: string - let defaultCidConfig: CidConfig.AsObject + let defaultCidConfig: ffs.CidConfig.AsObject it("should create an instance", async function () { this.timeout(30000) - const res = await ffs.create() + const res = await c.create() expect(res.id).not.empty expect(res.token).not.empty instanceId = res.id @@ -32,53 +27,53 @@ describe("ffs", () => { }) it("should list instances", async () => { - const res = await ffs.list() + const res = await c.list() expect(res.instancesList).length.greaterThan(0) }) it("should get instance id", async () => { - const res = await ffs.id() + const res = await c.id() expect(res.id).eq(instanceId) }) it("should get addrs", async () => { - const res = await ffs.addrs() + const res = await c.addrs() initialAddrs = res.addrsList expect(initialAddrs).length.greaterThan(0) }) it("should get the default config", async () => { - const res = await ffs.defaultConfig() + const res = await c.defaultConfig() expect(res.defaultConfig).not.undefined // eslint-disable-next-line @typescript-eslint/no-non-null-assertion defaultConfig = res.defaultConfig! }) it("should create a new addr", async () => { - const res = await ffs.newAddr("my addr") + const res = await c.newAddr("my addr") expect(res.addr).length.greaterThan(0) - const addrsRes = await ffs.addrs() + const addrsRes = await c.addrs() expect(addrsRes.addrsList).length(initialAddrs.length + 1) }) it("should set default config", async () => { - await ffs.setDefaultConfig(defaultConfig) + await c.setDefaultConfig(defaultConfig) }) it("should get info", async () => { - const res = await ffs.info() + const res = await c.info() expect(res.info).not.undefined }) it("should add to hot", async () => { const buffer = fs.readFileSync(`sample-data/samplefile`) - const res = await ffs.addToHot(buffer) + const res = await c.addToHot(buffer) expect(res.cid).length.greaterThan(0) cid = res.cid }) it("should get default cid config", async () => { - const res = await ffs.getDefaultCidConfig(cid) + const res = await c.getDefaultCidConfig(cid) expect(res.config?.cid).equal(cid) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion defaultCidConfig = res.config! @@ -87,18 +82,18 @@ describe("ffs", () => { let jobId: string it("should push config", async () => { - const res = await ffs.pushConfig(cid, withOverrideConfig(false), withConfig(defaultCidConfig)) + const res = await c.pushConfig(cid, withOverrideConfig(false), withConfig(defaultCidConfig)) expect(res.jobId).length.greaterThan(0) jobId = res.jobId }) it("should watch job", function (done) { this.timeout(180000) - const cancel = ffs.watchJobs((job) => { + const cancel = c.watchJobs((job) => { expect(job.errCause).empty - expect(job.status).not.equal(JobStatus.JOB_STATUS_CANCELED) - expect(job.status).not.equal(JobStatus.JOB_STATUS_FAILED) - if (job.status === JobStatus.JOB_STATUS_SUCCESS) { + expect(job.status).not.equal(ffs.JobStatus.JOB_STATUS_CANCELED) + expect(job.status).not.equal(ffs.JobStatus.JOB_STATUS_FAILED) + if (job.status === ffs.JobStatus.JOB_STATUS_SUCCESS) { cancel() done() } @@ -107,7 +102,7 @@ describe("ffs", () => { it("should watch logs", function (done) { this.timeout(10000) - const cancel = ffs.watchLogs( + const cancel = c.watchLogs( (logEvent) => { expect(logEvent.cid).not.empty cancel() @@ -119,17 +114,17 @@ describe("ffs", () => { }) it("should get cid config", async () => { - const res = await ffs.getCidConfig(cid) + const res = await c.getCidConfig(cid) expect(res.config?.cid).equal(cid) }) it("should show", async () => { - const res = await ffs.show(cid) + const res = await c.show(cid) expect(res.cidInfo).not.undefined }) it("should show all", async () => { - const res = await ffs.showAll() + const res = await c.showAll() expect(res).not.empty }) @@ -137,9 +132,9 @@ describe("ffs", () => { it("should replace", async () => { buffer = fs.readFileSync(`sample-data/samplefile2`) - const res0 = await ffs.addToHot(buffer) + const res0 = await c.addToHot(buffer) expect(res0.cid).length.greaterThan(0) - const res1 = await ffs.replace(cid, res0.cid) + const res1 = await c.replace(cid, res0.cid) expect(res1.jobId).length.greaterThan(0) cid = res0.cid jobId = res1.jobId @@ -147,11 +142,11 @@ describe("ffs", () => { it("should watch replace job", function (done) { this.timeout(180000) - const cancel = ffs.watchJobs((job) => { + const cancel = c.watchJobs((job) => { expect(job.errCause).empty - expect(job.status).not.equal(JobStatus.JOB_STATUS_CANCELED) - expect(job.status).not.equal(JobStatus.JOB_STATUS_FAILED) - if (job.status === JobStatus.JOB_STATUS_SUCCESS) { + expect(job.status).not.equal(ffs.JobStatus.JOB_STATUS_CANCELED) + expect(job.status).not.equal(ffs.JobStatus.JOB_STATUS_FAILED) + if (job.status === ffs.JobStatus.JOB_STATUS_SUCCESS) { cancel() done() } @@ -159,12 +154,12 @@ describe("ffs", () => { }) it("should get", async () => { - const bytes = await ffs.get(cid) + const bytes = await c.get(cid) expect(bytes.byteLength).equal(buffer.byteLength) }) it("should list payment channels", async () => { - await ffs.listPayChannels() + await c.listPayChannels() }) it("should create a payment channel", async () => { @@ -176,7 +171,7 @@ describe("ffs", () => { }) it("should push disable storage job", async () => { - const newConf: CidConfig.AsObject = { + const newConf: ffs.CidConfig.AsObject = { cid, repairable: false, cold: { @@ -187,18 +182,18 @@ describe("ffs", () => { enabled: false, }, } - const res0 = await ffs.pushConfig(cid, withOverrideConfig(true), withConfig(newConf)) + const res0 = await c.pushConfig(cid, withOverrideConfig(true), withConfig(newConf)) expect(res0).not.undefined jobId = res0.jobId }) it("should watch disable storage job", function (done) { this.timeout(180000) - const cancel = ffs.watchJobs((job) => { + const cancel = c.watchJobs((job) => { expect(job.errCause).empty - expect(job.status).not.equal(JobStatus.JOB_STATUS_CANCELED) - expect(job.status).not.equal(JobStatus.JOB_STATUS_FAILED) - if (job.status === JobStatus.JOB_STATUS_SUCCESS) { + expect(job.status).not.equal(ffs.JobStatus.JOB_STATUS_CANCELED) + expect(job.status).not.equal(ffs.JobStatus.JOB_STATUS_FAILED) + if (job.status === ffs.JobStatus.JOB_STATUS_SUCCESS) { cancel() done() } @@ -206,16 +201,16 @@ describe("ffs", () => { }) it("should remove", async () => { - await ffs.remove(cid) + await c.remove(cid) }) it("should send fil", async () => { - const addrs = await ffs.addrs() + const addrs = await c.addrs() expect(addrs.addrsList).lengthOf(2) - await ffs.sendFil(addrs.addrsList[0].addr, addrs.addrsList[1].addr, 10) + await c.sendFil(addrs.addrsList[0].addr, addrs.addrsList[1].addr, 10) }) it("should close", async () => { - await ffs.close() + await c.close() }) }) diff --git a/src/ffs/index.ts b/src/ffs/index.ts index 4e182e74..5ff39dca 100644 --- a/src/ffs/index.ts +++ b/src/ffs/index.ts @@ -1,70 +1,19 @@ import { grpc } from "@improbable-eng/grpc-web" -import { - AddrsRequest, - AddrsResponse, - AddToHotRequest, - AddToHotResponse, - CidConfig, - CloseRequest, - ColdConfig, - CreatePayChannelRequest, - CreatePayChannelResponse, - CreateRequest, - CreateResponse, - DefaultConfig, - DefaultConfigRequest, - DefaultConfigResponse, - FilConfig, - FilRenew, - GetCidConfigRequest, - GetCidConfigResponse, - GetDefaultCidConfigRequest, - GetDefaultCidConfigResponse, - GetRequest, - HotConfig, - IDRequest, - IDResponse, - InfoRequest, - InfoResponse, - IpfsConfig, - Job, - ListAPIRequest, - ListAPIResponse, - ListPayChannelsRequest, - ListPayChannelsResponse, - LogEntry, - NewAddrRequest, - NewAddrResponse, - PushConfigRequest, - PushConfigResponse, - RedeemPayChannelRequest, - RemoveRequest, - ReplaceRequest, - ReplaceResponse, - SendFilRequest, - SetDefaultConfigRequest, - ShowAllRequest, - ShowAllResponse, - ShowRequest, - ShowResponse, - WatchJobsRequest, - WatchLogsRequest, -} from "@textile/grpc-powergate-client/dist/ffs/rpc/rpc_pb" import { RPCService, RPCServiceClient, } from "@textile/grpc-powergate-client/dist/ffs/rpc/rpc_pb_service" -import { Config } from "../types" +import { Config, ffs } from "../types" import { promise } from "../util" -type PushConfigOption = (req: PushConfigRequest) => void +type PushConfigOption = (req: ffs.PushConfigRequest) => void /** * Allows you to override an existing storage configuration * @param override Whether or not to override any existing storage configuration * @returns The resulting option */ -export const withOverrideConfig = (override: boolean) => (req: PushConfigRequest) => { +export const withOverrideConfig = (override: boolean) => (req: ffs.PushConfigRequest) => { req.setHasOverrideConfig(true) req.setOverrideConfig(override) } @@ -74,8 +23,8 @@ export const withOverrideConfig = (override: boolean) => (req: PushConfigRequest * @param config The storage configuration to use * @returns The resulting option */ -export const withConfig = (config: CidConfig.AsObject) => (req: PushConfigRequest) => { - const c = new CidConfig() +export const withConfig = (config: ffs.CidConfig.AsObject) => (req: ffs.PushConfigRequest) => { + const c = new ffs.CidConfig() c.setCid(config.cid) c.setRepairable(config.repairable) if (config.hot) { @@ -88,14 +37,14 @@ export const withConfig = (config: CidConfig.AsObject) => (req: PushConfigReques req.setConfig(c) } -type WatchLogsOption = (res: WatchLogsRequest) => void +type WatchLogsOption = (res: ffs.WatchLogsRequest) => void /** * Control whether or not to include the history of log events * @param includeHistory Whether or not to include the history of log events * @returns The resulting option */ -export const withHistory = (includeHistory: boolean) => (req: WatchLogsRequest) => { +export const withHistory = (includeHistory: boolean) => (req: ffs.WatchLogsRequest) => { req.setHistory(includeHistory) } @@ -104,7 +53,7 @@ export const withHistory = (includeHistory: boolean) => (req: WatchLogsRequest) * @param jobId The job id to show events for * @returns The resulting option */ -export const withJobId = (jobId: string) => (req: WatchLogsRequest) => { +export const withJobId = (jobId: string) => (req: ffs.WatchLogsRequest) => { req.setJid(jobId) } @@ -123,8 +72,8 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { */ create: () => promise( - (cb) => client.create(new CreateRequest(), cb), - (res: CreateResponse) => res.toObject(), + (cb) => client.create(new ffs.CreateRequest(), cb), + (res: ffs.CreateResponse) => res.toObject(), ), /** @@ -133,8 +82,8 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { */ list: () => promise( - (cb) => client.listAPI(new ListAPIRequest(), cb), - (res: ListAPIResponse) => res.toObject(), + (cb) => client.listAPI(new ffs.ListAPIRequest(), cb), + (res: ffs.ListAPIResponse) => res.toObject(), ), /** @@ -143,8 +92,8 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { */ id: () => promise( - (cb) => client.iD(new IDRequest(), getMeta(), cb), - (res: IDResponse) => res.toObject(), + (cb) => client.iD(new ffs.IDRequest(), getMeta(), cb), + (res: ffs.IDResponse) => res.toObject(), ), /** @@ -153,8 +102,8 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { */ addrs: () => promise( - (cb) => client.addrs(new AddrsRequest(), getMeta(), cb), - (res: AddrsResponse) => res.toObject(), + (cb) => client.addrs(new ffs.AddrsRequest(), getMeta(), cb), + (res: ffs.AddrsResponse) => res.toObject(), ), /** @@ -163,8 +112,8 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { */ defaultConfig: () => promise( - (cb) => client.defaultConfig(new DefaultConfigRequest(), getMeta(), cb), - (res: DefaultConfigResponse) => res.toObject(), + (cb) => client.defaultConfig(new ffs.DefaultConfigRequest(), getMeta(), cb), + (res: ffs.DefaultConfigResponse) => res.toObject(), ), /** @@ -175,13 +124,13 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @returns Information about the newly created address */ newAddr: (name: string, type?: "bls" | "secp256k1", makeDefault?: boolean) => { - const req = new NewAddrRequest() + const req = new ffs.NewAddrRequest() req.setName(name) req.setAddressType(type || "bls") req.setMakeDefault(makeDefault || false) return promise( (cb) => client.newAddr(req, getMeta(), cb), - (res: NewAddrResponse) => res.toObject(), + (res: ffs.NewAddrResponse) => res.toObject(), ) }, @@ -191,11 +140,11 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @returns The storage config prepped for the provided cid */ getDefaultCidConfig: (cid: string) => { - const req = new GetDefaultCidConfigRequest() + const req = new ffs.GetDefaultCidConfigRequest() req.setCid(cid) return promise( (cb) => client.getDefaultCidConfig(req, getMeta(), cb), - (res: GetDefaultCidConfigResponse) => res.toObject(), + (res: ffs.GetDefaultCidConfigResponse) => res.toObject(), ) }, @@ -205,11 +154,11 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @returns The storage config for the provided cid */ getCidConfig: (cid: string) => { - const req = new GetCidConfigRequest() + const req = new ffs.GetCidConfigRequest() req.setCid(cid) return promise( (cb) => client.getCidConfig(req, getMeta(), cb), - (res: GetCidConfigResponse) => res.toObject(), + (res: ffs.GetCidConfigResponse) => res.toObject(), ) }, @@ -217,8 +166,8 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * Set the default storage config for this FFS instance * @param config The new default storage config */ - setDefaultConfig: (config: DefaultConfig.AsObject) => { - const c = new DefaultConfig() + setDefaultConfig: (config: ffs.DefaultConfig.AsObject) => { + const c = new ffs.DefaultConfig() c.setRepairable(config.repairable) if (config.hot) { c.setHot(hotObjToMessage(config.hot)) @@ -226,7 +175,7 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { if (config.cold) { c.setCold(coldObjToMessage(config.cold)) } - const req = new SetDefaultConfigRequest() + const req = new ffs.SetDefaultConfigRequest() req.setConfig(c) return promise( (cb) => client.setDefaultConfig(req, getMeta(), cb), @@ -242,11 +191,11 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @returns The current storage config for the provided cid */ show: (cid: string) => { - const req = new ShowRequest() + const req = new ffs.ShowRequest() req.setCid(cid) return promise( (cb) => client.show(req, getMeta(), cb), - (res: ShowResponse) => res.toObject(), + (res: ffs.ShowResponse) => res.toObject(), ) }, @@ -256,8 +205,8 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { */ info: () => promise( - (cb) => client.info(new InfoRequest(), getMeta(), cb), - (res: InfoResponse) => res.toObject(), + (cb) => client.info(new ffs.InfoRequest(), getMeta(), cb), + (res: ffs.InfoResponse) => res.toObject(), ), /** @@ -266,8 +215,8 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @param jobs A list of job ids to watch * @returns A function that can be used to cancel watching */ - watchJobs: (handler: (event: Job.AsObject) => void, ...jobs: string[]) => { - const req = new WatchJobsRequest() + watchJobs: (handler: (event: ffs.Job.AsObject) => void, ...jobs: string[]) => { + const req = new ffs.WatchJobsRequest() req.setJidsList(jobs) const stream = client.watchJobs(req, getMeta()) stream.on("data", (res) => { @@ -287,11 +236,11 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @returns A function that can be used to cancel watching */ watchLogs: ( - handler: (event: LogEntry.AsObject) => void, + handler: (event: ffs.LogEntry.AsObject) => void, cid: string, ...opts: WatchLogsOption[] ) => { - const req = new WatchLogsRequest() + const req = new ffs.WatchLogsRequest() req.setCid(cid) opts.forEach((opt) => opt(req)) const stream = client.watchLogs(req, getMeta()) @@ -311,12 +260,12 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @returns The job id of the job executing the storage configuration */ replace: (cid1: string, cid2: string) => { - const req = new ReplaceRequest() + const req = new ffs.ReplaceRequest() req.setCid1(cid1) req.setCid2(cid2) return promise( (cb) => client.replace(req, getMeta(), cb), - (res: ReplaceResponse) => res.toObject(), + (res: ffs.ReplaceResponse) => res.toObject(), ) }, @@ -327,14 +276,14 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @returns The job id of the job executing the storage configuration */ pushConfig: (cid: string, ...opts: PushConfigOption[]) => { - const req = new PushConfigRequest() + const req = new ffs.PushConfigRequest() req.setCid(cid) opts.forEach((opt) => { opt(req) }) return promise( (cb) => client.pushConfig(req, getMeta(), cb), - (res: PushConfigResponse) => res.toObject(), + (res: ffs.PushConfigResponse) => res.toObject(), ) }, @@ -343,7 +292,7 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @param cid The cid to remove */ remove: (cid: string) => { - const req = new RemoveRequest() + const req = new ffs.RemoveRequest() req.setCid(cid) return promise( (cb) => client.remove(req, getMeta(), cb), @@ -367,7 +316,7 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { return tmp } let final = new Uint8Array() - const req = new GetRequest() + const req = new ffs.GetRequest() req.setCid(cid) const stream = client.get(req, getMeta()) stream.on("data", (resp) => { @@ -390,7 +339,7 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @param amount The amount of FIL to send */ sendFil: (from: string, to: string, amount: number) => { - const req = new SendFilRequest() + const req = new ffs.SendFilRequest() req.setFrom(from) req.setTo(to) req.setAmount(amount) @@ -407,7 +356,7 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { */ close: () => promise( - (cb) => client.close(new CloseRequest(), getMeta(), cb), + (cb) => client.close(new ffs.CloseRequest(), getMeta(), cb), () => { // nothing to return }, @@ -421,10 +370,10 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { */ addToHot: (input: Uint8Array) => { // TODO: figure out how to stream data in here, or at least stream to the server - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { const client = grpc.client(RPCService.AddToHot, config) client.onMessage((message) => { - resolve(message.toObject() as AddToHotResponse.AsObject) + resolve(message.toObject() as ffs.AddToHotResponse.AsObject) }) client.onEnd((code, msg) => { if (code !== grpc.Code.OK) { @@ -434,7 +383,7 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { } }) client.start(getMeta()) - const req = new AddToHotRequest() + const req = new ffs.AddToHotRequest() req.setChunk(input) client.send(req) client.finishSend() @@ -447,8 +396,8 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { */ listPayChannels: () => promise( - (cb) => client.listPayChannels(new ListPayChannelsRequest(), getMeta(), cb), - (res: ListPayChannelsResponse) => res.toObject().payChannelsList, + (cb) => client.listPayChannels(new ffs.ListPayChannelsRequest(), getMeta(), cb), + (res: ffs.ListPayChannelsResponse) => res.toObject().payChannelsList, ), /** @@ -459,13 +408,13 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @returns Information about the payment channel */ createPayChannel: (from: string, to: string, amt: number) => { - const req = new CreatePayChannelRequest() + const req = new ffs.CreatePayChannelRequest() req.setFrom(from) req.setTo(to) req.setAmount(amt) return promise( (cb) => client.createPayChannel(req, getMeta(), cb), - (res: CreatePayChannelResponse) => res.toObject(), + (res: ffs.CreatePayChannelResponse) => res.toObject(), ) }, @@ -474,7 +423,7 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { * @param payChannelAddr The address of the payment channel to redeem */ redeemPayChannel: (payChannelAddr: string) => { - const req = new RedeemPayChannelRequest() + const req = new ffs.RedeemPayChannelRequest() req.setPayChannelAddr(payChannelAddr) return promise( (cb) => client.redeemPayChannel(req, getMeta(), cb), @@ -490,17 +439,17 @@ export const createFFS = (config: Config, getMeta: () => grpc.Metadata) => { */ showAll: () => promise( - (cb) => client.showAll(new ShowAllRequest(), getMeta(), cb), - (res: ShowAllResponse) => res.toObject().cidInfosList, + (cb) => client.showAll(new ffs.ShowAllRequest(), getMeta(), cb), + (res: ffs.ShowAllResponse) => res.toObject().cidInfosList, ), } } -function coldObjToMessage(obj: ColdConfig.AsObject) { - const cold = new ColdConfig() +function coldObjToMessage(obj: ffs.ColdConfig.AsObject) { + const cold = new ffs.ColdConfig() cold.setEnabled(obj.enabled) if (obj.filecoin) { - const fc = new FilConfig() + const fc = new ffs.FilConfig() fc.setAddr(obj.filecoin.addr) fc.setCountryCodesList(obj.filecoin.countryCodesList) fc.setDealMinDuration(obj.filecoin.dealMinDuration) @@ -509,7 +458,7 @@ function coldObjToMessage(obj: ColdConfig.AsObject) { fc.setRepFactor(obj.filecoin.repFactor) fc.setTrustedMinersList(obj.filecoin.trustedMinersList) if (obj.filecoin.renew) { - const renew = new FilRenew() + const renew = new ffs.FilRenew() renew.setEnabled(obj.filecoin.renew.enabled) renew.setThreshold(obj.filecoin.renew.threshold) fc.setRenew(renew) @@ -519,12 +468,12 @@ function coldObjToMessage(obj: ColdConfig.AsObject) { return cold } -function hotObjToMessage(obj: HotConfig.AsObject) { - const hot = new HotConfig() +function hotObjToMessage(obj: ffs.HotConfig.AsObject) { + const hot = new ffs.HotConfig() hot.setAllowUnfreeze(obj.allowUnfreeze) hot.setEnabled(obj.enabled) if (obj?.ipfs) { - const ipfs = new IpfsConfig() + const ipfs = new ffs.IpfsConfig() ipfs.setAddTimeout(obj.ipfs.addTimeout) hot.setIpfs(ipfs) } diff --git a/src/health/index.spec.ts b/src/health/index.spec.ts index 04ac38dd..79828de7 100644 --- a/src/health/index.spec.ts +++ b/src/health/index.spec.ts @@ -1,13 +1,13 @@ -import { Status } from "@textile/grpc-powergate-client/dist/health/rpc/rpc_pb" import { expect } from "chai" import { createHealth } from "." +import { health } from "../types" import { getTransport, host } from "../util" describe("health", () => { - const health = createHealth({ host, transport: getTransport() }) + const c = createHealth({ host, transport: getTransport() }) it("should check health", async () => { - const status = await health.check() - expect(status.status).equal(Status.STATUS_OK) + const status = await c.check() + expect(status.status).equal(health.Status.STATUS_OK) }) }) diff --git a/src/health/index.ts b/src/health/index.ts index 41331d18..e4f9206c 100644 --- a/src/health/index.ts +++ b/src/health/index.ts @@ -1,6 +1,5 @@ -import { CheckRequest, CheckResponse } from "@textile/grpc-powergate-client/dist/health/rpc/rpc_pb" import { RPCServiceClient } from "@textile/grpc-powergate-client/dist/health/rpc/rpc_pb_service" -import { Config } from "../types" +import { Config, health } from "../types" import { promise } from "../util" /** @@ -17,8 +16,8 @@ export const createHealth = (config: Config) => { */ check: () => promise( - (cb) => client.check(new CheckRequest(), cb), - (resp: CheckResponse) => resp.toObject(), + (cb) => client.check(new health.CheckRequest(), cb), + (resp: health.CheckResponse) => resp.toObject(), ), } } diff --git a/src/index.ts b/src/index.ts index 2dfb6798..d8b08f26 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,9 +2,11 @@ import { createFFS } from "./ffs" import { createHealth } from "./health" import { createMiners } from "./miners" import { createNet } from "./net" -import { Config } from "./types" +import { Config, ffs, health, miners, net } from "./types" import { getTransport, host, useToken } from "./util" +export { ffs, health, miners, net } + const defaultConfig: Config = { host, transport: getTransport(), diff --git a/src/miners/index.spec.ts b/src/miners/index.spec.ts index efd0ad06..3aaea820 100644 --- a/src/miners/index.spec.ts +++ b/src/miners/index.spec.ts @@ -3,10 +3,10 @@ import { createMiners } from "." import { getTransport, host } from "../util" describe("miners", () => { - const miners = createMiners({ host, transport: getTransport() }) + const c = createMiners({ host, transport: getTransport() }) it("should get the index", async () => { - const status = await miners.get() - expect(status.index).not.undefined + const res = await c.get() + expect(res.index).not.undefined }) }) diff --git a/src/miners/index.ts b/src/miners/index.ts index 461b0149..1ef08ef9 100644 --- a/src/miners/index.ts +++ b/src/miners/index.ts @@ -1,6 +1,5 @@ -import { GetRequest, GetResponse } from "@textile/grpc-powergate-client/dist/index/miner/rpc/rpc_pb" import { RPCServiceClient } from "@textile/grpc-powergate-client/dist/index/miner/rpc/rpc_pb_service" -import { Config } from "../types" +import { Config, miners } from "../types" import { promise } from "../util" /** @@ -17,8 +16,8 @@ export const createMiners = (config: Config) => { */ get: () => promise( - (cb) => client.get(new GetRequest(), cb), - (resp: GetResponse) => resp.toObject(), + (cb) => client.get(new miners.GetRequest(), cb), + (resp: miners.GetResponse) => resp.toObject(), ), } } diff --git a/src/net/index.spec.ts b/src/net/index.spec.ts index 9794dc99..2eb62279 100644 --- a/src/net/index.spec.ts +++ b/src/net/index.spec.ts @@ -4,17 +4,17 @@ import { createNet } from "." import { getTransport, host } from "../util" describe("net", () => { - const net = createNet({ host, transport: getTransport() }) + const c = createNet({ host, transport: getTransport() }) let peers: PeersResponse.AsObject it("should query peers", async () => { - peers = await net.peers() + peers = await c.peers() expect(peers.peersList).length.greaterThan(0) }) it("should get listen address", async () => { - const listenAddr = await net.listenAddr() + const listenAddr = await c.listenAddr() expect(listenAddr.addrInfo?.addrsList).length.greaterThan(0) expect(listenAddr.addrInfo?.id).length.greaterThan(0) }) @@ -24,7 +24,7 @@ describe("net", () => { if (!peerId) { assert.fail("no peer id") } - const peer = await net.findPeer(peerId) + const peer = await c.findPeer(peerId) expect(peer.peerInfo).not.undefined }) @@ -33,7 +33,7 @@ describe("net", () => { if (!peerId) { assert.fail("no peer id") } - const resp = await net.connectedness(peerId) + const resp = await c.connectedness(peerId) expect(resp.connectedness).equal(Connectedness.CONNECTEDNESS_CONNECTED) }) @@ -42,7 +42,7 @@ describe("net", () => { if (!peerInfo) { assert.fail("no peer info") } - await net.disconnectPeer(peerInfo.id) - await net.connectPeer(peerInfo) + await c.disconnectPeer(peerInfo.id) + await c.connectPeer(peerInfo) }) }) diff --git a/src/net/index.ts b/src/net/index.ts index 0481f226..6082912f 100644 --- a/src/net/index.ts +++ b/src/net/index.ts @@ -1,18 +1,5 @@ -import { - ConnectednessRequest, - ConnectednessResponse, - ConnectPeerRequest, - DisconnectPeerRequest, - FindPeerRequest, - FindPeerResponse, - ListenAddrRequest, - ListenAddrResponse, - PeerAddrInfo, - PeersRequest, - PeersResponse, -} from "@textile/grpc-powergate-client/dist/net/rpc/rpc_pb" import { RPCServiceClient } from "@textile/grpc-powergate-client/dist/net/rpc/rpc_pb_service" -import { Config } from "../types" +import { Config, net } from "../types" import { promise } from "../util" /** @@ -29,8 +16,8 @@ export const createNet = (config: Config) => { */ listenAddr: () => promise( - (cb) => client.listenAddr(new ListenAddrRequest(), cb), - (res: ListenAddrResponse) => res.toObject(), + (cb) => client.listenAddr(new net.ListenAddrRequest(), cb), + (res: net.ListenAddrResponse) => res.toObject(), ), /** @@ -39,8 +26,8 @@ export const createNet = (config: Config) => { */ peers: () => promise( - (cb) => client.peers(new PeersRequest(), cb), - (res: PeersResponse) => res.toObject(), + (cb) => client.peers(new net.PeersRequest(), cb), + (res: net.PeersResponse) => res.toObject(), ), /** @@ -49,11 +36,11 @@ export const createNet = (config: Config) => { * @returns The peer info */ findPeer: (peerId: string) => { - const req = new FindPeerRequest() + const req = new net.FindPeerRequest() req.setPeerId(peerId) return promise( (cb) => client.findPeer(req, cb), - (res: FindPeerResponse) => res.toObject(), + (res: net.FindPeerResponse) => res.toObject(), ) }, @@ -61,11 +48,11 @@ export const createNet = (config: Config) => { * Connect to a peer * @param peerInfo The peer info specifying the peer to connect to */ - connectPeer: (peerInfo: PeerAddrInfo.AsObject) => { - const info = new PeerAddrInfo() + connectPeer: (peerInfo: net.PeerAddrInfo.AsObject) => { + const info = new net.PeerAddrInfo() info.setId(peerInfo.id) info.setAddrsList(peerInfo.addrsList) - const req = new ConnectPeerRequest() + const req = new net.ConnectPeerRequest() req.setPeerInfo(info) return promise( (cb) => client.connectPeer(req, cb), @@ -81,11 +68,11 @@ export const createNet = (config: Config) => { * @returns Information about the connectedness to the peer */ connectedness: (peerId: string) => { - const req = new ConnectednessRequest() + const req = new net.ConnectednessRequest() req.setPeerId(peerId) return promise( (cb) => client.connectedness(req, cb), - (res: ConnectednessResponse) => res.toObject(), + (res: net.ConnectednessResponse) => res.toObject(), ) }, @@ -94,7 +81,7 @@ export const createNet = (config: Config) => { * @param peerId The peer id to disconnect from */ disconnectPeer: (peerId: string) => { - const req = new DisconnectPeerRequest() + const req = new net.DisconnectPeerRequest() req.setPeerId(peerId) return promise( (cb) => client.disconnectPeer(req, cb), diff --git a/src/types.ts b/src/types.ts index 74215e4f..42d560dc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,8 @@ import { grpc } from "@improbable-eng/grpc-web" +import * as ffs from "@textile/grpc-powergate-client/dist/ffs/rpc/rpc_pb" +import * as health from "@textile/grpc-powergate-client/dist/health/rpc/rpc_pb" +import * as miners from "@textile/grpc-powergate-client/dist/index/miner/rpc/rpc_pb" +import * as net from "@textile/grpc-powergate-client/dist/net/rpc/rpc_pb" /** * Object that allows you to configure the Powergate client @@ -7,3 +11,5 @@ export interface Config extends grpc.RpcOptions { host: string authToken?: string } + +export { ffs, health, miners, net }