Skip to content

Commit

Permalink
Merge pull request #5 from PinataCloud/refactor/custom-headers
Browse files Browse the repository at this point in the history
Refactor/custom headers
  • Loading branch information
stevedylandev authored Aug 14, 2024
2 parents 497fea7 + e63fb72 commit a44b197
Show file tree
Hide file tree
Showing 29 changed files with 558 additions and 349 deletions.
30 changes: 16 additions & 14 deletions src/core/authentication/testAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,27 @@ export const testAuthentication = async (config: PinataConfig | undefined) => {
throw new ValidationError("Pinata configuration or JWT is missing");
}

const headers: Record<string, string> = {
Authorization: `Bearer ${config?.pinataJwt}`,
};
let headers: Record<string, string>;
let endpoint: string = "https://api.pinata.cloud";

if (config.customHeaders) {
Object.assign(headers, config.customHeaders);
if (config.endpointUrl) {
endpoint = config.endpointUrl;
}

// biome-ignore lint/complexity/useLiteralKeys: non-issue
headers["Source"] = headers["Source"] || "sdk/testAuthentication";
if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Source: "sdk/testAuthentication",
};
}

try {
const request = await fetch(
"https://api.pinata.cloud/data/testAuthentication",
{
method: "GET",
headers: headers,
},
);
const request = await fetch(`${endpoint}/data/testAuthentication`, {
method: "GET",
headers: headers,
});
if (!request.ok) {
const errorData = await request.json();
if (request.status === 401) {
Expand Down
24 changes: 15 additions & 9 deletions src/core/data/listFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,26 @@ export const listFiles = async (
}
}

const url = `https://api.pinata.cloud/data/pinList?status=pinned&${params.toString()}`;
let endpoint: string = "https://api.pinata.cloud";

if (config.endpointUrl) {
endpoint = config.endpointUrl;
}

const url = `${endpoint}/data/pinList?status=pinned&${params.toString()}`;

try {
const headers: Record<string, string> = {
Authorization: `Bearer ${config?.pinataJwt}`,
};
let headers: Record<string, string>;

if (config.customHeaders) {
Object.assign(headers, config.customHeaders);
if (config.customHeaders && Object.keys(config.customHeaders).length > 0) {
headers = { ...config.customHeaders };
} else {
headers = {
Authorization: `Bearer ${config.pinataJwt}`,
Source: "sdk/listFiles",
};
}

// biome-ignore lint/complexity/useLiteralKeys: non-issue
headers["Source"] = headers["Source"] || "sdk/listFiles";

const request = await fetch(url, {
method: "GET",
headers: headers,
Expand Down
24 changes: 15 additions & 9 deletions src/core/data/pinJobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,24 @@ export const pinJobs = async (
if (offset) params.append("offset", offset.toString());
}

const url = `https://api.pinata.cloud/pinning/pinJobs?${params.toString()}`;
let endpoint: string = "https://api.pinata.cloud";

const headers: Record<string, string> = {
Authorization: `Bearer ${config?.pinataJwt}`,
};

if (config.customHeaders) {
Object.assign(headers, config.customHeaders);
if (config.endpointUrl) {
endpoint = config.endpointUrl;
}

// biome-ignore lint/complexity/useLiteralKeys: non-issue
headers["Source"] = headers["Source"] || "sdk/pinJobs";
const url = `${endpoint}/pinning/pinJobs?${params.toString()}`;

let headers: Record<string, string>;

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

try {
const request = await fetch(url, {
Expand Down
24 changes: 14 additions & 10 deletions src/core/data/pinnedFileUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,25 @@ export const pinnedFileCount = async (
throw new ValidationError("Pinata configuration or JWT is missing");
}

const url = "https://api.pinata.cloud/data/userPinnedDataTotal";
let endpoint: string = "https://api.pinata.cloud";

const headers: Record<string, string> = {
Authorization: `Bearer ${config?.pinataJwt}`,
};

if (config.customHeaders) {
Object.assign(headers, config.customHeaders);
if (config.endpointUrl) {
endpoint = config.endpointUrl;
}

// biome-ignore lint/complexity/useLiteralKeys: non-issue
headers["Source"] = headers["Source"] || "sdk/pinnedFileUsage";
let headers: Record<string, string>;

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

try {
const request = await fetch(url, {
const request = await fetch(`${endpoint}/data/userPinnedDataTotal`, {
method: "GET",
headers: headers,
});
Expand Down
24 changes: 14 additions & 10 deletions src/core/data/totalStorageUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,25 @@ export const totalStorageUsage = async (
throw new ValidationError("Pinata configuration or JWT is missing");
}

const url = "https://api.pinata.cloud/data/userPinnedDataTotal";
let endpoint: string = "https://api.pinata.cloud";

const headers: Record<string, string> = {
Authorization: `Bearer ${config?.pinataJwt}`,
};

if (config.customHeaders) {
Object.assign(headers, config.customHeaders);
if (config.endpointUrl) {
endpoint = config.endpointUrl;
}

// biome-ignore lint/complexity/useLiteralKeys: non-issue
headers["Source"] = headers["Source"] || "sdk/totalStorageUsage";
let headers: Record<string, string>;

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

try {
const request = await fetch(url, {
const request = await fetch(`${endpoint}/data/userPinnedDataTotal`, {
method: "GET",
headers: headers,
});
Expand Down
35 changes: 19 additions & 16 deletions src/core/data/updateMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,30 @@ export const updateMetadata = async (
keyvalues: options.keyValues,
};

const headers: Record<string, string> = {
"Content-Type": "application/json",
Authorization: `Bearer ${config?.pinataJwt}`,
};
let headers: Record<string, string>;

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

// biome-ignore lint/complexity/useLiteralKeys: non-issue
headers["Source"] = headers["Source"] || "sdk/updateMetadata";
let endpoint: string = "https://api.pinata.cloud";

if (config.endpointUrl) {
endpoint = config.endpointUrl;
}

try {
const request = await fetch(
"https://api.pinata.cloud/pinning/hashMetadata",
{
method: "PUT",
headers: headers,
body: JSON.stringify(data),
},
);
const request = await fetch(`${endpoint}/pinning/hashMetadata`, {
method: "PUT",
headers: headers,
body: JSON.stringify(data),
});

if (!request.ok) {
const errorData = await request.json();
Expand Down
35 changes: 19 additions & 16 deletions src/core/groups/addToGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,30 @@ export const addToGroup = async (
cids: options.cids,
});

const headers: Record<string, string> = {
"Content-Type": "application/json",
Authorization: `Bearer ${config?.pinataJwt}`,
};
let headers: Record<string, string>;

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

// biome-ignore lint/complexity/useLiteralKeys: non-issue
headers["Source"] = headers["Source"] || "sdk/addToGroup";
let endpoint: string = "https://api.pinata.cloud";

if (config.endpointUrl) {
endpoint = config.endpointUrl;
}

try {
const request = await fetch(
`https://api.pinata.cloud/groups/${options.groupId}/cids`,
{
method: "PUT",
headers: headers,
body: data,
},
);
const request = await fetch(`${endpoint}/groups/${options.groupId}/cids`, {
method: "PUT",
headers: headers,
body: data,
});

if (!request.ok) {
const errorData = await request.json();
Expand Down
24 changes: 15 additions & 9 deletions src/core/groups/createGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,26 @@ export const createGroup = async (

const data = JSON.stringify(options);

const headers: Record<string, string> = {
"Content-Type": "application/json",
Authorization: `Bearer ${config?.pinataJwt}`,
};
let headers: Record<string, string>;

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

// biome-ignore lint/complexity/useLiteralKeys: non-issue
headers["Source"] = headers["Source"] || "sdk/createGroup";
let endpoint: string = "https://api.pinata.cloud";

if (config.endpointUrl) {
endpoint = config.endpointUrl;
}

try {
const request = await fetch("https://api.pinata.cloud/groups", {
const request = await fetch(`${endpoint}/groups`, {
method: "POST",
headers: headers,
body: data,
Expand Down
33 changes: 18 additions & 15 deletions src/core/groups/deleteGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,29 @@ export const deleteGroup = async (
throw new ValidationError("Pinata configuration or JWT is missing");
}

const headers: Record<string, string> = {
"Content-Type": "application/json",
Authorization: `Bearer ${config?.pinataJwt}`,
};
let headers: Record<string, string>;

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

// biome-ignore lint/complexity/useLiteralKeys: non-issue
headers["Source"] = headers["Source"] || "sdk/deleteGroup";
let endpoint: string = "https://api.pinata.cloud";

if (config.endpointUrl) {
endpoint = config.endpointUrl;
}

try {
const request = await fetch(
`https://api.pinata.cloud/groups/${options.groupId}`,
{
method: "DELETE",
headers: headers,
},
);
const request = await fetch(`${endpoint}/groups/${options.groupId}`, {
method: "DELETE",
headers: headers,
});

if (!request.ok) {
const errorData = await request.json();
Expand Down
Loading

0 comments on commit a44b197

Please sign in to comment.