0.10.21 • Published 1 year ago
@durbin2022/durbin-app-webhook v0.10.21
Durbin Technologies - App Webhooks
Do yarn add @durbin2022/durbin-app-webhook
and use!
API Docs
/* It all starts with this import */
import hooks from "@durbin2022/durbin-app-webhook";
/* For the API endpoints and Middlewares */
import express from "express";
const app = express();
/* Which kind of database you are planning to use?
* Currently we support only `typeorm` which in turn can use postgres, sql and even mongodb!
*/
hooks.use("typeorm");
/* There are certain parameters which are required for the proper functioning
* of the webhooks. You can use `.set(param, value)` for the same
*/
/* This express app will be finally used in turn with the webhooks.
* The `POST /authenticate` endpoint will be served via this express instance.
*/
hooks.set("express-app", app);
/* You need to set the billable units with which this app will work.
* These billable units will be required and accounts will read and give proper pricing amount for the same.
* The names need to unique.
*/
hooks.set("billable-units", [
{ name: "unit-name", description: "What is this unit about?" },
]);
/* When creating an app on the Admin Panel of Accounts, you will get a Client ID and
* Client Secret. The app needs to know them for proper internal functioning.
*/
hooks.set("client-id", process.env.CLIENT_ID);
hooks.set("client-secret", process.env.CLIENT_SECRET);
/* Optional: Webhook Path. This should match with accounts' knowledge else it won't work.
* Default is `/webhook`. It can be changed with this set parameter.
*/
hooks.set("webhook-path", "/hookmeup");
/* Bomb has been planted! Fills up Coke on Database and gets ready to serve!
*/
await hooks.arm();
/* Middlewares that durbin-app-webhook provides you
*/
/* `hooks.authenticated()` middleware will set the req.authenticated property on Request.
* It will now either be `false` or will have the `uuid` of the user using the app
*/
app.use(hooks.authenticated());
/* `hooks.limitOrUse("unit")` will use (by default) 1 unit of the user and will set it in the
* billing cycle for them. [Pre-requisite: hooks.authenticated() must run before this]
* If they have reached their limit, it will return a 400 response with "MONTHLY_QUOTA_REACHED" in the body.
*/
app.get("/use-unit", hooks.limitOrUse("unit-name"));
app.get("/custom-path", (req) => {
if (req.authenticated)
/* Used up (by default) 1 unit of user from their billing table
* If they have reached their limit, it will return `false`, else it will use the 1 unit and return `true`
*/
hooks.addTransaction(req.authenticated, "unit-name");
});
Webhook Paths for Accounts Integration
- For inserting/updating max credits of an account
POST /webhook/upsert-credits
Authorization: Bearer <token>
{
"uuid": "blah-blah-blah",
"max": [
{ "unit": "understandable-unit", "value": 180 },
{ "unit": "understandable-unit", "value": 200 }
]
}
- Every Billing Cycle - Get user's usage for Billing
POST /webhook/billing-cycle
Authorization: Bearer <token>
{
"uuid": "blah-blah-blah"
}
Returns the user's usage before resetting it to 0.
[
{ "unit": "understandable-unit", "value": 180 },
{ "unit": "understandable-unit", "value": 200 }
]
- Delete an User - Remove their Permission to use the app
POST /webhook/remove-user
Authorization: Bearer <token>
{
"uuid": "blah-blah-blah"
}
Returns the user's usage before deleting the records.
[
{ "unit": "understandable-unit", "value": 180 },
{ "unit": "understandable-unit", "value": 200 }
]