Skip to content

Commit

Permalink
feat: add plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Mar 30, 2024
0 parents commit 3711f19
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 0 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Publish package

on: workflow_dispatch

permissions:
contents: write

jobs:
publish_package:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Bun
uses: oven-sh/setup-bun@v1

- id: changelog
name: Generate changelog
run: bun scripts/generate-changelog.ts

- name: Install modules
run: bun install

- name: Install Node
uses: actions/setup-node@v4
with:
node-version: "20.x"
registry-url: "https://registry.npmjs.org"
scope: "@gramio"

- name: Publish package
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: GitHub Release
uses: ncipollo/release-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
tag: v${{ steps.changelog.outputs.version }}
name: v${{ steps.changelog.outputs.version }}
body: ${{ steps.changelog.outputs.changelog }}
draft: false
prerelease: false
generateReleaseNotes: true
177 changes: 177 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store

test.ts
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @gramio/auto-retry

A plugin that catches errors with the retry_after field (rate limit errors), waits for the specified time and repeats the API request.

```ts
import { Bot } from "gramio";
import { autoRetry } from "@gramio/auto-retry";

const bot = new Bot(process.env.TOKEN!)
.extend(autoRetry())
.command("start", async (context) => {
for (let index = 0; index < 100; index++) {
await context.reply(`some ${index}`);
}
})
.onStart(console.log);

bot.start();
```
Binary file added bun.lockb
Binary file not shown.
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@gramio/auto-retry",
"version": "0.0.1",
"main": "dist/index.js",
"devDependencies": {
"@types/bun": "^1.0.12",
"typescript": "^5.4.3",
"gramio": "^0.0.25"
},
"peerDependencies": {
"gramio": "^0.0.25"
},
"description": "Auto-retry plugin for GramIO",
"files": [
"dist"
],
"homepage": "https://github.com/gramiojs/auto-retry",
"keywords": [
"gramio",
"gramio-plugin",
"auto-retry",
"api",
"telegram",
"telegram-bot",
"telegram-bot-api"
],
"readme": "https://gramio.netlify.app/",
"scripts": {
"prepublishOnly": "tsc && bunx tsc-alias"
},
"type": "commonjs"
}
36 changes: 36 additions & 0 deletions scripts/generate-changelog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { execSync } from "node:child_process";
import { randomUUID } from "node:crypto";
import { appendFileSync } from "node:fs";
import { EOL } from "node:os";

function getLatestTag() {
try {
return execSync("git describe --abbrev=0 --tags").toString().trim();
} catch (e) {
console.warn(e);
return execSync("git rev-list --max-parents=0 HEAD").toString().trim();
}
}

const commits = execSync(
`git log ${getLatestTag()}..HEAD --pretty="format:%s%b"`,
)
.toString()
.trim()
.split("\n")
.reverse();

console.log(getLatestTag(), commits);

const version = execSync("npm pkg get version").toString().replace(/"/gi, "");

const delimiter = `---${randomUUID()}---${EOL}`;

if (process.env.GITHUB_OUTPUT)
appendFileSync(
process.env.GITHUB_OUTPUT,
`changelog<<${delimiter}${commits.join(
EOL.repeat(2),
)}${EOL}${delimiter}version=${version}${EOL}`,
);
else console.log("Not github actions");
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Plugin } from "gramio";

export function autoRetry() {
return new Plugin("@gramio/auto-retry").onResponseError(
async (error, api) => {
if (error.payload?.retry_after) {
console.log(error.method, error.payload);
setTimeout(
//@ts-expect-error
() => api[error.method](error.params),
error.payload.retry_after * 1000,
);
}
},
);
}
21 changes: 21 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ES2022",
"module": "NodeNext",
"moduleDetection": "force",

/* Bundler mode */
"moduleResolution": "NodeNext",
/* Linting */
"skipLibCheck": true,
"strict": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"baseUrl": "./src",
"rootDir": "./src",
"outDir": "./dist",
},
"include": ["src"]
}

0 comments on commit 3711f19

Please sign in to comment.