npm package: @giorginakh/my-utils
Description: A tiny utility package in TypeScript
Author: Giorgi Nakhutsrishvili
Latest version: 1.0.3

# Utility Functions Documentation

This module provides utility functions for sending webhooks, sleeping for a delay, selecting random proxies, and basic greetings. It's designed to be used in applications that handle webhook interactions and proxy routing.

## Table of Contents

- [greet](#greet)
- [sleep](#sleep)
- [getRandomProxy](#getrandomproxy)
- [sendWebhook](#sendwebhook)
- [sendWebhookWithForm](#sendwebhookwithform)

---

## `greet(name: string): string`

Returns a greeting message.

### Parameters:

- `name` _(string)_: The name of the person to greet.

### Returns:

- `string`: A message in the format `"Hello, {name}!"`.

### Example:

```ts
import { greet } from "./utils";

console.log(greet("Alice")); // Hello, Alice!
```

---

## `sleep(ms: number): Promise<void>`

Delays execution for a specified number of milliseconds.

### Parameters:

- `ms` _(number)_: The time to sleep in milliseconds.

### Returns:

- `Promise<void>`: Resolves after the given time.

### Example:

```ts
import { sleep } from "./utils";

async function delayedAction() {
	console.log("Waiting...");
	await sleep(2000);
	console.log("Done waiting.");
}
```

---

## `getRandomProxy(proxies: string[]): string`

Selects a random proxy string from an array.

### Parameters:

- `proxies` _(string\[])_: An array of proxy strings.

### Returns:

- `string`: A randomly selected proxy.

### Example:

```ts
import { getRandomProxy } from "./utils";

const proxyList = ["http://proxy1.com", "http://proxy2.com"];
const selectedProxy = getRandomProxy(proxyList);
console.log(`Using proxy: ${selectedProxy}`);
```

---

## `sendWebhook(webhookUrl: string, webhookData: {}, proxyAgent?: any): Promise<void>`

Sends a JSON payload to a specified webhook URL. Retries once after 2 seconds if the error is not a 400.

### Parameters:

- `webhookUrl` _(string)_: The URL to send the webhook to.
- `webhookData` _(object)_: The data payload to send.
- `proxyAgent` _(optional)_: An optional HTTP/HTTPS proxy agent.

### Example:

```ts
import { sendWebhook } from "./utils";

const url = "https://discord.com/api/webhooks/...";
const data = {
	content: "Hello from bot!",
};

sendWebhook(url, data);
```

---

## `sendWebhookWithForm(webhookUrl: string, form: any, proxyAgent?: any): Promise<void>`

Sends multipart/form-data (e.g., with files) to a webhook URL. Retries once after 2 seconds if the error is not a 400.

### Parameters:

- `webhookUrl` _(string)_: The URL to send the webhook to.
- `form` _(any)_: The `form-data` instance to send.
- `proxyAgent` _(optional)_: An optional HTTP/HTTPS proxy agent.

### Example:

```ts
import FormData from "form-data";
import fs from "fs";
import { sendWebhookWithForm } from "./utils";

const form = new FormData();
form.append("file", fs.createReadStream("image.jpg"));
form.append("payload_json", JSON.stringify({ content: "Here’s an image" }));

const webhookUrl = "https://discord.com/api/webhooks/...";
sendWebhookWithForm(webhookUrl, form);
```

---

### Notes:

- Proxy support is provided through optional `httpAgent` and `httpsAgent`.
- Functions are designed with basic retry logic for robustness.

---