@gramio/files
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,4 +0,20 @@ | ||
export * from "./utils"; | ||
export * from "./media-methods-helper"; | ||
export * from "./media-input"; | ||
export * from "./media-upload"; | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./utils"), exports); | ||
__exportStar(require("./media-methods-helper"), exports); | ||
__exportStar(require("./media-input"), exports); | ||
__exportStar(require("./media-upload"), exports); |
@@ -0,3 +1,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MediaInput = void 0; | ||
/** Class-helper with static methods that represents the content of a media message to be sent. */ | ||
export class MediaInput { | ||
class MediaInput { | ||
/** | ||
@@ -64,1 +67,2 @@ * Represents an animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent. | ||
} | ||
exports.MediaInput = MediaInput; |
import { ApiMethods, TelegramInputFile } from "@gramio/types"; | ||
export type Extractor = { | ||
name: string; | ||
type: "array" | "union"; | ||
property: string; | ||
}; | ||
type MethodsWithMediaUpload = { | ||
[Method in keyof ApiMethods]?: [ | ||
(params: NonNullable<Parameters<ApiMethods[Method]>[0]>) => boolean, | ||
{ | ||
name: string; | ||
type?: "array" | "union"; | ||
property?: string; | ||
}[] | null | ||
Extractor[] | null | ||
]; | ||
@@ -11,0 +12,0 @@ }; |
@@ -1,2 +0,5 @@ | ||
export function isFile(file) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MEDIA_METHODS = exports.isFile = void 0; | ||
function isFile(file) { | ||
if (!file || typeof file !== "object") | ||
@@ -6,4 +9,5 @@ return false; | ||
} | ||
exports.isFile = isFile; | ||
/** @codegenerated */ | ||
export const MEDIA_METHODS = { | ||
exports.MEDIA_METHODS = { | ||
setWebhook: [(params) => isFile(params.certificate), null], | ||
@@ -10,0 +14,0 @@ sendPhoto: [(params) => isFile(params.photo), null], |
@@ -1,6 +0,12 @@ | ||
import fs from "node:fs/promises"; | ||
import { basename } from "node:path"; | ||
import { convertStreamToBuffer } from "./utils"; | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MediaUpload = void 0; | ||
const promises_1 = __importDefault(require("node:fs/promises")); | ||
const node_path_1 = require("node:path"); | ||
const utils_1 = require("./utils"); | ||
/** Class-helper with static methods for file uploading. */ | ||
export class MediaUpload { | ||
class MediaUpload { | ||
/** | ||
@@ -10,4 +16,4 @@ * Method for uploading Media File by local path. | ||
static async path(path, filename) { | ||
const buffer = await fs.readFile(path); | ||
return new File([buffer], filename ?? basename(path)); | ||
const buffer = await promises_1.default.readFile(path); | ||
return new File([buffer], filename ?? (0, node_path_1.basename)(path)); | ||
} | ||
@@ -18,3 +24,3 @@ /** | ||
static async stream(stream, filename = "file.stream") { | ||
const buffer = await convertStreamToBuffer(stream); | ||
const buffer = await (0, utils_1.convertStreamToBuffer)(stream); | ||
return new File([buffer], filename); | ||
@@ -35,3 +41,3 @@ } | ||
return new File([buffer], filename ?? | ||
(typeof url === "string" ? basename(url) : basename(url.pathname))); | ||
(typeof url === "string" ? (0, node_path_1.basename)(url) : (0, node_path_1.basename)(url.pathname))); | ||
} | ||
@@ -45,1 +51,2 @@ /** | ||
} | ||
exports.MediaUpload = MediaUpload; |
@@ -1,16 +0,23 @@ | ||
import { randomBytes } from "node:crypto"; | ||
import { MEDIA_METHODS } from "./media-methods-helper"; | ||
export function isMediaUpload(method, params) { | ||
const mediaMethod = MEDIA_METHODS[method]; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.convertStreamToBuffer = exports.convertJsonToFormData = exports.isMediaUpload = void 0; | ||
const node_crypto_1 = require("node:crypto"); | ||
const media_methods_helper_1 = require("./media-methods-helper"); | ||
function isMediaUpload(method, params) { | ||
const mediaMethod = media_methods_helper_1.MEDIA_METHODS[method]; | ||
if (!mediaMethod) | ||
return false; | ||
// Check is params has File??? | ||
// [INFO] Check is params has File??? | ||
return mediaMethod[0](params); | ||
} | ||
exports.isMediaUpload = isMediaUpload; | ||
function generateAttachId() { | ||
return randomBytes(12).toString("hex"); | ||
return (0, node_crypto_1.randomBytes)(12).toString("hex"); | ||
} | ||
export async function convertJsonToFormData(method, params) { | ||
function isExtractor(value, type, params) { | ||
return value.type === type; | ||
} | ||
async function convertJsonToFormData(method, params) { | ||
const formData = new FormData(); | ||
const mediaMethod = MEDIA_METHODS[method]; | ||
const mediaMethod = media_methods_helper_1.MEDIA_METHODS[method]; | ||
if (!mediaMethod) | ||
@@ -20,5 +27,3 @@ throw new Error("no media method"); | ||
for (const extractorValue of extractor) { | ||
if (extractorValue.type === "union" && extractorValue.property) { | ||
// Элемент неявно имеет тип "any", так как выражение типа "string" не может использоваться для индексации типа | ||
//@ts-expect-error | ||
if (isExtractor(extractorValue, "union", params)) { | ||
let file = params[extractorValue.property][extractorValue.name]; | ||
@@ -31,8 +36,6 @@ if (file instanceof Promise) | ||
formData.set(attachId, file); | ||
//@ts-expect-error | ||
params[extractorValue.property][extractorValue.name] = | ||
`attach://${attachId}`; | ||
} | ||
if (extractorValue.type === "array" && extractorValue.property) { | ||
//@ts-expect-error | ||
if (isExtractor(extractorValue, "array", params)) { | ||
const array = params[extractorValue.property]; | ||
@@ -43,3 +46,2 @@ for (const [index, element] of array.entries()) { | ||
file = await file; | ||
console.log(file); | ||
if (!(file instanceof Blob)) | ||
@@ -49,3 +51,2 @@ continue; | ||
formData.set(attachId, file); | ||
//@ts-expect-error | ||
params[extractorValue.property][index][extractorValue.name] = | ||
@@ -68,3 +69,4 @@ `attach://${attachId}`; | ||
} | ||
export function convertStreamToBuffer(stream) { | ||
exports.convertJsonToFormData = convertJsonToFormData; | ||
function convertStreamToBuffer(stream) { | ||
return new Promise((resolve) => { | ||
@@ -76,1 +78,2 @@ const chunks = []; | ||
} | ||
exports.convertStreamToBuffer = convertStreamToBuffer; |
{ | ||
"name": "@gramio/files", | ||
"main": "./dist/index.js", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"type": "commonjs", | ||
"description": "Library for uploading files to Telegram and etc with files", | ||
"keywords": [ | ||
"gramio", | ||
"telegram", | ||
"file", | ||
"files", | ||
"upload" | ||
], | ||
"scripts": { | ||
@@ -6,0 +15,0 @@ "generate": "bun scripts/generate.ts", |
# @gramio/files | ||
[`@gramio/files`](https://github.com/gramiojs/files) is built-in GramIO plugin. You can also use it outside of this framework because it is framework-agnostic. | ||
## Please see [Documentation](https://gramio.netlify.app/files/overview.html) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15182
377
6
1