diff --git a/packages/interface-ipfs-core/type/pin/remote/service.ts b/packages/interface-ipfs-core/type/pin/remote/service.ts index 0f50081a92..8885219bba 100644 --- a/packages/interface-ipfs-core/type/pin/remote/service.ts +++ b/packages/interface-ipfs-core/type/pin/remote/service.ts @@ -16,7 +16,8 @@ export interface API { /** * List registered remote pinning services. */ - ls(options:ListOptions & AbortOptions):Promise + ls(options: { stat: true } & AbortOptions): Promise + ls(options?: AbortOptions):Promise } export interface Credentials { @@ -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 diff --git a/packages/ipfs-http-client/src/pin/remote/service.js b/packages/ipfs-http-client/src/pin/remote/service.js index 659b25849a..07f0c21051 100644 --- a/packages/ipfs-http-client/src/pin/remote/service.js +++ b/packages/ipfs-http-client/src/pin/remote/service.js @@ -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 { @@ -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', { @@ -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) { @@ -98,8 +101,9 @@ class Service { case 'invalid': { return { status: 'invalid' } } - default: - return undefined + default: { + return { status: json.Status } + } } } @@ -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)