diff --git a/src/client.ts b/src/client.ts index caa52da..0c3b09e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -39,303 +39,153 @@ export class Client { }; } - 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", }); } - 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, + ...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); } - 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", - }); + getInvoice(paymentHash: string) { + return this.httpGet("/invoices/${paymentHash}"); } - sendPayment( - params: SendPaymentRequestParams, - request_options?: Partial, - ): Promise { - return rest({ - auth: this.auth, - ...this.defaultRequestOptions, - ...request_options, - endpoint: `/payments/bolt11`, - request_body: params, - method: "POST", - }); + decodeInvoice(paymentRequest: string) { + return this.httpGet("/decode/bolt11/${paymentRequest}"); + } + + createInvoice(invoice: InvoiceRequestParams) { + return this.httpPost("/invoices", invoice); + } + + 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, + })), + } + : { ...args, custom_records: args.customRecords }; + + return this.httpPost(endpoint, body); + } + + sendPayment(params: SendPaymentRequestParams) { + return this.httpPost("/payments/bolt11", params); } sendBoostagram( args: SendBoostagramRequestParams | SendBoostagramRequestParams[], - request_options?: Partial, ) { - 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, - ) { - const params = { + sendBoostagramToAlbyAccount(args: SendBoostagramToAlbyRequestParams) { + return this.httpPost("/payments/keysend", { destination: "030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3", - custom_records: { - "696969": args.account, - }, + 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", }); } - 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); } }