Skip to content

Commit

Permalink
Merge pull request #3 from PinataCloud/chore/update-custom-headers
Browse files Browse the repository at this point in the history
chore: Updated and refactored custom headers feature
  • Loading branch information
stevedylandev authored Oct 10, 2024
2 parents 76afa4c + ca7eb42 commit c65f7cc
Show file tree
Hide file tree
Showing 32 changed files with 159 additions and 31 deletions.
5 changes: 4 additions & 1 deletion src/core/data/listFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export const listFiles = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/data/pinJobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ export const pinJobs = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,

...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
5 changes: 4 additions & 1 deletion src/core/data/pinnedFileUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export const pinnedFileCount = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
5 changes: 4 additions & 1 deletion src/core/data/totalStorageUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export const totalStorageUsage = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/data/updateMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export const updateMetadata = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
5 changes: 4 additions & 1 deletion src/core/gateway/analyticsDateInterval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ export const analyticsDateInterval = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
5 changes: 4 additions & 1 deletion src/core/gateway/analyticsTopUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ export const analyticsTopUsage = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/gateway/deleteSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export const deleteSwap = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/gateway/swapCid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export const swapCid = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/gateway/swapHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export const swapHistory = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/groups/addToGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export const addToGroup = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/groups/createGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export const createGroup = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/groups/deleteGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ export const deleteGroup = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/groups/getGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export const getGroup = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/groups/listGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export const listGroups = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/groups/removeFromGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export const removeFromGroup = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/groups/updateGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export const updateGroup = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/keys/createKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export const createKey = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/keys/listKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export const listKeys = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/keys/revokeKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export const revokeKeys = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
15 changes: 15 additions & 0 deletions src/core/pinataSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ export class PinataSDK {
this.signatures.updateConfig(this.config);
}

setNewJwt(jwt: string): void {
if (!this.config) {
this.config = { pinataJwt: "" };
}
this.config.pinataJwt = jwt;

// Update headers for all sub-modules
this.upload.updateConfig(this.config);
this.gateways.updateConfig(this.config);
this.usage.updateConfig(this.config);
this.keys.updateConfig(this.config);
this.groups.updateConfig(this.config);
this.signatures.updateConfig(this.config);
}

testAuthentication(): Promise<AuthTestResponse> {
return testAuthentication(this.config);
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/pinning/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ export const uploadBase64 = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${jwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${jwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/pinning/cid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export const uploadCid = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${jwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${jwt}`,
Expand Down
5 changes: 4 additions & 1 deletion src/core/pinning/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export const uploadFile = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${jwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${jwt}`,
Expand Down
5 changes: 4 additions & 1 deletion src/core/pinning/fileArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ export const uploadFileArray = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${jwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${jwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/pinning/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ export const uploadJson = async <T extends JsonBody>(
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${jwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${jwt}`,
Expand Down
5 changes: 4 additions & 1 deletion src/core/pinning/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export const uploadStream = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${jwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${jwt}`,
Expand Down
5 changes: 4 additions & 1 deletion src/core/pinning/unpin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export const unpinFile = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
5 changes: 4 additions & 1 deletion src/core/pinning/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ export const uploadUrl = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${jwt}`,
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${jwt}`,
Expand Down
6 changes: 5 additions & 1 deletion src/core/signatures/addSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ export const addSignature = async (
let headers: Record<string, string>;

if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
"Content-Type": "application/json",
...config.customHeaders,
};
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Expand Down
Loading

0 comments on commit c65f7cc

Please sign in to comment.