Skip to content

Commit

Permalink
feat: pass change isFile guard to isBlob
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Jul 7, 2024
1 parent ebe59dc commit 72aa765
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ dist
node_modules
tg-bot-api
test.ts
tsconfig.tsbuildinfo
tsconfig.tsbuildinfo
build.ts
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
"devDependencies": {
"@biomejs/biome": "1.7.3",
"@types/bun": "^1.1.6",
"@types/node": "^20.14.9",
"@types/node": "^20.14.10",
"prettier": "^3.3.2",
"typescript": "^5.5.3"
},
"dependencies": {
"@gramio/types": "^7.6.0"
"@gramio/types": "^7.7.0"
},
"files": [
"dist"
Expand Down
12 changes: 6 additions & 6 deletions scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ fs.writeFile(
[Method in keyof APIMethods]?: [(params: (NonNullable<APIMethodParams<Method>>)) => boolean, Extractor[] | null];
};
/** Guard for check is it File or Promise */
export function isFile(file?: TelegramInputFile | object | string) {
if(!file || typeof file !== "object") return false;
/** Guard for check is it {@link Blob} or {@link Promise} */
export function isBlob(blob?: TelegramInputFile | object | string) {
if (!blob || typeof blob !== "object") return false;
return file instanceof File || file instanceof Promise;
return blob instanceof Blob || blob instanceof Promise;
}
/**
Expand All @@ -138,15 +138,15 @@ fs.writeFile(
return `${key}: [(params) => ${value
.map((x) => {
if (x.type === "array")
return `params.${x.property}.some(x => "${x.name}" in x && isFile(x.${x.name}))`;
return `params.${x.property}.some(x => "${x.name}" in x && isBlob(x.${x.name}))`;
return /* ts */ `${
x.type === "union"
? `"${x.name}" in params${
x.property ? `.${x.property}` : ""
} && `
: ""
}isFile(params.${
}isBlob(params.${
x.property ? `${x.property}.${x.name}` : `${x.name}`
})`;
})
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
*/

export * from "./utils";
export { isFile, MEDIA_METHODS } from "./media-methods-helper";
export { isBlob, MEDIA_METHODS } from "./media-methods-helper";
export * from "./media-input";
export * from "./media-upload";
48 changes: 24 additions & 24 deletions src/media-methods-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type MethodsWithMediaUpload = {
];
};

/** Guard for check is it {@link File} or {@link Promise} */
export function isFile(file?: TelegramInputFile | object | string) {
if (!file || typeof file !== "object") return false;
/** Guard for check is it {@link Blob} or {@link Promise} */
export function isBlob(blob?: TelegramInputFile | object | string) {
if (!blob || typeof blob !== "object") return false;

return file instanceof File || file instanceof Promise;
return blob instanceof Blob || blob instanceof Promise;
}

/**
Expand All @@ -29,31 +29,31 @@ export function isFile(file?: TelegramInputFile | object | string) {
* @codegenerated
* */
export const MEDIA_METHODS: MethodsWithMediaUpload = {
setWebhook: [(params) => isFile(params.certificate), null],
sendPhoto: [(params) => isFile(params.photo), null],
setWebhook: [(params) => isBlob(params.certificate), null],
sendPhoto: [(params) => isBlob(params.photo), null],
sendAudio: [
(params) => isFile(params.audio) || isFile(params.thumbnail),
(params) => isBlob(params.audio) || isBlob(params.thumbnail),
null,
],
sendDocument: [
(params) => isFile(params.document) || isFile(params.thumbnail),
(params) => isBlob(params.document) || isBlob(params.thumbnail),
null,
],
sendVideo: [
(params) => isFile(params.video) || isFile(params.thumbnail),
(params) => isBlob(params.video) || isBlob(params.thumbnail),
null,
],
sendAnimation: [
(params) => isFile(params.animation) || isFile(params.thumbnail),
(params) => isBlob(params.animation) || isBlob(params.thumbnail),
null,
],
sendVoice: [(params) => isFile(params.voice), null],
sendVoice: [(params) => isBlob(params.voice), null],
sendVideoNote: [
(params) => isFile(params.video_note) || isFile(params.thumbnail),
(params) => isBlob(params.video_note) || isBlob(params.thumbnail),
null,
],
sendPaidMedia: [
(params) => params.media.some((x) => "media" in x && isFile(x.media)),
(params) => params.media.some((x) => "media" in x && isBlob(x.media)),
[
{
name: "media",
Expand All @@ -64,8 +64,8 @@ export const MEDIA_METHODS: MethodsWithMediaUpload = {
],
sendMediaGroup: [
(params) =>
params.media.some((x) => "media" in x && isFile(x.media)) ||
params.media.some((x) => "thumbnail" in x && isFile(x.thumbnail)),
params.media.some((x) => "media" in x && isBlob(x.media)) ||
params.media.some((x) => "thumbnail" in x && isBlob(x.thumbnail)),
[
{
name: "media",
Expand All @@ -79,11 +79,11 @@ export const MEDIA_METHODS: MethodsWithMediaUpload = {
},
],
],
setChatPhoto: [(params) => isFile(params.photo), null],
setChatPhoto: [(params) => isBlob(params.photo), null],
editMessageMedia: [
(params) =>
("media" in params.media && isFile(params.media.media)) ||
("thumbnail" in params.media && isFile(params.media.thumbnail)),
("media" in params.media && isBlob(params.media.media)) ||
("thumbnail" in params.media && isBlob(params.media.thumbnail)),
[
{
name: "media",
Expand All @@ -97,11 +97,11 @@ export const MEDIA_METHODS: MethodsWithMediaUpload = {
},
],
],
sendSticker: [(params) => isFile(params.sticker), null],
uploadStickerFile: [(params) => isFile(params.sticker), null],
sendSticker: [(params) => isBlob(params.sticker), null],
uploadStickerFile: [(params) => isBlob(params.sticker), null],
createNewStickerSet: [
(params) =>
params.stickers.some((x) => "sticker" in x && isFile(x.sticker)),
params.stickers.some((x) => "sticker" in x && isBlob(x.sticker)),
[
{
name: "sticker",
Expand All @@ -110,7 +110,7 @@ export const MEDIA_METHODS: MethodsWithMediaUpload = {
},
],
],
addStickerToSet: [(params) => isFile(params.sticker.sticker), null],
replaceStickerInSet: [(params) => isFile(params.sticker.sticker), null],
setStickerSetThumbnail: [(params) => isFile(params.thumbnail), null],
addStickerToSet: [(params) => isBlob(params.sticker.sticker), null],
replaceStickerInSet: [(params) => isBlob(params.sticker.sticker), null],
setStickerSetThumbnail: [(params) => isBlob(params.thumbnail), null],
};

0 comments on commit 72aa765

Please sign in to comment.