Skip to content

Commit

Permalink
Merge pull request #367 from Secreto31126/improved-types
Browse files Browse the repository at this point in the history
Improved types
  • Loading branch information
Secreto31126 authored Aug 14, 2024
2 parents 2a4236e + 0f6697b commit 7a5f6df
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whatsapp-api-js",
"version": "4.0.0",
"version": "4.0.1",
"author": "Secreto31126",
"description": "A TypeScript server agnostic Whatsapp's Official API framework",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ export class WhatsAppAPI<EmittersReturnType = void> {
message: ClientMessage,
batch_size = 50,
delay = 1000
): Array<ReturnType<typeof this.sendMessage>> {
const responses = [] as ReturnType<typeof this.sendMessage>[];
): Array<ReturnType<WhatsAppAPI["sendMessage"]>> {
const responses = [] as ReturnType<WhatsAppAPI["sendMessage"]>[];

if (batch_size < 1) {
throw new RangeError("batch_size must be greater than 0");
Expand Down
4 changes: 2 additions & 2 deletions src/messages/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class Contacts extends ClientMessage {
* @override
* @internal
*/
_build() {
_build(): string {
return JSON.stringify(this.component);
}
}
Expand Down Expand Up @@ -245,7 +245,7 @@ export class Birthday extends ContactUniqueComponent {
* @override
* @internal
*/
_build() {
_build(): string {
return this.birthday;
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/messages/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ export class Template extends ClientMessage {
* @param code - The one time password to be sent
* @returns A Template object for the API
*/
static OTP(name: string, language: string | Language, code: string) {
static OTP(
name: string,
language: string | Language,
code: string
): Template {
return new Template(
name,
language,
Expand Down Expand Up @@ -324,7 +328,7 @@ export abstract class ButtonComponent implements TemplateComponent {
* @override
* @internal
*/
_build(pointers: BuildingPointers) {
_build(pointers: BuildingPointers): this {
this.index = pointers.button_counter++;
return this;
}
Expand Down Expand Up @@ -603,7 +607,7 @@ export class SkipButtonComponent extends ButtonComponent {
* @override
* @internal
*/
_build(pointers: BuildingPointers) {
_build(pointers: BuildingPointers): this {
pointers.button_counter++;
return null as unknown as this;
}
Expand Down Expand Up @@ -638,7 +642,7 @@ export class HeaderComponent implements TemplateComponent {
* @override
* @internal
*/
_build() {
_build(): this {
return this;
}
}
Expand Down Expand Up @@ -762,7 +766,7 @@ export class BodyComponent implements TemplateComponent {
* @internal
* @throws If theres_only_body is false and one of the parameters is a string and it's over 1024 characters
*/
_build({ theres_only_body }: BuildingPointers) {
_build({ theres_only_body }: BuildingPointers): this {
// If it needs to check for the shorter max text length
if (!theres_only_body) {
for (const param of this.parameters) {
Expand Down Expand Up @@ -868,7 +872,7 @@ export class CarouselComponent
* @override
* @internal
*/
_build() {
_build(): this {
return this;
}
}
Expand Down Expand Up @@ -919,7 +923,7 @@ export class CarouselCard implements ClientBuildableMessageComponent {
* @override
* @internal
*/
_build(ptr: { counter: number }) {
_build(ptr: { counter: number }): this {
this.card_index = ptr.counter++;
return this;
}
Expand Down Expand Up @@ -974,7 +978,7 @@ export class LTOComponent implements TemplateComponent {
* @override
* @internal
*/
_build() {
_build(): this {
return this;
}
}
4 changes: 2 additions & 2 deletions src/middleware/adonis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware {
* @param req - The request object from AdonisJS
* @returns The status code to be sent to the client
*/
async handle_post(req: Request) {
async handle_post(req: Request): Promise<number> {
try {
await this.post(
req.body() as PostData,
Expand Down Expand Up @@ -74,7 +74,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware {
* @returns The challenge string to be sent to the client
* @throws The error code
*/
handle_get(req: Request) {
handle_get(req: Request): string {
try {
return this.get(req.qs() as GetParams);
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class WhatsAppAPI extends WebStandardMiddleware {
* @param req - The request object
* @returns The status code to be sent to the client
*/
async handle_post(req: Request) {
async handle_post(req: Request): Promise<number> {
return super.handle_post(req);
}

Expand Down Expand Up @@ -61,7 +61,7 @@ export class WhatsAppAPI extends WebStandardMiddleware {
* @returns The challenge string to be sent to the client
* @throws The error code
*/
handle_get(req: Request) {
handle_get(req: Request): string {
return super.handle_get(req);
}
}
4 changes: 2 additions & 2 deletions src/middleware/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class WhatsAppAPI extends WebStandardMiddleware {
* @param req - The request object
* @returns The status code to be sent to the client
*/
async handle_post(req: Request) {
async handle_post(req: Request): Promise<number> {
return super.handle_post(req);
}

Expand Down Expand Up @@ -61,7 +61,7 @@ export class WhatsAppAPI extends WebStandardMiddleware {
* @returns The challenge string to be sent to the client
* @throws The error code
*/
handle_get(req: Request) {
handle_get(req: Request): string {
return super.handle_get(req);
}
}
4 changes: 2 additions & 2 deletions src/middleware/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware {
* @param req - The request object from Express.js
* @returns The status code to be sent to the client
*/
async handle_post(req: Request) {
async handle_post(req: Request): Promise<number> {
try {
await this.post(
JSON.parse(req.body ?? "{}"),
Expand Down Expand Up @@ -85,7 +85,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware {
* @returns The challenge string to be sent to the client
* @throws The error code
*/
handle_get(req: Request) {
handle_get(req: Request): string {
try {
return this.get(req.query as GetParams);
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class WhatsAppAPI extends WebStandardMiddleware {
* @param req - The request object
* @returns The status code to be sent to the client
*/
async handle_post(req: Request) {
async handle_post(req: Request): Promise<number> {
return super.handle_post(req);
}

Expand Down Expand Up @@ -57,7 +57,7 @@ export class WhatsAppAPI extends WebStandardMiddleware {
* @returns The challenge string to be sent to the client
* @throws The error code
*/
handle_get(req: Request) {
handle_get(req: Request): string {
return super.handle_get(req);
}
}
4 changes: 2 additions & 2 deletions src/middleware/node-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware {
* @param req - The request object
* @returns The status code to be sent to the client
*/
async handle_post(req: IncomingMessage) {
async handle_post(req: IncomingMessage): Promise<number> {
/**
* Copy pasted from an issue on Deno's repository :)
*
Expand Down Expand Up @@ -101,7 +101,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware {
* @returns The challenge string to be sent to the client
* @throws The error code
*/
handle_get(req: IncomingMessage) {
handle_get(req: IncomingMessage): string {
try {
return this.get(
Object.fromEntries(
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class WhatsAppAPI extends WebStandardMiddleware {
* @param req - The request object
* @returns The status code to be sent to the client
*/
async handle_post(req: Request) {
async handle_post(req: Request): Promise<number> {
return super.handle_post(req);
}

Expand Down Expand Up @@ -63,7 +63,7 @@ export class WhatsAppAPI extends WebStandardMiddleware {
* @returns The challenge string to be sent to the client
* @throws The error code
*/
handle_get(req: Request) {
handle_get(req: Request): string {
return super.handle_get(req);
}
}
4 changes: 2 additions & 2 deletions src/middleware/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class WhatsAppAPI extends NodeHTTPMiddleware {
* @param req - The request object
* @returns The status code to be sent to the client
*/
handle_post(req: VercelRequest) {
handle_post(req: VercelRequest): Promise<number> {
return super.handle_post(req);
}

Expand Down Expand Up @@ -78,7 +78,7 @@ export class WhatsAppAPI extends NodeHTTPMiddleware {
* @returns The challenge string to be sent to the client
* @throws The error code
*/
handle_get(req: VercelRequest) {
handle_get(req: VercelRequest): string {
try {
return this.get(req.query as GetParams);
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/web-standard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware {
* @param req - The request object
* @returns The status code to be sent to the client
*/
async handle_post(req: Request) {
async handle_post(req: Request): Promise<number> {
try {
const body = await req.text();

Expand All @@ -39,7 +39,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware {
* @returns The challenge string to be sent to the client
* @throws The error code
*/
handle_get(req: Request) {
handle_get(req: Request): string {
try {
return this.get(
Object.fromEntries(new URL(req.url).searchParams) as GetParams
Expand Down
Loading

0 comments on commit 7a5f6df

Please sign in to comment.