Skip to content

Commit

Permalink
Merge pull request #336 from Secreto31126/fetch-media-fix
Browse files Browse the repository at this point in the history
Fetch media fix
  • Loading branch information
Secreto31126 authored May 10, 2024
2 parents b7cde0e + 29a3415 commit e8d0011
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
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

0 comments on commit e8d0011

Please sign in to comment.