-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
api_client.ts
724 lines (705 loc) · 25.5 KB
/
api_client.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
import objectAssignDeep from 'object-assign-deep'
import MegalodonEntity from '../entity'
import PleromaEntity from './entity'
import Response from '../response'
import { RequestCanceledError } from '../cancel'
import proxyAgent, { ProxyConfig } from '../proxy_config'
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from '../default'
import WebSocket from './web_socket'
import NotificationType from '../notification'
import PleromaNotificationType from './notification'
namespace PleromaAPI {
export namespace Entity {
export type Account = PleromaEntity.Account
export type Activity = PleromaEntity.Activity
export type Application = PleromaEntity.Application
export type AsyncAttachment = PleromaEntity.AsyncAttachment
export type Attachment = PleromaEntity.Attachment
export type Card = PleromaEntity.Card
export type Context = PleromaEntity.Context
export type Conversation = PleromaEntity.Conversation
export type Emoji = PleromaEntity.Emoji
export type FeaturedTag = PleromaEntity.FeaturedTag
export type Field = PleromaEntity.Field
export type Filter = PleromaEntity.Filter
export type History = PleromaEntity.History
export type IdentityProof = PleromaEntity.IdentityProof
export type Instance = PleromaEntity.Instance
export type List = PleromaEntity.List
export type Marker = PleromaEntity.Marker
export type Mention = PleromaEntity.Mention
export type Notification = PleromaEntity.Notification
export type Poll = PleromaEntity.Poll
export type PollOption = PleromaEntity.PollOption
export type Preferences = PleromaEntity.Preferences
export type PushSubscription = PleromaEntity.PushSubscription
export type Reaction = PleromaEntity.Reaction
export type Relationship = PleromaEntity.Relationship
export type Report = PleromaEntity.Report
export type Results = PleromaEntity.Results
export type ScheduledStatus = PleromaEntity.ScheduledStatus
export type Source = PleromaEntity.Source
export type Stats = PleromaEntity.Stats
export type Status = PleromaEntity.Status
export type StatusParams = PleromaEntity.StatusParams
export type Tag = PleromaEntity.Tag
export type Token = PleromaEntity.Token
export type URLs = PleromaEntity.URLs
}
export namespace Converter {
export const decodeNotificationType = (t: PleromaEntity.NotificationType): MegalodonEntity.NotificationType => {
switch (t) {
case PleromaNotificationType.Mention:
return NotificationType.Mention
case PleromaNotificationType.Reblog:
return NotificationType.Reblog
case PleromaNotificationType.Favourite:
return NotificationType.Favourite
case PleromaNotificationType.Follow:
return NotificationType.Follow
case PleromaNotificationType.Poll:
return NotificationType.PollExpired
case PleromaNotificationType.PleromaEmojiReaction:
return NotificationType.EmojiReaction
case PleromaNotificationType.FollowRequest:
return NotificationType.FollowRequest
default:
return t
}
}
export const encodeNotificationType = (t: MegalodonEntity.NotificationType): PleromaEntity.NotificationType => {
switch (t) {
case NotificationType.Follow:
return PleromaNotificationType.Follow
case NotificationType.Favourite:
return PleromaNotificationType.Favourite
case NotificationType.Reblog:
return PleromaNotificationType.Reblog
case NotificationType.Mention:
return PleromaNotificationType.Mention
case NotificationType.PollExpired:
return PleromaNotificationType.Poll
case NotificationType.EmojiReaction:
return PleromaNotificationType.PleromaEmojiReaction
case NotificationType.FollowRequest:
return PleromaNotificationType.FollowRequest
default:
return t
}
}
export const account = (a: Entity.Account): MegalodonEntity.Account => a
export const activity = (a: Entity.Activity): MegalodonEntity.Activity => a
export const application = (a: Entity.Application): MegalodonEntity.Application => a
export const attachment = (a: Entity.Attachment): MegalodonEntity.Attachment => a
export const async_attachment = (a: Entity.AsyncAttachment) => {
if (a.url) {
return {
id: a.id,
type: a.type,
url: a.url!,
remote_url: a.remote_url,
preview_url: a.preview_url,
text_url: a.text_url,
meta: a.meta,
description: a.description,
blurhash: a.blurhash
} as MegalodonEntity.Attachment
} else {
return a as MegalodonEntity.AsyncAttachment
}
}
export const card = (c: Entity.Card): MegalodonEntity.Card => c
export const context = (c: Entity.Context): MegalodonEntity.Context => ({
ancestors: c.ancestors.map(a => status(a)),
descendants: c.descendants.map(d => status(d))
})
export const conversation = (c: Entity.Conversation): MegalodonEntity.Conversation => ({
id: c.id,
accounts: c.accounts.map(a => account(a)),
last_status: c.last_status ? status(c.last_status) : null,
unread: c.unread
})
export const emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => e
export const featured_tag = (f: Entity.FeaturedTag): MegalodonEntity.FeaturedTag => f
export const field = (f: Entity.Field): MegalodonEntity.Field => f
export const filter = (f: Entity.Filter): MegalodonEntity.Filter => f
export const history = (h: Entity.History): MegalodonEntity.History => h
export const identity_proof = (i: Entity.IdentityProof): MegalodonEntity.IdentityProof => i
export const instance = (i: Entity.Instance): MegalodonEntity.Instance => i
export const list = (l: Entity.List): MegalodonEntity.List => l
export const marker = (m: Entity.Marker): MegalodonEntity.Marker => {
return {
notifications: {
last_read_id: m.notifications.last_read_id,
version: m.notifications.version,
updated_at: m.notifications.updated_at,
unread_count: m.notifications.pleroma.unread_count
}
}
}
export const mention = (m: Entity.Mention): MegalodonEntity.Mention => m
export const notification = (n: Entity.Notification): MegalodonEntity.Notification => {
if (n.status && n.emoji) {
return {
id: n.id,
account: n.account,
created_at: n.created_at,
status: status(n.status),
emoji: n.emoji,
type: decodeNotificationType(n.type)
}
} else if (n.status) {
return {
id: n.id,
account: n.account,
created_at: n.created_at,
status: status(n.status),
type: decodeNotificationType(n.type)
}
} else {
return {
id: n.id,
account: n.account,
created_at: n.created_at,
type: decodeNotificationType(n.type)
}
}
}
export const poll = (p: Entity.Poll): MegalodonEntity.Poll => p
export const pollOption = (p: Entity.PollOption): MegalodonEntity.PollOption => p
export const preferences = (p: Entity.Preferences): MegalodonEntity.Preferences => p
export const push_subscription = (p: Entity.PushSubscription): MegalodonEntity.PushSubscription => p
export const reaction = (r: Entity.Reaction): MegalodonEntity.Reaction => r
export const relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => ({
id: r.id,
following: r.following,
followed_by: r.followed_by,
blocking: r.blocking,
blocked_by: r.blocked_by,
muting: r.muting,
muting_notifications: r.muting_notifications,
requested: r.requested,
domain_blocking: r.domain_blocking,
showing_reblogs: r.showing_reblogs,
endorsed: r.endorsed,
notifying: r.subscribing
})
export const report = (r: Entity.Report): MegalodonEntity.Report => r
export const results = (r: Entity.Results): MegalodonEntity.Results => ({
accounts: r.accounts.map(a => account(a)),
statuses: r.statuses.map(s => status(s)),
hashtags: r.hashtags.map(h => tag(h))
})
export const scheduled_status = (s: Entity.ScheduledStatus): MegalodonEntity.ScheduledStatus => ({
id: s.id,
scheduled_at: s.scheduled_at,
params: s.params,
media_attachments: s.media_attachments.map(m => attachment(m))
})
export const source = (s: Entity.Source): MegalodonEntity.Source => s
export const stats = (s: Entity.Stats): MegalodonEntity.Stats => s
export const status = (s: Entity.Status): MegalodonEntity.Status => ({
id: s.id,
uri: s.uri,
url: s.url,
account: account(s.account),
in_reply_to_id: s.in_reply_to_id,
in_reply_to_account_id: s.in_reply_to_account_id,
reblog: s.reblog ? status(s.reblog) : null,
content: s.content,
plain_content: s.pleroma.content?.['text/plain'] ? s.pleroma.content['text/plain'] : null,
created_at: s.created_at,
emojis: s.emojis.map(e => emoji(e)),
replies_count: s.replies_count,
reblogs_count: s.reblogs_count,
favourites_count: s.favourites_count,
reblogged: s.reblogged,
favourited: s.favourited,
muted: s.muted,
sensitive: s.sensitive,
spoiler_text: s.spoiler_text,
visibility: s.visibility,
media_attachments: s.media_attachments.map(m => attachment(m)),
mentions: s.mentions.map(m => mention(m)),
tags: s.tags.map(t => tag(t)),
card: s.card ? card(s.card) : null,
poll: s.poll ? poll(s.poll) : null,
application: s.application ? application(s.application) : null,
language: s.language,
pinned: s.pinned,
emoji_reactions: s.pleroma.emoji_reactions ? s.pleroma.emoji_reactions.map(r => reaction(r)) : [],
bookmarked: s.bookmarked ? s.bookmarked : false,
quote: s.reblog !== null && s.reblog.content !== s.content
})
export const status_params = (s: Entity.StatusParams): MegalodonEntity.StatusParams => s
export const tag = (t: Entity.Tag): MegalodonEntity.Tag => t
export const token = (t: Entity.Token): MegalodonEntity.Token => t
export const urls = (u: Entity.URLs): MegalodonEntity.URLs => u
}
/**
* Interface
*/
export interface Interface {
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
put<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
putForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
patch<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
patchForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
post<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
cancel(): void
socket(path: string, stream: string, params?: string): WebSocket
}
/**
* Mastodon API client.
*
* Using axios for request, you will handle promises.
*/
export class Client implements Interface {
static DEFAULT_SCOPE = DEFAULT_SCOPE
static DEFAULT_URL = 'https://pleroma.io'
static NO_REDIRECT = NO_REDIRECT
private accessToken: string | null
private baseUrl: string
private userAgent: string
private abortController: AbortController
private proxyConfig: ProxyConfig | false = false
/**
* @param baseUrl hostname or base URL
* @param accessToken access token from OAuth2 authorization
* @param userAgent UserAgent is specified in header on request.
* @param proxyConfig Proxy setting, or set false if don't use proxy.
*/
constructor(
baseUrl: string,
accessToken: string | null = null,
userAgent: string = DEFAULT_UA,
proxyConfig: ProxyConfig | false = false
) {
this.accessToken = accessToken
this.baseUrl = baseUrl
this.userAgent = userAgent
this.proxyConfig = proxyConfig
this.abortController = new AbortController()
axios.defaults.signal = this.abortController.signal
}
/**
* GET request to mastodon REST API.
* @param path relative path from baseUrl
* @param params Query parameters
* @param headers Request header object
*/
public async get<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
params: params,
headers: Object.assign(
{
'User-Agent': this.userAgent,
// To avoid: https://github.com/axios/axios/issues/5346
'Accept-Encoding': 'gzip,deflate,compress'
},
headers
)
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios
.get<T>(this.baseUrl + path, options)
.catch((err: Error) => {
if (axios.isCancel(err)) {
throw new RequestCanceledError(err.message)
} else {
throw err
}
})
.then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* PUT request to mastodon REST API.
* @param path relative path from baseUrl
* @param params Form data. If you want to post file, please use FormData()
* @param headers Request header object
*/
public async put<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: Object.assign(
{
'User-Agent': this.userAgent,
// To avoid: https://github.com/axios/axios/issues/5346
'Accept-Encoding': 'gzip,deflate,compress'
},
headers
),
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios
.put<T>(this.baseUrl + path, params, options)
.catch((err: Error) => {
if (axios.isCancel(err)) {
throw new RequestCanceledError(err.message)
} else {
throw err
}
})
.then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* PUT request to mastodon REST API for multipart.
* @param path relative path from baseUrl
* @param params Form data. If you want to post file, please use FormData()
* @param headers Request header object
*/
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: Object.assign(
{
'User-Agent': this.userAgent,
// To avoid: https://github.com/axios/axios/issues/5346
'Accept-Encoding': 'gzip,deflate,compress'
},
headers
),
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios
.putForm<T>(this.baseUrl + path, params, options)
.catch((err: Error) => {
if (axios.isCancel(err)) {
throw new RequestCanceledError(err.message)
} else {
throw err
}
})
.then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* PATCH request to mastodon REST API.
* @param path relative path from baseUrl
* @param params Form data. If you want to post file, please use FormData()
* @param headers Request header object
*/
public async patch<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: Object.assign(
{
'User-Agent': this.userAgent,
// To avoid: https://github.com/axios/axios/issues/5346
'Accept-Encoding': 'gzip,deflate,compress'
},
headers
),
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios
.patch<T>(this.baseUrl + path, params, options)
.catch((err: Error) => {
if (axios.isCancel(err)) {
throw new RequestCanceledError(err.message)
} else {
throw err
}
})
.then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* PATCH request to mastodon REST API for multipart.
* @param path relative path from baseUrl
* @param params Form data. If you want to post file, please use FormData()
* @param headers Request header object
*/
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: Object.assign(
{
'User-Agent': this.userAgent,
// To avoid: https://github.com/axios/axios/issues/5346
'Accept-Encoding': 'gzip,deflate,compress'
},
headers
),
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios
.patchForm<T>(this.baseUrl + path, params, options)
.catch((err: Error) => {
if (axios.isCancel(err)) {
throw new RequestCanceledError(err.message)
} else {
throw err
}
})
.then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* POST request to mastodon REST API.
* @param path relative path from baseUrl
* @param params Form data
* @param headers Request header object
*/
public async post<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: Object.assign(
{
'User-Agent': this.userAgent,
// To avoid: https://github.com/axios/axios/issues/5346
'Accept-Encoding': 'gzip,deflate,compress'
},
headers
),
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios.post<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* POST request to mastodon REST API for multipart.
* @param path relative path from baseUrl
* @param params Form data
* @param headers Request header object
*/
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: Object.assign(
{
'User-Agent': this.userAgent,
// To avoid: https://github.com/axios/axios/issues/5346
'Accept-Encoding': 'gzip,deflate,compress'
},
headers
),
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios.postForm<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* DELETE request to mastodon REST API.
* @param path relative path from baseUrl
* @param params Form data
* @param headers Request header object
*/
public async del<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
data: params,
headers: Object.assign(
{
'User-Agent': this.userAgent,
// To avoid: https://github.com/axios/axios/issues/5346
'Accept-Encoding': 'gzip,deflate,compress'
},
headers
),
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.accessToken) {
options = objectAssignDeep({}, options, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
})
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios
.delete(this.baseUrl + path, options)
.catch((err: Error) => {
if (axios.isCancel(err)) {
throw new RequestCanceledError(err.message)
} else {
throw err
}
})
.then((resp: AxiosResponse) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}
/**
* Cancel all requests in this instance.
* @returns void
*/
public cancel(): void {
return this.abortController.abort()
}
/**
* Get connection and receive websocket connection for Pleroma API.
*
* @param path relative path from baseUrl: normally it is `/streaming`.
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
* @returns WebSocket, which inherits from EventEmitter
*/
public socket(path: string, stream: string, params?: string): WebSocket {
if (!this.accessToken) {
throw new Error('accessToken is required')
}
const url = this.baseUrl + path
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent, this.proxyConfig)
process.nextTick(() => {
streaming.start()
})
return streaming
}
}
}
export default PleromaAPI