diff --git a/.env.example b/.env.example index 8a9b1f05..3c6ed263 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,8 @@ -HOST="localhost" -PORT="3000" -DATABASE_URL="mysql://root:12345@localhost:3306/baileys_api" -RECONNECT_INTERVAL="5000" -MAX_RECONNECT_RETRIES="5" -SSE_MAX_QR_GENERATION="10" -LOG_LEVEL="warn" \ No newline at end of file +NODE_ENV=development +LOG_LEVEL=debug +DATABASE_URL=postgres://user:password@localhost:5432/baileys_api +PORT=3000 +MAX_RECONNECT_ATTEMPTS=5 +RECONNECT_INTERVAL=5000 +MAX_QR_ATTEMPTS=5 +PAIR_CODE_TIMEOUT=600000 \ No newline at end of file diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index b9470778..00000000 --- a/.eslintignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules/ -dist/ diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 6c7e090d..00000000 --- a/.eslintrc.js +++ /dev/null @@ -1,14 +0,0 @@ -module.exports = { - root: true, - env: { - node: true, - }, - parser: '@typescript-eslint/parser', - plugins: ['@typescript-eslint'], - extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'], - rules: { - '@typescript-eslint/consistent-type-imports': 'error', - '@typescript-eslint/no-unused-vars': 'error', - '@typescript-eslint/no-non-null-assertion': 'off', - }, -}; diff --git a/.gitignore b/.gitignore index 64a35588..a14702c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,34 @@ -node_modules/ -dist/ -debug/ -prisma/migrations/ +# dependencies (bun install) +node_modules + +# output +out +dist +*.tgz + +# code coverage +coverage +*.lcov + +# logs +logs +_.log +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# dotenv environment variable files .env -*.tgz \ No newline at end of file +.env.development.local +.env.test.local +.env.production.local +.env.local + +# caches +.eslintcache +.cache +*.tsbuildinfo + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/.prettierignore b/.prettierignore deleted file mode 100644 index ecc79b0a..00000000 --- a/.prettierignore +++ /dev/null @@ -1,3 +0,0 @@ -node_modules/ -dist/ -debug/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..e1b90940 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,19 @@ +{ + "[javascript]": { + "editor.defaultFormatter": "biomejs.biome" + }, + "[typescript]": { + "editor.defaultFormatter": "biomejs.biome" + }, + "[json]": { + "editor.defaultFormatter": "biomejs.biome" + }, + "[jsonc]": { + "editor.defaultFormatter": "biomejs.biome" + }, + "editor.formatOnSave": true, + "editor.codeActionsOnSave": { + "source.fixAll.biome": "always", + "source.organizeImports.biome": "always" + } +} diff --git a/README.md b/README.md index 0a723fc3..8d98c5d9 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ -> 🚨 **NOTICE**: `@adiwajshing/baileys` which is this project relied on, has been discontinued. Thus, this project will be archived and stopped receiving updates anymore. Thanks everyone who's been part of this❤️ +> 🚨 **NOTICE**: This project status is still WIP --- # Baileys API -An implementation of [@adiwajshing/Baileys](https://github.com/adiwajshing/Baileys) as a simple REST API with multiple device support +An implementation of [baileys](https://github.com/WhiskeySockets/baileys) as REST API that supports managing multiple connections at once ## Requirements -- **NodeJS** version **14.5.0** or higher -- **Prisma** [supported databases](https://www.prisma.io/docs/reference/database-reference/supported-databases). Tested on MySQL and PostgreSQL +- Latest version of **NodeJS v20** or any higher major versions +- **PostgreSQL** database. It's possible to use other databases, but you might've to update the **drizzle** schema and adjust some part of the codes ## Installation @@ -31,66 +31,61 @@ You can skip this part if you're using the prebuilt one from the release page ## Setup -1. Copy the `.env.example` file and rename it into `.env`, then update your [connection url](https://www.prisma.io/docs/reference/database-reference/connection-urls) in the `DATABASE_URL` field -1. Update your [provider](https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#fields) in the `prisma/schema.prisma` file if you're using database other than MySQL -1. Run your [migration](https://www.prisma.io/docs/reference/api-reference/command-reference#prisma-migrate) +1. Copy the `.env.example` file and rename it into `.env`, then update your connection url in the `DATABASE_URL` field +1. Run the migration ```sh -npx prisma migrate (dev|deploy) +npm run db:migrate ``` or push the schema ```sh -npx prisma db push +npm run db:push ``` -Don't forget to always re-run those whenever there's a change on the `prisma/schema.prisma` file +Don't forget to always re-run those whenever there's a change on the `migrations/` directory ## `.env` Configurations ```env -# Listening Host -HOST="localhost" - -# Listening Port -PORT="3000" - -# Database Connection URL -DATABASE_URL="mysql://root:12345@localhost:3306/baileys_api" - -# Reconnect Interval (in Milliseconds) -RECONNECT_INTERVAL="5000" - -# Maximum Reconnect Attempts -MAX_RECONNECT_RETRIES="5" - -# Maximum SSE QR Generation Attempts -SSE_MAX_QR_GENERATION="10" - -# Pino Logger Level -LOG_LEVEL="warn" +# Environment +NODE_ENV=development +# Pino log level +LOG_LEVEL=debug +# Database connection url +DATABASE_URL=postgres://user:password@localhost:5432/baileys_api +# App port +PORT=3000 +# Max reconnect attempts before a connection is destroyed +MAX_RECONNECT_ATTEMPTS=5 +# Interval for each reconnect, this uses exponential backoff +RECONNECT_INTERVAL=5000 +# Max qr generation attempts before a connection is destroyed +MAX_QR_ATTEMPTS=5 +# Timeout for authenticating using pair code before a connection is destroyed +PAIR_CODE_TIMEOUT=600000 ``` ## Usage 1. Make sure you have completed the **Installation** and **Setup** step -1. You can then start the app using the `start` script +2. You can then start the app using the `start` script ```sh npm run start ``` -1. Now the endpoint should be available according to your environment variables configuration. Default is at `http://localhost:3000` +3. Now the endpoint should be available according to your environment variables configuration. Default is at `http://localhost:3000` ## API Docs -The API documentation is available online [here](https://documenter.getpostman.com/view/18988925/2s8Z73zWbg). You can also import the **Postman Collection File** `(postman_collection.json)` into your Postman App alternatively +The API follow OpenAPI v3.0 spec that's accessible at `/doc`. Additionally, a SwaggerUI is also available at `/ui` ## Notes -- There's no authentication, you may want to implement your own. I don't want to force anyone into using a specific authentication method, choose whatever you love +- There's no default authentication, you may want to implement your own. There's no opinionated way for authentication, use whatever suits your need ## Notice -This project is intended for learning purpose only, don't use it for spamming or any activities that's prohibited by **WhatsApp** +This project is not affiliated in any way, and is not intended for spamming or any activities that's prohibited by **WhatsApp** \ No newline at end of file diff --git a/biome.json b/biome.json new file mode 100644 index 00000000..0d9ee963 --- /dev/null +++ b/biome.json @@ -0,0 +1,40 @@ +{ + "$schema": "https://biomejs.dev/schemas/2.1.2/schema.json", + "vcs": { + "enabled": true, + "clientKind": "git", + "useIgnoreFile": true + }, + "files": { + "ignoreUnknown": false + }, + "formatter": { + "enabled": true, + "indentStyle": "tab" + }, + "linter": { + "enabled": true, + "rules": { + "recommended": true, + "correctness": { + "noUnusedImports": "warn" + }, + "style": { + "noNonNullAssertion": "off" + } + } + }, + "javascript": { + "formatter": { + "quoteStyle": "double" + } + }, + "assist": { + "enabled": true, + "actions": { + "source": { + "organizeImports": "on" + } + } + } +} diff --git a/drizzle.config.ts b/drizzle.config.ts new file mode 100644 index 00000000..c1976927 --- /dev/null +++ b/drizzle.config.ts @@ -0,0 +1,10 @@ +import { defineConfig } from "drizzle-kit"; +import { env } from "./src/lib/env"; + +export default defineConfig({ + dialect: "postgresql", + dbCredentials: { url: env.DATABASE_URL }, + out: "./migrations/", + schema: "./src/lib/db/schema.ts", + casing: "snake_case", +}); diff --git a/examples/sse.html b/examples/sse.html index 80172ab1..1bc020fe 100644 --- a/examples/sse.html +++ b/examples/sse.html @@ -1,24 +1,157 @@ - -
- - - -No token available
+