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

Commit e1ec19d

Browse files
committed
fix: make ls type more intelegent in regards stats
1 parent 9669fc6 commit e1ec19d

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

packages/interface-ipfs-core/type/pin/remote/service.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export interface API {
1616
/**
1717
* List registered remote pinning services.
1818
*/
19-
ls(options:ListOptions & AbortOptions):Promise<RemotePinService[]>
19+
ls(options: { stat: true } & AbortOptions): Promise<RemotePinServiceWithStat[]>
20+
ls(options?: AbortOptions):Promise<RemotePinService[]>
2021
}
2122

2223
export interface Credentials {
@@ -41,20 +42,13 @@ export interface RemotePinService {
4142
endpoint: URL
4243
}
4344

45+
export interface RemotePinServiceWithStat extends RemotePinService {
4446
/**
4547
* Pin count on the remote service. It is fetched from the remote service and
4648
* is done only if `pinCount` option is used. Furthermore it may not be
4749
* present if service was unreachable.
4850
*/
49-
stat?: Stat
50-
}
51-
52-
export interface ListOptions {
53-
/**
54-
* If `true` will try to fetch and include current pin count on the remote
55-
* service.
56-
*/
57-
stat?: boolean
51+
stat: Stat
5852
}
5953

6054
export type Stat = ValidStat | InvalidStat

packages/ipfs-http-client/src/pin/remote/service.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const toUrlSearchParams = require('../../lib/to-url-search-params')
99
* @typedef {import('interface-ipfs-core/type/basic').AbortOptions} AbortOptions
1010
* @typedef {import('interface-ipfs-core/type/pin/remote/service').API} API
1111
* @typedef {import('interface-ipfs-core/type/pin/remote/service').Credentials} Credentials
12-
* @typedef {import('interface-ipfs-core/type/pin/remote/service').ListOptions} ListOptions
1312
* @typedef {import('interface-ipfs-core/type/pin/remote/service').RemotePinService} RemotePinService
13+
* @typedef {import('interface-ipfs-core/type/pin/remote/service').RemotePinServiceWithStat} RemotePinServiceWithStat
1414
* @implements {API}
1515
*/
1616
class Service {
@@ -56,8 +56,9 @@ class Service {
5656
}
5757

5858
/**
59+
* @template {true} Stat
5960
* @param {Client} client
60-
* @param {ListOptions & AbortOptions & HttpOptions} [options]
61+
* @param {{ stat?: Stat } & AbortOptions & HttpOptions} [options]
6162
*/
6263
static async ls (client, { stat, timeout, signal, headers } = {}) {
6364
const response = await client.post('pin/remote/service/ls', {
@@ -68,24 +69,26 @@ class Service {
6869
})
6970
/** @type {{RemoteServices: Object[]}} */
7071
const { RemoteServices } = await response.json()
71-
return RemoteServices.map(Service.decodeRemoteService)
72+
73+
/** @type {Stat extends true ? RemotePinServiceWithStat[] : RemotePinService []} */
74+
return (RemoteServices.map(Service.decodeRemoteService))
7275
}
7376

7477
/**
7578
* @param {Object} json
76-
* @returns {import('interface-ipfs-core/type/pin/remote/service').RemotePinService}
79+
* @returns {RemotePinServiceWithStat}
7780
*/
7881
static decodeRemoteService (json) {
7982
return {
8083
service: json.Service,
8184
endpoint: new URL(json.ApiEndpoint),
82-
stat: Service.decodeStat(json.stat)
85+
stat: json.stat && Service.decodeStat(json.stat)
8386
}
8487
}
8588

8689
/**
8790
* @param {Object} json
88-
* @returns {import('interface-ipfs-core/type/pin/remote/service').Stat|undefined}
91+
* @returns {import('interface-ipfs-core/type/pin/remote/service').Stat}
8992
*/
9093
static decodeStat (json) {
9194
switch (json.Status) {
@@ -98,8 +101,9 @@ class Service {
98101
case 'invalid': {
99102
return { status: 'invalid' }
100103
}
101-
default:
102-
return undefined
104+
default: {
105+
return { status: json.Status }
106+
}
103107
}
104108
}
105109

@@ -128,7 +132,7 @@ class Service {
128132
/**
129133
* List registered remote pinning services.
130134
*
131-
* @param {ListOptions & AbortOptions & HttpOptions} [options]
135+
* @param {{ stat?: true } & AbortOptions & HttpOptions} [options]
132136
*/
133137
ls (options) {
134138
return Service.ls(this.client, options)

0 commit comments

Comments
 (0)