@pinata/sdk 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/pinata-sdk.js +64 -52
- package/lib/pinata-sdk.min.js +5 -5
- package/package.json +1 -2
- package/src/commands/pinning/pinFromFS.ts +14 -4
- package/src/index.ts +1 -0
- package/src/require-babel-polyfill.ts +5 -0
- package/src/third-party/global.d.ts +9 -0
- package/test/commands/pinning/pinFromFS.ts +43 -4
- package/types/commands/pinning/pinFromFS.d.ts +1 -0
- package/types/index.d.ts +1 -0
- package/types/require-babel-polyfill.d.ts +2 -0
- package/webpack.config.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pinata/sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Official SDK for the Pinata IPFS platform",
|
|
5
5
|
"main": "lib/pinata-sdk.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -64,7 +64,6 @@
|
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
66
|
"axios": "^0.21.1",
|
|
67
|
-
"base-path-converter": "^1.0.2",
|
|
68
67
|
"form-data": "^2.3.3",
|
|
69
68
|
"is-ipfs": "^0.6.0",
|
|
70
69
|
"path": "^0.12.7"
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
import NodeFormData from 'form-data';
|
|
3
3
|
|
|
4
|
-
import basePathConverter from 'base-path-converter';
|
|
5
4
|
import { PinataConfig } from '../..';
|
|
6
5
|
import {
|
|
7
6
|
PinataPinOptions,
|
|
@@ -12,6 +11,16 @@ import fs from 'fs';
|
|
|
12
11
|
|
|
13
12
|
import path from 'path';
|
|
14
13
|
|
|
14
|
+
export function normalizePath(p: string) {
|
|
15
|
+
const splitPath = path.normalize(p).split(path.sep).filter(file => file.length > 0 && file !== '..');
|
|
16
|
+
let folderFile = splitPath;
|
|
17
|
+
if (splitPath.length > 1) {
|
|
18
|
+
folderFile = splitPath.slice(splitPath.length - 2);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return path.join.apply(null, folderFile);
|
|
22
|
+
}
|
|
23
|
+
|
|
15
24
|
export default async function pinFromFS(
|
|
16
25
|
config: PinataConfig,
|
|
17
26
|
sourcePath: string,
|
|
@@ -32,11 +41,12 @@ export default async function pinFromFS(
|
|
|
32
41
|
}
|
|
33
42
|
const files = await fs.promises.readdir(sourcePath);
|
|
34
43
|
const data = new NodeFormData();
|
|
35
|
-
|
|
36
44
|
files.forEach((file: string) => {
|
|
45
|
+
|
|
46
|
+
const filepath = path.join(sourcePath, file);
|
|
37
47
|
//for each file stream, we need to include the correct relative file path
|
|
38
|
-
data.append('file', fs.createReadStream(
|
|
39
|
-
filepath:
|
|
48
|
+
data.append('file', fs.createReadStream(filepath), {
|
|
49
|
+
filepath: normalizePath(filepath)
|
|
40
50
|
});
|
|
41
51
|
});
|
|
42
52
|
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
-
import pinFromFS
|
|
2
|
+
import pinFromFS, {
|
|
3
|
+
normalizePath
|
|
4
|
+
} from '../../../src/commands/pinning/pinFromFS';
|
|
3
5
|
jest.mock('axios');
|
|
4
6
|
|
|
5
7
|
//common values
|
|
@@ -12,7 +14,12 @@ test('200 status is returned', () => {
|
|
|
12
14
|
};
|
|
13
15
|
(axios.post as jest.Mock).mockResolvedValue(goodStatus);
|
|
14
16
|
expect.assertions(1);
|
|
15
|
-
expect(
|
|
17
|
+
expect(
|
|
18
|
+
pinFromFS(
|
|
19
|
+
{ pinataApiKey: 'test', pinataSecretApiKey: 'test' },
|
|
20
|
+
testSource
|
|
21
|
+
)
|
|
22
|
+
).resolves.toEqual(goodStatus.data);
|
|
16
23
|
});
|
|
17
24
|
|
|
18
25
|
test('Result other than 200 status is returned', () => {
|
|
@@ -21,12 +28,44 @@ test('Result other than 200 status is returned', () => {
|
|
|
21
28
|
};
|
|
22
29
|
(axios.post as jest.Mock).mockResolvedValue(badStatus);
|
|
23
30
|
expect.assertions(1);
|
|
24
|
-
expect(
|
|
31
|
+
expect(
|
|
32
|
+
pinFromFS(
|
|
33
|
+
{ pinataApiKey: 'test', pinataSecretApiKey: 'test' },
|
|
34
|
+
testSource
|
|
35
|
+
)
|
|
36
|
+
).rejects.toEqual(
|
|
37
|
+
Error(`unknown server response while pinning to IPFS: ${badStatus}`)
|
|
38
|
+
);
|
|
25
39
|
});
|
|
26
40
|
|
|
27
41
|
test('Rejection handled', () => {
|
|
28
42
|
(axios.post as jest.Mock).mockRejectedValue('test error');
|
|
29
43
|
expect.assertions(1);
|
|
30
|
-
expect(
|
|
44
|
+
expect(
|
|
45
|
+
pinFromFS(
|
|
46
|
+
{ pinataApiKey: 'test', pinataSecretApiKey: 'test' },
|
|
47
|
+
testSource
|
|
48
|
+
)
|
|
49
|
+
).rejects.toEqual('test error');
|
|
31
50
|
});
|
|
32
51
|
|
|
52
|
+
test('normalize path', () => {
|
|
53
|
+
const useCases = [
|
|
54
|
+
{
|
|
55
|
+
start: '/Users/somebody/Documents//test-folder-issue/screen.png',
|
|
56
|
+
end: 'test-folder-issue/screen.png'
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
{
|
|
60
|
+
start: '../test/commands/data/pinlist-sample-api.ts',
|
|
61
|
+
end: 'data/pinlist-sample-api.ts'
|
|
62
|
+
},
|
|
63
|
+
{ start: '../test/log.txt', end: 'test/log.txt' },
|
|
64
|
+
{ start: './cat.pdf', end: 'cat.pdf' },
|
|
65
|
+
{ start: '../cat.pdf', end: 'cat.pdf' }
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
useCases.forEach((p) => {
|
|
69
|
+
expect(normalizePath(p.start)).toEqual(p.end);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { PinataConfig } from '../..';
|
|
2
2
|
import { PinataPinOptions, PinataPinResponse } from './pinFileToIPFS';
|
|
3
|
+
export declare function normalizePath(p: string): string;
|
|
3
4
|
export default function pinFromFS(config: PinataConfig, sourcePath: string, options?: PinataPinOptions): Promise<PinataPinResponse>;
|
package/types/index.d.ts
CHANGED
package/webpack.config.js
CHANGED