Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
326 changes: 88 additions & 238 deletions src/oauth/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,303 +39,153 @@ export class Client {
};
}

accountBalance(
// eslint-disable-next-line @typescript-eslint/ban-types
params: {},
request_options?: Partial<RequestOptions>,
): Promise<GetAccountBalanceResponse> {
// HTTP handlers
private httpGet<T>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having httpGet httpPost httpDelete methods. can we have generic http Request method here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, Actually I have my end semester exams going on that's why I was not able to reply. I will fix the issue asap.

endpoint: string,
params?: any,
options?: Partial<RequestOptions>,
): Promise<T> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/balance`,
...options,
endpoint,
params,
method: "GET",
});
}

signMessage(
message: SignMessageRequestParams,
request_options?: Partial<RequestOptions>,
): Promise<SignMessageResponse> {
private httpPost<T>(
endpoint: string,
body?: any,
options?: Partial<RequestOptions>,
): Promise<T> {
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<RequestOptions>,
) {
private httpDelete<T>(
endpoint: string,
options?: Partial<RequestOptions>,
): Promise<T> {
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<RequestOptions>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why params and request_options from all the methods were removed. this will break things for package users. we should refactor but not change current working of library

): Promise<GetAccountInformationResponse> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/user/me`,
params,
method: "GET",
});
accountBalance(): Promise<GetAccountBalanceResponse> {
return this.httpGet<GetAccountBalanceResponse>("/balance");
}

accountValue4Value(
// eslint-disable-next-line @typescript-eslint/ban-types
params: {},
request_options?: Partial<RequestOptions>,
) {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/user/value4value`,
params,
method: "GET",
});
signMessage(message: SignMessageRequestParams): Promise<SignMessageResponse> {
return this.httpPost<SignMessageResponse>("/signatures", message);
}

incomingInvoices(
params: GetInvoicesRequestParams,
request_options?: Partial<RequestOptions>,
): Promise<Invoice[]> {
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<RequestOptions>,
): Promise<Invoice[]> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/invoices/outgoing`,
params,
method: "GET",
});
accountInformation(): Promise<GetAccountInformationResponse> {
return this.httpGet<GetAccountInformationResponse>("/user/me");
}

invoices(
params: GetInvoicesRequestParams,
request_options?: Partial<RequestOptions>,
): Promise<Invoice[]> {
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<RequestOptions>,
): Promise<Invoice> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/invoices/${paymentHash}`,
method: "GET",
});
incomingInvoices(params: GetInvoicesRequestParams) {
return this.httpGet<Invoice[]>("/invoices/incoming", params);
}

decodeInvoice(
paymentRequest: string,
request_options?: Partial<RequestOptions>,
): Promise<DecodedInvoice> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/decode/bolt11/${paymentRequest}`,
method: "GET",
});
outgoingInvoices(params: GetInvoicesRequestParams) {
return this.httpGet<Invoice[]>("/invoices/outgoing", params);
}

createInvoice(
invoice: InvoiceRequestParams,
request_options?: Partial<RequestOptions>,
): Promise<Invoice> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/invoices`,
request_body: invoice,
method: "POST",
});
invoices(params: GetInvoicesRequestParams) {
return this.httpGet<Invoice[]>("/invoices", params);
}

keysend(
args: KeysendRequestParams | KeysendRequestParams[],
request_options?: Partial<RequestOptions>,
): Promise<SendPaymentResponse> {
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<Invoice>("/invoices/${paymentHash}");
}

sendPayment(
params: SendPaymentRequestParams,
request_options?: Partial<RequestOptions>,
): Promise<SendPaymentResponse> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/payments/bolt11`,
request_body: params,
method: "POST",
});
decodeInvoice(paymentRequest: string) {
return this.httpGet<DecodedInvoice>("/decode/bolt11/${paymentRequest}");
}

createInvoice(invoice: InvoiceRequestParams) {
return this.httpPost<Invoice>("/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<SendPaymentResponse>(endpoint, body);
}

sendPayment(params: SendPaymentRequestParams) {
return this.httpPost<SendPaymentResponse>("/payments/bolt11", params);
}

sendBoostagram(
args: SendBoostagramRequestParams | SendBoostagramRequestParams[],
request_options?: Partial<RequestOptions>,
) {
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<RequestOptions>,
) {
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<RequestOptions>,
): Promise<CreateWebhookEndpointResponse> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/webhook_endpoints`,
request_body: params,
method: "POST",
});
createWebhookEndpoint(params: CreateWebhookEndpointParams) {
return this.httpPost<CreateWebhookEndpointResponse>(
"/webhook_endpoints",
params,
);
}

deleteWebhookEndpoint(
id: string,
request_options?: Partial<RequestOptions>,
): Promise<BaseWebhookEndpointResponse> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/webhook_endpoints/${id}`,
method: "DELETE",
});
deleteWebhookEndpoint(id: string) {
return this.httpDelete<BaseWebhookEndpointResponse>(
"/webhook_endpoints/${id}",
);
}

getSwapInfo(
request_options?: Partial<RequestOptions>,
): Promise<SwapInfoResponse> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/swaps/info`,
method: "GET",
});
getSwapInfo() {
return this.httpGet<SwapInfoResponse>("/swaps/info");
}

createSwap(
params: CreateSwapParams,
request_options?: Partial<RequestOptions>,
): Promise<CreateSwapResponse> {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/swaps`,
method: "POST",
request_body: params,
});
createSwap(params: CreateSwapParams) {
return this.httpPost<CreateSwapResponse>("/swaps", params);
}
}
Loading