@pinata/sdk 1.1.26 → 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 (54) hide show
  1. package/.babelrc +4 -3
  2. package/.nvmrc +1 -1
  3. package/README.md +103 -19
  4. package/lib/pinata-sdk.js +615 -157
  5. package/lib/pinata-sdk.min.js +5 -5
  6. package/package.json +3 -2
  7. package/src/commands/data/getFilesByCount/getFilesByCount.js +67 -0
  8. package/src/commands/data/pinList/pinList.js +6 -11
  9. package/src/commands/data/pinList/queryBuilder.js +4 -0
  10. package/src/commands/data/testAuthentication.js +4 -11
  11. package/src/commands/data/userPinnedDataTotal.js +4 -11
  12. package/src/commands/pinning/hashMetadata.js +4 -11
  13. package/src/commands/pinning/hashPinPolicy.js +4 -10
  14. package/src/commands/pinning/pinByHash.js +4 -11
  15. package/src/commands/pinning/pinFileToIPFS.js +5 -16
  16. package/src/commands/pinning/pinFromFS.js +6 -23
  17. package/src/commands/pinning/pinJSONToIPFS.js +4 -10
  18. package/src/commands/pinning/pinJobs/pinJobs.js +4 -10
  19. package/src/commands/pinning/unpin.js +4 -11
  20. package/src/commands/pinning/userPinPolicy.js +4 -10
  21. package/src/constants.js +3 -0
  22. package/src/index.js +63 -13
  23. package/src/util/validators.js +88 -12
  24. package/test/commands/data/getFilesByCount/getFilesByCount.test.js +211 -0
  25. package/test/commands/data/pinList/pinList.test.js +100 -26
  26. package/test/commands/data/pinlist-sample-api.js +540 -0
  27. package/test/commands/data/testAuthentication.test.js +17 -13
  28. package/test/commands/data/userPinnedDataTotal.test.js +3 -3
  29. package/test/commands/pinning/hashMetadata.test.js +7 -7
  30. package/test/commands/pinning/hashPinPolicy.test.js +6 -6
  31. package/test/commands/pinning/pinByHash.test.js +4 -4
  32. package/test/commands/pinning/pinFileToIPFS.test.js +5 -5
  33. package/test/commands/pinning/pinFromFS.js +3 -3
  34. package/test/commands/pinning/pinJSONToIPFS.test.js +4 -4
  35. package/test/commands/pinning/pinJobs/pinJobs.test.js +3 -3
  36. package/test/commands/pinning/unpin.test.js +5 -5
  37. package/test/commands/pinning/userPinPolicy.test.js +51 -22
  38. package/test/util/validators.test.js +114 -72
  39. package/types/commands/data/getFilesByCount/getFilesByCount.d.ts +12 -0
  40. package/types/commands/data/pinList/pinList.d.ts +2 -2
  41. package/types/commands/data/testAuthentication.d.ts +1 -1
  42. package/types/commands/data/userPinnedDataTotal.d.ts +1 -1
  43. package/types/commands/pinning/hashMetadata.d.ts +1 -1
  44. package/types/commands/pinning/hashPinPolicy.d.ts +1 -1
  45. package/types/commands/pinning/pinByHash.d.ts +1 -1
  46. package/types/commands/pinning/pinFileToIPFS.d.ts +1 -1
  47. package/types/commands/pinning/pinFromFS.d.ts +1 -1
  48. package/types/commands/pinning/pinJSONToIPFS.d.ts +1 -1
  49. package/types/commands/pinning/pinJobs/pinJobs.d.ts +1 -1
  50. package/types/commands/pinning/unpin.d.ts +1 -1
  51. package/types/commands/pinning/userPinPolicy.d.ts +1 -1
  52. package/types/constants.d.ts +1 -0
  53. package/types/index.d.ts +9 -174
  54. 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.1.26",
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",
@@ -36,7 +36,8 @@
36
36
  "devDependencies": {
37
37
  "@babel/cli": "^7.8.4",
38
38
  "@babel/core": "^7.8.4",
39
- "@babel/preset-env": "^7.9.0",
39
+ "@babel/plugin-transform-runtime": "^7.19.1",
40
+ "@babel/preset-env": "^7.19.4",
40
41
  "@babel/register": "^7.8.3",
41
42
  "@types/node": "^15.6.1",
42
43
  "acorn": "^7.1.0",
@@ -0,0 +1,67 @@
1
+ import pinList from '../pinList/pinList';
2
+
3
+ export default function getFilesByCount(
4
+ config,
5
+
6
+ filters = {},
7
+ maxCount = -1
8
+ ) {
9
+ if (maxCount === 0) {
10
+ throw Error("Max count can't be 0");
11
+ }
12
+
13
+ const asyncIterable = {
14
+ [Symbol.asyncIterator]: () => {
15
+ let i = 0;
16
+ const pageLimit = 10;
17
+ let pageOffset = 0;
18
+ let cache = [];
19
+ let keepLooping = false;
20
+
21
+ return {
22
+ next() {
23
+ return new Promise((resolve, reject) => {
24
+ if (i === 0 || (i % pageLimit === 0 && keepLooping)) {
25
+ resolve(
26
+ pinList(config, {
27
+ filters,
28
+ ...{ pageOffset, pageLimit }
29
+ }).then((resp) => {
30
+ cache = resp.rows;
31
+ pageOffset = pageOffset + cache.length;
32
+
33
+ // If the user requested all pins we have to keep looping if the limit page was not fulfill
34
+ keepLooping =
35
+ cache.length !== 0 &&
36
+ (maxCount === -1 ?
37
+ cache.length === pageLimit :
38
+ i < maxCount);
39
+ })
40
+ );
41
+ }
42
+ resolve();
43
+ }).then(() => {
44
+ const valueToReturn = cache[i % pageLimit];
45
+
46
+ const done = !(
47
+ (maxCount === -1 ? i < pageOffset : i < maxCount) &&
48
+ keepLooping
49
+ );
50
+
51
+ i++;
52
+ return Promise.resolve({
53
+ value: valueToReturn,
54
+ done
55
+ });
56
+ });
57
+ },
58
+ return() {
59
+ // This will be reached if the consumer called 'break' or 'return' early in the loop.
60
+ return Promise.resolve({ value: i, done: true });
61
+ }
62
+ };
63
+ }
64
+ };
65
+
66
+ return asyncIterable;
67
+ }
@@ -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
 
@@ -8,11 +8,11 @@ import queryBuilder from './queryBuilder';
8
8
  * Pin List
9
9
  * @param {string} pinataApiKey
10
10
  * @param {string} pinataSecretApiKey
11
- * @param {string} filters
11
+ * @param {any} filters
12
12
  * @returns {Promise<unknown>}
13
13
  */
14
- export default function pinList(pinataApiKey, pinataSecretApiKey, filters) {
15
- validateApiKeys(pinataApiKey, pinataSecretApiKey);
14
+ export default function pinList(config, filters = {}) {
15
+ filters = {...filters, ...{includeCount: 'false' }};
16
16
 
17
17
  const baseEndpoint = `${baseUrl}/data/pinList`;
18
18
  const endpoint = queryBuilder(baseEndpoint, filters);
@@ -20,13 +20,8 @@ export default function pinList(pinataApiKey, pinataSecretApiKey, filters) {
20
20
  return new Promise((resolve, reject) => {
21
21
  axios.get(
22
22
  endpoint,
23
- {
24
- withCredentials: true,
25
- headers: {
26
- 'pinata_api_key': pinataApiKey,
27
- 'pinata_secret_api_key': pinataSecretApiKey
28
- }
29
- }).then(function (result) {
23
+ {...createConfigForAxiosHeaders(config)})
24
+ .then(function (result) {
30
25
  if (result.status !== 200) {
31
26
  reject(new Error(`unknown server response while attempting to retrieve user pin list: ${result}`));
32
27
  }
@@ -35,6 +35,10 @@ export default function queryBuilder(baseUrl, filters) {
35
35
 
36
36
  if (filters) {
37
37
  // now we need to construct the actual URL based on the given filters provided
38
+ if (filters.includeCount) {
39
+ filterQuery = filterQuery + `includeCount=${filters.includeCount}&`;
40
+ }
41
+
38
42
  if (filters.hashContains) {
39
43
  if (typeof filters.hashContains !== 'string') {
40
44
  throw new Error('hashContains value is not a string');
@@ -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';