@pinata/sdk 1.2.0 → 1.2.1

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.
Files changed (50) hide show
  1. package/README.md +21 -5
  2. package/lib/pinata-sdk.js +305 -155
  3. package/lib/pinata-sdk.min.js +5 -5
  4. package/package.json +1 -1
  5. package/src/commands/data/getFilesByCount/getFilesByCount.js +2 -5
  6. package/src/commands/data/pinList/pinList.js +4 -11
  7. package/src/commands/data/testAuthentication.js +4 -11
  8. package/src/commands/data/userPinnedDataTotal.js +4 -11
  9. package/src/commands/pinning/hashMetadata.js +4 -11
  10. package/src/commands/pinning/hashPinPolicy.js +4 -10
  11. package/src/commands/pinning/pinByHash.js +4 -11
  12. package/src/commands/pinning/pinFileToIPFS.js +5 -16
  13. package/src/commands/pinning/pinFromFS.js +6 -23
  14. package/src/commands/pinning/pinJSONToIPFS.js +4 -10
  15. package/src/commands/pinning/pinJobs/pinJobs.js +4 -10
  16. package/src/commands/pinning/unpin.js +4 -11
  17. package/src/commands/pinning/userPinPolicy.js +4 -10
  18. package/src/constants.js +3 -0
  19. package/src/index.js +60 -15
  20. package/src/util/validators.js +88 -12
  21. package/test/commands/data/getFilesByCount/getFilesByCount.test.js +17 -21
  22. package/test/commands/data/pinList/pinList.test.js +20 -15
  23. package/test/commands/data/testAuthentication.test.js +17 -13
  24. package/test/commands/data/userPinnedDataTotal.test.js +3 -3
  25. package/test/commands/pinning/hashMetadata.test.js +7 -7
  26. package/test/commands/pinning/hashPinPolicy.test.js +6 -6
  27. package/test/commands/pinning/pinByHash.test.js +4 -4
  28. package/test/commands/pinning/pinFileToIPFS.test.js +5 -5
  29. package/test/commands/pinning/pinFromFS.js +3 -3
  30. package/test/commands/pinning/pinJSONToIPFS.test.js +4 -4
  31. package/test/commands/pinning/pinJobs/pinJobs.test.js +3 -3
  32. package/test/commands/pinning/unpin.test.js +5 -5
  33. package/test/commands/pinning/userPinPolicy.test.js +51 -22
  34. package/test/util/validators.test.js +114 -72
  35. package/types/commands/data/getFilesByCount/getFilesByCount.d.ts +1 -1
  36. package/types/commands/data/pinList/pinList.d.ts +1 -1
  37. package/types/commands/data/testAuthentication.d.ts +1 -1
  38. package/types/commands/data/userPinnedDataTotal.d.ts +1 -1
  39. package/types/commands/pinning/hashMetadata.d.ts +1 -1
  40. package/types/commands/pinning/hashPinPolicy.d.ts +1 -1
  41. package/types/commands/pinning/pinByHash.d.ts +1 -1
  42. package/types/commands/pinning/pinFileToIPFS.d.ts +1 -1
  43. package/types/commands/pinning/pinFromFS.d.ts +1 -1
  44. package/types/commands/pinning/pinJSONToIPFS.d.ts +1 -1
  45. package/types/commands/pinning/pinJobs/pinJobs.d.ts +1 -1
  46. package/types/commands/pinning/unpin.d.ts +1 -1
  47. package/types/commands/pinning/userPinPolicy.d.ts +1 -1
  48. package/types/constants.d.ts +1 -0
  49. package/types/index.d.ts +9 -174
  50. package/types/util/validators.d.ts +34 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinata/sdk",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Official SDK for the Pinata IPFS platform",
5
5
  "main": "lib/pinata-sdk.js",
6
6
  "types": "types/index.d.ts",
@@ -1,14 +1,11 @@
1
- import { validateApiKeys } from '../../../util/validators';
2
1
  import pinList from '../pinList/pinList';
3
2
 
