Skip to content

Commit

Permalink
chore: remove upload media handler for now, no need of it just adding…
Browse files Browse the repository at this point in the history
… complexity to the lib as of now, will add if somebody needs it.

Signed-off-by: sarthakjdev <jsarthak448@gmail.com>
  • Loading branch information
sarthakjdev committed May 29, 2024
1 parent fd07f52 commit a35d2ad
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 99 deletions.
2 changes: 0 additions & 2 deletions packages/wapi.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"license": "Apache-2.0",
"devDependencies": {
"@types/express": "^4.17.18",
"@types/mime-types": "^2.1.4",
"@wapijs/eslint-config": "workspace:*",
"@wapijs/prettier-config": "workspace:*",
"@wapijs/typescript-config": "workspace:*",
Expand All @@ -59,7 +58,6 @@
"dependencies": {
"@microsoft/api-extractor": "^7.43.1",
"express": "^4.18.2",
"mime-types": "^2.1.35",
"zod": "^3.22.2"
},
"packageManager": "pnpm@9.1.0",
Expand Down
75 changes: 1 addition & 74 deletions packages/wapi.js/src/manager/media/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type CloudApiRequestResourceType,
} from "../../client/schema";
import { type MediaUploadResponseSchemaType } from "./schema";
import { lookup } from "mime-types";


/**
* Manager to handle media.
Expand Down Expand Up @@ -80,77 +80,4 @@ export class MediaManager extends BaseManager implements MediaManagerInterface {
return false;
}
}

/**
* Upload media to WhatsApp.
* @param {Object} params - The parameters for uploading media.
* @param {string} params.filePath - The file path of the media to upload. This can either be a local file path or a source url of a downloadable media asset.
* @param {string} params.mediaType - The type of the media.
* @memberof MediaManager
* @returns {Promise<string>} - A promise that resolves to the media ID of the uploaded media.
*/
async upload(params: {
filePath: string;
}): Promise<z.infer<typeof MediaUploadResponseSchemaType>> {
console.log({ params })

const mimeType = lookup(params.filePath);

console.log({ params, mimeType })

if (!mimeType) {
throw new Error("Invalid file path or type");
}

const response =
await this.client.requester.requestCloudApi<CloudApiRequestResourceType.Media>(
{
path: `/${this.client.phoneNumberId}/media`,
body: JSON.stringify({
file: `@${params.filePath};type=${mimeType}`,
messaging_product: "whatsapp",
}),
method: "POST",
},
);

console.log({ response });

const parsedResponse = CloudApiResponseSchemaType.safeParse(response);
if (parsedResponse.success) {
const responseData = parsedResponse.data;

if (
responseData &&
typeof responseData === "object" &&
"id" in responseData
) {
// api returned errored response
return {
status: "success",
data: {
mediaId: responseData.id as string,
},
};
} else if (typeof responseData === "object" && "error" in responseData) {
return {
status: "error",
error: {
description: responseData.error.error_data.details,
title: responseData.error.message,
errorCode: responseData.error.code,
errorSubCode: responseData.error.error_subcode,
},
};
} else {
throw new Error(
"Failed to parse response, Report to sarthak@softlancer.co urgently. or raise an issue on github.",
);
}
} else {
throw new Error(
"Failed to parse response, Report to sarthak@softlancer.co urgently. or raise an issue on github.",
);
}
}
}
11 changes: 0 additions & 11 deletions packages/wapi.js/src/manager/media/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ export interface MediaManagerInterface extends BaseManagerInterface {
z.infer<typeof CloudApiGetMediaUrlRequestSuccessResponseSchemaType>
>;

/**
* Upload a media file
* @param {Object} params - The parameters for uploading the media file
* @param {string} params.filePath - The file path of the media file
* @param {string} params.mediaType - The type of the media file
* @returns {Promise<string>} The ID of the uploaded media file
*/
upload: (params: {
filePath: string;
}) => Promise<z.infer<typeof MediaUploadResponseSchemaType>>;

/**
* Delete a media file by its ID
* @param {string} mediaId - The ID of the media file to delete
Expand Down
99 changes: 87 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a35d2ad

Please sign in to comment.