Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: make ls type more intelegent in regards stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Dec 11, 2020
1 parent 9669fc6 commit e1ec19d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
14 changes: 4 additions & 10 deletions packages/interface-ipfs-core/type/pin/remote/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export interface API {
/**
* List registered remote pinning services.
*/
ls(options:ListOptions & AbortOptions):Promise<RemotePinService[]>
ls(options: { stat: true } & AbortOptions): Promise<RemotePinServiceWithStat[]>
ls(options?: AbortOptions):Promise<RemotePinService[]>
}

export interface Credentials {
Expand All @@ -41,20 +42,13 @@ export interface RemotePinService {
endpoint: URL
}

export interface RemotePinServiceWithStat extends RemotePinService {
/**
* Pin count on the remote service. It is fetched from the remote service and
* is done only if `pinCount` option is used. Furthermore it may not be
* present if service was unreachable.
*/
stat?: Stat
}

export interface ListOptions {
/**
* If `true` will try to fetch and include current pin count on the remote
* service.
*/
stat?: boolean
stat: Stat
}

export type Stat = ValidStat | InvalidStat
Expand Down
22 changes: 13 additions & 9 deletions packages/ipfs-http-client/src/pin/remote/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const toUrlSearchParams = require('../../lib/to-url-search-params')
* @typedef {import('interface-ipfs-core/type/basic').AbortOptions} AbortOptions
* @typedef {import('interface-ipfs-core/type/pin/remote/service').API} API
* @typedef {import('interface-ipfs-core/type/pin/remote/service').Credentials} Credentials
* @typedef {import('interface-ipfs-core/type/pin/remote/service').ListOptions} ListOptions
* @typedef {import('interface-ipfs-core/type/pin/remote/service').RemotePinService} RemotePinService
* @typedef {import('interface-ipfs-core/type/pin/remote/service').RemotePinServiceWithStat} RemotePinServiceWithStat
* @implements {API}
*/
class Service {
Expand Down Expand Up @@ -56,8 +56,9 @@ class Service {
}

/**
* @template {true} Stat
* @param {Client} client
* @param {ListOptions & AbortOptions & HttpOptions} [options]
* @param {{ stat?: Stat } & AbortOptions & HttpOptions} [options]
*/
static async ls (client, { stat, timeout, signal, headers } = {}) {
const response = await client.post('pin/remote/service/ls', {
Expand All @@ -68,24 +69,26 @@ class Service {
})
/** @type {{RemoteServices: Object[]}} */
const { RemoteServices } = await response.json()
return RemoteServices.map(Service.decodeRemoteService)

/** @type {Stat extends true ? RemotePinServiceWithStat[] : RemotePinService []} */
return (RemoteServices.map(Service.decodeRemoteService))
}

/**
* @param {Object} json
* @returns {import('interface-ipfs-core/type/pin/remote/service').RemotePinService}
* @returns {RemotePinServiceWithStat}
*/
static decodeRemoteService (json) {
return {
service: json.Service,
endpoint: new URL(json.ApiEndpoint),
stat: Service.decodeStat(json.stat)
stat: json.stat && Service.decodeStat(json.stat)
}
}

/**
* @param {Object} json
* @returns {import('interface-ipfs-core/type/pin/remote/service').Stat|undefined}
* @returns {import('interface-ipfs-core/type/pin/remote/service').Stat}
*/
static decodeStat (json) {
switch (json.Status) {
Expand All @@ -98,8 +101,9 @@ class Service {
case 'invalid': {
return { status: 'invalid' }
}
default:
return undefined
default: {
return { status: json.Status }
}
}
}

Expand Down Expand Up @@ -128,7 +132,7 @@ class Service {
/**
* List registered remote pinning services.
*
* @param {ListOptions & AbortOptions & HttpOptions} [options]
* @param {{ stat?: true } & AbortOptions & HttpOptions} [options]
*/
ls (options) {
return Service.ls(this.client, options)
Expand Down

0 comments on commit e1ec19d

Please sign in to comment.