@pinata/sdk 2.1.0 → 3.0.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.
Files changed (81) hide show
  1. package/LICENSE +1 -2
  2. package/README.md +58 -695
  3. package/index.d.ts +9 -0
  4. package/index.js +6 -0
  5. package/index.mjs +4 -0
  6. package/legacy.js +15 -0
  7. package/package.json +58 -83
  8. package/react/index.d.ts +1 -0
  9. package/react/index.js +3 -0
  10. package/react/index.mjs +1 -0
  11. package/.babelrc +0 -5
  12. package/.editorconfig +0 -12
  13. package/.eslintrc +0 -173
  14. package/.husky/pre-commit +0 -6
  15. package/.nvmrc +0 -1
  16. package/.travis.yml +0 -5
  17. package/lib/pinata-sdk.js +0 -24526
  18. package/lib/pinata-sdk.min.js +0 -22
  19. package/pinata.png +0 -0
  20. package/src/commands/data/getFilesByCount/getFilesByCount.ts +0 -66
  21. package/src/commands/data/index.ts +0 -4
  22. package/src/commands/data/pinList/pinList.ts +0 -77
  23. package/src/commands/data/pinList/queryBuilder.ts +0 -239
  24. package/src/commands/data/testAuthentication.ts +0 -31
  25. package/src/commands/data/userPinnedDataTotal.ts +0 -24
  26. package/src/commands/pinning/hashMetadata.ts +0 -56
  27. package/src/commands/pinning/index.ts +0 -8
  28. package/src/commands/pinning/pinByHash.ts +0 -65
  29. package/src/commands/pinning/pinFileToIPFS.ts +0 -118
  30. package/src/commands/pinning/pinFromFS.ts +0 -60
  31. package/src/commands/pinning/pinJSONToIPFS.ts +0 -47
  32. package/src/commands/pinning/pinJobs/pinJobs.ts +0 -50
  33. package/src/commands/pinning/pinJobs/queryBuilder.ts +0 -75
  34. package/src/commands/pinning/unpin.ts +0 -32
  35. package/src/constants.ts +0 -4
  36. package/src/index.ts +0 -124
  37. package/src/require-babel-polyfill.ts +0 -5
  38. package/src/third-party/base-path-converter.d.ts +0 -3
  39. package/src/third-party/global.d.ts +0 -9
  40. package/src/third-party/is-ipfs.d.ts +0 -5
  41. package/src/util/errorResponse.ts +0 -10
  42. package/src/util/validators.ts +0 -182
  43. package/test/commands/data/getFilesByCount/getFilesByCount.test.ts +0 -212
  44. package/test/commands/data/pinList/pinList.test.ts +0 -102
  45. package/test/commands/data/pinList/queryBuilder.test.ts +0 -439
  46. package/test/commands/data/pinlist-sample-api.ts +0 -540
  47. package/test/commands/data/testAuthentication.test.ts +0 -34
  48. package/test/commands/data/userPinnedDataTotal.test.ts +0 -29
  49. package/test/commands/pinning/hashMetadata.test.ts +0 -76
  50. package/test/commands/pinning/pinByHash.test.ts +0 -46
  51. package/test/commands/pinning/pinFileToIPFS.test.ts +0 -53
  52. package/test/commands/pinning/pinFromFS.ts +0 -71
  53. package/test/commands/pinning/pinJSONToIPFS.test.ts +0 -42
  54. package/test/commands/pinning/pinJobs/pinJobs.test.ts +0 -29
  55. package/test/commands/pinning/pinJobs/queryBuilder.test.ts +0 -120
  56. package/test/commands/pinning/unpin.test.ts +0 -46
  57. package/test/util/validators.test.ts +0 -234
  58. package/tsconfig.json +0 -23
  59. package/types/commands/data/getFilesByCount/getFilesByCount.d.ts +0 -14
  60. package/types/commands/data/index.d.ts +0 -4
  61. package/types/commands/data/pinList/pinList.d.ts +0 -45
  62. package/types/commands/data/pinList/queryBuilder.d.ts +0 -1
  63. package/types/commands/data/testAuthentication.d.ts +0 -5
  64. package/types/commands/data/userPinnedDataTotal.d.ts +0 -2
  65. package/types/commands/pinning/hashMetadata.d.ts +0 -3
  66. package/types/commands/pinning/hashPinPolicy.d.ts +0 -9
  67. package/types/commands/pinning/index.d.ts +0 -8
  68. package/types/commands/pinning/pinByHash.d.ts +0 -14
  69. package/types/commands/pinning/pinFileToIPFS.d.ts +0 -25
  70. package/types/commands/pinning/pinFromFS.d.ts +0 -4
  71. package/types/commands/pinning/pinJSONToIPFS.d.ts +0 -3
  72. package/types/commands/pinning/pinJobs/pinJobs.d.ts +0 -20
  73. package/types/commands/pinning/pinJobs/queryBuilder.d.ts +0 -1
  74. package/types/commands/pinning/unpin.d.ts +0 -2
  75. package/types/commands/pinning/userPinPolicy.d.ts +0 -8
  76. package/types/constants.d.ts +0 -2
  77. package/types/index.d.ts +0 -39
  78. package/types/require-babel-polyfill.d.ts +0 -2
  79. package/types/util/errorResponse.d.ts +0 -1
  80. package/types/util/validators.d.ts +0 -37
  81. package/webpack.config.js +0 -58
