Skip to content

Commit c919d46

Browse files
authored
feat(ipfs): add IPNS API (#932)
1 parent b1e9d0c commit c919d46

File tree

5 files changed

+425
-3
lines changed

5 files changed

+425
-3
lines changed

packages/clients/src/api/ipfs/v1alpha1/api.gen.ts

Lines changed: 193 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ import {
88
waitForResource,
99
} from '../../../bridge'
1010
import type { Region, WaitForOptions } from '../../../bridge'
11-
import { PIN_TRANSIENT_STATUSES } from './content.gen'
11+
import { NAME_TRANSIENT_STATUSES, PIN_TRANSIENT_STATUSES } from './content.gen'
1212
import {
1313
marshalCreatePinByCIDRequest,
1414
marshalCreatePinByURLRequest,
1515
marshalCreateVolumeRequest,
16+
marshalIpnsApiCreateNameRequest,
17+
marshalIpnsApiImportKeyNameRequest,
18+
marshalIpnsApiUpdateNameRequest,
1619
marshalReplacePinRequest,
1720
marshalUpdateVolumeRequest,
21+
unmarshalExportKeyNameResponse,
22+
unmarshalListNamesResponse,
1823
unmarshalListPinsResponse,
1924
unmarshalListVolumesResponse,
25+
unmarshalName,
2026
unmarshalPin,
2127
unmarshalReplacePinResponse,
2228
unmarshalVolume,
@@ -27,12 +33,22 @@ import type {
2733
CreateVolumeRequest,
2834
DeletePinRequest,
2935
DeleteVolumeRequest,
36+
ExportKeyNameResponse,
3037
GetPinRequest,
3138
GetVolumeRequest,
39+
IpnsApiCreateNameRequest,
40+
IpnsApiDeleteNameRequest,
41+
IpnsApiExportKeyNameRequest,
42+
IpnsApiGetNameRequest,
43+
IpnsApiImportKeyNameRequest,
44+
IpnsApiListNamesRequest,
45+
IpnsApiUpdateNameRequest,
46+
ListNamesResponse,
3247
ListPinsRequest,
3348
ListPinsResponse,
3449
ListVolumesRequest,
3550
ListVolumesResponse,
51+
Name,
3652
Pin,
3753
ReplacePinRequest,
3854
ReplacePinResponse,
@@ -328,3 +344,179 @@ export class API extends ParentAPI {
328344
urlParams: urlParams(['volume_id', request.volumeId]),
329345
})
330346
}
347+
348+
/** IPFS Naming service API. */
349+
export class IpnsAPI extends ParentAPI {
350+
/** Lists the available regions of the API. */
351+
public static readonly LOCALITIES: Region[] = ['fr-par', 'nl-ams', 'pl-waw']
352+
353+
/**
354+
* Create a new name. You can use the `ipns key` command to list and generate
355+
* more names and their respective keys.
356+
*
357+
* @param request - The request {@link IpnsApiCreateNameRequest}
358+
* @returns A Promise of Name
359+
*/
360+
createName = (request: Readonly<IpnsApiCreateNameRequest>) =>
361+
this.client.fetch<Name>(
362+
{
363+
body: JSON.stringify(
364+
marshalIpnsApiCreateNameRequest(request, this.client.settings),
365+
),
366+
headers: jsonContentHeaders,
367+
method: 'POST',
368+
path: `/ipfs/v1alpha1/regions/${validatePathParam(
369+
'region',
370+
request.region ?? this.client.settings.defaultRegion,
371+
)}/names`,
372+
},
373+
unmarshalName,
374+
)
375+
376+
/**
377+
* Get information about a name. Retrieve information about a specific name.
378+
*
379+
* @param request - The request {@link IpnsApiGetNameRequest}
380+
* @returns A Promise of Name
381+
*/
382+
getName = (request: Readonly<IpnsApiGetNameRequest>) =>
383+
this.client.fetch<Name>(
384+
{
385+
method: 'GET',
386+
path: `/ipfs/v1alpha1/regions/${validatePathParam(
387+
'region',
388+
request.region ?? this.client.settings.defaultRegion,
389+
)}/names/${validatePathParam('nameId', request.nameId)}`,
390+
},
391+
unmarshalName,
392+
)
393+
394+
/**
395+
* Waits for {@link Name} to be in a final state.
396+
*
397+
* @param request - The request {@link GetNameRequest}
398+
* @param options - The waiting options
399+
* @returns A Promise of Name
400+
*/
401+
waitForName = (
402+
request: Readonly<IpnsApiGetNameRequest>,
403+
options?: Readonly<WaitForOptions<Name>>,
404+
) =>
405+
waitForResource(
406+
options?.stop ??
407+
(res => Promise.resolve(!NAME_TRANSIENT_STATUSES.includes(res.status))),
408+
this.getName,
409+
request,
410+
options,
411+
)
412+
413+
/**
414+
* Delete an existing name. Delete a name by its ID.
415+
*
416+
* @param request - The request {@link IpnsApiDeleteNameRequest}
417+
*/
418+
deleteName = (request: Readonly<IpnsApiDeleteNameRequest>) =>
419+
this.client.fetch<void>({
420+
method: 'DELETE',
421+
path: `/ipfs/v1alpha1/regions/${validatePathParam(
422+
'region',
423+
request.region ?? this.client.settings.defaultRegion,
424+
)}/names/${validatePathParam('nameId', request.nameId)}`,
425+
})
426+
427+
protected pageOfListNames = (
428+
request: Readonly<IpnsApiListNamesRequest> = {},
429+
) =>
430+
this.client.fetch<ListNamesResponse>(
431+
{
432+
method: 'GET',
433+
path: `/ipfs/v1alpha1/regions/${validatePathParam(
434+
'region',
435+
request.region ?? this.client.settings.defaultRegion,
436+
)}/names`,
437+
urlParams: urlParams(
438+
['order_by', request.orderBy ?? 'created_at_asc'],
439+
['organization_id', request.organizationId],
440+
['page', request.page],
441+
[
442+
'page_size',
443+
request.pageSize ?? this.client.settings.defaultPageSize,
444+
],
445+
['project_id', request.projectId],
446+
),
447+
},
448+
unmarshalListNamesResponse,
449+
)
450+
451+
/**
452+
* List all names by a Project ID. Retrieve information about all names from a
453+
* Project ID.
454+
*
455+
* @param request - The request {@link IpnsApiListNamesRequest}
456+
* @returns A Promise of ListNamesResponse
457+
*/
458+
listNames = (request: Readonly<IpnsApiListNamesRequest> = {}) =>
459+
enrichForPagination('names', this.pageOfListNames, request)
460+
461+
/**
462+
* Update name information. Update name information (CID, tag, name...).
463+
*
464+
* @param request - The request {@link IpnsApiUpdateNameRequest}
465+
* @returns A Promise of Name
466+
*/
467+
updateName = (request: Readonly<IpnsApiUpdateNameRequest>) =>
468+
this.client.fetch<Name>(
469+
{
470+
body: JSON.stringify(
471+
marshalIpnsApiUpdateNameRequest(request, this.client.settings),
472+
),
473+
headers: jsonContentHeaders,
474+
method: 'PATCH',
475+
path: `/ipfs/v1alpha1/regions/${validatePathParam(
476+
'region',
477+
request.region ?? this.client.settings.defaultRegion,
478+
)}/names/${validatePathParam('nameId', request.nameId)}`,
479+
},
480+
unmarshalName,
481+
)
482+
483+
/**
484+
* Export your private key. Export a private key by its ID.
485+
*
486+
* @param request - The request {@link IpnsApiExportKeyNameRequest}
487+
* @returns A Promise of ExportKeyNameResponse
488+
*/
489+
exportKeyName = (request: Readonly<IpnsApiExportKeyNameRequest>) =>
490+
this.client.fetch<ExportKeyNameResponse>(
491+
{
492+
method: 'GET',
493+
path: `/ipfs/v1alpha1/regions/${validatePathParam(
494+
'region',
495+
request.region ?? this.client.settings.defaultRegion,
496+
)}/names/${validatePathParam('nameId', request.nameId)}/export-key`,
497+
},
498+
unmarshalExportKeyNameResponse,
499+
)
500+
501+
/**
502+
* Import your private key. Import a private key.
503+
*
504+
* @param request - The request {@link IpnsApiImportKeyNameRequest}
505+
* @returns A Promise of Name
506+
*/
507+
importKeyName = (request: Readonly<IpnsApiImportKeyNameRequest>) =>
508+
this.client.fetch<Name>(
509+
{
510+
body: JSON.stringify(
511+
marshalIpnsApiImportKeyNameRequest(request, this.client.settings),
512+
),
513+
headers: jsonContentHeaders,
514+
method: 'POST',
515+
path: `/ipfs/v1alpha1/regions/${validatePathParam(
516+
'region',
517+
request.region ?? this.client.settings.defaultRegion,
518+
)}/names/import-key`,
519+
},
520+
unmarshalName,
521+
)
522+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// This file was automatically generated. DO NOT EDIT.
22
// If you have any remark or suggestion do not hesitate to open an issue.
3-
import type { PinStatus } from './types.gen'
3+
import type { NameStatus, PinStatus } from './types.gen'
4+
5+
/** Lists transient statutes of the enum {@link NameStatus}. */
6+
export const NAME_TRANSIENT_STATUSES: NameStatus[] = ['queued', 'publishing']
47

58
/** Lists transient statutes of the enum {@link PinStatus}. */
69
export const PIN_TRANSIENT_STATUSES: PinStatus[] = ['queued', 'pinning']

packages/clients/src/api/ipfs/v1alpha1/index.gen.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
// This file was automatically generated. DO NOT EDIT.
22
// If you have any remark or suggestion do not hesitate to open an issue.
3-
export { API } from './api.gen'
3+
export { API, IpnsAPI } from './api.gen'
44
export * from './content.gen'
55
export type {
66
CreatePinByCIDRequest,
77
CreatePinByURLRequest,
88
CreateVolumeRequest,
99
DeletePinRequest,
1010
DeleteVolumeRequest,
11+
ExportKeyNameResponse,
1112
GetPinRequest,
1213
GetVolumeRequest,
14+
IpnsApiCreateNameRequest,
15+
IpnsApiDeleteNameRequest,
16+
IpnsApiExportKeyNameRequest,
17+
IpnsApiGetNameRequest,
18+
IpnsApiImportKeyNameRequest,
19+
IpnsApiListNamesRequest,
20+
IpnsApiUpdateNameRequest,
21+
ListNamesRequestOrderBy,
22+
ListNamesResponse,
1323
ListPinsRequest,
1424
ListPinsRequestOrderBy,
1525
ListPinsResponse,
1626
ListVolumesRequest,
1727
ListVolumesRequestOrderBy,
1828
ListVolumesResponse,
29+
Name,
30+
NameStatus,
1931
Pin,
2032
PinCID,
2133
PinCIDMeta,

packages/clients/src/api/ipfs/v1alpha1/marshalling.gen.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ import type {
1010
CreatePinByCIDRequest,
1111
CreatePinByURLRequest,
1212
CreateVolumeRequest,
13+
ExportKeyNameResponse,
14+
IpnsApiCreateNameRequest,
15+
IpnsApiImportKeyNameRequest,
16+
IpnsApiUpdateNameRequest,
17+
ListNamesResponse,
1318
ListPinsResponse,
1419
ListVolumesResponse,
20+
Name,
1521
Pin,
1622
PinCID,
1723
PinCIDMeta,
@@ -64,6 +70,27 @@ const unmarshalPinInfo = (data: unknown) => {
6470
} as PinInfo
6571
}
6672

73+
export const unmarshalName = (data: unknown) => {
74+
if (!isJSONObject(data)) {
75+
throw new TypeError(
76+
`Unmarshalling the type 'Name' failed as data isn't a dictionary.`,
77+
)
78+
}
79+
80+
return {
81+
createdAt: unmarshalDate(data.created_at),
82+
key: data.key,
83+
name: data.name,
84+
nameId: data.name_id,
85+
projectId: data.project_id,
86+
region: data.region,
87+
status: data.status,
88+
tags: data.tags,
89+
updatedAt: unmarshalDate(data.updated_at),
90+
value: data.value,
91+
} as Name
92+
}
93+
6794
export const unmarshalPin = (data: unknown) => {
6895
if (!isJSONObject(data)) {
6996
throw new TypeError(
@@ -101,6 +128,36 @@ export const unmarshalVolume = (data: unknown) => {
101128
} as Volume
102129
}
103130

131+
export const unmarshalExportKeyNameResponse = (data: unknown) => {
132+
if (!isJSONObject(data)) {
133+
throw new TypeError(
134+
`Unmarshalling the type 'ExportKeyNameResponse' failed as data isn't a dictionary.`,
135+
)
136+
}
137+
138+
return {
139+
createdAt: unmarshalDate(data.created_at),
140+
nameId: data.name_id,
141+
privateKey: data.private_key,
142+
projectId: data.project_id,
143+
publicKey: data.public_key,
144+
updatedAt: unmarshalDate(data.updated_at),
145+
} as ExportKeyNameResponse
146+
}
147+
148+
export const unmarshalListNamesResponse = (data: unknown) => {
149+
if (!isJSONObject(data)) {
150+
throw new TypeError(
151+
`Unmarshalling the type 'ListNamesResponse' failed as data isn't a dictionary.`,
152+
)
153+
}
154+
155+
return {
156+
names: unmarshalArrayOfObject(data.names, unmarshalName),
157+
totalCount: data.total_count,
158+
} as ListNamesResponse
159+
}
160+
104161
export const unmarshalListPinsResponse = (data: unknown) => {
105162
if (!isJSONObject(data)) {
106163
throw new TypeError(
@@ -180,6 +237,34 @@ export const marshalCreateVolumeRequest = (
180237
project_id: request.projectId ?? defaults.defaultProjectId,
181238
})
182239

240+
export const marshalIpnsApiCreateNameRequest = (
241+
request: IpnsApiCreateNameRequest,
242+
defaults: DefaultValues,
243+
): Record<string, unknown> => ({
244+
name: request.name,
245+
project_id: request.projectId ?? defaults.defaultProjectId,
246+
value: request.value,
247+
})
248+
249+
export const marshalIpnsApiImportKeyNameRequest = (
250+
request: IpnsApiImportKeyNameRequest,
251+
defaults: DefaultValues,
252+
): Record<string, unknown> => ({
253+
name: request.name,
254+
private_key: request.privateKey,
255+
project_id: request.projectId ?? defaults.defaultProjectId,
256+
value: request.value,
257+
})
258+
259+
export const marshalIpnsApiUpdateNameRequest = (
260+
request: IpnsApiUpdateNameRequest,
261+
defaults: DefaultValues,
262+
): Record<string, unknown> => ({
263+
name: request.name,
264+
tags: request.tags,
265+
value: request.value,
266+
})
267+
183268
export const marshalReplacePinRequest = (
184269
request: ReplacePinRequest,
185270
defaults: DefaultValues,

0 commit comments

Comments
 (0)