@pinata/sdk 2.1.0 → 3.0.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 (81) hide show
  1. package/LICENSE +1 -2
  2. package/README.md +59 -694
  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 +62 -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,118 +0,0 @@
1
- import axios from 'axios';
2
- import { baseUrl } from '../../constants';
3
- import NodeFormData from 'form-data';
4
- import stream from 'stream';
5
- import {
6
- createConfigForAxiosHeadersWithFormData,
7
- validateMetadata,
8
- validatePinataOptions
9
- } from '../../util/validators';
10
- import { handleError } from '../../util/errorResponse';
11
- import { PinataConfig, PinataMetadata } from '../..';
12
-
13
- export interface PinataPinPolicyItem {
14
- id: string;
15
- desiredReplicationCount: number;
16
- }
17
- export interface PinataOptions {
18
- hostNodes?: string[] | undefined;
19
- cidVersion?: 0 | 1;
20
- wrapWithDirectory?: boolean;
21
- customPinPolicy?: {
22
- regions: PinataPinPolicyItem[];
23
- };
24
- }
25
-
26
- export interface PinataPinResponse {
27
- IpfsHash: string;
28
- PinSize: number;
29
- Timestamp: string;
30
- }
31
- export interface PinataPinOptions {
32
- pinataMetadata?: PinataMetadata;
33
- pinataOptions?: PinataOptions | undefined;
34
- }
35
- const endpoint = `${baseUrl}/pinning/pinFileToIPFS`;
36
-
37
- export function uploadToIPFS(
38
- config: PinataConfig,
39
- data: NodeFormData,
40
- options?: PinataPinOptions
41
- ): Promise<PinataPinResponse> {
42
- return new Promise((resolve, reject) => {
43
- if (options && options.pinataMetadata) {
44
- validateMetadata(options.pinataMetadata);
45
- data.append(
46
- 'pinataMetadata',
47
- JSON.stringify(options.pinataMetadata)
48
- );
49
- }
50
- if (options && options.pinataOptions) {
51
- validatePinataOptions(options.pinataOptions);
52
- data.append('pinataOptions', JSON.stringify(options.pinataOptions));
53
- }
54
-
55
- axios
56
- .post(
57
- endpoint,
58
- data,
59
- createConfigForAxiosHeadersWithFormData(
60
- config,
61
- data.getBoundary()
62
- )
63
- )
64
- .then(function (result) {
65
- if (result.status !== 200) {
66
- reject(
67
- new Error(
68
- `unknown server response while pinning File to IPFS: ${result}`
69
- )
70
- );
71
- }
72
- resolve(result.data);
73
- })
74
- .catch(function (error) {
75
- const formattedError = handleError(error);
76
- reject(formattedError);
77
- });
78
- });
79
- }
80
-
81
- export default function pinFileToIPFS(
82
- config: PinataConfig,
83
- readStream: any,
84
- options?: PinataPinOptions
85
- ): Promise<PinataPinResponse> {
86
- return new Promise((resolve, reject) => {
87
- const data = new NodeFormData();
88
-
89
- if (
90
- !(
91
- options?.pinataMetadata?.name &&
92
- typeof options.pinataMetadata.name === 'string' &&
93
- options.pinataMetadata.name.length > 0
94
- )
95
- ) {
96
- throw Error(
97
- 'filename was not provide, make sure to provide options.pinataMetadata.name'
98
- );
99
- }
100
-
101
- data.append('file', readStream, {
102
- filename: options.pinataMetadata.name
103
- });
104
-
105
- if (
106
- !(
107
- readStream instanceof stream.Readable ||
108
- readStream instanceof NodeFormData
109
- )
110
- ) {
111
- reject(
112
- new Error('readStream is not a readable stream or form data')
113
- );
114
- }
115
-
116
- resolve(uploadToIPFS(config, data, options));
117
- });
118
- }
@@ -1,60 +0,0 @@
1
-
2
- import NodeFormData from 'form-data';
3
-
4
- import { PinataConfig } from '../..';
5
- import {
6
- PinataPinOptions,
7
- PinataPinResponse,
8
- uploadToIPFS
9
- } from './pinFileToIPFS';
10
- import fs from 'fs';
11
-
12
- import path from 'path';
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
-
24
- export default async function pinFromFS(
25
- config: PinataConfig,
26
- sourcePath: string,
27
- options?: PinataPinOptions
28
- ): Promise<PinataPinResponse> {
29
- try {
30
- const stats = await fs.promises.stat(sourcePath);
31
- if (stats.isFile()) {
32
-
33
- const stream = fs.createReadStream(sourcePath);
34
- const data = new NodeFormData();
35
-
36
- data.append('file', stream, {
37
- filename: path.basename(sourcePath)
38
- });
39
-
40
- return uploadToIPFS(config, data, options);
41
- }
42
- const files = await fs.promises.readdir(sourcePath);
43
- const data = new NodeFormData();
44
- files.forEach((file: string) => {
45
-
46
- const filepath = path.join(sourcePath, file);
47
- //for each file stream, we need to include the correct relative file path
48
- data.append('file', fs.createReadStream(filepath), {
49
- filepath: normalizePath(filepath)
50
- });
51
- });
52
-
53
- return uploadToIPFS(config, data, options);
54
-
55
- } catch (err) {
56
- console.log(err);
57
- throw err;
58
- }
59
-
60
- }
@@ -1,47 +0,0 @@
1
- import axios from 'axios';
2
- import { baseUrl } from '../../constants';
3
- import { createConfigForAxiosHeaders, validateMetadata, validatePinataOptions } from '../../util/validators';
4
- import { handleError } from '../../util/errorResponse';
5
- import { PinataConfig } from '../..';
6
- import { PinataPinOptions, PinataPinResponse } from './pinFileToIPFS';
7
-
8
- export default function pinJSONToIPFS(config: PinataConfig, body: any, options? : PinataPinOptions):Promise<PinataPinResponse> {
9
-
10
- let requestBody: any = body;
11
-
12
- if (typeof body !== 'object') {
13
- throw new Error('body must be a valid JSON object');
14
- }
15
-
16
- if (options) {
17
- requestBody = {
18
- pinataContent: body
19
- };
20
- if (options.pinataMetadata) {
21
- validateMetadata(options.pinataMetadata);
22
- requestBody.pinataMetadata = options.pinataMetadata;
23
- }
24
- if (options.pinataOptions) {
25
- validatePinataOptions(options.pinataOptions);
26
- requestBody.pinataOptions = options.pinataOptions;
27
- }
28
- }
29
-
30
- const endpoint = `${baseUrl}/pinning/pinJSONToIPFS`;
31
-
32
- return new Promise((resolve, reject) => {
33
- axios.post(
34
- endpoint,
35
- requestBody,
36
- {...createConfigForAxiosHeaders(config)})
37
- .then(function (result) {
38
- if (result.status !== 200) {
39
- reject(new Error(`unknown server response while pinning JSON to IPFS: ${result}`));
40
- }
41
- resolve(result.data);
42
- }).catch(function (error) {
43
- const formattedError = handleError(error);
44
- reject(formattedError);
45
- });
46
- });
47
- }
@@ -1,50 +0,0 @@
1
- import axios from 'axios';
2
- import { baseUrl } from '../../../constants';
3
- import { createConfigForAxiosHeaders } from '../../../util/validators';
4
- import queryBuilder from './queryBuilder';
5
- import { handleError } from '../../../util/errorResponse';
6
- import { PinataConfig } from '../../..';
7
-
8
- export interface PinataPinJobsResponseRow {
9
- id: number | string;
10
- ipfs_pin_hash: string;
11
- date_queued: string;
12
- name: string | undefined | null;
13
- status: string;
14
- }
15
- export interface PinataPinJobsResponse {
16
- count: number;
17
- rows: PinataPinJobsResponseRow[];
18
- }
19
-
20
- export interface PinataPinJobsFilterOptions {
21
- sort: 'ASC' | 'DESC';
22
- status?: string | undefined;
23
- ipfs_pin_hash?: string | undefined;
24
- limit?: number | undefined;
25
- offset?: number | undefined;
26
- }
27
-
28
- export default function pinJobs(config: PinataConfig, filters? : PinataPinJobsFilterOptions): Promise<PinataPinJobsResponse> {
29
-
30
- let endpoint = `${baseUrl}/pinning/pinJobs`;
31
-
32
- if (filters) {
33
- endpoint = queryBuilder(endpoint, filters);
34
- }
35
-
36
- return new Promise((resolve, reject) => {
37
- axios.get(
38
- endpoint,
39
- {...createConfigForAxiosHeaders(config)})
40
- .then(function (result) {
41
- if (result.status !== 200) {
42
- reject(new Error(`unknown server response while attempting to retrieve pin jobs: ${result}`));
43
- }
44
- resolve(result.data);
45
- }).catch(function (error) {
46
- const formattedError = handleError(error);
47
- reject(formattedError);
48
- });
49
- });
50
- }
@@ -1,75 +0,0 @@
1
- import isIPFS from 'is-ipfs';
2
-
3
- export default function queryBuilder(baseUrl: string, filters?: any): string {
4
- if (!baseUrl) {
5
- throw new Error('no baseUrl provided');
6
- }
7
-
8
- if (!filters) {
9
- return baseUrl;
10
- }
11
-
12
- let newUrl = baseUrl;
13
-
14
- let firstFilterApplied = false; // we want to make sure that the "?" character is only applied once
15
-
16
- function addFilter(newFilter: string) {
17
- if (firstFilterApplied) {
18
- newUrl = `${newUrl}&${newFilter}`;
19
- } else {
20
- firstFilterApplied = true;
21
- newUrl = `${newUrl}?${newFilter}`;
22
- }
23
- }
24
-
25
- if (filters) {
26
- // now we need to construct the actual URL based on the given filters provided
27
- if (filters.sort) {
28
- if (filters.sort === 'ASC' ||
29
- filters.sort === 'DESC') {
30
- addFilter(`sort=${filters.sort}`);
31
- } else {
32
- throw new Error(`Unknown sort value: ${filters.sort} provided`);
33
- }
34
- }
35
-
36
- if (filters.status) {
37
- if (filters.status === 'searching' ||
38
- filters.status === 'expired' ||
39
- filters.status === 'over_free_limit' ||
40
- filters.status === 'over_max_size' ||
41
- filters.status === 'invalid_object' ||
42
- filters.status === 'bad_host_node'
43
- ) {
44
- addFilter(`status=${filters.status}`);
45
- } else {
46
- throw new Error(`Unknown status value: ${filters.status} provided`);
47
- }
48
- }
49
-
50
- if (filters.ipfs_pin_hash) {
51
- if (isIPFS.cid(filters.ipfs_pin_hash)) {
52
- addFilter(`ipfs_pin_hash=${filters.ipfs_pin_hash}`);
53
- } else {
54
- throw new Error(`Invalid IPFS hash: ${filters.ipfs_pin_hash}`);
55
- }
56
- }
57
-
58
- if (filters.limit) {
59
- if (Number.isInteger(filters.limit) && (filters.limit > 0) && (filters.limit < 100)) {
60
- addFilter(`limit=${filters.limit}`);
61
- } else {
62
- throw new Error(`Invalid limit: ${filters.limit}. Valid limits are 1-100`);
63
- }
64
- }
65
-
66
- if (filters.offset) {
67
- if (Number.isInteger(filters.offset) && (filters.offset > 0)) {
68
- addFilter(`offset=${filters.offset}`);
69
- } else {
70
- throw new Error(`Invalid offset: ${filters.offset}. Please provide a positive integer for the offset`);
71
- }
72
- }
73
- }
74
- return newUrl;
75
- }
@@ -1,32 +0,0 @@
1
- import axios from 'axios';
2
- import { baseUrl } from '../../constants';
3
- import { createConfigForAxiosHeaders } from '../../util/validators';
4
- import isIPFS from 'is-ipfs';
5
- import { handleError } from '../../util/errorResponse';
6
- import { PinataConfig } from '../..';
7
-
8
- export default function unpin(config: PinataConfig, hashToUnpin: string) {
9
- if (!hashToUnpin) {
10
- throw new Error('hashToUnpin value is required for removing a pin from Pinata');
11
- }
12
- if (!isIPFS.cid(hashToUnpin)) {
13
- throw new Error(`${hashToUnpin} is an invalid IPFS CID`);
14
- }
15
-
16
- const endpoint = `${baseUrl}/pinning/unpin/${hashToUnpin}`;
17
-
18
- return new Promise((resolve, reject) => {
19
- axios.delete(
20
- endpoint,
21
- {...createConfigForAxiosHeaders(config)})
22
- .then(function (result) {
23
- if (result.status !== 200) {
24
- reject(new Error(`unknown server response while removing pin from IPFS: ${result}`));
25
- }
26
- resolve(result.data);
27
- }).catch(function (error) {
28
- const formattedError = handleError(error);
29
- reject(formattedError);
30
- });
31
- });
32
- }
package/src/constants.ts DELETED
@@ -1,4 +0,0 @@
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.ts DELETED
@@ -1,124 +0,0 @@
1
- import './require-babel-polyfill';
2
- import pinByHash, {
3
- PinataPinByHashPinOptions
4
- } from './commands/pinning/pinByHash';
5
- import hashMetadata from './commands/pinning/hashMetadata';
6
- import pinFileToIPFS, {
7
- PinataPinOptions
8
- } from './commands/pinning/pinFileToIPFS';
9
- import pinFromFS from './commands/pinning/pinFromFS';
10
- import pinJSONToIPFS from './commands/pinning/pinJSONToIPFS';
11
- import pinJobs, {
12
- PinataPinJobsFilterOptions
13
- } from './commands/pinning/pinJobs/pinJobs';
14
- import unpin from './commands/pinning/unpin';
15
- import testAuthentication from './commands/data/testAuthentication';
16
- import pinList, {
17
- PinataMetadata,
18
- PinataPinListFilterOptions
19
- } from './commands/data/pinList/pinList';
20
- import getFilesByCount from './commands/data/getFilesByCount/getFilesByCount';
21
- import userPinnedDataTotal from './commands/data/userPinnedDataTotal';
22
-
23
- export interface PinataConfig {
24
- pinataApiKey?: string;
25
- pinataSecretApiKey?: string;
26
- pinataJWTKey?: string;
27
- }
28
-
29
- function refactorConfig(pinataConfigParam: PinataConfig): PinataConfig {
30
- const config: PinataConfig = {};
31
- if (pinataConfigParam.pinataApiKey) {
32
- config.pinataApiKey = pinataConfigParam.pinataApiKey;
33
- }
34
-
35
- if (pinataConfigParam.pinataSecretApiKey) {
36
- config.pinataSecretApiKey = pinataConfigParam.pinataSecretApiKey;
37
- }
38
-
39
- if (pinataConfigParam.pinataJWTKey) {
40
- config.pinataJWTKey = pinataConfigParam.pinataJWTKey;
41
- }
42
-
43
- return config;
44
- }
45
- function sanitizeConfig(
46
- pinataApiKey?: string | PinataConfig,
47
- pinataSecretApiKey?: string
48
- ): PinataConfig {
49
- let config: PinataConfig = {};
50
-
51
- if (
52
- typeof pinataApiKey === 'string' &&
53
- typeof pinataSecretApiKey === 'string'
54
- ) {
55
- config.pinataApiKey = pinataApiKey;
56
- config.pinataSecretApiKey = pinataSecretApiKey;
57
- }
58
-
59
- const isPinataConfigParam = typeof pinataApiKey === 'object';
60
- if (isPinataConfigParam) {
61
- config = refactorConfig(pinataApiKey);
62
- }
63
-
64
- return config;
65
- }
66
-
67
- class PinataClient {
68
- config: PinataConfig;
69
-
70
- constructor(
71
- pinataApiKey?: string | PinataConfig,
72
- pinataSecretApiKey?: string
73
- ) {
74
- this.config = sanitizeConfig(pinataApiKey, pinataSecretApiKey);
75
- }
76
-
77
- pinByHash(hashToPin: string, options?: PinataPinByHashPinOptions) {
78
- return pinByHash(this.config, hashToPin, options);
79
- }
80
-
81
- hashMetadata(ipfsPinHash: string, metadata: PinataMetadata) {
82
- return hashMetadata(this.config, ipfsPinHash, metadata);
83
- }
84
-
85
- pinFileToIPFS(readableStream: any, options?: PinataPinOptions) {
86
- return pinFileToIPFS(this.config, readableStream, options);
87
- }
88
-
89
- pinFromFS(sourcePath: string, options?: PinataPinOptions) {
90
- return pinFromFS(this.config, sourcePath, options);
91
- }
92
- pinJSONToIPFS(body: any, options?: PinataPinOptions) {
93
- return pinJSONToIPFS(this.config, body, options);
94
- }
95
- pinJobs(filters?: PinataPinJobsFilterOptions) {
96
- return pinJobs(this.config, filters);
97
- }
98
- unpin(hashToUnpin: string) {
99
- return unpin(this.config, hashToUnpin);
100
- }
101
-
102
- pinList(filters: PinataPinListFilterOptions) {
103
- return pinList(this.config, filters);
104
- }
105
-
106
- getFilesByCount(filters: PinataPinListFilterOptions, maxCount?: number) {
107
- return getFilesByCount(this.config, filters, maxCount);
108
- }
109
-
110
- testAuthentication() {
111
- return testAuthentication(this.config);
112
- }
113
- userPinnedDataTotal() {
114
- return userPinnedDataTotal(this.config);
115
- }
116
- }
117
-
118
- module.exports = PinataClient;
119
-
120
- export * from './commands/data';
121
- export * from './commands/pinning';
122
-
123
- export default PinataClient;
124
-
@@ -1,5 +0,0 @@
1
- export default (() => {
2
- if (!global || !global._babelPolyfill) {
3
- require('babel-polyfill');
4
- }
5
- })();
@@ -1,3 +0,0 @@
1
- declare module 'base-path-converter' {
2
- export default function(sourcePath: string, filePath: string): string;
3
- }
@@ -1,9 +0,0 @@
1
- export {};
2
-
3
- declare global {
4
- namespace NodeJS {
5
- interface Global {
6
- _babelPolyfill: any;
7
- }
8
- }
9
- }
@@ -1,5 +0,0 @@
1
- declare module 'is-ipfs' {
2
- export function cid (cid: string): boolean
3
- export function isPeerMultiaddr(addr: string): boolean;
4
- export function peerMultiaddr(addr: string): boolean;
5
- }
@@ -1,10 +0,0 @@
1
- export const handleError = (error : any) => {
2
- if (error && error.response && error.response && error.response.data && error.response.data.error) {
3
- return error.response.data.error;
4
- } else if (error.data && error.data.error) {
5
- return error.data.error;
6
- } else if (error.response && error.response.error) {
7
- return error.response.error;
8
- }
9
- return error;
10
- };