@pinata/sdk 1.2.1 → 2.0.0-beta.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 (74) hide show
  1. package/.babelrc +1 -1
  2. package/.eslintrc +7 -7
  3. package/.husky/pre-commit +6 -0
  4. package/.nvmrc +1 -1
  5. package/README.md +3 -3
  6. package/lib/pinata-sdk.js +494 -468
  7. package/lib/pinata-sdk.min.js +5 -5
  8. package/package.json +83 -71
  9. package/src/commands/data/getFilesByCount/{getFilesByCount.js → getFilesByCount.ts} +8 -9
  10. package/src/commands/data/index.ts +4 -0
  11. package/src/commands/data/pinList/pinList.ts +77 -0
  12. package/src/commands/data/pinList/{queryBuilder.js → queryBuilder.ts} +6 -16
  13. package/src/commands/data/{testAuthentication.js → testAuthentication.ts} +8 -9
  14. package/src/commands/data/{userPinnedDataTotal.js → userPinnedDataTotal.ts} +4 -9
  15. package/src/commands/pinning/{hashMetadata.js → hashMetadata.ts} +9 -11
  16. package/src/commands/pinning/index.ts +8 -0
  17. package/src/commands/pinning/{pinByHash.js → pinByHash.ts} +22 -11
  18. package/src/commands/pinning/{pinFileToIPFS.js → pinFileToIPFS.ts} +27 -11
  19. package/src/commands/pinning/{pinFromFS.js → pinFromFS.ts} +14 -19
  20. package/src/commands/pinning/{pinJSONToIPFS.js → pinJSONToIPFS.ts} +5 -11
  21. package/src/commands/pinning/pinJobs/{pinJobs.js → pinJobs.ts} +23 -9
  22. package/src/commands/pinning/pinJobs/{queryBuilder.js → queryBuilder.ts} +2 -8
  23. package/src/commands/pinning/{unpin.js → unpin.ts} +3 -9
  24. package/src/{constants.js → constants.ts} +0 -0
  25. package/src/index.ts +120 -0
  26. package/src/third-party/base-path-converter.d.ts +3 -0
  27. package/src/third-party/is-ipfs.d.ts +5 -0
  28. package/src/third-party/recursive-fs.d.ts +5 -0
  29. package/src/util/{errorResponse.js → errorResponse.ts} +1 -1
  30. package/src/util/{validators.js → validators.ts} +14 -33
  31. package/test/commands/data/getFilesByCount/{getFilesByCount.test.js → getFilesByCount.test.ts} +23 -22
  32. package/test/commands/data/pinList/{pinList.test.js → pinList.test.ts} +25 -26
  33. package/test/commands/data/pinList/{queryBuilder.test.js → queryBuilder.test.ts} +9 -10
  34. package/test/commands/data/pinlist-sample-api.ts +540 -0
  35. package/test/commands/data/{testAuthentication.test.js → testAuthentication.test.ts} +13 -13
  36. package/test/commands/data/{userPinnedDataTotal.test.js → userPinnedDataTotal.test.ts} +4 -4
  37. package/test/commands/pinning/{hashMetadata.test.js → hashMetadata.test.ts} +7 -8
  38. package/test/commands/pinning/{pinByHash.test.js → pinByHash.test.ts} +4 -5
  39. package/test/commands/pinning/{pinFileToIPFS.test.js → pinFileToIPFS.test.ts} +5 -7
  40. package/test/commands/pinning/{pinFromFS.js → pinFromFS.ts} +5 -6
  41. package/test/commands/pinning/{pinJSONToIPFS.test.js → pinJSONToIPFS.test.ts} +4 -5
  42. package/test/commands/pinning/pinJobs/{pinJobs.test.js → pinJobs.test.ts} +4 -4
  43. package/test/commands/pinning/pinJobs/{queryBuilder.test.js → queryBuilder.test.ts} +5 -5
  44. package/test/commands/pinning/{unpin.test.js → unpin.test.ts} +4 -5
  45. package/test/util/validators.test.ts +234 -0
  46. package/tsconfig.json +2 -2
  47. package/types/commands/data/getFilesByCount/getFilesByCount.d.ts +4 -2
  48. package/types/commands/data/index.d.ts +4 -0
  49. package/types/commands/data/pinList/pinList.d.ts +45 -8
  50. package/types/commands/data/pinList/queryBuilder.d.ts +1 -7
  51. package/types/commands/data/testAuthentication.d.ts +5 -7
  52. package/types/commands/data/userPinnedDataTotal.d.ts +2 -7
  53. package/types/commands/pinning/hashMetadata.d.ts +3 -9
  54. package/types/commands/pinning/index.d.ts +8 -0
  55. package/types/commands/pinning/pinByHash.d.ts +14 -9
  56. package/types/commands/pinning/pinFileToIPFS.d.ts +25 -1
  57. package/types/commands/pinning/pinFromFS.d.ts +3 -9
  58. package/types/commands/pinning/pinJSONToIPFS.d.ts +3 -9
  59. package/types/commands/pinning/pinJobs/pinJobs.d.ts +20 -8
  60. package/types/commands/pinning/pinJobs/queryBuilder.d.ts +1 -7
  61. package/types/commands/pinning/unpin.d.ts +2 -8
  62. package/types/constants.d.ts +2 -2
  63. package/types/index.d.ts +37 -165
  64. package/types/util/errorResponse.d.ts +1 -1
  65. package/types/util/validators.d.ts +20 -37
  66. package/webpack.config.js +50 -53
  67. package/src/commands/data/pinList/pinList.js +0 -34
  68. package/src/commands/pinning/hashPinPolicy.js +0 -51
  69. package/src/commands/pinning/userPinPolicy.js +0 -40
  70. package/src/index.js +0 -307
  71. package/test/commands/data/pinlist-sample-api.js +0 -540
  72. package/test/commands/pinning/hashPinPolicy.test.js +0 -65
  73. package/test/commands/pinning/userPinPolicy.test.js +0 -80
  74. package/test/util/validators.test.js +0 -234
