-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
207 lines (188 loc) · 5.89 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { callApi } from './services/http.service';
import { IOrder, IEditOrder, IGetOrder, IQueryOrder } from './interfaces';
import * as qs from 'querystring';
import { OrderResponse } from './types/index';
import { STATE } from './enums';
/**
*
* @param order
* @returns object
*/
export async function createOrder(order: IOrder): Promise<OrderResponse> {
try {
if (!order.message && !order.file) throw new Error('Invalid Parameters');
const data = (order as unknown) as qs.ParsedUrlQueryInput;
const response = await callApi.post('/order', qs.stringify(data));
return {
error: false,
auth_token: response.data?.auth_token,
uuid: response.data?.uuid,
lightning_invoice_id: response.data?.lightning_invoice?.id,
description: response.data?.lightning_invoice?.description,
status: response.data?.lightning_invoice?.status,
msatoshi: response.data?.lightning_invoice?.msatoshi,
payreq: response.data?.lightning_invoice?.payreq,
expires_at: response.data?.lightning_invoice?.expires_at,
created_at: response.data?.lightning_invoice?.created_at,
metadata: { ...response.data?.lightning_invoice?.metadata },
};
} catch (error) {
return {
error: true,
message:
error.message || error.response?.statusText || 'An Error occured!',
};
}
}
export async function increaseBid(order: IEditOrder): Promise<OrderResponse> {
try {
const data = {
auth_token: order.auth_token,
bid_increase: order.bid_increase,
};
const response = await callApi.post(
`/order/${order.uuid}/bump`,
qs.stringify(data)
);
return {
error: false,
auth_token: response.data?.auth_token,
uuid: response.data?.uuid,
lightning_invoice_id: response.data?.lightning_invoice?.id,
description: response.data?.lightning_invoice?.description,
status: response.data?.lightning_invoice?.status,
msatoshi: response.data?.lightning_invoice?.msatoshi,
payreq: response.data?.lightning_invoice?.payreq,
expires_at: response.data?.lightning_invoice?.expires_at,
created_at: response.data?.lightning_invoice?.created_at,
metadata: { ...response.data?.lightning_invoice?.metadata },
};
} catch (error) {
return {
error: true,
message:
error.message || error.response?.statusText || 'An Error occured!',
};
}
}
export async function getOrder(order: IGetOrder): Promise<OrderResponse> {
try {
const response = await callApi.get(`/order/${order.uuid}`, {
headers: {
'X-Auth-Token': order.auth_token,
},
});
return {
error: false,
bid: response.data.bid,
message_digest: response.data.message_digest,
status: response.data.status,
uuid: response.data.uuid,
created_at: response.data.created_at,
started_transmission_at: response.data.started_transmission_at,
ended_transmission_at: response.data.ended_transmission_at,
tx_seq_num: response.data.tx_seq_num,
unpaid_bid: response.data.unpaid_bid,
};
} catch (error) {
return {
error: true,
message:
error.message || error.response?.statusText || 'An Error occured!',
};
}
}
export async function deleteOrder(order: IGetOrder): Promise<OrderResponse> {
try {
const response = await callApi.delete(`/order/${order.uuid}`, {
headers: {
'X-Auth-Token': order.auth_token,
},
});
return {
error: false,
message: response.data.message_digest,
};
} catch (error) {
return {
error: true,
message:
error.message || error.response?.statusText || 'An Error occured!',
};
}
}
export async function getOrders(order: IQueryOrder): Promise<OrderResponse> {
try {
if (!(order.state in STATE)) throw new Error('Invalid request');
const data = ({
limit: order.limit,
before: order.before,
} as unknown) as qs.ParsedUrlQueryInput;
const query =
data.limit || data.before
? qs.stringify({
...(data.limit && {
limit: data.limit,
}),
...(data.before && {
before: data.before,
}),
})
: '';
const response = await callApi.get(`/orders/${order.state}?${query}`);
return {
error: false,
data: response.data,
};
} catch (error) {
return {
error: true,
message:
error.message || error.response?.statusText || 'An Error occured!',
};
}
}
export async function getInfo(): Promise<OrderResponse> {
try {
const response = await callApi.get('/info');
return {
error: false,
id: response.data?.id,
alias: response.data.alias,
color: response.data.color,
number_of_peers: response.data.num_peers,
number_of_active_channels: response.data.num_active_channels,
number_of_inactive_channels: response.data.num_inactive_channels,
number_of_pending_channels: response.data.num_pending_channels,
binding: response.data.binding,
msatoshi_fees_collected: response.data.msatoshi_fees_collected,
fees_collected_msat: response.data.fees_collected_msat,
lightning_directory: response.data['lightning-dir'],
address: response.data?.address,
version: response.data?.version,
blockheight: response.data?.blockheight,
network: response.data?.network,
};
} catch (error) {
return {
error: true,
message:
error.message || error.response?.statusText || 'An Error occured!',
};
}
}
export async function subscribeToChannels(
channels: Array<string>
): Promise<any> {
try {
const list = channels.join(',');
const results = (await callApi.get(`/subscribe/${list}`)) || [];
return results;
} catch (error) {
return {
error: true,
message:
error.message || error.response?.statusText || 'An Error occured!',
};
}
}