Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthakjdev committed Oct 22, 2023
1 parent c64e4e3 commit 72d6af7
Show file tree
Hide file tree
Showing 26 changed files with 4,604 additions and 4,233 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = {
],
"no-empty-pattern": "off",
"no-inner-declarations": "off",
"no-console": "error",
"no-console": "off",
"no-debugger": "error",
"no-alert": "error",
"no-useless-escape": "error",
Expand Down
13 changes: 9 additions & 4 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ jobs:
- name: User Node.js LTS
uses: actions/setup-node@v2

- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
with:
run_install: false

- name: Install Dependencies
run: yarn install --frozen-lockfile
run: pnpm install --frozen-lockfile

- name: Lint
run: yarn run lint
run: pnpm run lint

- name: Linter Failed
if: ${{ failure() }}
Expand Down Expand Up @@ -52,10 +57,10 @@ jobs:
uses: actions/checkout@v3

- name: Install Dependencies
run: yarn install --frozen-lockfile
run: pnpm install --frozen-lockfile

- name: Build the packages
run: yarn build
run: pnpm build

- name: Build Success
if: ${{ success() }}
Expand Down
13 changes: 9 additions & 4 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ jobs:
- name: User Node.js LTS
uses: actions/setup-node@v2

- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
with:
run_install: false

- name: Install Dependencies
run: yarn install --frozen-lockfile
run: pnpm install --frozen-lockfile

- name: Lint
run: yarn run lint
run: pnpm run lint

- name: Linter Failed
if: ${{ failure() }}
Expand Down Expand Up @@ -52,10 +57,10 @@ jobs:
uses: actions/checkout@v3

- name: Install Dependencies
run: yarn install --frozen-lockfile
run: pnpm install --frozen-lockfile

- name: Build the packages
run: yarn build
run: pnpm build

- name: Build Success
if: ${{ success() }}
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/validate-pr-title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ on:
- review_requested
- auto_merge_enabled

# Ensures that only one deploy task per branch/environment will run at a time!
concurrency:
group: check-pr-title-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
validatePrTitle:
validate-pr-title:
name: Validate PR title
runs-on: ubuntu-latest
permissions:
Expand Down
15 changes: 0 additions & 15 deletions .github/workflows/yarn-lock-changes.yml

This file was deleted.

20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ For reference visit WhatsApp Docs [here](https://developers.facebook.com/docs/wh
## Scripts:

```
yarn run build : To build the project
yarn run watch: To compile the project in watch mode or transpile the project
yarn run clean-install : To clean install the modules removing the previous ones
yarn run lint : TO check for the eslint errors
yarn run lint:fix : To fix the fixable eslint errors
pnpm run build : To build the project
pnpm run watch: To compile the project in watch mode or transpile the project
pnpm run clean-install : To clean install the modules removing the previous ones
pnpm run lint : TO check for the eslint errors
pnpm run lint:fix : To fix the fixable eslint errors
```

<br/><hr/><br/>
Expand Down Expand Up @@ -107,13 +107,13 @@ Refer to this [Contributing.md](./CONTRIBUTING.md) if you have any difficulty in
- After cloning the project in your local, run the following command to install the dependencies required by the project:

```sh
yarn install --frozen-lockfile
pnpm install --frozen-lockfile
```

- Build the project using the following command:

```sh
yarn build
pnpm build
```

<br/><hr/><br/>
Expand All @@ -125,11 +125,11 @@ yarn build
2. Run the following commands:

```sh
yarn install <path-to-wapi.js>
yarn link <path-to-wapi.js>
pnpm install <path-to-wapi.js>
pnpm link <path-to-wapi.js>
```

NOTE: Make sure you have already built the library using `yarn build`
NOTE: Make sure you have already built the library using `pnpm build`

<br/><hr/><br/>

Expand Down
4 changes: 3 additions & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Client } from "wapi.js";