package/package.json CHANGED
@@ -1,74 +1,86 @@
1
1
  {
2
- "name": "@pinata/sdk",
3
- "version": "1.2.1",
4
- "description": "Official SDK for the Pinata IPFS platform",
5
- "main": "lib/pinata-sdk.js",
6
- "types": "types/index.d.ts",
7
- "scripts": {
8
- "build": "webpack --env dev && webpack --env build && npm run test && tsc",
9
- "coverage": "jest --coverage",
10
- "dev": "webpack --progress --colors --watch --env dev",
11
- "html": "open coverage/index.html",
12
- "lint": "eslint src --quiet ",
13
- "lint:fix": "eslint src --fix",
14
- "test": "./node_modules/.bin/jest --env=node",
15
- "repl": "node -i -e \"$(< ./lib/pinata-sdk.js)\""
16
- },
17
- "repository": {
18
- "type": "git",
19
- "url": "https://github.com/PinataCloud/Pinata-SDK.git"
20
- },
21
- "keywords": [
22
- "webpack",
23
- "es6",
24
- "starter",
25
- "library",
26
- "universal",
27
- "umd",
28
- "commonjs"
29
- ],
30
- "author": "Matt Ober",
31
- "license": "MIT",
32
- "bugs": {
33
- "url": "https://github.com/PinataCloud/Pinata-SDK/issues"
34
- },
35
- "homepage": "https://github.com/PinataCloud/Pinata-SDK",
36
- "devDependencies": {
37
- "@babel/cli": "^7.8.4",
38
- "@babel/core": "^7.8.4",
39
- "@babel/plugin-transform-runtime": "^7.19.1",
40
- "@babel/preset-env": "^7.19.4",
41
- "@babel/register": "^7.8.3",
42
- "@types/node": "^15.6.1",
43
- "acorn": "^7.1.0",
44
- "babel-eslint": "^10.0.3",
45
- "babel-loader": "^8.0.6",
46
- "babel-plugin-add-module-exports": "^1.0.2",
47
- "babel-polyfill": "^6.26.0",
48
- "eslint": "^6.8.0",
49
- "eslint-loader": "^3.0.3",
50
- "jest": "^25.1.0",
51
- "typescript": "^4.2.3",
52
- "uglifyjs-webpack-plugin": "^2.2.0",
53
- "webpack": "^4.41.6",
54
- "webpack-cli": "^3.3.11",
55
- "yargs": "^10.0.3"
56
- },
57
- "dependencies": {
58
- "axios": "^0.21.1",
59
- "base-path-converter": "^1.0.2",
60
- "form-data": "^2.3.3",
61
- "is-ipfs": "^0.6.0",
62
- "recursive-fs": "^1.1.2"
63
- },
64
- "jest": {
65
- "collectCoverage": true,
66
- "coverageReporters": [
67
- "text",
68
- "html"
2
+ "name": "@pinata/sdk",
3
+ "version": "2.0.0-beta.0",
4
+ "description": "Official SDK for the Pinata IPFS platform",
5
+ "main": "lib/pinata-sdk.js",
6
+ "types": "types/index.d.ts",
7
+ "scripts": {
8
+ "prepare": "husky install",
9
+ "build": "npm run build-dev && npm run build-prod && npm run test && tsc",
10
+ "build-dev": "webpack --env dev",
11
+ "build-prod": "webpack --env build",
12
+ "coverage": "jest --coverage",
13
+ "dev": "webpack --progress --colors --watch --env dev",
14
+ "html": "open coverage/index.html",
15
+ "lint": "eslint src --quiet ",
16
+ "lint:fix": "eslint src test --fix",
17
+ "test": "./node_modules/.bin/jest --env=node",
18
+ "repl": "node -i -e \"$(< ./lib/pinata-sdk.js)\""
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/PinataCloud/Pinata-SDK.git"
23
+ },
24
+ "keywords": [
25
+ "webpack",
26
+ "es6",
27
+ "starter",
28
+ "library",
29
+ "universal",
30
+ "umd",
31
+ "commonjs"
69
32
  ],
70
- "moduleFileExtensions": [
71
- "js"
72
- ]
73
- }
33
+ "author": "Matt Ober",
34
+ "license": "MIT",
35
+ "bugs": {
36
+ "url": "https://github.com/PinataCloud/Pinata-SDK/issues"
37
+ },
38
+ "homepage": "https://github.com/PinataCloud/Pinata-SDK",
39
+ "devDependencies": {
40
+ "@babel/cli": "^7.8.4",
41
+ "@babel/core": "^7.8.4",
42
+ "@babel/plugin-transform-runtime": "^7.19.1",
43
+ "@babel/preset-env": "^7.19.4",
44
+ "@babel/preset-typescript": "^7.18.6",
45
+ "@babel/register": "^7.8.3",
46
+ "@types/jest": "^29.2.0",
47
+ "@types/node": "^15.6.1",
48
+ "@typescript-eslint/eslint-plugin": "^5.41.0",
49
+ "@typescript-eslint/parser": "^5.41.0",
50
+ "acorn": "^7.1.0",
51
+ "babel-eslint": "^10.0.3",
52
+ "babel-loader": "^8.0.6",
53
+ "babel-plugin-add-module-exports": "^1.0.2",
54
+ "babel-polyfill": "^6.26.0",
55
+ "eslint": "^8.26.0",
56
+ "eslint-loader": "^3.0.3",
57
+ "husky": "^8.0.0",
58
+ "jest": "^25.1.0",
59
+ "source-map-loader": "^4.0.1",
60
+ "typescript": "^4.8.4",
61
+ "uglifyjs-webpack-plugin": "^2.2.0",
62
+ "webpack": "^4.41.6",
63
+ "webpack-cli": "^3.3.11"
64
+ },
65
+ "dependencies": {
66
+ "axios": "^0.21.1",
67
+ "base-path-converter": "^1.0.2",
68
+ "form-data": "^2.3.3",
69
+ "is-ipfs": "^0.6.0",
70
+ "recursive-fs": "^1.1.2"
71
+ },
72
+ "jest": {
73
+ "collectCoverage": true,
74
+ "coverageReporters": [
75
+ "text",
76
+ "html"
77
+ ],
78
+ "moduleFileExtensions": [
79
+ "ts",
80
+ "tsx",
81
+ "js",
82
+ "jsx",
83
+ "json"
84
+ ]
85
+ }
74
86
  }
@@ -1,9 +1,10 @@
1
- import pinList from '../pinList/pinList';
1
+ import { PinataConfig } from '../../..';
2
+ import pinList, { PinataPin, PinataPinListFilterOptions } from '../pinList/pinList';
2
3
 
3
4
  export default function getFilesByCount(
4
- config,
5
+ config: PinataConfig,
5
6
 
6
- filters = {},
7
+ filters: PinataPinListFilterOptions = {},
7
8
  maxCount = -1
8
9
  ) {
9
10
  if (maxCount === 0) {
@@ -15,7 +16,7 @@ export default function getFilesByCount(
15
16
  let i = 0;
16
17
  const pageLimit = 10;
17
18
  let pageOffset = 0;
18
- let cache = [];
19
+ let cache: PinataPin[] = [];
19
20
  let keepLooping = false;
20
21
 
21
22
  return {
@@ -24,7 +25,7 @@ export default function getFilesByCount(
24
25
  if (i === 0 || (i % pageLimit === 0 && keepLooping)) {
25
26
  resolve(
26
27
  pinList(config, {
27
- filters,
28
+ ...filters,
28
29
  ...{ pageOffset, pageLimit }
29
30
  }).then((resp) => {
30
31
  cache = resp.rows;
@@ -33,13 +34,11 @@ export default function getFilesByCount(
33
34
  // If the user requested all pins we have to keep looping if the limit page was not fulfill
34
35
  keepLooping =
35
36
  cache.length !== 0 &&
36
- (maxCount === -1 ?
37
- cache.length === pageLimit :
38
- i < maxCount);
37
+ (maxCount === -1 ? cache.length === pageLimit : i < maxCount);
39
38
  })
40
39
  );
41
40
  }
42
- resolve();
41
+ resolve(0);
43
42
  }).then(() => {
44
43
  const valueToReturn = cache[i % pageLimit];
45
44
 
@@ -0,0 +1,4 @@
1
+ export * from '../data/pinList/pinList';
2
+ export * from '../data/pinList/queryBuilder';
3
+ export * from './testAuthentication';
4
+ export * from './userPinnedDataTotal';
@@ -0,0 +1,77 @@
1
+ import axios from 'axios';
2
+ import { baseUrl } from '../../../constants';
3
+ import { createConfigForAxiosHeaders } from '../../../util/validators';
4
+ import { handleError } from '../../../util/errorResponse';
5
+ import queryBuilder from './queryBuilder';
6
+ import { PinataConfig } from '../../..';
7
+ export interface PinataPinRegion {
8
+ regionId: string;
9
+ currentReplicationCount: number;
10
+ desiredReplicationCount: number;
11
+ }
12
+
13
+ export interface PinataMetadata { [key: string]: string | number | null }
14
+ export interface PinataMetadataFilter {
15
+ name?: string | undefined;
16
+ keyvalues: { [key: string]: {
17
+ value: string;
18
+ op: string;
19
+ } }
20
+ }
21
+ export interface PinataPin {
22
+ id: string | number;
23
+ ipfs_pin_hash: string;
24
+ size: number;
25
+ user_id: string | number;
26
+ date_pinned: string;
27
+ date_unpinned: string | null;
28
+ metadata: PinataMetadata,
29
+ regions: PinataPinRegion[];
30
+ }
31
+
32
+ export type PinataPinListFilterOptions = {
33
+ hashContains?: string | undefined;
34
+ pinStart?: string | undefined;
35
+ pinEnd?: string | undefined;
36
+ unpinStart?: string | undefined;
37
+ unpinEnd?: string | undefined;
38
+ pinSizeMin?: number | undefined;
39
+ pinSizeMax?: number | undefined;
40
+ status?: string | undefined;
41
+ pageLimit?: number | undefined;
42
+ pageOffset?: number | undefined;
43
+ metadata?: PinataMetadataFilter | undefined;
44
+ };
45
+
46
+ export interface PinataPinListResponse {
47
+ rows: PinataPin[];
48
+ }
49
+
50
+ export default function pinList(
51
+ config: PinataConfig,
52
+ filters:PinataPinListFilterOptions = {}
53
+ ): Promise<PinataPinListResponse> {
54
+ filters = { ...filters, ...{ includeCount: 'false' } };
55
+
56
+ const baseEndpoint = `${baseUrl}/data/pinList`;
57
+ const endpoint = queryBuilder(baseEndpoint, filters);
58
+
59
+ return new Promise((resolve, reject) => {
60
+ axios
61
+ .get(endpoint, { ...createConfigForAxiosHeaders(config) })
62
+ .then(function (result) {
63
+ if (result.status !== 200) {
64
+ reject(
65
+ new Error(
66
+ `unknown server response while attempting to retrieve user pin list: ${result}`
67
+ )
68
+ );
69
+ }
70
+ resolve(result.data);
71
+ })
72
+ .catch(function (error) {
73
+ const formattedError = handleError(error);
74
+ reject(formattedError);
75
+ });
76
+ });
77
+ }
@@ -1,10 +1,6 @@
1
- /**
2
- * Validate and return data
3
- * @param {*} dateToValidate
4
- * @returns {*}
5
- */
6
- function validateAndReturnDate(dateToValidate) {
7
- let dateParsed = new Date(Date.parse(dateToValidate));
1
+
2
+ function validateAndReturnDate(dateToValidate: any): any {
3
+ const dateParsed = new Date(Date.parse(dateToValidate));
8
4
 
9
5
  try {
10
6
  if (dateParsed.toISOString() === dateToValidate) {
@@ -17,13 +13,7 @@ function validateAndReturnDate(dateToValidate) {
17
13
  }
18
14
  }
19
15
 
20
- /**
21
- * Query Builder
22
- * @param {string} baseUrl
23
- * @param {*} filters
24
- * @returns {string}
25
- */
26
- export default function queryBuilder(baseUrl, filters) {
16
+ export default function queryBuilder(baseUrl: string, filters?: any): string {
27
17
  if (!baseUrl) {
28
18
  throw new Error('no baseUrl provided');
29
19
  }
@@ -106,11 +96,11 @@ export default function queryBuilder(baseUrl, filters) {
106
96
  throw new Error('metadata keyvalues must be an object');
107
97
  }
108
98
 
109
- const prunedKeyValues = {}; // we want to make sure we're only grabbing the values we want for the query, and nothing extra
99
+ const prunedKeyValues: any = {}; // we want to make sure we're only grabbing the values we want for the query, and nothing extra
110
100
 
111
101
  Object.entries(filters.metadata.keyvalues).forEach((keyValue) => {
112
102
  const key = keyValue[0];
113
- const value = keyValue[1];
103
+ const value: any = keyValue[1];
114
104
 
115
105
  if (typeof value !== 'object') {
116
106
  throw new Error(`keyValue: ${key} is not an object`);
@@ -1,15 +1,14 @@
1
1
  import axios from 'axios';
2
- import { baseUrl } from './../../constants';
2
+ import { baseUrl } from '../../constants';
3
3
  import {createConfigForAxiosHeaders} from '../../util/validators';
4
4
  import { handleError } from '../../util/errorResponse';
5
+ import { PinataConfig } from '../..';
5
6
 
6
- /**
7
- * Test Authentication
8
- * @param {string} pinataApiKey
9
- * @param {string} pinataSecretApiKey
10
- * @returns {Promise<unknown>}
11
- */
12
- export default function testAuthentication(config) {
7
+ export interface PinataTestAuthenticationResponse {
8
+ authenticated: boolean;
9
+ }
10
+
11
+ export default function testAuthentication(config: PinataConfig): Promise<PinataTestAuthenticationResponse> {
13
12
  // test authentication to make sure that the user's provided keys are legit
14
13
  const endpoint = `${baseUrl}/data/testAuthentication`;
15
14
 
@@ -29,4 +28,4 @@ export default function testAuthentication(config) {
29
28
  reject(formattedError);
30
29
  });
31
30
  });
32
- };
31
+ }
@@ -1,16 +1,11 @@
1
1
  import axios from 'axios';
2
- import { baseUrl } from './../../constants';
2
+ import { baseUrl } from '../../constants';
3
3
  import { createConfigForAxiosHeaders } from '../../util/validators';
4
4
  import { handleError } from '../../util/errorResponse';
5
+ import { PinataConfig } from '../..';
5
6
 
6
- /**
7
- * User Pinned Data Total
8
- * @param {string} pinataApiKey
9
- * @param {string} pinataSecretApiKey
10
- * @returns {Promise<unknown>}
11
- */
12
- export default function userPinnedDataTotal(config) {
13
- let endpoint = `${baseUrl}/data/userPinnedDataTotal`;
7
+ export default function userPinnedDataTotal(config: PinataConfig): Promise<number> {
8
+ const endpoint = `${baseUrl}/data/userPinnedDataTotal`;
14
9
 
15
10
  return new Promise((resolve, reject) => {
16
11
  axios.get(
@@ -1,18 +1,12 @@
1
1
  import axios from 'axios';
2
- import { baseUrl } from './../../constants';
2
+ import { baseUrl } from '../../constants';
3
3
  import { createConfigForAxiosHeaders, validateMetadata } from '../../util/validators';
4
4
  import isIPFS from 'is-ipfs';
5
5
  import { handleError } from '../../util/errorResponse';
6
+ import { PinataConfig } from '../..';
7
+ import { PinataMetadata } from '../data/pinList/pinList';
6
8
 
7
- /**
8
- * Hash Meta Data
9
- * @param {string} pinataApiKey
10
- * @param {string} pinataSecretApiKey
11
- * @param {*} ipfsPinHash
12
- * @param {*} metadata
13
- * @returns {Promise<unknown>}
14
- */
15
- export default function hashMetadata(config, ipfsPinHash, metadata) {
9
+ export default function hashMetadata(config: PinataConfig, ipfsPinHash: string, metadata:PinataMetadata): Promise<any> {
16
10
  if (!ipfsPinHash) {
17
11
  throw new Error('ipfsPinHash value is required for changing the pin policy of a pin');
18
12
  }
@@ -28,7 +22,11 @@ export default function hashMetadata(config, ipfsPinHash, metadata) {
28
22
  validateMetadata(metadata);
29
23
 
30
24
  const endpoint = `${baseUrl}/pinning/hashMetadata`;
31
- const body = {
25
+ const body: {
26
+ ipfsPinHash: string,
27
+ name?: any,
28
+ keyvalues?: any
29
+ } = {
32
30
  ipfsPinHash: ipfsPinHash
33
31
  };
34
32
 
@@ -0,0 +1,8 @@
1
+ export * from './pinJobs/pinJobs';
2
+ export * from './pinJobs/queryBuilder';
3
+ export * from './hashMetadata';
4
+ export * from './pinByHash';
5
+ export * from './pinFileToIPFS';
6
+ export * from './pinFromFS';
7
+ export * from './pinJSONToIPFS';
8
+ export * from './unpin';
@@ -1,18 +1,25 @@
1
1
  import axios from 'axios';
2
- import { baseUrl } from './../../constants';
2
+ import { baseUrl } from '../../constants';
3
3
  import { createConfigForAxiosHeaders, validateMetadata } from '../../util/validators';
4
4
  import isIPFS from 'is-ipfs';
5
5
  import { handleError } from '../../util/errorResponse';
6
+ import { PinataConfig } from '../..';
7
+ import { PinataOptions } from './pinFileToIPFS';
8
+ import { PinataMetadata } from '../data/pinList/pinList';
6
9
 
7
- /**
8
- * Pin By Hash
9
- * @param {string} pinataApiKey
10
- * @param {string} pinataSecretApiKey
11
- * @param {*} hashToPin
12
- * @param {*} options
13
- * @returns {Promise<unknown>}
14
- */
15
- export default function pinByHash(config, hashToPin, options) {
10
+ export interface PinataPinByHashPinOptions {
11
+ pinataMetadata?: PinataMetadata;
12
+ pinataOptions?: PinataOptions ;
13
+ }
14
+
15
+ export interface PinataPinByHashResponse {
16
+ id: number | string;
17
+ ipfsHash: string;
18
+ status: string;
19
+ name: string;
20
+ }
21
+
22
+ export default function pinByHash(config: PinataConfig, hashToPin: string, options: any): Promise<any> {
16
23
  if (!hashToPin) {
17
24
  throw new Error('hashToPin value is required for pinning by hash');
18
25
  }
@@ -21,7 +28,11 @@ export default function pinByHash(config, hashToPin, options) {
21
28
  }
22
29
 
23
30
  const endpoint = `${baseUrl}/pinning/pinByHash`;
24
- const body = {
31
+ const body : {
32
+ hashToPin: any,
33
+ pinataOptions: any,
34
+ pinataMetadata?: any,
35
+ } = {
25
36
  hashToPin: hashToPin,
26
37
  pinataOptions: {}
27
38
  };
@@ -1,19 +1,35 @@
1
1
  import axios from 'axios';
2
- import { baseUrl } from './../../constants';
2
+ import { baseUrl } from '../../constants';
3
3
  import NodeFormData from 'form-data';
4
4
  import stream from 'stream';
5
5
  import {createConfigForAxiosHeadersWithFormData, validateMetadata, validatePinataOptions} from '../../util/validators';
6
6
  import { handleError } from '../../util/errorResponse';
7
+ import { PinataConfig } from '../..';
7
8
 
8
- /**
9
- * Pin File to IPFS
10
- * @param {string} pinataApiKey
11
- * @param {string} pinataSecretApiKey
12
- * @param {*} readStream
13
- * @param {*} options
14
- * @returns {Promise<unknown>}
15
- */
16
- export default function pinFileToIPFS(config, readStream, options) {
9
+ export interface PinataPinPolicyItem {
10
+ id: string;
11
+ desiredReplicationCount: number;
12
+ }
13
+ export interface PinataOptions {
14
+ hostNodes?: string[] | undefined;
15
+ cidVersion?: 0 | 1 ;
16
+ wrapWithDirectory?: boolean;
17
+ customPinPolicy?: {
18
+ regions: PinataPinPolicyItem[];
19
+ };
20
+ }
21
+
22
+ export interface PinataPinResponse {
23
+ IpfsHash: string;
24
+ PinSize: number;
25
+ Timestamp: string;
26
+ }
27
+ export interface PinataPinOptions {
28
+ pinataMetadata?: { [key: string]: string | number | null },
29
+ pinataOptions?: PinataOptions | undefined;
30
+ }
31
+
32
+ export default function pinFileToIPFS(config: PinataConfig, readStream: any, options?: PinataPinOptions):Promise<PinataPinResponse> {
17
33
  return new Promise((resolve, reject) => {
18
34
 
19
35
  const data = new NodeFormData();
@@ -39,7 +55,7 @@ export default function pinFileToIPFS(config, readStream, options) {
39
55
  axios.post(
40
56
  endpoint,
41
57
  readStream instanceof NodeFormData ? readStream : data,
42
- createConfigForAxiosHeadersWithFormData(config, data._boundary)
58
+ createConfigForAxiosHeadersWithFormData(config, data.getBoundary())
43
59
  ).then(function (result) {
44
60
  if (result.status !== 200) {
45
61
  reject(new Error(`unknown server response while pinning File to IPFS: ${result}`));
@@ -1,28 +1,23 @@
1
1
  import axios from 'axios';
2
- import { baseUrl } from './../../constants';
2
+ import { baseUrl } from '../../constants';
3
3
  import NodeFormData from 'form-data';
4
4
  import {createConfigForAxiosHeadersWithFormData, validateMetadata, validatePinataOptions} from '../../util/validators';
5
5
  import basePathConverter from 'base-path-converter';
6
6
  import { handleError } from '../../util/errorResponse';
7
- const fs = require('fs');
8
- const recursive = require('recursive-fs');
7
+ import { PinataConfig } from '../..';
8
+ import { PinataPinOptions, PinataPinResponse } from './pinFileToIPFS';
9
+ import * as fs from 'fs';
10
+ import * as recursive from 'recursive-fs';
9
11
 
10
- /**
11
- * PinFromFS
12
- * @param {string} pinataApiKey
13
- * @param {string} pinataSecretApiKey
14
- * @param {string} sourcePath
15
- * @param {*} options
16
- * @returns {Promise<unknown>}
17
- */
18
- export default function pinFromFS(config, sourcePath, options) {
12
+ export default function pinFromFS(config: PinataConfig, sourcePath: string, options?: PinataPinOptions): Promise<PinataPinResponse> {
19
13
 
20
14
  return new Promise((resolve, reject) => {
21
15
  const endpoint = `${baseUrl}/pinning/pinFileToIPFS`;
22
16
 
23
- fs.stat(sourcePath, (err, stats) => {
17
+ // eslint-disable-next-line consistent-return
18
+ fs.stat(sourcePath, (err: any, stats: any) => {
24
19
  if (err) {
25
- reject(err);
20
+ return reject(err);
26
21
  }
27
22
  if (stats.isFile()) {
28
23
  //we need to create a single read stream instead of reading the directory recursively
@@ -44,7 +39,7 @@ export default function pinFromFS(config, sourcePath, options) {
44
39
  axios.post(
45
40
  endpoint,
46
41
  data,
47
- createConfigForAxiosHeadersWithFormData(config, data._boundary))
42
+ createConfigForAxiosHeadersWithFormData(config, data.getBoundary()))
48
43
  .then(function (result) {
49
44
  if (result.status !== 200) {
50
45
  reject(new Error(`unknown server response while pinning File to IPFS: ${result}`));
@@ -55,14 +50,14 @@ export default function pinFromFS(config, sourcePath, options) {
55
50
  reject(formattedError);
56
51
  });
57
52
  } else {
58
- recursive.readdirr(sourcePath, function (err, dirs, files) {
53
+ recursive.readdirr(sourcePath, function (err: any, dirs: any, files: any) {
59
54
  if (err) {
60
55
  reject(new Error(err));
61
56
  }
62
57
 
63
- let data = new NodeFormData();
58
+ const data = new NodeFormData();
64
59
 
65
- files.forEach((file) => {
60
+ files.forEach((file: any) => {
66
61
  //for each file stream, we need to include the correct relative file path
67
62
  data.append('file', fs.createReadStream(file), {
68
63
  filepath: basePathConverter(sourcePath, file)
@@ -83,7 +78,7 @@ export default function pinFromFS(config, sourcePath, options) {
83
78
  axios.post(
84
79
  endpoint,
85
80
  data,
86
- createConfigForAxiosHeadersWithFormData(config, data._boundary))
81
+ createConfigForAxiosHeadersWithFormData(config, data.getBoundary()))
87
82
  .then(function (result) {
88
83
  if (result.status !== 200) {
89
84
  reject(new Error(`unknown server response while pinning File to IPFS: ${result}`));
@@ -1,19 +1,13 @@
1
1
  import axios from 'axios';
2
- import { baseUrl } from './../../constants';
2
+ import { baseUrl } from '../../constants';
3
3
  import { createConfigForAxiosHeaders, validateMetadata, validatePinataOptions } from '../../util/validators';
4
4
  import { handleError } from '../../util/errorResponse';
5
+ import { PinataConfig } from '../..';
6
+ import { PinataPinOptions, PinataPinResponse } from './pinFileToIPFS';
5
7
 
6
- /**
7
- * Pin JSON to IPFS
8
- * @param {string} pinataApiKey
9
- * @param {string} pinataSecretApiKey
10
- * @param {*} body
11
- * @param {*} options
12
- * @returns {Promise<unknown>}
13
- */
14
- export default function pinJSONToIPFS(config, body, options) {
8
+ export default function pinJSONToIPFS(config: PinataConfig, body: any, options? : PinataPinOptions):Promise<PinataPinResponse> {
15
9
 
16
- let requestBody = body;
10
+ let requestBody: any = body;
17
11
 
18
12
  if (typeof body !== 'object') {
19
13
  throw new Error('body must be a valid JSON object');