Skip to content

Commit 514db80

Browse files
committed
init
0 parents  commit 514db80

File tree

12 files changed

+491
-0
lines changed

12 files changed

+491
-0
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: CI workflow
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
test:
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
matrix:
16+
node-version: [10.x, 12.x, 14.x]
17+
os: [ubuntu-latest, windows-latest]
18+
steps:
19+
- uses: actions/checkout@v2
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v1
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
- name: Install Dependencies
25+
run: npm install --ignore-scripts
26+
- name: Test
27+
run: npm run test

.gitignore

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Docusaurus cache and generated files
108+
.docusaurus
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
# TernJS port file
120+
.tern-port
121+
122+
# Stores VSCode versions used for testing VSCode extensions
123+
.vscode-test
124+
125+
# yarn v2
126+
.yarn/cache
127+
.yarn/unplugged
128+
.yarn/build-state.yml
129+
.yarn/install-state.gz
130+
.pnp.*
131+
132+
# Vim swap files
133+
*.swp
134+
135+
# macOS files
136+
.DS_Store
137+
138+
# Clinic
139+
.clinic
140+
141+
# lock files
142+
bun.lockb
143+
package-lock.json
144+
pnpm-lock.yaml
145+
yarn.lock
146+
147+
# editor files
148+
.vscode
149+
.idea
150+
151+
#tap files
152+
.tap/
153+
154+
# test tap report
155+
out.tap

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.taprc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ts: false
2+
jsx: false
3+
coverage: false
4+
files:
5+
- test/**/*.test.js

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# fastify-grammy
2+
3+
![CI](https://github.com/blasdfaa/fastify-grammy/workflows/CI/badge.svg)
4+
[![NPM version](https://img.shields.io/npm/v/fastify-grammy.svg?style=flat)](https://www.npmjs.com/package/fastify-grammy)
5+
6+
## 🚧 In development
7+
8+
This library is a work in progress and in active development.
9+
10+
Supports Fastify versions `5.x`
11+
12+
## Install
13+
14+
```
15+
npm i fastify-grammy
16+
```
17+
18+
## Usage
19+
20+
Require `fastify-grammy` and register.
21+
22+
```js
23+
const fastify = require('fastify')()
24+
25+
fastify.register(require('fastify-grammy'), {
26+
token: 'your-tg-bot-token'
27+
})
28+
29+
fastify.listen({ port: 3000 })
30+
```
31+
32+
## Acknowledgements
33+
34+
## License
35+
36+
Licensed under [MIT](./LICENSE).<br/>

errors.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { createError } = require('@fastify/error')
2+
3+
const MissingBotTokenError = createError(
4+
'FST_ERR_GRAMMY_MISSING_BOT_TOKEN',
5+
'Missing bot token'
6+
)
7+
const NameAlreadyRegisterError = createError(
8+
'FST_ERR_GRAMMY_NAME_ALREADY_REGISTER',
9+
'\'%s\' instance name has already been registered'
10+
)
11+
const AlreadyRegisterError = createError(
12+
'FST_ERR_GRAMMY_ALREADY_REGISTER',
13+
'bot has already been registered'
14+
)
15+
16+
module.exports = {
17+
MissingBotTokenError,
18+
NameAlreadyRegisterError,
19+
AlreadyRegisterError
20+
}

index.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { FastifyPluginCallback } from 'fastify'
2+
import { Bot } from 'grammy'
3+
4+
declare module 'fastify' {
5+
export interface FastifyInstance {
6+
grammy: Bot
7+
}
8+
}
9+
10+
type FastifyGrammy = FastifyPluginCallback<fastifyGrammy.FastifyGrammyOptions>
11+
12+
declare namespace fastifyGrammy {
13+
export interface FastifyGrammyOptions {
14+
token: string
15+
name?: string
16+
}
17+
18+
export const fastifyGrammy: FastifyGrammy
19+
export { FastifyGrammy as default }
20+
}
21+
22+
declare function fastifyGrammy (...params: Parameters<FastifyGrammy>): ReturnType<FastifyGrammy>
23+
export = fastifyGrammy

index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
const fp = require('fastify-plugin')
4+
const { Bot } = require('grammy')
5+
const { NameAlreadyRegisterError, AlreadyRegisterError, MissingBotTokenError } = require('./errors')
6+
7+
function fastifyGrammy (fastify, options, next) {
8+
const { name, token } = options
9+
10+
if (!token) {
11+
return next(new MissingBotTokenError())
12+
}
13+
14+
const bot = new Bot(token)
15+
16+
if (name) {
17+
if (!fastify.grammy) {
18+
fastify.decorate('grammy', {})
19+
}
20+
21+
if (fastify.grammy[name]) {
22+
return next(new NameAlreadyRegisterError(name))
23+
}
24+
25+
fastify.grammy[name] = bot
26+
} else {
27+
if (fastify.grammy) {
28+
return next(new AlreadyRegisterError())
29+
} else {
30+
fastify.decorate('grammy', bot)
31+
}
32+
}
33+
34+
fastify.addHook('onClose', () => bot.stop())
35+
36+
next()
37+
}
38+
39+
module.exports = fp(fastifyGrammy, {
40+
fastify: '5.x',
41+
name: 'fastify-grammy'
42+
})
43+
module.exports.default = fastifyGrammy
44+
module.exports.fastifyGrammy = fastifyGrammy

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "fastify-grammy",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "npm run lint && npm run unit",
11+
"lint": "standard && npm run lint:typescript",
12+
"lint:typescript": "ts-standard",
13+
"test:unit": "tap",
14+
"unit": "node --test",
15+
"release": "npm run test && changelogen --release && npm publish && git push --follow-tags"
16+
},
17+
"keywords": [],
18+
"author": "",
19+
"license": "MIT",
20+
"types": "index.d.ts",
21+
"dependencies": {
22+
"@fastify/error": "^4.0.0",
23+
"fastify-plugin": "^4.0.0",
24+
"grammy": "^1.31.0"
25+
},
26+
"devDependencies": {
27+
"@types/node": "^20.4.4",
28+
"changelogen": "^0.5.7",
29+
"fastify": "^5.0.0",
30+
"fastify-tsconfig": "^2.0.0",
31+
"standard": "^17.0.0",
32+
"tap": "^19.2.5",
33+
"ts-standard": "^12.0.1",
34+
"tsd": "^0.30.4",
35+
"typescript": "5.2.2"
36+
},
37+
"tsd": {
38+
"directory": "test"
39+
}
40+
}

test/helper.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fp = require('fastify-plugin')
2+
const fastifyGrammy = require('..')
3+
const Fastify = require('fastify')
4+
const assert = require('node:assert')
5+
6+
async function register (t, options = {}) {
7+
const app = Fastify()
8+
t.after(() => app.close())
9+
10+
await app.register(fp(fastifyGrammy), options)
11+
12+
const ready = await app.ready()
13+
assert.ok(ready)
14+
15+
return app
16+
}
17+
18+
module.exports = { register }

0 commit comments

Comments
 (0)