Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch media fix #336

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whatsapp-api-js",
"version": "3.0.0",
"version": "3.0.1",
"author": "Secreto31126",
"description": "A TypeScript server agnostic Whatsapp's Official API framework",
"license": "MIT",
Expand Down
21 changes: 17 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,16 @@ export class WhatsAppAPI {
* @throws If url is not a valid url
*/
fetchMedia(url: string): Promise<Response> {
// Hacky way to check if the url is valid and throw if invalid
return this._authenticatedRequest(new URL(url));
/**
* Hacky way to check if the url is valid and throw if invalid
*
* @see https://github.com/Secreto31126/whatsapp-api-js/issues/335#issuecomment-2103814359
*/
return this._authenticatedRequest(new URL(url), {
// Thanks @tecoad
"User-Agent":
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
});
}

/**
Expand Down Expand Up @@ -936,16 +944,21 @@ export class WhatsAppAPI {
*
* @internal
* @param url - The url to request to
* @param headers - The headers to pass to the request (Authorization is already included)
* @returns The fetch response
* @throws If url is not specified
*/
_authenticatedRequest(url: string | URL | Request): Promise<Response> {
_authenticatedRequest(
url: string | URL | Request,
headers = {}
): Promise<Response> {
// Keep the check to ensure on runtime that no weird stuff happens
if (!url) throw new Error("URL must be specified");

return this.fetch(url, {
headers: {
Authorization: `Bearer ${this.token}`
Authorization: `Bearer ${this.token}`,
...headers
}
});
}
Expand Down
23 changes: 21 additions & 2 deletions test/index.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1406,14 +1406,16 @@ describe("WhatsAppAPI", function () {
});

describe("Fetch", function () {
it("should GET fetch an url with the Token", async function () {
it("should GET fetch an url with the Token and the known to work User-Agent", async function () {
const expectedResponse = {};

clientExample
.intercept({
path: `/`,
headers: {
Authorization: `Bearer ${token}`
Authorization: `Bearer ${token}`,
"User-Agent":
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
}
})
.reply(200, expectedResponse)
Expand Down Expand Up @@ -1956,6 +1958,23 @@ describe("WhatsAppAPI", function () {
Whatsapp._authenticatedRequest("https://example.com/");
});

it("should make an authenticated request to any url with custom headers", async function () {
clientExample
.intercept({
path: "/",
headers: {
Authorization: `Bearer ${token}`,
blablabla: "blablabla"
}
})
.reply(200)
.times(1);

Whatsapp._authenticatedRequest("https://example.com/", {
blablabla: "blablabla"
});
});

it("should fail if the url param is not defined", function () {
throws(function () {
Whatsapp._authenticatedRequest(undefined);
Expand Down
Loading