@@ -1,182 +0,0 @@
1
- import isIPFS from 'is-ipfs';
2
- import { PinataConfig } from '..';
3
- import { ERROR_NO_CREDENTIALS_PROVIDED} from '../constants';
4
-
5
- export interface axiosHeaders
6
- { maxContentLength: number;
7
- maxBodyLength: number;
8
- headers: {
9
- [key: string]: any
10
- };
11
- withCredentials?: boolean;
12
- }
13
-
14
- export function validateApiKeys(pinataApiKey?: string, pinataSecretApiKey?: string) {
15
- if (!pinataApiKey || pinataApiKey === '') {
16
- throw new Error(
17
- 'No pinataApiKey provided! Please provide your pinata api key as an argument when you start this script'
18
- );
19
- }
20
- if (!pinataSecretApiKey || pinataSecretApiKey === '') {
21
- throw new Error(
22
- 'No pinataSecretApiKey provided! Please provide your pinata secret api key as an argument when you start this script'
23
- );
24
- }
25
- }
26
-
27
- export function createConfigForAxiosHeaders(config: PinataConfig) {
28
- if (
29
- config.pinataApiKey && config.pinataApiKey.length > 0 &&
30
- config.pinataSecretApiKey && config.pinataSecretApiKey.length > 0
31
- ) {
32
- return {
33
- withCredentials: true,
34
- headers: {
35
- pinata_api_key: config.pinataApiKey,
36
- pinata_secret_api_key: config.pinataSecretApiKey
37
- }
38
- };
39
- }
40
-
41
- if (config.pinataJWTKey && config.pinataJWTKey.length > 0) {
42
- return {
43
- headers: {
44
- Authorization: `Bearer ${config.pinataJWTKey}`
45
- }
46
- };
47
- }
48
-
49
- throw new Error(ERROR_NO_CREDENTIALS_PROVIDED);
50
- }
51
-
52
- export function createConfigForAxiosHeadersWithFormData(config: PinataConfig, boundaryValue: string) {
53
- const requestOptions: axiosHeaders = {
54
- ...createConfigForAxiosHeaders(config),
55
- maxContentLength: Infinity, //this is needed to prevent axios from erroring out with large files
56
- maxBodyLength: Infinity
57
- };
58
-
59
- requestOptions.headers['Content-type'] = `multipart/form-data; boundary=${boundaryValue}`;
60
- return requestOptions;
61
- }
62
-
63
- export function validateHostNodes(hostNodes: any) {
64
- if (!Array.isArray(hostNodes)) {
65
- throw new Error('host_nodes value must be an array');
66
- }
67
- hostNodes.forEach((node) => {
68
- if (!isIPFS.peerMultiaddr(node)) {
69
- throw new Error(
70
- `host_node array entry: ${node} is not a valid peer multiaddr`
71
- );
72
- }
73
- });
74
- }
75
-
76
- export function validateMetadata(metadata: any) {
77
- if (metadata.name) {
78
- if (
79
- !(
80
- typeof metadata.name === 'string' ||
81
- metadata.name instanceof String
82
- )
83
- ) {
84
- throw new Error('metadata name must be of type string');
85
- }
86
- }
87
-
88
- if (metadata.keyvalues) {
89
- if (!(typeof metadata.keyvalues === 'object')) {
90
- throw new Error('metatadata keyvalues must be an object');
91
- }
92
-
93
- let i = 0;
94
-
95
- Object.entries(metadata.keyvalues).forEach(function (keyValue: any) {
96
- if (i > 9) {
97
- throw new Error(
98
- 'No more than 10 keyvalues can be provided for metadata entries'
99
- );
100
- }
101
- // we want to make sure that the input is a string, a boolean, or a number, so we don't get an object passed in by accident
102
- if (
103
- !(
104
- typeof keyValue[1] === 'string' ||
105
- typeof keyValue[1] === 'boolean' ||
106
- !isNaN(keyValue[1])
107
- )
108
- ) {
109
- throw new Error(
110
- 'Metadata keyvalue values must be strings, booleans, or numbers'
111
- );
112
- }
113
- i++;
114
- });
115
- }
116
- }
117
-
118
- export function validatePinPolicyStructure(pinPolicy: { regions: any[]; }) {
119
- //this function takes in a pin policy and checks the JSON structure to make sure it's valid
120
- if (!pinPolicy) {
121
- throw new Error('No pin policy provided');
122
- }
123
-
124
- if (!pinPolicy.regions) {
125
- throw new Error('No regions provided in pin policy');
126
- }
127
- if (pinPolicy.regions.length) {
128
- pinPolicy.regions.forEach((region) => {
129
- if (
130
- !region.id ||
131
- !(
132
- Object.prototype.toString.call(region.id) ===
133
- '[object String]'
134
- )
135
- ) {
136
- throw new Error('region id must be a string');
137
- }
138
-
139
- if (
140
- !(
141
- region.desiredReplicationCount ||
142
- region.desiredReplicationCount === 0
143
- ) ||
144
- !Number.isInteger(region.desiredReplicationCount)
145
- ) {
146
- throw new Error('desiredReplicationCount must be an integer');
147
- }
148
- });
149
- }
150
- }
151
-
152
- export function validatePinataOptions(options: { cidVersion?: number; wrapWithDirectory?: boolean; hostNodes?: any; customPinPolicy?: any; }) {
153
- if (typeof options !== 'object') {
154
- throw new Error('options must be an object');
155
- }
156
-
157
- if (options.cidVersion) {
158
- // eslint-disable-next-line eqeqeq
159
- if (options.cidVersion != 0 && options.cidVersion != 1) {
160
- throw new Error('unsupported or invalid cidVersion');
161
- }
162
- }
163
- if (options.wrapWithDirectory) {
164
- // eslint-disable-next-line eqeqeq
165
- if (
166
- options.wrapWithDirectory !== true &&
167
- options.wrapWithDirectory !== false
168
- ) {
169
- throw new Error(
170
- 'wrapWithDirectory must be a boolean value of true or false'
171
- );
172
- }
173
- }
174
-
175
- if (options.hostNodes) {
176
- validateHostNodes(options.hostNodes);
177
- }
178
-
179
- if (options.customPinPolicy) {
180
- validatePinPolicyStructure(options.customPinPolicy);
181
- }
182
- }
@@ -1,212 +0,0 @@
1
- import getFilesByCount from '../../../../src/commands/data/getFilesByCount/getFilesByCount';
2
- import axios from 'axios';
3
- import {
4
- APIData,
5
- pinListAxiosMockPages,
6
- fakeHeaders
7
- } from '../pinlist-sample-api';
8
- import { PinataConfig, PinataPin, PinataPinListFilterOptions } from '../../../../src';
9
-
10
- jest.mock('axios');
11
-
12
- const metadataName = (item) => {
13
- return item.metadata.name;
14
- };
15
- const callIterableObject = async (config: PinataConfig, filterToApply: PinataPinListFilterOptions, pinsCount?: number) => {
16
- const pins: PinataPin[] = [];
17
- for await (const item of getFilesByCount(
18
- config,
19
- filterToApply,
20
- pinsCount
21
- )) {
22
- pins.push(metadataName(item));
23
- }
24
-
25
- return pins;
26
- };
27
-
28
- describe('Get Files By Count', () => {
29
- const pinataConfig = {
30
- pinataApiKey: fakeHeaders.headers.pinata_api_key,
31
- pinataSecretApiKey: fakeHeaders.headers.pinata_secret_api_key
32
- };
33
- const filterToApply = {
34
- status: 'pinned'
35
- };
36
-
37
- afterEach(() => {
38
- jest.resetAllMocks();
39
- });
40
-
41
- test('Request 17 pins, pins available', async () => {
42
- const pinsToRequest = 17;
43
-
44
- (axios.get as jest.Mock)
45
- .mockResolvedValueOnce(pinListAxiosMockPages.firstPage.response)
46
- .mockResolvedValueOnce(pinListAxiosMockPages.secondPage.response);
47
-
48
- const pins = await callIterableObject(
49
- pinataConfig,
50
- filterToApply,
51
- pinsToRequest
52
- );
53
-
54
- expect(pins).toEqual(
55
- APIData.rows.slice(0, pinsToRequest).map(metadataName)
56
- );
57
- expect(pins).toHaveLength(pinsToRequest);
58
- expect(axios.get).toHaveBeenNthCalledWith(
59
- 1,
60
- pinListAxiosMockPages.firstPage.url,
61
- pinListAxiosMockPages.firstPage.headers
62
- );
63
- expect(axios.get).toHaveBeenNthCalledWith(
64
- 2,
65
- pinListAxiosMockPages.secondPage.url,
66
- pinListAxiosMockPages.secondPage.headers
67
- );
68
-
69
- expect(axios.get).toHaveBeenCalledTimes(2);
70
- });
71
-
72
- test('Request 6 pins, pins available', async () => {
73
- const pinsToRequest = 6;
74
-
75
- (axios.get as jest.Mock).mockResolvedValueOnce(
76
- pinListAxiosMockPages.firstPage.response
77
- );
78
- const pins = await callIterableObject(
79
- pinataConfig,
80
- filterToApply,
81
- pinsToRequest
82
- );
83
- expect(pins).toEqual(
84
- APIData.rows.slice(0, pinsToRequest).map(metadataName)
85
- );
86
-
87
- expect(pins).toHaveLength(pinsToRequest);
88
- expect(axios.get).toHaveBeenNthCalledWith(
89
- 1,
90
- pinListAxiosMockPages.firstPage.url,
91
- pinListAxiosMockPages.firstPage.headers
92
- );
93
- expect(axios.get).toHaveBeenCalledTimes(1);
94
- });
95
-
96
- test('Request 25 pins, pins not available', async () => {
97
- const pinsToRequest = 25;
98
-
99
- (axios.get as jest.Mock)
100
- .mockResolvedValueOnce(pinListAxiosMockPages.firstPage.response)
101
- .mockResolvedValueOnce(pinListAxiosMockPages.secondPage.response)
102
- .mockResolvedValueOnce(
103
- pinListAxiosMockPages.thirdPageEmpty.response
104
- );
105
- const pins = await callIterableObject(
106
- pinataConfig,
107
- filterToApply,
108
- pinsToRequest
109
- );
110
-
111
- expect(pins).toEqual(
112
- APIData.rows.slice(0, pinsToRequest).map(metadataName)
113
- );
114
-
115
- expect(pins).toHaveLength(20);
116
- expect(axios.get).toHaveBeenNthCalledWith(
117
- 1,
118
- pinListAxiosMockPages.firstPage.url,
119
- pinListAxiosMockPages.firstPage.headers
120
- );
121
- expect(axios.get).toHaveBeenNthCalledWith(
122
- 2,
123
- pinListAxiosMockPages.secondPage.url,
124
- pinListAxiosMockPages.secondPage.headers
125
- );
126
- expect(axios.get).toHaveBeenNthCalledWith(
127
- 3,
128
- pinListAxiosMockPages.thirdPageEmpty.url,
129
- pinListAxiosMockPages.thirdPageEmpty.headers
130
- );
131
-
132
- expect(axios.get).toHaveBeenCalledTimes(3);
133
- });
134
-
135
- test('Request 3 pins, pins not available', async () => {
136
- const pinsToRequest = 3;
137
-
138
- (axios.get as jest.Mock).mockResolvedValueOnce(
139
- pinListAxiosMockPages.firstPageEmpty.response
140
- );
141
- const pins = await callIterableObject(
142
- pinataConfig,
143
- filterToApply,
144
- pinsToRequest
145
- );
146
-
147
- expect(pins).toEqual([]);
148
-
149
- expect(pins).toHaveLength(0);
150
- expect(axios.get).toHaveBeenNthCalledWith(
151
- 1,
152
- pinListAxiosMockPages.firstPageEmpty.url,
153
- pinListAxiosMockPages.firstPageEmpty.headers
154
- );
155
-
156
- expect(axios.get).toHaveBeenCalledTimes(1);
157
- });
158
-
159
- test('Request all pins, pins available', async () => {
160
- (axios.get as jest.Mock)
161
- .mockResolvedValueOnce(pinListAxiosMockPages.firstPage.response)
162
- .mockResolvedValueOnce(pinListAxiosMockPages.secondPage.response)
163
- .mockResolvedValueOnce(
164
- pinListAxiosMockPages.thirdPageEmpty.response
165
- );
166
- const pins = await callIterableObject(
167
- pinataConfig,
168
- filterToApply
169
- );
170
- expect(pins).toEqual(APIData.rows.map(metadataName));
171
-
172
- expect(pins).toHaveLength(APIData.rows.length);
173
- expect(axios.get).toHaveBeenNthCalledWith(
174
- 1,
175
- pinListAxiosMockPages.firstPage.url,
176
- pinListAxiosMockPages.firstPage.headers
177
- );
178
- expect(axios.get).toHaveBeenNthCalledWith(
179
- 2,
180
- pinListAxiosMockPages.secondPage.url,
181
- pinListAxiosMockPages.secondPage.headers
182
- );
183
- expect(axios.get).toHaveBeenNthCalledWith(
184
- 3,
185
- pinListAxiosMockPages.thirdPageEmpty.url,
186
- pinListAxiosMockPages.thirdPageEmpty.headers
187
- );
188
-
189
- expect(axios.get).toHaveBeenCalledTimes(3);
190
- });
191
-
192
- test('Request all pins, pins not available', async () => {
193
- (axios.get as jest.Mock).mockResolvedValueOnce(
194
- pinListAxiosMockPages.firstPageEmpty.response
195
- );
196
- const pins = await callIterableObject(
197
- pinataConfig,
198
- filterToApply
199
- );
200
-
201
- expect(pins).toEqual([]);
202
-
203
- expect(pins).toHaveLength(0);
204
- expect(axios.get).toHaveBeenNthCalledWith(
205
- 1,
206
- pinListAxiosMockPages.firstPageEmpty.url,
207
- pinListAxiosMockPages.firstPageEmpty.headers
208
- );
209
-
210
- expect(axios.get).toHaveBeenCalledTimes(1);
211
- });
212
- });
@@ -1,102 +0,0 @@
1
- import axios from 'axios';
2
- import pinList from '../../../../src/commands/data/pinList/pinList';
3
- import {
4
- pinListAxiosMockPages,
5
- fakeHeaders
6
- } from '../pinlist-sample-api';
7
-
8
- jest.mock('axios');
9
-
10
- describe('Pin List by page', () => {
11
- afterEach(() => {
12
- jest.resetAllMocks();
13
- });
14
-
15
- test('Call two pages in a row', async () => {
16
- (axios.get as jest.Mock)
17
- .mockResolvedValueOnce(pinListAxiosMockPages.firstPage.response)
18
- .mockResolvedValueOnce(pinListAxiosMockPages.secondPage.response);
19
-
20
- const resp = await pinList(
21
- {
22
- pinataApiKey: fakeHeaders.headers.pinata_api_key,
23
- pinataSecretApiKey: fakeHeaders.headers.pinata_secret_api_key
24
- },
25
- { pageLimit: 10, pageOffset: 0, status: 'pinned' }
26
- );
27
- const resp2 = await pinList(
28
- {
29
- pinataApiKey: fakeHeaders.headers.pinata_api_key,
30
- pinataSecretApiKey: fakeHeaders.headers.pinata_secret_api_key
31
- },
32
- { pageLimit: 10, pageOffset: 10, status: 'pinned' }
33
- );
34
- expect(resp).toEqual(pinListAxiosMockPages.firstPage.response.data);
35
- expect(resp2).toEqual(pinListAxiosMockPages.secondPage.response.data);
36
-
37
- expect(axios.get).toHaveBeenNthCalledWith(
38
- 1,
39
- pinListAxiosMockPages.firstPage.url,
40
- pinListAxiosMockPages.firstPage.headers
41
- );
42
- expect(axios.get).toHaveBeenNthCalledWith(
43
- 2,
44
- pinListAxiosMockPages.secondPage.url,
45
- pinListAxiosMockPages.secondPage.headers
46
- );
47
-
48
- expect(axios.get).toHaveBeenCalledTimes(2);
49
- });
50
-
51
- test('Call empty page', async () => {
52
- (axios.get as jest.Mock).mockResolvedValueOnce(
53
- pinListAxiosMockPages.firstPageEmpty.response
54
- );
55
-
56
- const resp = await pinList(
57
- {
58
- pinataApiKey: fakeHeaders.headers.pinata_api_key,
59
- pinataSecretApiKey: fakeHeaders.headers.pinata_secret_api_key
60
- },
61
- { pageLimit: 10, pageOffset: 0, status: 'pinned' }
62
- );
63
-
64
- expect(resp).toEqual({ rows: [] });
65
-
66
- expect(axios.get).toHaveBeenNthCalledWith(
67
- 1,
68
- pinListAxiosMockPages.firstPageEmpty.url,
69
- pinListAxiosMockPages.firstPageEmpty.headers
70
- );
71
-
72
- expect(axios.get).toHaveBeenCalledTimes(1);
73
- });
74
- test('Result other than 200 status is returned', () => {
75
- const badStatus = {
76
- status: 700
77
- };
78
- (axios.get as jest.Mock).mockResolvedValue(badStatus);
79
- expect.assertions(1);
80
- expect(pinList({ pinataApiKey: 'test', pinataSecretApiKey: 'test' })).rejects.toEqual(
81
- Error(
82
- `unknown server response while attempting to retrieve user pin list: ${badStatus}`
83
- )
84
- );
85
- });
86
-
87
- test('200 status is returned', () => {
88
- const goodStatus = {
89
- status: 200,
90
- data: 'testData'
91
- };
92
- (axios.get as jest.Mock).mockResolvedValue(goodStatus);
93
- expect.assertions(1);
94
- expect(pinList({ pinataApiKey: 'test', pinataSecretApiKey: 'test' })).resolves.toEqual(goodStatus.data);
95
- });
96
-
97
- test('Rejection handled', () => {
98
- (axios.get as jest.Mock).mockRejectedValue('test error');
99
- expect.assertions(1);
100
- expect(pinList({ pinataApiKey: 'test', pinataSecretApiKey: 'test' })).rejects.toEqual('test error');
101
- });
102
- });