new Client();
new Client({

});
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"name": "wapijs",
"version": "0.1.0",
"version": "0.0.0",
"description": "wapi.js is a node library built for developing whatsapp cloud API based app in a user friendly way.",
"workspaces": [
"packages/*",
"apps/*"
],
"private": true,
"scripts": {
"watch": "turbo watch --concurrency=3",
Expand Down Expand Up @@ -56,5 +52,6 @@
"prettier": "^3.0.3",
"turbo": "^1.10.13",
"typescript": "^5.2.2"
}
},
"packageManager": "pnpm@8.7.0"
}
17 changes: 9 additions & 8 deletions packages/component-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"scripts": {
"watch": "yarn tsc --watch",
"watch": "pnpm tsc --watch",
"test": "vitest run",
"clean": "rm -rf ./dist",
"build:cjs": "yarn tsc -p tsconfig-cjs.json --pretty",
"build:esm": "yarn tsc -p tsconfig.json --pretty",
"build": "yarn run clean && concurrently -n esm,cjs \"npm run build:esm\" \"npm run build:cjs\"",
"lint": "yarn eslint . --max-warnings=0",
"pretty": "yarn prettier --write \"src/**/*.ts\"",
"clean-install": "rm -rf ./node_modules && yarn install --frozen-lockfile"
"build:cjs": "pnpm tsc -p tsconfig-cjs.json --pretty",
"build:esm": "pnpm tsc -p tsconfig.json --pretty",
"build": "pnpm run clean && concurrently -n esm,cjs \"npm run build:esm\" \"npm run build:cjs\"",
"lint": "pnpm eslint . --max-warnings=0",
"pretty": "pnpm prettier --write \"src/**/**/*.ts\"",
"clean-install": "rm -rf ./node_modules && pnpm install --frozen-lockfile"
},
"keywords": [],
"authors": [
Expand All @@ -37,5 +37,6 @@
"dependencies": {
"ts-enum-util": "^4.0.2",
"zod": "^3.22.2"
}
},
"packageManager": "pnpm@8.7.0"
}
17 changes: 9 additions & 8 deletions packages/wapi.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"scripts": {
"watch": "yarn tsc --watch",
"watch": "pnpm tsc --watch",
"test": "vitest run",
"clean": "rm -rf ./dist",
"build:cjs": "yarn tsc -p tsconfig-cjs.json --pretty",
"build:esm": "yarn tsc -p tsconfig.json --pretty",
"build": "yarn run clean && concurrently -n esm,cjs \"npm run build:esm\" \"npm run build:cjs\"",
"lint": "yarn eslint . --max-warnings=0",
"pretty": "yarn prettier --write \"src/**/*.ts\"",
"clean-install": "rm -rf ./node_modules && yarn install --frozen-lockfile",
"build:cjs": "pnpm tsc -p tsconfig-cjs.json --pretty",
"build:esm": "pnpm tsc -p tsconfig.json --pretty",
"build": "pnpm run clean && concurrently -n esm,cjs \"npm run build:esm\" \"npm run build:cjs\"",
"lint": "pnpm eslint . --max-warnings=0",
"pretty": "pnpm prettier --write \"src/**/*.ts\"",
"clean-install": "rm -rf ./node_modules && pnpm install --frozen-lockfile",
"doc:gen": "typedoc --out docs-md --entryPointStrategy Expand ./src"
},
"keywords": [],
Expand Down Expand Up @@ -41,5 +41,6 @@
"dayjs": "^1.11.9",
"express": "^4.18.2",
"zod": "^3.22.2"
}
},
"packageManager": "pnpm@8.7.0"
}
33 changes: 28 additions & 5 deletions packages/wapi.js/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { Webhook } from '~/webhook'
import { PhoneNumberManager } from '~/manager/phone'
import { MediaManager } from '~/manager/media'
import { RequestClient } from './request-client'
import { type ClientInterface } from './interface'

