@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinata/sdk",
3
- "version": "2.0.0",
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(file), {
39
- filepath: basePathConverter(sourcePath, file)
48
+ data.append('file', fs.createReadStream(filepath), {
49
+ filepath: normalizePath(filepath)
40
50
  });
41
51
  });
42
52
 
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import './require-babel-polyfill';
1
2
  import pinByHash, {
2
3
  PinataPinByHashPinOptions
3
4
  } from './commands/pinning/pinByHash';
@@ -0,0 +1,5 @@
1
+ export default (() => {
2
+ if (!global || !global._babelPolyfill) {
3
+ require('babel-polyfill');
4
+ }
5
+ })();
@@ -0,0 +1,9 @@
1
+ export {};
2
+
3
+ declare global {
4
+ namespace NodeJS {
5
+ interface Global {
6
+ _babelPolyfill: any;
7
+ }
8
+ }
9
+ }
@@ -1,5 +1,7 @@
1
1
  import axios from 'axios';
2
- import pinFromFS from '../../../src/commands/pinning/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(pinFromFS({ pinataApiKey: 'test', pinataSecretApiKey: 'test' }, testSource)).resolves.toEqual(goodStatus.data);
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(pinFromFS({ pinataApiKey: 'test', pinataSecretApiKey: 'test' }, testSource)).rejects.toEqual(Error(`unknown server response while pinning to IPFS: ${badStatus}`));
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(pinFromFS({ pinataApiKey: 'test', pinataSecretApiKey: 'test' }, testSource)).rejects.toEqual('test error');
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
@@ -1,3 +1,4 @@
1
+ import './require-babel-polyfill';
1
2
  import { PinataPinByHashPinOptions } from './commands/pinning/pinByHash';
2
3
  import { PinataPinOptions } from './commands/pinning/pinFileToIPFS';
3
4
  import { PinataPinJobsFilterOptions } from './commands/pinning/pinJobs/pinJobs';
@@ -0,0 +1,2 @@
1
+ declare const _default: void;
2
+ export default _default;
package/webpack.config.js CHANGED
@@ -16,7 +16,7 @@ module.exports = (env) => {
16
16
  }
17
17
  return {
18
18
  mode: mode,
19
- entry: ['babel-polyfill', __dirname + '/src/index.ts'],
19
+ entry: [ __dirname + '/src/index.ts'],
20
20
  devtool: 'inline-source-map',
21
21
  output: {
22
22
  path: __dirname + '/lib',