Skip to content

Commit

Permalink
fix: media manager, error response schema parsings and other minor fi…
Browse files Browse the repository at this point in the history
…xes (#28)
  • Loading branch information
sarthakjdev authored May 29, 2024
1 parent 533406d commit 6feec43
Show file tree
Hide file tree
Showing 34 changed files with 425 additions and 4,968 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ errors.log
.eslintrc.js
packages/**
apps/**
template/**
42 changes: 12 additions & 30 deletions .github/workflows/lint-and-build.yml → .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
name: Check Build
name: Build and Lint

on:
push:
branches-ignore:
- "master"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
lint-and-build:
build-and-lint:
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
Expand All @@ -26,6 +30,12 @@ jobs:
- name: Install Dependencies
run: pnpm install --frozen-lockfile

# need to build before lint check because packages is using interlinked workspace dependencies
- name: Build the packages
id: build
if: ${{ success() && !failure() }}
run: pnpm build

- name: Lint
id: lint
run: pnpm run lint
Expand All @@ -51,31 +61,3 @@ jobs:
} else {
throw new Error('Pull request data not found')
}
- name: Build the packages
id: build
if: ${{ success() && !failure() }}
run: pnpm build

- name: Build Failed
if: ${{ failure() && !steps.lint.outcome == 'failure' }}
uses: actions/github-script@v6
with:
script: |
const { data: pullRequestData } = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: context.payload.after
})
if(pullRequestData[0]){
await github.rest.issues.createComment({
issue_number: pullRequestData[0].number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `#### ❌ Build failed:
`
})
} else {
throw new Error('Pull request data not found')
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ cache
docs.json
docs.api.json
docs/**
temp/**
.DS_Store
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
#--------------------------------------------

# (Add your project-specific overrides here)
!/template/**
!/template/**
node_modules
Binary file added apps/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/create-wapi-app/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
template/
.eslintrc.js
.eslintrc.json
dist/
build/
node_modules/
4 changes: 0 additions & 4 deletions packages/create-wapi-app/.eslintrc.js

This file was deleted.

3 changes: 3 additions & 0 deletions packages/create-wapi-app/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@wapijs/eslint-config/config.node.js"]
}
4 changes: 1 addition & 3 deletions packages/create-wapi-app/src/create-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import {
import path from "node:path";
import process from "node:process";
import { URL } from "node:url";
import fastGlob from "fast-glob";
import { glob } from "fast-glob";
import chalk from "chalk";
import { installPackages } from "./reusable-function.js";
import { PackageManagerEnum } from "./type.js";

const glob = fastGlob.glob;

export async function createWhatsappBot(options: {
directory: string;
doInstallPackage: boolean;
Expand Down
46 changes: 24 additions & 22 deletions packages/create-wapi-app/template/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "[REPLACE_ME]",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"lint": "prettier --check . && eslint --ext .js,.mjs,.cjs --format=pretty src",
"format": "prettier --write . && eslint --ext .js,.mjs,.cjs --fix --format=pretty src",
"start": "node --require dotenv/config src/index.js",
"deploy": "node --require dotenv/config src/util/deploy.js"
},
"dependencies": {
"@wapijs/wapi.js": "^0.0.2",
"dotenv": "^16.3.1"
},
"devDependencies": {
"eslint": "^8.53.0",
"eslint-config-neon": "^0.1.57",
"eslint-formatter-pretty": "^5.0.0",
"prettier": "^3.1.0",
"zod": "^3.22.4"
}
"$schema": "https://json.schemastore.org/package.json",
"name": "my-whatsapp-bot",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"lint": "prettier --check . && eslint --ext .js,.mjs,.cjs --format=pretty src",
"format": "prettier --write . && eslint --ext .js,.mjs,.cjs --fix --format=pretty src",
"start": "pnpm build && node dist/index.js",
"build": "tsc -p ./tsconfig.json"
},
"dependencies": {
"@wapijs/wapi.js": "^0.0.7",
"dotenv": "^16.3.1"
},
"devDependencies": {
"@types/node": "^20.5.9",
"eslint": "^8.53.0",
"eslint-config-neon": "^0.1.57",
"eslint-formatter-pretty": "^5.0.0",
"prettier": "^3.1.0",
"typescript": "5.4.5",
"zod": "^3.22.4"
}
}
4 changes: 2 additions & 2 deletions packages/create-wapi-app/template/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { whatsappClient } from "./utils/client.js"
import { TextMessage } from '@wapijs/wapi.js'
import { TextMessage, type WapiEventDataMap } from '@wapijs/wapi.js'
import { readdir } from 'node:fs/promises'


async function loadEventListeners() {
const events = await readdir(`${__dirname}/events/`)
events
.filter((file) => file.endsWith('.js'))
.map((file) => whatsappClient.on(file.split('.js')[0], require(`${__dirname}/events/${file}`).default))
.map((file) => whatsappClient.on(file.split('.js')[0] as keyof WapiEventDataMap, require(`${__dirname}/events/${file}`).default))
}

async function init() {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-wapi-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "@wapijs/typescript-config/node.base.json",
"include": ["src/", "*.ts", "./.eslintrc.js", "bin"],
"include": ["src/", "*.ts", ".eslintrc.js", "bin"],
"exclude": ["template/"],
"compilerOptions": {
"module": "NodeNext",
Expand Down
14 changes: 7 additions & 7 deletions packages/example-chat-bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ function init() {
],
'Help & Support': [
{
question:
'Is wapi.js free and open-source? qwerrqwrf3r3r32r23r23r23r23r2rqrqrerere23re23r23re',
question: 'Is wapi.js free and open-source?',
answer: "Yes, it's completely free and open-source under the Apache 2.0 License."
},
{
Expand Down Expand Up @@ -77,13 +76,18 @@ function init() {
})
})

whatsappClient.on('Error', error => {
console.log('Error', error.message)
})

whatsappClient.on('TextMessage', async message => {
console.log('Text Message')
if (message.text.data.text.toLowerCase() === 'hello') {
await message.client.message.send({
const response = await message.client.message.send({
message: listMessage,
phoneNumber: message.context.from
})
console.log({ response })
} else {
await message.reply({
message: new TextMessage({
Expand Down Expand Up @@ -112,10 +116,6 @@ function init() {
})
})

whatsappClient.on('Error', error => {
console.error(error)
})

whatsappClient.initiate()
}

Expand Down
Binary file added packages/example-chat-bot/src/media.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions packages/wapi.js/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './src/client/index'
export * from './src/manager'
export * from './src/structures'
export * from './src/webhook/events/audio/interface'
export * from './src/webhook/events/audio/index'
Expand Down Expand Up @@ -43,6 +42,4 @@ export * from './src/webhook/events/unknown/interface'
export * from './src/webhook/events/unknown/index'
export * from './src/webhook/events/video/interface'
export * from './src/webhook/events/video/index'
export * from './src/webhook/index'
export * from './src/webhook/schema'
export * from './src/webhook/type'
9 changes: 0 additions & 9 deletions packages/wapi.js/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { EventEmitter } from "node:events";

import { Webhook } from "../webhook";
import { PhoneNumberManager } from "../manager/phone";
import { MediaManager } from "../manager/media";
import { RequestClient } from "./request-client";
import { ClientStatusEnum, type ClientInterface } from "./interface";
Expand All @@ -15,13 +14,6 @@ import { type WapiEventDataMap } from "../webhook/type";
* @class Client
*/
export class Client extends EventEmitter implements ClientInterface {
/**
* Phone number manager to verify phone numbers for your
* @type {PhoneNumberManager}
* @memberof Client
*/
phone: PhoneNumberManager;

/**
* Media manager to upload, get and media via whatsapp cloud api
* @type {MediaManager}
Expand Down Expand Up @@ -110,7 +102,6 @@ export class Client extends EventEmitter implements ClientInterface {
webhookEndpoint: params.webhookEndpoint,
port: params.port,
});
this.phone = new PhoneNumberManager({ client: this });
this.media = new MediaManager({
client: this,
});
Expand Down
17 changes: 6 additions & 11 deletions packages/wapi.js/src/client/interface.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { type WapiEventDataMap } from "../webhook/type";
import { type PhoneNumberManager } from "../manager/phone";
import { type MediaManager } from "../manager/media";
import { type Webhook } from "../webhook";
import { type MessageManager } from "../manager/message";
import { type Client } from "./index";
import { type RequestClient } from "./request-client";
import { type z } from "zod";
import { type WapiMessageResponseSchemaType } from "./schema";
import { type CloudApiRequesterResourceTypeToResponseTypeMap } from "./schema";

/**
* Represents the interface for the Wapi client.
* @interface ClientInterface
*/
export interface ClientInterface {
/**
* The phone number manager.
*/
phone: PhoneNumberManager;
/**
* The media manager.
*/
Expand All @@ -40,7 +33,7 @@ export interface ClientInterface {
/**
* The request client.
*/
requester: RequestClient;
requester: RequestClientInterface;
/**
* Emits an event with the specified event name and data.
* @param {T} eventName - The name of the event.
Expand Down Expand Up @@ -113,11 +106,13 @@ export interface RequestClientInterface {
* @param {'GET' | 'POST' | 'DELETE'} params.method - The HTTP method of the request.
* @returns Returns a promise that resolves when the request is completed.
*/
requestCloudApi(params: {
requestCloudApi<
T extends keyof CloudApiRequesterResourceTypeToResponseTypeMap,
>(params: {
path: string;
body: string;
method: "GET" | "POST" | "DELETE";
}): Promise<z.infer<typeof WapiMessageResponseSchemaType>>;
}): Promise<CloudApiRequesterResourceTypeToResponseTypeMap[T]>;
}

/**
Expand Down
Loading

0 comments on commit 6feec43

Please sign in to comment.