Skip to content

Commit c5590f5

Browse files
alexloikoOleksandr Loiko
andauthored
ALL-3061 Added IPFS get method (#1000)
Co-authored-by: Oleksandr Loiko <[email protected]>
1 parent 9706f40 commit c5590f5

File tree

8 files changed

+74
-13
lines changed

8 files changed

+74
-13
lines changed

β€ŽCHANGELOG.mdβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [4.1.14] - 2023.10.23
2+
### Added
3+
- Added IPFS get file data method
4+
15
## [4.1.13] - 2023.10.20
26
### Changed
37
- Fixed CONTRACT_ADDRESS_LOG_EVENT data in getAll() Notification method

β€ŽREADME.mdβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ Explore the world of fungible tokens, manage their properties, and track your as
119119
| [Get metadata of a fungible token](https://docs.tatum.io/docs/fungible-tokens/get-metadata-of-a-fungible-token) |
120120
| [Create a fungible token](https://docs.tatum.io/docs/fungible-tokens/create-a-fungible-token) |
121121

122+
### πŸ“ IPFS
123+
124+
Enables you as a developer to use IPFS to store and retrieve your media.
125+
126+
| Documentation |
127+
|--------------------------------------------------------------------|
128+
| [Upload file to IPFS](https://docs.tatum.io/docs/ipfs/upload-file) |
129+
130+
122131
### β›½ Fee Estimation
123132

124133
Stay updated with real-time fee insights and ensure smooth transactions without overpaying.

β€Žpackage.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tatumio/tatum",
3-
"version": "4.1.13",
3+
"version": "4.1.14",
44
"description": "Tatum JS SDK",
55
"author": "Tatum",
66
"repository": "https://github.com/tatumio/tatum-js",

β€Žsrc/connector/connector.dto.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface GetUrl<PARAMS = DefaultParamsType> {
55
path?: string
66
basePath?: string
77
params?: PARAMS
8+
isDownload?: boolean
89
}
910

1011
export interface SdkRequest<PARAMS = DefaultParamsType, BODY = DefaultBodyType> extends GetUrl<PARAMS> {

β€Žsrc/connector/tatum.connector.tsβ€Ž

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ export class TatumConnector {
1414
constructor(private readonly id: string) {}
1515

1616
public async get<RESPONSE, PARAMS extends DefaultParamsType = DefaultParamsType>(request: GetUrl<PARAMS>) {
17-
return this.request<RESPONSE, PARAMS>({ ...request, method: 'GET' })
17+
return this.request<RESPONSE, PARAMS>({ ...request, method: 'GET' }) as Promise<RESPONSE>
1818
}
1919

2020
public async rpcCall<RESPONSE>(url: string, body: JsonRpcCall | JsonRpcCall[]) {
21-
return this.request<RESPONSE>({ body, method: 'POST' }, 0, url)
21+
return this.request<RESPONSE>({ body, method: 'POST' }, 0, url) as Promise<RESPONSE>
2222
}
2323

2424
public async post<RESPONSE, BODY extends DefaultBodyType = DefaultBodyType>(
2525
request: SdkRequest<DefaultParamsType, BODY>,
2626
) {
27-
return this.request<RESPONSE, DefaultParamsType, BODY>({ ...request, method: 'POST' })
27+
return this.request<RESPONSE, DefaultParamsType, BODY>({
28+
...request,
29+
method: 'POST',
30+
}) as Promise<RESPONSE>
2831
}
2932

3033
public async delete<RESPONSE>(request: GetUrl) {
31-
return this.request<RESPONSE>({ ...request, method: 'DELETE' })
34+
return this.request<RESPONSE>({ ...request, method: 'DELETE' }) as Promise<RESPONSE>
3235
}
3336

3437
public async uploadFile<RESPONSE>(request: FileUploadRequest) {
@@ -37,18 +40,24 @@ export class TatumConnector {
3740
return this.request<RESPONSE>(
3841
{ ...request, method: 'POST', body: formData, basePath: Constant.TATUM_API_URL.V3 },
3942
0,
40-
)
43+
) as Promise<RESPONSE>
44+
}
45+
46+
public async getFile<RESPONSE, PARAMS extends DefaultParamsType = DefaultParamsType>(
47+
request: GetUrl<PARAMS>,
48+
) {
49+
return this.request<RESPONSE, PARAMS>({ ...request, method: 'GET', isDownload: true }) as Promise<Blob>
4150
}
4251

4352
private async request<
4453
RESPONSE,
4554
PARAMS extends DefaultParamsType = DefaultParamsType,
4655
BODY extends DefaultBodyType = DefaultBodyType,
4756
>(
48-
{ path, params, body, method, basePath }: SdkRequest<PARAMS, BODY>,
57+
{ path, params, body, method, basePath, isDownload }: SdkRequest<PARAMS, BODY>,
4958
retry = 0,
5059
externalUrl?: string,
51-
): Promise<RESPONSE> {
60+
): Promise<RESPONSE | Blob | undefined> {
5261
const url = externalUrl || this.getUrl({ path, params, basePath })
5362
const isUpload = body && body instanceof FormData
5463
const headers = isUpload ? Utils.getBasicHeaders(this.id) : Utils.getHeaders(this.id)
@@ -71,7 +80,7 @@ export class TatumConnector {
7180
try {
7281
const res = await fetch(url, request)
7382
const end = Date.now() - start
74-
const responseBody = await res.clone().text()
83+
const responseBody = isDownload ? `Binary data` : await res.clone().text()
7584

7685
// Structure your log entry here
7786
Utils.log({
@@ -93,15 +102,21 @@ export class TatumConnector {
93102
})
94103

95104
if (res.ok) {
96-
return responseBody ? await res.json() : undefined
105+
if (!responseBody) {
106+
return undefined
107+
}
108+
if (isDownload) {
109+
return await res.blob()
110+
}
111+
return await res.json()
97112
}
98113

99114
// Retry only in case of 5xx error
100115
if (res.status >= 500 && res.status < 600) {
101116
return await this.retry(url, request, res, retry)
102117
}
103118

104-
return await Promise.reject(responseBody)
119+
throw responseBody
105120
} catch (error) {
106121
const end = Date.now() - start
107122
Utils.log({
@@ -151,7 +166,7 @@ export class TatumConnector {
151166
request: RequestInit,
152167
response: Response,
153168
retry: number,
154-
): Promise<RESPONSE> {
169+
): Promise<RESPONSE | Blob | undefined> {
155170
const { retryDelay, retryCount } = Container.of(this.id).get(CONFIG)
156171
if (!retryCount) {
157172
Utils.log({

β€Žsrc/service/ipfs/ipfs.dto.tsβ€Ž

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ export interface UploadFile {
44
*/
55
file: BlobPart
66
}
7+
8+
export interface GetFile {
9+
/**
10+
* File id to be retrieved
11+
*/
12+
id: string
13+
}

β€Žsrc/service/ipfs/ipfs.tsβ€Ž

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Container, Service } from 'typedi'
22
import { TatumConnector } from '../../connector/tatum.connector'
33
import { CONFIG, ErrorUtils, ResponseDto } from '../../util'
44
import { TatumConfig } from '../tatum'
5-
import { UploadFile } from './ipfs.dto'
5+
import { GetFile, UploadFile } from './ipfs.dto'
66

77
@Service({
88
factory: (data: { id: string }) => {
@@ -32,4 +32,18 @@ export class Ipfs {
3232
}),
3333
)
3434
}
35+
36+
/**
37+
* Get file binary data from the IPFS storage.
38+
* @param body Body of the request with file to be uploaded.
39+
* @returns Blob IPFS file binary data.
40+
* @returns ResponseDto<null> is error occurred.
41+
*/
42+
async getFile(body: GetFile): Promise<Blob | ResponseDto<null>> {
43+
return ErrorUtils.tryFailBlob(() =>
44+
this.connector.getFile<Blob>({
45+
path: `ipfs/${body.id}`,
46+
}),
47+
)
48+
}
3549
}

β€Žsrc/util/error.tsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ export const ErrorUtils = {
5656
}
5757
}
5858
},
59+
tryFailBlob: async (f: (() => Promise<Blob>) | (() => Blob)): Promise<Blob | ResponseDto<null>> => {
60+
try {
61+
return await f()
62+
} catch (e) {
63+
return {
64+
data: null,
65+
status: Status.ERROR,
66+
error: ErrorUtils.toErrorWithMessage(e),
67+
}
68+
}
69+
},
5970
formatErrorMsg: (message: string) => {
6071
return message.replace('attr.', '')
6172
},

0 commit comments

Comments
Β (0)