Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
feat: remove ipfs-http-client
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed May 10, 2023
1 parent af19f13 commit 17b3456
Show file tree
Hide file tree
Showing 7 changed files with 2,402 additions and 3,194 deletions.
91 changes: 91 additions & 0 deletions e2e/utils/utils.ts/ipfsServerClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import axios, { AxiosInstance } from 'axios';
import FormData from 'form-data';
import { Readable } from 'stream';
import { Logger, log } from '../../../src/lib/Logger';

export default class IpfsServerClient {
private instance: AxiosInstance;

constructor({
baseURL,
projectId,
apiKeySecret,
}: {
baseURL: string;
projectId: string;
apiKeySecret: string;
}) {
if (!baseURL)
log.throwMissingArgumentError(Logger.message.no_base_url, {
location: Logger.location.IPFS_SERVER_CONSTRUCTOR,
});

if (!projectId)
log.throwMissingArgumentError(Logger.message.no_projectId_supplied, {
location: Logger.location.IPFS_SERVER_CONSTRUCTOR,
});

if (!apiKeySecret)
log.throwMissingArgumentError(Logger.message.no_secretId_supplied, {
location: Logger.location.IPFS_SERVER_CONSTRUCTOR,
});

this.instance = axios.create({
baseURL,
headers: {
authorization: `Basic ${Buffer.from(`${projectId}:${apiKeySecret}`).toString('base64')}`,
},
});
}

async add(content: string | Readable): Promise<string> {
const formData = new FormData();
formData.append('file', content);

try {
const response = await this.instance.post('/add', formData, {
headers: {
'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`,
},
});
return response.data.Hash;
} catch (error) {
return log.throwArgumentError(
Logger.message.an_error_occured_with_ipfs_api,
'file',
content,
{
location: Logger.location.IPFSSERVICE_UPLOADFILE,
error: `${error}`,
},
);
}
}

async addAll(files: { path: string; content: any }[]): Promise<any> {
const formData = new FormData();

files.forEach(file => {
formData.append(file.path, file.content, {
filename: file.path,
});
});

try {
const response = await this.instance.post(`/add?wrap-with-directory=true`, formData, {
headers: {
'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`,
},
});

const arr = response.data.split('\n').filter(Boolean).map(JSON.parse);
const lastItem = arr.pop();
return lastItem.Hash;
} catch (error) {
return log.throwArgumentError(Logger.message.an_error_occured_with_ipfs_api, 'file', files, {
location: Logger.location.IPFSSERVICE_UPLOADFILE,
error: `${error}`,
});
}
}
}
2 changes: 2 additions & 0 deletions integration-test/sdk.nfts.search.test-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ loadEnv();

describe('E2E Test: Sdk (read)', () => {
let sdk: SDK;
jest.setTimeout(120 * 1000);

beforeAll(() => {
const auth = new Auth({
privateKey: process.env.WALLET_PRIVATE_KEY,
Expand Down
Loading

0 comments on commit 17b3456

Please sign in to comment.