From a5996869eba68f2823bd32cf5e266ee42f18de9e Mon Sep 17 00:00:00 2001 From: guptamukund22 Date: Tue, 1 Apr 2025 14:53:59 +0530 Subject: [PATCH 1/3] Refactor: Modularize the code --- src/client.ts | 329 +++++++++++++------------------------------------- 1 file changed, 86 insertions(+), 243 deletions(-) diff --git a/src/client.ts b/src/client.ts index caa52da..18ac164 100644 --- a/src/client.ts +++ b/src/client.ts @@ -30,312 +30,155 @@ export class Client { constructor( auth: string | AuthClient, - requestOptions?: Partial, + requestOptions?: Partial ) { this.auth = typeof auth === "string" ? new OAuth2Bearer(auth) : auth; this.defaultRequestOptions = { ...requestOptions, - user_agent: requestOptions?.user_agent, + user_agent: requestOptions?.user_agent }; } - accountBalance( - // eslint-disable-next-line @typescript-eslint/ban-types - params: {}, - request_options?: Partial, - ): Promise { + // HTTP handlers + private httpGet( + endpoint: string, + params?: any, + options?: Partial + ): Promise { return rest({ auth: this.auth, ...this.defaultRequestOptions, - ...request_options, - endpoint: `/balance`, + ...options, + endpoint, params, - method: "GET", + method: "GET" }); } - signMessage( - message: SignMessageRequestParams, - request_options?: Partial, - ): Promise { + private httpPost( + endpoint: string, + body?: any, + options?: Partial + ): Promise { return rest({ auth: this.auth, ...this.defaultRequestOptions, - ...request_options, - endpoint: `/signatures`, - request_body: message, - method: "POST", + ...options, + endpoint, + request_body: body, + method: "POST" }); } - accountSummary( - // eslint-disable-next-line @typescript-eslint/ban-types - params: {}, - request_options?: Partial, - ) { + private httpDelete( + endpoint: string, + options?: Partial + ): Promise { return rest({ auth: this.auth, ...this.defaultRequestOptions, - ...request_options, - endpoint: `/user/summary`, - params, - method: "GET", + ...options, + endpoint, + method: "DELETE" }); } - accountInformation( - // eslint-disable-next-line @typescript-eslint/ban-types - params: {}, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/user/me`, - params, - method: "GET", - }); + accountBalance(): Promise { + return this.httpGet("/balance"); } - accountValue4Value( - // eslint-disable-next-line @typescript-eslint/ban-types - params: {}, - request_options?: Partial, - ) { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/user/value4value`, - params, - method: "GET", - }); + signMessage(message: SignMessageRequestParams): Promise { + return this.httpPost("/signatures", message); } - incomingInvoices( - params: GetInvoicesRequestParams, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/invoices/incoming`, - params, - method: "GET", - }); + accountSummary() { + return this.httpGet("/user/summary"); } - outgoingInvoices( - params: GetInvoicesRequestParams, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/invoices/outgoing`, - params, - method: "GET", - }); + accountInformation(): Promise { + return this.httpGet("/user/me"); } - invoices( - params: GetInvoicesRequestParams, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/invoices`, - params, - method: "GET", - }); + accountValue4Value() { + return this.httpGet("/user/value4value"); } - getInvoice( - paymentHash: string, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/invoices/${paymentHash}`, - method: "GET", - }); + incomingInvoices(params: GetInvoicesRequestParams) { + return this.httpGet("/invoices/incoming", params); } - decodeInvoice( - paymentRequest: string, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/decode/bolt11/${paymentRequest}`, - method: "GET", - }); + outgoingInvoices(params: GetInvoicesRequestParams) { + return this.httpGet("/invoices/outgoing", params); } - createInvoice( - invoice: InvoiceRequestParams, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/invoices`, - request_body: invoice, - method: "POST", - }); + invoices(params: GetInvoicesRequestParams) { + return this.httpGet("/invoices", params); + } + + getInvoice(paymentHash: string) { + return this.httpGet('/invoices/${paymentHash}'); + } + + decodeInvoice(paymentRequest: string) { + return this.httpGet('/decode/bolt11/${paymentRequest}'); + } + + createInvoice(invoice: InvoiceRequestParams) { + return this.httpPost("/invoices", invoice); } keysend( - args: KeysendRequestParams | KeysendRequestParams[], - request_options?: Partial, - ): Promise { - let endpoint, request_body; - if (Array.isArray(args)) { - endpoint = "/payments/keysend/multi"; - request_body = { - keysends: args.map((args) => ({ - ...args, - custom_records: args.customRecords, - })), - }; - } else { - endpoint = "/payments/keysend"; - request_body = { - ...args, - custom_records: args.customRecords, - }; - } - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint, - request_body, - method: "POST", - }); + args: KeysendRequestParams | KeysendRequestParams[] + ) { + const isMulti = Array.isArray(args); + const endpoint = isMulti ? "/payments/keysend/multi" : "/payments/keysend"; + const body = isMulti + ? { keysends: args.map(a => ({ ...a, custom_records: a.customRecords })) } + : { ...args, custom_records: args.customRecords }; + + return this.httpPost(endpoint, body); } - sendPayment( - params: SendPaymentRequestParams, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/payments/bolt11`, - request_body: params, - method: "POST", - }); + sendPayment(params: SendPaymentRequestParams) { + return this.httpPost("/payments/bolt11", params); } sendBoostagram( - args: SendBoostagramRequestParams | SendBoostagramRequestParams[], - request_options?: Partial, + args: SendBoostagramRequestParams | SendBoostagramRequestParams[] ) { - let endpoint, request_body; - if (Array.isArray(args)) { - endpoint = "/payments/keysend/multi"; - const keysends = args.map((b) => keysendParamsFromBoostagram(b)); - request_body = { keysends }; - } else { - endpoint = "/payments/keysend"; - request_body = keysendParamsFromBoostagram(args); - } + const isMulti = Array.isArray(args); + const endpoint = isMulti ? "/payments/keysend/multi" : "/payments/keysend"; + const body = isMulti + ? { keysends: args.map(keysendParamsFromBoostagram) } + : keysendParamsFromBoostagram(args); - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint, - request_body, - method: "POST", - }); + return this.httpPost(endpoint, body); } sendBoostagramToAlbyAccount( - args: SendBoostagramToAlbyRequestParams, - request_options?: Partial, + args: SendBoostagramToAlbyRequestParams ) { - const params = { - destination: - "030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3", - custom_records: { - "696969": args.account, - }, + return this.httpPost("/payments/keysend", { + destination: "030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3", + custom_records: { "696969": args.account }, amount: args.amount, - memo: args.memo, - }; - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/payments/keysend`, - request_body: params, - method: "POST", + memo: args.memo }); } - createWebhookEndpoint( - params: CreateWebhookEndpointParams, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/webhook_endpoints`, - request_body: params, - method: "POST", - }); + createWebhookEndpoint(params: CreateWebhookEndpointParams) { + return this.httpPost("/webhook_endpoints", params); } - deleteWebhookEndpoint( - id: string, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/webhook_endpoints/${id}`, - method: "DELETE", - }); + deleteWebhookEndpoint(id: string) { + return this.httpDelete('/webhook_endpoints/${id}'); } - getSwapInfo( - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/swaps/info`, - method: "GET", - }); + getSwapInfo() { + return this.httpGet("/swaps/info"); } - createSwap( - params: CreateSwapParams, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/swaps`, - method: "POST", - request_body: params, - }); + createSwap(params: CreateSwapParams) { + return this.httpPost("/swaps", params); } } From da95e45165217cc6860a69a23c3f615ee3635f52 Mon Sep 17 00:00:00 2001 From: guptamukund22 Date: Wed, 2 Apr 2025 20:14:33 +0530 Subject: [PATCH 2/3] Refactor: Fix formatting issues --- src/client.ts | 65 ++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/src/client.ts b/src/client.ts index 18ac164..0c3b09e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -30,20 +30,20 @@ export class Client { constructor( auth: string | AuthClient, - requestOptions?: Partial + requestOptions?: Partial, ) { this.auth = typeof auth === "string" ? new OAuth2Bearer(auth) : auth; this.defaultRequestOptions = { ...requestOptions, - user_agent: requestOptions?.user_agent + user_agent: requestOptions?.user_agent, }; } // HTTP handlers private httpGet( - endpoint: string, - params?: any, - options?: Partial + endpoint: string, + params?: any, + options?: Partial, ): Promise { return rest({ auth: this.auth, @@ -51,14 +51,14 @@ export class Client { ...options, endpoint, params, - method: "GET" + method: "GET", }); } private httpPost( - endpoint: string, - body?: any, - options?: Partial + endpoint: string, + body?: any, + options?: Partial, ): Promise { return rest({ auth: this.auth, @@ -66,20 +66,20 @@ export class Client { ...options, endpoint, request_body: body, - method: "POST" + method: "POST", }); } private httpDelete( - endpoint: string, - options?: Partial + endpoint: string, + options?: Partial, ): Promise { return rest({ auth: this.auth, ...this.defaultRequestOptions, ...options, endpoint, - method: "DELETE" + method: "DELETE", }); } @@ -116,24 +116,27 @@ export class Client { } getInvoice(paymentHash: string) { - return this.httpGet('/invoices/${paymentHash}'); + return this.httpGet("/invoices/${paymentHash}"); } decodeInvoice(paymentRequest: string) { - return this.httpGet('/decode/bolt11/${paymentRequest}'); + return this.httpGet("/decode/bolt11/${paymentRequest}"); } createInvoice(invoice: InvoiceRequestParams) { return this.httpPost("/invoices", invoice); } - keysend( - args: KeysendRequestParams | KeysendRequestParams[] - ) { + keysend(args: KeysendRequestParams | KeysendRequestParams[]) { const isMulti = Array.isArray(args); const endpoint = isMulti ? "/payments/keysend/multi" : "/payments/keysend"; - const body = isMulti - ? { keysends: args.map(a => ({ ...a, custom_records: a.customRecords })) } + const body = isMulti + ? { + keysends: args.map((a) => ({ + ...a, + custom_records: a.customRecords, + })), + } : { ...args, custom_records: args.customRecords }; return this.httpPost(endpoint, body); @@ -144,34 +147,38 @@ export class Client { } sendBoostagram( - args: SendBoostagramRequestParams | SendBoostagramRequestParams[] + args: SendBoostagramRequestParams | SendBoostagramRequestParams[], ) { const isMulti = Array.isArray(args); const endpoint = isMulti ? "/payments/keysend/multi" : "/payments/keysend"; - const body = isMulti + const body = isMulti ? { keysends: args.map(keysendParamsFromBoostagram) } : keysendParamsFromBoostagram(args); return this.httpPost(endpoint, body); } - sendBoostagramToAlbyAccount( - args: SendBoostagramToAlbyRequestParams - ) { + sendBoostagramToAlbyAccount(args: SendBoostagramToAlbyRequestParams) { return this.httpPost("/payments/keysend", { - destination: "030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3", + destination: + "030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3", custom_records: { "696969": args.account }, amount: args.amount, - memo: args.memo + memo: args.memo, }); } createWebhookEndpoint(params: CreateWebhookEndpointParams) { - return this.httpPost("/webhook_endpoints", params); + return this.httpPost( + "/webhook_endpoints", + params, + ); } deleteWebhookEndpoint(id: string) { - return this.httpDelete('/webhook_endpoints/${id}'); + return this.httpDelete( + "/webhook_endpoints/${id}", + ); } getSwapInfo() { From 54e51ae751923fd61a25d4dae38d7e2c02050581 Mon Sep 17 00:00:00 2001 From: guptamukund22 Date: Thu, 3 Apr 2025 22:01:38 +0530 Subject: [PATCH 3/3] Refactor : Modularize the code --- src/webln/OauthWeblnProvider.ts | 83 +++++++++++---------------------- 1 file changed, 27 insertions(+), 56 deletions(-) diff --git a/src/webln/OauthWeblnProvider.ts b/src/webln/OauthWeblnProvider.ts index 74318af..6a54d78 100644 --- a/src/webln/OauthWeblnProvider.ts +++ b/src/webln/OauthWeblnProvider.ts @@ -35,63 +35,46 @@ export class OauthWeblnProvider { } } - async enable() { - if (this.isExecuting) { - return; + private async execute(fn: () => Promise): Promise { + if (this.isExecuting) return; + this.isExecuting = true; + try { + return await fn(); + } catch (error) { + let message = "Unknown Error"; + if (error instanceof Error) { + message = error.message; + } + throw new Error(message); + } finally { + this.isExecuting = false; } + } + + async enable() { if (this.auth.token?.access_token) { return { enabled: true }; } - if (isBrowser()) { - try { - this.isExecuting = true; - await this.openAuthorization(); - } finally { - this.isExecuting = false; - } - } else { + if (!isBrowser()) { throw new Error("Missing access token"); } + await this.execute(() => this.openAuthorization()); } async sendPayment(invoice: string) { - if (this.isExecuting) { - return; - } - try { - this.isExecuting = true; + return this.execute(async () => { const result = await this.client.sendPayment({ invoice }); this.notify("sendPayment", result); - return { - preimage: result.payment_preimage, - }; - } catch (error) { - let message = "Unknown Error"; - if (error instanceof Error) message = error.message; - throw new Error(message); - } finally { - this.isExecuting = false; - } + return { preimage: result.payment_preimage }; + }); } async keysend(params: KeysendRequestParams) { - if (this.isExecuting) { - return; - } - try { - this.isExecuting = true; + return this.execute(async () => { const result = await this.client.keysend(params); this.notify("keysend", result); - return { - preimage: result.payment_preimage, - }; - } catch (error) { - let message = "Unknown Error"; - if (error instanceof Error) message = error.message; - throw new Error(message); - } finally { - this.isExecuting = false; - } + return { preimage: result.payment_preimage }; + }); } async getInfo() { @@ -101,26 +84,14 @@ export class OauthWeblnProvider { } async makeInvoice(params: RequestInvoiceArgs) { - if (this.isExecuting) { - return; - } - try { - this.isExecuting = true; + return this.execute(async () => { const result = await this.client.createInvoice({ amount: parseInt(params.amount.toString()), description: params.defaultMemo, }); this.notify("makeInvoice", result); - return { - paymentRequest: result.payment_request, - }; - } catch (error) { - let message = "Unknown Error"; - if (error instanceof Error) message = error.message; - throw new Error(message); - } finally { - this.isExecuting = false; - } + return { paymentRequest: result.payment_request }; + }); } async openAuthorization() {