Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #126 from PinataCloud/develop
Browse files Browse the repository at this point in the history
added support for JWT, closes #83
  • Loading branch information
polluterofminds authored Oct 19, 2022
2 parents c4882ec + 4bff6d3 commit 67f0424
Show file tree
Hide file tree
Showing 49 changed files with 851 additions and 712 deletions.
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@ npm install --save @pinata/sdk
```

## Setup
To start, simply require the Pinata SDK and set up an instance with your Pinata API Keys. Don't know what your keys are? Check out your [Account Page](https://pinata.cloud/account).
To start, simply require the Pinata SDK and set up an instance with your Pinata API Keys or your JWT key. Don't know what your keys are? Check out your [Account Page](https://pinata.cloud/account).
In the example below we provided with 3 ways to call the pinata SDK.

```javascript
// Use the api keys by providing the strings directly
const pinataSDK = require('@pinata/sdk');
const pinata = pinataSDK('yourPinataApiKey', 'yourPinataSecretApiKey');
```

```javascript
// Use the api keys by specifying your api key and api secret
const pinataSDK = require('@pinata/sdk');
const pinata = pinataSDK({ pinataApiKey: 'yourPinataApiKey', pinataSecretApiKey: 'yourPinataSecretApiKey' });

```

```javascript
// Use the JWT key
const pinataSDK = require('@pinata/sdk');
const pinata = pinataSDK({ pinataJWTKey: 'yourPinataJWTKey'});
```

Quickly test that you can connect to the API with the following call:
```javascript
pinata.testAuthentication().then((result) => {
Expand Down Expand Up @@ -191,13 +207,13 @@ pinata.pinFileToIPFS(readableStreamForFile, options).then((result) => {
});
```

<a name="pinFromFs-anchor"></a>
<a name="pinFromFS-anchor"></a>
### `pinFromFS`
Read from a location on your local file system and recursively pin the contents to IPFS (node.js only).

Both individual files, as well as directories can be read from.

##### `pinata.pinFromFs(sourcePath, options)`
##### `pinata.pinFromFS(sourcePath, options)`
##### Params
* `sourcePath` - The location on your local filesystem that should be read from.
* `options` (optional): A JSON object that can contain the following keyvalues:
Expand Down Expand Up @@ -468,7 +484,7 @@ Our libraries support auto-pagination. This feature easily handles fetching larg
To use the auto-pagination feature in Node 10+, simply iterate over a "list" call with the parameters you need in a for await loop.

<a name="getFilesByCount-anchor"></a>
### `getFilesByCount-anchor`
### `getFilesByCount`
This method support auto-pagination. This feature easily handles fetching large lists of pin records for your Pinata account without having to manually paginate results and perform subsequent requests. To use the auto-pagination feature in Node 10+.

##### `pinata.getFilesByCount(filters, count)`
Expand Down Expand Up @@ -537,7 +553,7 @@ Filter explanations:
date_pinned: (This is the timestamp for when this content was pinned - represented in ISO 8601 format),
date_unpinned: (This is the timestamp for when this content was unpinned (if null, then you still have the content pinned on Pinata),
metadata: {
name: (this will be the name of the file originally upuloaded, or the custom name you set),
name: (this will be the name of the file originally uploaded, or the custom name you set),
keyvalues: {
exampleCustomKey: "exampleCustomValue",
exampleCustomKey2: "exampleCustomValue2",
Expand Down
460 changes: 305 additions & 155 deletions lib/pinata-sdk.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions lib/pinata-sdk.min.js

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions src/commands/data/getFilesByCount/getFilesByCount.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { validateApiKeys } from '../../../util/validators';
import pinList from '../pinList/pinList';

export default function getFilesByCount(
pinataApiKey,
pinataSecretApiKey,
config,

filters = {},
maxCount = -1
) {
validateApiKeys(pinataApiKey, pinataSecretApiKey);
if (maxCount === 0) {
throw Error("Max count can't be 0");
}
Expand All @@ -26,7 +23,7 @@ export default function getFilesByCount(
return new Promise((resolve, reject) => {
if (i === 0 || (i % pageLimit === 0 && keepLooping)) {
resolve(
pinList(pinataApiKey, pinataSecretApiKey, {
pinList(config, {
filters,
...{ pageOffset, pageLimit }
}).then((resp) => {
Expand Down
15 changes: 4 additions & 11 deletions src/commands/data/pinList/pinList.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { baseUrl } from './../../../constants';
import { validateApiKeys } from '../../../util/validators';
import { createConfigForAxiosHeaders } from '../../../util/validators';
import { handleError } from '../../../util/errorResponse';
import queryBuilder from './queryBuilder';

Expand All @@ -11,9 +11,7 @@ import queryBuilder from './queryBuilder';
* @param {any} filters
* @returns {Promise<unknown>}
*/
export default function pinList(pinataApiKey, pinataSecretApiKey, filters = {}) {
validateApiKeys(pinataApiKey, pinataSecretApiKey);

export default function pinList(config, filters = {}) {
filters = {...filters, ...{includeCount: 'false' }};

const baseEndpoint = `${baseUrl}/data/pinList`;
Expand All @@ -22,13 +20,8 @@ export default function pinList(pinataApiKey, pinataSecretApiKey, filters = {})
return new Promise((resolve, reject) => {
axios.get(
endpoint,
{
withCredentials: true,
headers: {
'pinata_api_key': pinataApiKey,
'pinata_secret_api_key': pinataSecretApiKey
}
}).then(function (result) {
{...createConfigForAxiosHeaders(config)})
.then(function (result) {
if (result.status !== 200) {
reject(new Error(`unknown server response while attempting to retrieve user pin list: ${result}`));
}
Expand Down
15 changes: 4 additions & 11 deletions src/commands/data/testAuthentication.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { baseUrl } from './../../constants';
import {validateApiKeys} from '../../util/validators';
import {createConfigForAxiosHeaders} from '../../util/validators';
import { handleError } from '../../util/errorResponse';

/**
Expand All @@ -9,22 +9,15 @@ import { handleError } from '../../util/errorResponse';
* @param {string} pinataSecretApiKey
* @returns {Promise<unknown>}
*/
export default function testAuthentication(pinataApiKey, pinataSecretApiKey) {
validateApiKeys(pinataApiKey, pinataSecretApiKey);

export default function testAuthentication(config) {
// test authentication to make sure that the user's provided keys are legit
const endpoint = `${baseUrl}/data/testAuthentication`;

return new Promise((resolve, reject) => {
axios.get(
endpoint,
{
withCredentials: true,
headers: {
'pinata_api_key': pinataApiKey,
'pinata_secret_api_key': pinataSecretApiKey
}
}).then(function (result) {
{...createConfigForAxiosHeaders(config)})
.then(function (result) {
if (result.status !== 200) {
reject(new Error(`unknown server response while authenticating: ${result}`));
}
Expand Down
15 changes: 4 additions & 11 deletions src/commands/data/userPinnedDataTotal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { baseUrl } from './../../constants';
import { validateApiKeys } from '../../util/validators';
import { createConfigForAxiosHeaders } from '../../util/validators';
import { handleError } from '../../util/errorResponse';

/**
Expand All @@ -9,21 +9,14 @@ import { handleError } from '../../util/errorResponse';
* @param {string} pinataSecretApiKey
* @returns {Promise<unknown>}
*/
export default function userPinnedDataTotal(pinataApiKey, pinataSecretApiKey) {
validateApiKeys(pinataApiKey, pinataSecretApiKey);

export default function userPinnedDataTotal(config) {
let endpoint = `${baseUrl}/data/userPinnedDataTotal`;

return new Promise((resolve, reject) => {
axios.get(
endpoint,
{
withCredentials: true,
headers: {
'pinata_api_key': pinataApiKey,
'pinata_secret_api_key': pinataSecretApiKey
}
}).then(function (result) {
{...createConfigForAxiosHeaders(config)})
.then(function (result) {
if (result.status !== 200) {
reject(new Error(`unknown server response while attempting to retrieve pinned data total: ${result}`));
}
Expand Down
15 changes: 4 additions & 11 deletions src/commands/pinning/hashMetadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { baseUrl } from './../../constants';
import { validateApiKeys, validateMetadata } from '../../util/validators';
import { createConfigForAxiosHeaders, validateMetadata } from '../../util/validators';
import isIPFS from 'is-ipfs';
import { handleError } from '../../util/errorResponse';

Expand All @@ -12,9 +12,7 @@ import { handleError } from '../../util/errorResponse';
* @param {*} metadata
* @returns {Promise<unknown>}
*/
export default function hashMetadata(pinataApiKey, pinataSecretApiKey, ipfsPinHash, metadata) {
validateApiKeys(pinataApiKey, pinataSecretApiKey);

export default function hashMetadata(config, ipfsPinHash, metadata) {
if (!ipfsPinHash) {
throw new Error('ipfsPinHash value is required for changing the pin policy of a pin');
}
Expand Down Expand Up @@ -46,13 +44,8 @@ export default function hashMetadata(pinataApiKey, pinataSecretApiKey, ipfsPinHa
axios.put(
endpoint,
body,
{
withCredentials: true,
headers: {
'pinata_api_key': pinataApiKey,
'pinata_secret_api_key': pinataSecretApiKey
}
}).then(function (result) {
{...createConfigForAxiosHeaders(config)})
.then(function (result) {
if (result.status !== 200) {
reject(new Error(`unknown server response while changing metadata for hash: ${result}`));
}
Expand Down
14 changes: 4 additions & 10 deletions src/commands/pinning/hashPinPolicy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { baseUrl } from './../../constants';
import { validateApiKeys, validatePinPolicyStructure } from '../../util/validators';
import { createConfigForAxiosHeaders, validatePinPolicyStructure } from '../../util/validators';
import isIPFS from 'is-ipfs';
import { handleError } from '../../util/errorResponse';

Expand All @@ -12,8 +12,7 @@ import { handleError } from '../../util/errorResponse';
* @param {*} newPinPolicy
* @returns {Promise<unknown>}
*/
export default function hashPinPolicy(pinataApiKey, pinataSecretApiKey, ipfsPinHash, newPinPolicy) {
validateApiKeys(pinataApiKey, pinataSecretApiKey);
export default function hashPinPolicy(config, ipfsPinHash, newPinPolicy) {
validatePinPolicyStructure(newPinPolicy);

if (!ipfsPinHash) {
Expand All @@ -38,13 +37,8 @@ export default function hashPinPolicy(pinataApiKey, pinataSecretApiKey, ipfsPinH
axios.put(
endpoint,
body,
{
withCredentials: true,
headers: {
'pinata_api_key': pinataApiKey,
'pinata_secret_api_key': pinataSecretApiKey
}
}).then(function (result) {
{...createConfigForAxiosHeaders(config)})
.then(function (result) {
if (result.status !== 200) {
reject(new Error(`unknown server response while changing pin policy for hash: ${result}`));
}
Expand Down
15 changes: 4 additions & 11 deletions src/commands/pinning/pinByHash.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { baseUrl } from './../../constants';
import { validateApiKeys, validateMetadata } from '../../util/validators';
import { createConfigForAxiosHeaders, validateMetadata } from '../../util/validators';
import isIPFS from 'is-ipfs';
import { handleError } from '../../util/errorResponse';

Expand All @@ -12,9 +12,7 @@ import { handleError } from '../../util/errorResponse';
* @param {*} options
* @returns {Promise<unknown>}
*/
export default function pinByHash(pinataApiKey, pinataSecretApiKey, hashToPin, options) {
validateApiKeys(pinataApiKey, pinataSecretApiKey);

export default function pinByHash(config, hashToPin, options) {
if (!hashToPin) {
throw new Error('hashToPin value is required for pinning by hash');
}
Expand Down Expand Up @@ -42,13 +40,8 @@ export default function pinByHash(pinataApiKey, pinataSecretApiKey, hashToPin, o
axios.post(
endpoint,
body,
{
withCredentials: true,
headers: {
'pinata_api_key': pinataApiKey,
'pinata_secret_api_key': pinataSecretApiKey
}
}).then(function (result) {
{...createConfigForAxiosHeaders(config)})
.then(function (result) {
if (result.status !== 200) {
reject(new Error(`unknown server response while adding to pin queue: ${result}`));
}
Expand Down
21 changes: 5 additions & 16 deletions src/commands/pinning/pinFileToIPFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios';
import { baseUrl } from './../../constants';
import NodeFormData from 'form-data';
import stream from 'stream';
import {validateApiKeys, validateMetadata, validatePinataOptions} from '../../util/validators';
import {createConfigForAxiosHeadersWithFormData, validateMetadata, validatePinataOptions} from '../../util/validators';
import { handleError } from '../../util/errorResponse';

/**
Expand All @@ -13,9 +13,7 @@ import { handleError } from '../../util/errorResponse';
* @param {*} options
* @returns {Promise<unknown>}
*/
export default function pinFileToIPFS(pinataApiKey, pinataSecretApiKey, readStream, options) {
validateApiKeys(pinataApiKey, pinataSecretApiKey);

export default function pinFileToIPFS(config, readStream, options) {
return new Promise((resolve, reject) => {

const data = new NodeFormData();
Expand All @@ -38,20 +36,11 @@ export default function pinFileToIPFS(pinataApiKey, pinataSecretApiKey, readStre
data.append('pinataOptions', JSON.stringify(options.pinataOptions));
}
}

axios.post(
axios.post(
endpoint,
readStream instanceof NodeFormData ? readStream : data,
{
withCredentials: true,
maxContentLength: 'Infinity', //this is needed to prevent axios from erroring out with large files
maxBodyLength: 'Infinity',
headers: {
'Content-type': `multipart/form-data; boundary= ${data._boundary}`,
'pinata_api_key': pinataApiKey,
'pinata_secret_api_key': pinataSecretApiKey
}
}).then(function (result) {
createConfigForAxiosHeadersWithFormData(config, data._boundary)
).then(function (result) {
if (result.status !== 200) {
reject(new Error(`unknown server response while pinning File to IPFS: ${result}`));
}
Expand Down
29 changes: 6 additions & 23 deletions src/commands/pinning/pinFromFS.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { baseUrl } from './../../constants';
import NodeFormData from 'form-data';
import {validateApiKeys, validateMetadata, validatePinataOptions} from '../../util/validators';
import {createConfigForAxiosHeadersWithFormData, validateMetadata, validatePinataOptions} from '../../util/validators';
import basePathConverter from 'base-path-converter';
import { handleError } from '../../util/errorResponse';
const fs = require('fs');
Expand All @@ -15,8 +15,7 @@ const recursive = require('recursive-fs');
* @param {*} options
* @returns {Promise<unknown>}
*/
export default function pinFromFS(pinataApiKey, pinataSecretApiKey, sourcePath, options) {
validateApiKeys(pinataApiKey, pinataSecretApiKey);
export default function pinFromFS(config, sourcePath, options) {

return new Promise((resolve, reject) => {
const endpoint = `${baseUrl}/pinning/pinFileToIPFS`;
Expand Down Expand Up @@ -45,16 +44,8 @@ export default function pinFromFS(pinataApiKey, pinataSecretApiKey, sourcePath,
axios.post(
endpoint,
data,
{
withCredentials: true,
maxContentLength: 'Infinity', //this is needed to prevent axios from erroring out with large directories
maxBodyLength: 'Infinity',
headers: {
'Content-type': `multipart/form-data; boundary= ${data._boundary}`,
'pinata_api_key': pinataApiKey,
'pinata_secret_api_key': pinataSecretApiKey
}
}).then(function (result) {
createConfigForAxiosHeadersWithFormData(config, data._boundary))
.then(function (result) {
if (result.status !== 200) {
reject(new Error(`unknown server response while pinning File to IPFS: ${result}`));
}
Expand Down Expand Up @@ -92,16 +83,8 @@ export default function pinFromFS(pinataApiKey, pinataSecretApiKey, sourcePath,
axios.post(
endpoint,
data,
{
withCredentials: true,
maxContentLength: 'Infinity',
maxBodyLength: 'Infinity', //this is needed to prevent axios from erroring out with large directories
headers: {
'Content-type': `multipart/form-data; boundary= ${data._boundary}`,
'pinata_api_key': pinataApiKey,
'pinata_secret_api_key': pinataSecretApiKey
}
}).then(function (result) {
createConfigForAxiosHeadersWithFormData(config, data._boundary))
.then(function (result) {
if (result.status !== 200) {
reject(new Error(`unknown server response while pinning File to IPFS: ${result}`));
}
Expand Down
Loading

0 comments on commit 67f0424

Please sign in to comment.