Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: add form data as optional param
Browse files Browse the repository at this point in the history
  • Loading branch information
dionysuzx committed Mar 24, 2022
1 parent 3de2131 commit 104bb7c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/commands/pinning/pinFileToIPFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function pinFileToIPFS(pinataApiKey, pinataSecretApiKey, readStre

const endpoint = `${baseUrl}/pinning/pinFileToIPFS`;

if (!(readStream instanceof stream.Readable)) {
if (!(readStream instanceof stream.Readable || readStream instanceof NodeFormData)) {
reject(new Error('readStream is not a readable stream'));
}

Expand All @@ -41,7 +41,7 @@ export default function pinFileToIPFS(pinataApiKey, pinataSecretApiKey, readStre

axios.post(
endpoint,
data,
readStream instanceof NodeFormData ? readStream : data,
{
withCredentials: true,
maxContentLength: 'Infinity', //this is needed to prevent axios from erroring out with large files
Expand Down
19 changes: 16 additions & 3 deletions test/commands/pinning/pinFileToIPFS.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import axios from 'axios';
import pinFileToIPFS from'../../../src/commands/pinning/pinFileToIPFS';
import fs from 'fs';
import NodeFormData from 'form-data';
jest.mock('axios');

//common values
const nonStream = 'test';
const validStream = fs.createReadStream('./pinata.png');
const validFormData = new NodeFormData();
validFormData.append('file', validStream, {filepath: 'test/filepath'});


test('non-readableStream is passed in', () => {
expect(pinFileToIPFS('test', 'test', nonStream)).rejects.toEqual(Error('readStream is not a readable stream'));
test('non-readableStream and non-formData is passed in', () => {
expect(pinFileToIPFS('test', 'test', nonStream)).rejects.toEqual(Error('readStream is not a readable stream or form data'));

return undefined;
});

test('200 status is returned', () => {
test('200 status is returned with valid stream', () => {
const goodStatus = {
status: 200,
data: 'testData'
Expand All @@ -24,6 +27,16 @@ test('200 status is returned', () => {
expect(pinFileToIPFS('test', 'test', validStream)).resolves.toEqual(goodStatus.data);
});

test('200 status is returned with valid form data', () => {
const goodStatus = {
status: 200,
data: 'testData'
};
axios.post.mockResolvedValue(goodStatus);
expect.assertions(1);
expect(pinFileToIPFS('test', 'test', validFormData)).resolves.toEqual(goodStatus.data);
});

test('Result other than 200 status is returned', () => {
const badStatus = {
status: 700
Expand Down

0 comments on commit 104bb7c

Please sign in to comment.