4
3
  export default function getFilesByCount(
5
- pinataApiKey,
6
- pinataSecretApiKey,
4
+ config,
7
5
 
8
6
  filters = {},
9
7
  maxCount = -1
10
8
  ) {
11
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
12
9
  if (maxCount === 0) {
13
10
  throw Error("Max count can't be 0");
14
11
  }
@@ -26,7 +23,7 @@ export default function getFilesByCount(
26
23
  return new Promise((resolve, reject) => {
27
24
  if (i === 0 || (i % pageLimit === 0 && keepLooping)) {
28
25
  resolve(
29
- pinList(pinataApiKey, pinataSecretApiKey, {
26
+ pinList(config, {
30
27
  filters,
31
28
  ...{ pageOffset, pageLimit }
32
29
  }).then((resp) => {
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../../constants';
3
- import { validateApiKeys } from '../../../util/validators';
3
+ import { createConfigForAxiosHeaders } from '../../../util/validators';
4
4
  import { handleError } from '../../../util/errorResponse';
5
5
  import queryBuilder from './queryBuilder';
6
6
 
@@ -11,9 +11,7 @@ import queryBuilder from './queryBuilder';
11
11
  * @param {any} filters
12
12
  * @returns {Promise<unknown>}
13
13
  */
14
- export default function pinList(pinataApiKey, pinataSecretApiKey, filters = {}) {
15
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
16
-
14
+ export default function pinList(config, filters = {}) {
17
15
  filters = {...filters, ...{includeCount: 'false' }};
18
16
 
19
17
  const baseEndpoint = `${baseUrl}/data/pinList`;
@@ -22,13 +20,8 @@ export default function pinList(pinataApiKey, pinataSecretApiKey, filters = {})
22
20
  return new Promise((resolve, reject) => {
23
21
  axios.get(
24
22
  endpoint,
25
- {
26
- withCredentials: true,
27
- headers: {
28
- 'pinata_api_key': pinataApiKey,
29
- 'pinata_secret_api_key': pinataSecretApiKey
30
- }
31
- }).then(function (result) {
23
+ {...createConfigForAxiosHeaders(config)})
24
+ .then(function (result) {
32
25
  if (result.status !== 200) {
33
26
  reject(new Error(`unknown server response while attempting to retrieve user pin list: ${result}`));
34
27
  }
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../constants';
3
- import {validateApiKeys} from '../../util/validators';
3
+ import {createConfigForAxiosHeaders} from '../../util/validators';
4
4
  import { handleError } from '../../util/errorResponse';
5
5
 
6
6
  /**
@@ -9,22 +9,15 @@ import { handleError } from '../../util/errorResponse';
9
9
  * @param {string} pinataSecretApiKey
10
10
  * @returns {Promise<unknown>}
11
11
  */
12
- export default function testAuthentication(pinataApiKey, pinataSecretApiKey) {
13
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
14
-
12
+ export default function testAuthentication(config) {
15
13
  // test authentication to make sure that the user's provided keys are legit
16
14
  const endpoint = `${baseUrl}/data/testAuthentication`;
17
15
 
18
16
  return new Promise((resolve, reject) => {
19
17
  axios.get(
20
18
  endpoint,
21
- {
22
- withCredentials: true,
23
- headers: {
24
- 'pinata_api_key': pinataApiKey,
25
- 'pinata_secret_api_key': pinataSecretApiKey
26
- }
27
- }).then(function (result) {
19
+ {...createConfigForAxiosHeaders(config)})
20
+ .then(function (result) {
28
21
  if (result.status !== 200) {
29
22
  reject(new Error(`unknown server response while authenticating: ${result}`));
30
23
  }
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../constants';
3
- import { validateApiKeys } from '../../util/validators';
3
+ import { createConfigForAxiosHeaders } from '../../util/validators';
4
4
  import { handleError } from '../../util/errorResponse';
5
5
 
6
6
  /**
@@ -9,21 +9,14 @@ import { handleError } from '../../util/errorResponse';
9
9
  * @param {string} pinataSecretApiKey
10
10
  * @returns {Promise<unknown>}
11
11
  */
12
- export default function userPinnedDataTotal(pinataApiKey, pinataSecretApiKey) {
13
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
14
-
12
+ export default function userPinnedDataTotal(config) {
15
13
  let endpoint = `${baseUrl}/data/userPinnedDataTotal`;
16
14
 
17
15
  return new Promise((resolve, reject) => {
18
16
  axios.get(
19
17
  endpoint,
20
- {
21
- withCredentials: true,
22
- headers: {
23
- 'pinata_api_key': pinataApiKey,
24
- 'pinata_secret_api_key': pinataSecretApiKey
25
- }
26
- }).then(function (result) {
18
+ {...createConfigForAxiosHeaders(config)})
19
+ .then(function (result) {
27
20
  if (result.status !== 200) {
28
21
  reject(new Error(`unknown server response while attempting to retrieve pinned data total: ${result}`));
29
22
  }
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../constants';
3
- import { validateApiKeys, validateMetadata } from '../../util/validators';
3
+ import { createConfigForAxiosHeaders, validateMetadata } from '../../util/validators';
4
4
  import isIPFS from 'is-ipfs';
5
5
  import { handleError } from '../../util/errorResponse';
6
6
 
@@ -12,9 +12,7 @@ import { handleError } from '../../util/errorResponse';
12
12
  * @param {*} metadata
13
13
  * @returns {Promise<unknown>}
14
14
  */
15
- export default function hashMetadata(pinataApiKey, pinataSecretApiKey, ipfsPinHash, metadata) {
16
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
17
-
15
+ export default function hashMetadata(config, ipfsPinHash, metadata) {
18
16
  if (!ipfsPinHash) {
19
17
  throw new Error('ipfsPinHash value is required for changing the pin policy of a pin');
20
18
  }
@@ -46,13 +44,8 @@ export default function hashMetadata(pinataApiKey, pinataSecretApiKey, ipfsPinHa
46
44
  axios.put(
47
45
  endpoint,
48
46
  body,
49
- {
50
- withCredentials: true,
51
- headers: {
52
- 'pinata_api_key': pinataApiKey,
53
- 'pinata_secret_api_key': pinataSecretApiKey
54
- }
55
- }).then(function (result) {
47
+ {...createConfigForAxiosHeaders(config)})
48
+ .then(function (result) {
56
49
  if (result.status !== 200) {
57
50
  reject(new Error(`unknown server response while changing metadata for hash: ${result}`));
58
51
  }
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../constants';
3
- import { validateApiKeys, validatePinPolicyStructure } from '../../util/validators';
3
+ import { createConfigForAxiosHeaders, validatePinPolicyStructure } from '../../util/validators';
4
4
  import isIPFS from 'is-ipfs';
5
5
  import { handleError } from '../../util/errorResponse';
6
6
 
@@ -12,8 +12,7 @@ import { handleError } from '../../util/errorResponse';
12
12
  * @param {*} newPinPolicy
13
13
  * @returns {Promise<unknown>}
14
14
  */
15
- export default function hashPinPolicy(pinataApiKey, pinataSecretApiKey, ipfsPinHash, newPinPolicy) {
16
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
15
+ export default function hashPinPolicy(config, ipfsPinHash, newPinPolicy) {
17
16
  validatePinPolicyStructure(newPinPolicy);
18
17
 
19
18
  if (!ipfsPinHash) {
@@ -38,13 +37,8 @@ export default function hashPinPolicy(pinataApiKey, pinataSecretApiKey, ipfsPinH
38
37
  axios.put(
39
38
  endpoint,
40
39
  body,
41
- {
42
- withCredentials: true,
43
- headers: {
44
- 'pinata_api_key': pinataApiKey,
45
- 'pinata_secret_api_key': pinataSecretApiKey
46
- }
47
- }).then(function (result) {
40
+ {...createConfigForAxiosHeaders(config)})
41
+ .then(function (result) {
48
42
  if (result.status !== 200) {
49
43
  reject(new Error(`unknown server response while changing pin policy for hash: ${result}`));
50
44
  }
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../constants';
3
- import { validateApiKeys, validateMetadata } from '../../util/validators';
3
+ import { createConfigForAxiosHeaders, validateMetadata } from '../../util/validators';
4
4
  import isIPFS from 'is-ipfs';
5
5
  import { handleError } from '../../util/errorResponse';
6
6
 
@@ -12,9 +12,7 @@ import { handleError } from '../../util/errorResponse';
12
12
  * @param {*} options
13
13
  * @returns {Promise<unknown>}
14
14
  */
15
- export default function pinByHash(pinataApiKey, pinataSecretApiKey, hashToPin, options) {
16
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
17
-
15
+ export default function pinByHash(config, hashToPin, options) {
18
16
  if (!hashToPin) {
19
17
  throw new Error('hashToPin value is required for pinning by hash');
20
18
  }
@@ -42,13 +40,8 @@ export default function pinByHash(pinataApiKey, pinataSecretApiKey, hashToPin, o
42
40
  axios.post(
43
41
  endpoint,
44
42
  body,
45
- {
46
- withCredentials: true,
47
- headers: {
48
- 'pinata_api_key': pinataApiKey,
49
- 'pinata_secret_api_key': pinataSecretApiKey
50
- }
51
- }).then(function (result) {
43
+ {...createConfigForAxiosHeaders(config)})
44
+ .then(function (result) {
52
45
  if (result.status !== 200) {
53
46
  reject(new Error(`unknown server response while adding to pin queue: ${result}`));
54
47
  }
@@ -2,7 +2,7 @@ import axios from 'axios';
2
2
  import { baseUrl } from './../../constants';
3
3
  import NodeFormData from 'form-data';
4
4
  import stream from 'stream';
5
- import {validateApiKeys, validateMetadata, validatePinataOptions} from '../../util/validators';
5
+ import {createConfigForAxiosHeadersWithFormData, validateMetadata, validatePinataOptions} from '../../util/validators';
6
6
  import { handleError } from '../../util/errorResponse';
7
7
 
8
8
  /**
@@ -13,9 +13,7 @@ import { handleError } from '../../util/errorResponse';
13
13
  * @param {*} options
14
14
  * @returns {Promise<unknown>}
15
15
  */
16
- export default function pinFileToIPFS(pinataApiKey, pinataSecretApiKey, readStream, options) {
17
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
18
-
16
+ export default function pinFileToIPFS(config, readStream, options) {
19
17
  return new Promise((resolve, reject) => {
20
18
 
21
19
  const data = new NodeFormData();
@@ -38,20 +36,11 @@ export default function pinFileToIPFS(pinataApiKey, pinataSecretApiKey, readStre
38
36
  data.append('pinataOptions', JSON.stringify(options.pinataOptions));
39
37
  }
40
38
  }
41
-
42
- axios.post(
39
+ axios.post(
43
40
  endpoint,
44
41
  readStream instanceof NodeFormData ? readStream : data,
45
- {
46
- withCredentials: true,
47
- maxContentLength: 'Infinity', //this is needed to prevent axios from erroring out with large files
48
- maxBodyLength: 'Infinity',
49
- headers: {
50
- 'Content-type': `multipart/form-data; boundary= ${data._boundary}`,
51
- 'pinata_api_key': pinataApiKey,
52
- 'pinata_secret_api_key': pinataSecretApiKey
53
- }
54
- }).then(function (result) {
42
+ createConfigForAxiosHeadersWithFormData(config, data._boundary)
43
+ ).then(function (result) {
55
44
  if (result.status !== 200) {
56
45
  reject(new Error(`unknown server response while pinning File to IPFS: ${result}`));
57
46
  }
@@ -1,7 +1,7 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../constants';
3
3
  import NodeFormData from 'form-data';
4
- import {validateApiKeys, validateMetadata, validatePinataOptions} from '../../util/validators';
4
+ import {createConfigForAxiosHeadersWithFormData, validateMetadata, validatePinataOptions} from '../../util/validators';
5
5
  import basePathConverter from 'base-path-converter';
6
6
  import { handleError } from '../../util/errorResponse';
7
7
  const fs = require('fs');
@@ -15,8 +15,7 @@ const recursive = require('recursive-fs');
15
15
  * @param {*} options
16
16
  * @returns {Promise<unknown>}
17
17
  */
18
- export default function pinFromFS(pinataApiKey, pinataSecretApiKey, sourcePath, options) {
19
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
18
+ export default function pinFromFS(config, sourcePath, options) {
20
19
 
21
20
  return new Promise((resolve, reject) => {
22
21
  const endpoint = `${baseUrl}/pinning/pinFileToIPFS`;
@@ -45,16 +44,8 @@ export default function pinFromFS(pinataApiKey, pinataSecretApiKey, sourcePath,
45
44
  axios.post(
46
45
  endpoint,
47
46
  data,
48
- {
49
- withCredentials: true,
50
- maxContentLength: 'Infinity', //this is needed to prevent axios from erroring out with large directories
51
- maxBodyLength: 'Infinity',
52
- headers: {
53
- 'Content-type': `multipart/form-data; boundary= ${data._boundary}`,
54
- 'pinata_api_key': pinataApiKey,
55
- 'pinata_secret_api_key': pinataSecretApiKey
56
- }
57
- }).then(function (result) {
47
+ createConfigForAxiosHeadersWithFormData(config, data._boundary))
48
+ .then(function (result) {
58
49
  if (result.status !== 200) {
59
50
  reject(new Error(`unknown server response while pinning File to IPFS: ${result}`));
60
51
  }
@@ -92,16 +83,8 @@ export default function pinFromFS(pinataApiKey, pinataSecretApiKey, sourcePath,
92
83
  axios.post(
93
84
  endpoint,
94
85
  data,
95
- {
96
- withCredentials: true,
97
- maxContentLength: 'Infinity',
98
- maxBodyLength: 'Infinity', //this is needed to prevent axios from erroring out with large directories
99
- headers: {
100
- 'Content-type': `multipart/form-data; boundary= ${data._boundary}`,
101
- 'pinata_api_key': pinataApiKey,
102
- 'pinata_secret_api_key': pinataSecretApiKey
103
- }
104
- }).then(function (result) {
86
+ createConfigForAxiosHeadersWithFormData(config, data._boundary))
87
+ .then(function (result) {
105
88
  if (result.status !== 200) {
106
89
  reject(new Error(`unknown server response while pinning File to IPFS: ${result}`));
107
90
  }
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../constants';
3
- import { validateApiKeys, validateMetadata, validatePinataOptions } from '../../util/validators';
3
+ import { createConfigForAxiosHeaders, validateMetadata, validatePinataOptions } from '../../util/validators';
4
4
  import { handleError } from '../../util/errorResponse';
5
5
 
6
6
  /**
@@ -11,8 +11,7 @@ import { handleError } from '../../util/errorResponse';
11
11
  * @param {*} options
12
12
  * @returns {Promise<unknown>}
13
13
  */
14
- export default function pinJSONToIPFS(pinataApiKey, pinataSecretApiKey, body, options) {
15
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
14
+ export default function pinJSONToIPFS(config, body, options) {
16
15
 
17
16
  let requestBody = body;
18
17
 
@@ -40,13 +39,8 @@ export default function pinJSONToIPFS(pinataApiKey, pinataSecretApiKey, body, op
40
39
  axios.post(
41
40
  endpoint,
42
41
  requestBody,
43
- {
44
- withCredentials: true,
45
- headers: {
46
- 'pinata_api_key': pinataApiKey,
47
- 'pinata_secret_api_key': pinataSecretApiKey
48
- }
49
- }).then(function (result) {
42
+ {...createConfigForAxiosHeaders(config)})
43
+ .then(function (result) {
50
44
  if (result.status !== 200) {
51
45
  reject(new Error(`unknown server response while pinning JSON to IPFS: ${result}`));
52
46
  }
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../../constants';
3
- import { validateApiKeys } from '../../../util/validators';
3
+ import { createConfigForAxiosHeaders } from '../../../util/validators';
4
4
  import queryBuilder from './queryBuilder';
5
5
  import { handleError } from '../../../util/errorResponse';
6
6
 
@@ -11,8 +11,7 @@ import { handleError } from '../../../util/errorResponse';
11
11
  * @param {*} filters
12
12
  * @returns {Promise<unknown>}
13
13
  */
14
- export default function pinJobs(pinataApiKey, pinataSecretApiKey, filters) {
15
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
14
+ export default function pinJobs(config, filters) {
16
15
 
17
16
  let endpoint = `${baseUrl}/pinning/pinJobs`;
18
17
 
@@ -23,13 +22,8 @@ export default function pinJobs(pinataApiKey, pinataSecretApiKey, filters) {
23
22
  return new Promise((resolve, reject) => {
24
23
  axios.get(
25
24
  endpoint,
26
- {
27
- withCredentials: true,
28
- headers: {
29
- 'pinata_api_key': pinataApiKey,
30
- 'pinata_secret_api_key': pinataSecretApiKey
31
- }
32
- }).then(function (result) {
25
+ {...createConfigForAxiosHeaders(config)})
26
+ .then(function (result) {
33
27
  if (result.status !== 200) {
34
28
  reject(new Error(`unknown server response while attempting to retrieve pin jobs: ${result}`));
35
29
  }
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../constants';
3
- import { validateApiKeys } from '../../util/validators';
3
+ import { createConfigForAxiosHeaders } from '../../util/validators';
4
4
  import isIPFS from 'is-ipfs';
5
5
  import { handleError } from '../../util/errorResponse';
6
6
 
@@ -11,9 +11,7 @@ import { handleError } from '../../util/errorResponse';
11
11
  * @param {string} hashToUnpin
12
12
  * @returns {Promise<unknown>}
13
13
  */
14
- export default function unpin(pinataApiKey, pinataSecretApiKey, hashToUnpin) {
15
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
16
-
14
+ export default function unpin(config, hashToUnpin) {
17
15
  if (!hashToUnpin) {
18
16
  throw new Error('hashToUnpin value is required for removing a pin from Pinata');
19
17
  }
@@ -26,13 +24,8 @@ export default function unpin(pinataApiKey, pinataSecretApiKey, hashToUnpin) {
26
24
  return new Promise((resolve, reject) => {
27
25
  axios.delete(
28
26
  endpoint,
29
- {
30
- withCredentials: true,
31
- headers: {
32
- 'pinata_api_key': pinataApiKey,
33
- 'pinata_secret_api_key': pinataSecretApiKey
34
- }
35
- }).then(function (result) {
27
+ {...createConfigForAxiosHeaders(config)})
28
+ .then(function (result) {
36
29
  if (result.status !== 200) {
37
30
  reject(new Error(`unknown server response while removing pin from IPFS: ${result}`));
38
31
  }
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { baseUrl } from './../../constants';
3
- import { validateApiKeys, validatePinPolicyStructure } from '../../util/validators';
3
+ import { createConfigForAxiosHeaders, validatePinPolicyStructure } from '../../util/validators';
4
4
  import { handleError } from '../../util/errorResponse';
5
5
 
6
6
  /**
@@ -10,8 +10,7 @@ import { handleError } from '../../util/errorResponse';
10
10
  * @param {*} newPinPolicy
11
11
  * @returns {Promise<unknown>}
12
12
  */
13
- export default function userPinPolicy(pinataApiKey, pinataSecretApiKey, newPinPolicy) {
14
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
13
+ export default function userPinPolicy(config, newPinPolicy) {
15
14
  validatePinPolicyStructure(newPinPolicy);
16
15
 
17
16
  if (!newPinPolicy) {
@@ -27,13 +26,8 @@ export default function userPinPolicy(pinataApiKey, pinataSecretApiKey, newPinPo
27
26
  axios.put(
28
27
  endpoint,
29
28
  body,
30
- {
31
- withCredentials: true,
32
- headers: {
33
- 'pinata_api_key': pinataApiKey,
34
- 'pinata_secret_api_key': pinataSecretApiKey
35
- }
36
- }).then(function (result) {
29
+ {...createConfigForAxiosHeaders(config)})
30
+ .then(function (result) {
37
31
  if (result.status !== 200) {
38
32
  reject(new Error(`unknown server response while changing pin policy for user: ${result}`));
39
33
  }
package/src/constants.js CHANGED
@@ -1 +1,4 @@
1
1
  export const baseUrl = 'https://api.pinata.cloud';
2
+
3
+ export const ERROR_NO_CREDENTIALS_PROVIDED =
4
+ 'No credentials provided! Please provide your pinata api key and pinata secret api key or your pinata JWT key as an argument when you start this script';
package/src/index.js CHANGED
@@ -199,8 +199,16 @@ import userPinnedDataTotal from './commands/data/userPinnedDataTotal';
199
199
  * @returns {Promise<number>}
200
200
  */
201
201
 
202
+ /**
203
+ * @typedef {Object} PinataConfig
204
+ * @property {string} [pinataApiKey]
205
+ * @property {string} [pinataSecretApiKey]
206
+ * @property {string} [pinataJWTKey]
207
+ */
208
+
202
209
  /**
203
210
  * @typedef PinataClient
211
+ * @property {PinataConfig} config
204
212
  * @property {pinByHash} pinByHash
205
213
  * @property {hashMetadata} hashMetadata
206
214
  * @property {pinFileToIPFS} pinFileToIPFS
@@ -213,50 +221,87 @@ import userPinnedDataTotal from './commands/data/userPinnedDataTotal';
213
221
  * @property {userPinnedDataTotal} userPinnedDataTotal
214
222
  */
215
223
 
224
+ function refactorConfig(pinataConfigParam) {
225
+ const config = {};
226
+ if (pinataConfigParam.pinataApiKey) {
227
+ config.pinataApiKey = pinataConfigParam.pinataApiKey;
228
+ }
229
+
230
+ if (pinataConfigParam.pinataSecretApiKey) {
231
+ config.pinataSecretApiKey = pinataConfigParam.pinataSecretApiKey;
232
+ }
233
+
234
+ if (pinataConfigParam.pinataJWTKey) {
235
+ config.pinataJWTKey = pinataConfigParam.pinataJWTKey;
236
+ }
237
+
238
+ return config;
239
+ }
240
+ function sanitizeConfig(pinataApiKey, pinataSecretApiKey) {
241
+ let config = {};
242
+
243
+ if (
244
+ typeof pinataApiKey === 'string' &&
245
+ typeof pinataSecretApiKey === 'string'
246
+ ) {
247
+ config.pinataApiKey = pinataApiKey;
248
+ config.pinataSecretApiKey = pinataSecretApiKey;
249
+ }
250
+
251
+ const isPinataConfigParam = typeof pinataApiKey === 'object';
252
+ if (isPinataConfigParam) {
253
+ config = refactorConfig(pinataApiKey);
254
+ }
255
+
256
+ return config;
257
+ }
258
+
216
259
  /**
217
260
  * Pinata Client
218
261
  *
219
- * @param {string} pinataApiKey
220
- * @param {string} pinataSecretApiKey
262
+ * @param {string | PinataConfig} [pinataApiKey]
263
+ * @param {string} [pinataSecretApiKey]
221
264
  * @returns {PinataClient}
222
265
  */
223
266
  export default function pinataClient(pinataApiKey, pinataSecretApiKey) {
224
- let client = {};
267
+ const client = {
268
+ config: sanitizeConfig(pinataApiKey, pinataSecretApiKey)
269
+ };
225
270
 
226
271
  // setting up the actual calls you can make using this package
227
272
  client.pinByHash = function (hashToPin, options) {
228
- return pinByHash(pinataApiKey, pinataSecretApiKey, hashToPin, options);
273
+ return pinByHash(client.config, hashToPin, options);
229
274
  };
230
275
  client.hashMetadata = function (ipfsPinHash, metadata) {
231
- return hashMetadata(pinataApiKey, pinataSecretApiKey, ipfsPinHash, metadata);
276
+ return hashMetadata(client.config, ipfsPinHash, metadata);
232
277
  };
233
278
  client.pinFileToIPFS = function (readableStream, options) {
234
- return pinFileToIPFS(pinataApiKey, pinataSecretApiKey, readableStream, options);
279
+ return pinFileToIPFS(client.config, readableStream, options);
235
280
  };
236
281
  client.pinFromFS = function (sourcePath, options) {
237
- return pinFromFS(pinataApiKey, pinataSecretApiKey, sourcePath, options);
282
+ return pinFromFS(client.config, sourcePath, options);
238
283
  };
239
284
  client.pinJSONToIPFS = function (body, options) {
240
- return pinJSONToIPFS(pinataApiKey, pinataSecretApiKey, body, options);
285
+ return pinJSONToIPFS(client.config, body, options);
241
286
  };
242
287
  client.pinJobs = function (filters) {
243
- return pinJobs(pinataApiKey, pinataSecretApiKey, filters);
288
+ return pinJobs(client.config, filters);
244
289
  };
245
290
  client.unpin = function (hashToUnpin) {
246
- return unpin(pinataApiKey, pinataSecretApiKey, hashToUnpin);
291
+ return unpin(client.config, hashToUnpin);
247
292
  };
248
293
  client.testAuthentication = function () {
249
- return testAuthentication(pinataApiKey, pinataSecretApiKey);
294
+ return testAuthentication(client.config);
250
295
  };
251
296
  client.pinList = function (filters) {
252
- return pinList(pinataApiKey, pinataSecretApiKey, filters);
297
+ return pinList(client.config, filters);
253
298
  };
254
299
 
255
- client.getFilesByCount = function (filters, maxCount,) {
256
- return getFilesByCount(pinataApiKey, pinataSecretApiKey, filters, maxCount);
300
+ client.getFilesByCount = function (filters, maxCount) {
301
+ return getFilesByCount(client.config, filters, maxCount);
257
302
  };
258
303
  client.userPinnedDataTotal = function () {
259
- return userPinnedDataTotal(pinataApiKey, pinataSecretApiKey);
304
+ return userPinnedDataTotal(client.config);
260
305
  };
261
306
  return client;
262
307
  }