export class Client extends EventEmitter {
export class Client extends EventEmitter implements ClientInterface {
phone: PhoneNumberManager
media: MediaManager
webhook: Webhook
requester: RequestClient
status: 'Ready' | 'Idle' | null = null
readyAtTimeStamp: Date | null = null

private static baseUrl = 'cloud.whatsapp.com'

Expand All @@ -30,8 +33,10 @@ export class Client extends EventEmitter {
webhookEndpoint: params.webhookEndpoint,
port: params.port
})
this.phone = new PhoneNumberManager()
this.media = new MediaManager()
this.phone = new PhoneNumberManager(this)
this.media = new MediaManager({
client: this
})
this.requester = new RequestClient({
accessToken: params.apiAccessToken,
businessAccountId: params.businessAccountId,
Expand All @@ -55,9 +60,27 @@ export class Client extends EventEmitter {
return super.on(eventName, listener)
}

initiate() {
getReadyAt() {
return this.readyAtTimeStamp
}

updateAccessToken(accessToken: string) {
this.requester.accessToken = accessToken
}

updateSenderPhoneNumberId(phoneNumber: string) {
this.requester.phoneNumberId = phoneNumber
}

async initiate() {
this.webhook.listen(() => {
// ! TODO: emit the ready event here
this.status = 'Ready'
this.emit('Ready', null)
})

this.readyAtTimeStamp = new Date()
await Promise.resolve()

return true
}
}
38 changes: 38 additions & 0 deletions packages/wapi.js/src/client/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { type EventDataMap } from '~/webhook/schema'
import type { Client, MediaManagerInterface, PhoneNumberManagerInterface, Webhook } from '../..'
import type { RequestClient } from './request-client'

export interface ClientInterface {
phone: PhoneNumberManagerInterface
media: MediaManagerInterface
webhook: Webhook
requester: RequestClient
status: 'Ready' | 'Idle' | null
readyAtTimeStamp: Date | null
getReadyAt: () => Date | null

emit<T extends keyof EventDataMap>(eventName: T, data: EventDataMap[T]): boolean
on<T extends keyof EventDataMap>(eventName: T, listener: (data: EventDataMap[T]) => void): this
initiate: () => Promise<boolean>
updateAccessToken(accessToken: string): void
updateSenderPhoneNumberId(phoneNumber: string): void
}

export interface RequestClientInterface {
host: string
protocol: string
phoneNumberId: string
businessAccountId: string
apiVersion: string
client: Client
userAgent: string
accessToken: string

getRequestUrl(): string

requestCloudApi(params: {
path: string
body: any

Check warning on line 35 in packages/wapi.js/src/client/interface.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
method: 'GET' | 'POST' | 'DELETE'
}): Promise<void>
}
19 changes: 13 additions & 6 deletions packages/wapi.js/src/client/request-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ export class RequestClient {
return `${this.protocol}/${this.host}/${this.phoneNumberId}`
}

async requestCloudApi(body: any) {
async requestCloudApi({
body,
path,
method = 'POST'
}: {
path: string
body?: any

Check warning on line 45 in packages/wapi.js/src/client/request-client.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
method?: 'GET' | 'POST' | 'DELETE'
}) {
try {
const requestUrl = this.getRequestUrl()

// ! implement timeout here

const response = await fetch(requestUrl, {
method: 'POST',
const response = await fetch(`${requestUrl}/${path}`, {
method: method,
body,
headers: {
'Content-Type': 'application/json',
Expand All @@ -52,7 +58,8 @@ export class RequestClient {
}
})

return response.json()
const responseBody = await response.json()
return responseBody
} catch (error) {
if (error instanceof Error) this.client.emit('Error', error)
}
Expand Down
9 changes: 8 additions & 1 deletion packages/wapi.js/src/manager/base/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { type Client } from '../../..'
import { type BaseManagerInterface } from './interface'

export class BaseManager implements BaseManagerInterface {}
export class BaseManager implements BaseManagerInterface {
client: Client

constructor(client: Client) {
this.client = client
}
}
6 changes: 5 additions & 1 deletion packages/wapi.js/src/manager/base/interface.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export interface BaseManagerInterface {}
import { type Client } from '../../..'

export interface BaseManagerInterface {
client: Client
}
Loading

0 comments on commit 72d6af7

Please sign in to comment.