bookworms-slack-webhook
Advanced tools
Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "bookworms-slack-webhook", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "This is a simple module to add a route into your server and have slack updates.", | ||
@@ -39,4 +39,5 @@ "main": "./index.js", | ||
"fastify-formbody": "^5.2.0", | ||
"fastify-plugin": "^3.0.1", | ||
"slackify-markdown": "^4.3.1" | ||
} | ||
} |
@@ -49,50 +49,13 @@ <p align="center"> | ||
```JavaScript | ||
import { expressWorms, fastWorms } from "bookworms-slack-webhook"; | ||
``` | ||
### Express | ||
There is a different export for the two webservers but they have the same interface. | ||
```JavaScript | ||
await expressWorms( | ||
app, | ||
path, | ||
route | ||
); | ||
await fastWorms( | ||
app, | ||
path, | ||
route | ||
); | ||
``` | ||
- app - _Required_ the instance of your webserver | ||
- path - _Required_ where the Bookworms bookmarks are coming from, this could be a local or remote `YAML` file. For more information see [Bookworms](https://github.com/thearegee/bookworms) | ||
- route - the path on your webservers hostname Slack will use as a webhook. This parameter is optional with a default value of: `/webhooks/slack/bookworms` | ||
### Parsing POST in Fastify | ||
Currently because of an issue raised here: [https://github.com/fastify/help/issues/642](https://github.com/fastify/help/issues/642) | ||
You need to ensure [`fastify-formbody`](https://github.com/fastify/fastify-formbody) is registered in your webserver. | ||
See the Fastify example for more details. | ||
### Examples | ||
Below are simple examples of building a simple webserver with a webhook for Slack to use. | ||
#### Express | ||
```JavaScript | ||
import express from "express"; | ||
import { expressWorms } from "bookworms-slack-webhook"; | ||
import { expressWorms } from "../../index.js"; | ||
const app = express(); | ||
const port = 3000; | ||
expressWorms( | ||
app, | ||
"https://raw.githubusercontent.com/thearegee/bookworms/main/demo/config/bookmarks.yaml" | ||
); | ||
await expressWorms(app, { | ||
path: "https://raw.githubusercontent.com/thearegee/bookworms/main/demo/config/bookmarks.yaml", | ||
}); | ||
@@ -108,7 +71,11 @@ app.get("/", (req, res) => { | ||
#### Fastify | ||
- app - _Required_ the instance of your express server | ||
- options - object of options for setting up Bookworms | ||
- path - _Required_ where the Bookworms bookmarks are coming from, this could be a local or remote `YAML` file. For more information see [Bookworms](https://github.com/thearegee/bookworms) | ||
- route - the path on your webservers hostname Slack will use as a webhook. This parameter is optional with a default value of: `/webhooks/slack/bookworms` | ||
### Fastify | ||
```JavaScript | ||
import Fastify from "fastify"; | ||
import fastifyForm from "fastify-formbody"; | ||
import { fastWorms } from "../../index.js"; | ||
@@ -118,9 +85,6 @@ const app = Fastify(); | ||
app.register(fastifyForm); | ||
app.register(fastWorms, { | ||
path: "https://raw.githubusercontent.com/thearegee/bookworms/main/demo/config/bookmarks.yaml", | ||
}); | ||
await fastWorms( | ||
app, | ||
"https://raw.githubusercontent.com/thearegee/bookworms/main/demo/config/bookmarks.yaml" | ||
); | ||
app.get("/", (request, reply) => { | ||
@@ -135,2 +99,6 @@ reply.send("Hello World!"); | ||
- options - object of options for setting up Bookworms | ||
- path - _Required_ where the Bookworms bookmarks are coming from, this could be a local or remote `YAML` file. For more information see [Bookworms](https://github.com/thearegee/bookworms) | ||
- route - the path on your webservers hostname Slack will use as a webhook. This parameter is optional with a default value of: `/webhooks/slack/bookworms` | ||
## Integrated with Slack | ||
@@ -137,0 +105,0 @@ |
import express from "express"; | ||
// including to see if we can handle if already added | ||
import bodyParser from "body-parser"; | ||
import { expressWorms } from "../../index.js"; | ||
@@ -8,9 +6,6 @@ const app = express(); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
await expressWorms(app, { | ||
path: "https://raw.githubusercontent.com/thearegee/bookworms/main/demo/config/bookmarks.yaml", | ||
}); | ||
await expressWorms( | ||
app, | ||
"https://raw.githubusercontent.com/thearegee/bookworms/main/demo/config/bookmarks.yaml" | ||
); | ||
app.get("/", (req, res) => { | ||
@@ -17,0 +12,0 @@ res.send("Hello World!"); |
@@ -9,8 +9,6 @@ import Fastify from "fastify"; | ||
app.register(fastifyForm); | ||
app.register(fastWorms, { | ||
path: "https://raw.githubusercontent.com/thearegee/bookworms/main/demo/config/bookmarks.yaml", | ||
}); | ||
await fastWorms( | ||
app, | ||
"https://raw.githubusercontent.com/thearegee/bookworms/main/demo/config/bookmarks.yaml" | ||
); | ||
app.get("/", (request, reply) => { | ||
@@ -17,0 +15,0 @@ reply.send("Hello World!"); |
@@ -5,7 +5,9 @@ import bodyParser from "body-parser"; | ||
export default async (server, path, route = defaultRoute) => { | ||
export default async (server, options) => { | ||
const urlencodedParser = bodyParser.urlencoded({ extended: false }); | ||
await init(path); | ||
options = options || {}; | ||
options.route = options.route ? options.route : defaultRoute; | ||
await init(options.path); | ||
server.post(route, urlencodedParser, (req, res) => { | ||
server.post(options.route, urlencodedParser, (req, res) => { | ||
const { body } = req; | ||
@@ -12,0 +14,0 @@ requestHandler(body, res); |
@@ -1,30 +0,25 @@ | ||
// import FastifyBodyParser from "fastify-formbody"; | ||
import fp from "fastify-plugin"; | ||
import FastifyBodyParser from "fastify-formbody"; | ||
import { init } from "../integrations/bookworms.js"; | ||
import { requestHandler, defaultRoute } from "../utilities/index.js"; | ||
export default async (server, path, route = defaultRoute) => { | ||
await init(path); | ||
async function fastWorms(server, options, next) { | ||
options = options || {}; | ||
options.route = options.route ? options.route : defaultRoute; | ||
await init(options.path); | ||
// Because of an issue, raised here: | ||
// https://github.com/fastify/help/issues/642 | ||
// you currently need to globally have fastify-formbody registered in the server | ||
if (!server.hasContentTypeParser("application/x-www-form-urlencoded")) { | ||
server.register(FastifyBodyParser); | ||
} | ||
// server.register((instance, options, next) => { | ||
// // This is a bit ugly but for now this might work | ||
// // checking to see if Fastify already has FastifyBodyParser | ||
// try { | ||
// instance.register(FastifyBodyParser).post(route, (req, res) => { | ||
// requestHandler(req.body, res); | ||
// }); | ||
// } catch (error) { | ||
// server.post(route, (req, res) => { | ||
// requestHandler(req.body, res); | ||
// }); | ||
// } | ||
// next(); | ||
// }); | ||
server.post(route, (req, res) => { | ||
server.post(options.route, (req, res) => { | ||
requestHandler(req.body, res); | ||
}); | ||
}; | ||
next(); | ||
} | ||
export default fp(await fastWorms, { | ||
fastify: ">=3", | ||
name: "fastworms", | ||
}); |
Sorry, the diff of this file is not supported yet
441570
5
168
124
+ Addedfastify-plugin@^3.0.1