@@ -2,9 +2,20 @@ import { Readable } from "stream";
2
2
import { get , post , stream , del , postBinary } from "./http" ;
3
3
import * as Types from "./types" ;
4
4
import * as URL from "./urls" ;
5
- import { toArray , detectContentType } from "./util" ;
6
5
import { JSONParseError } from "./exceptions" ;
7
6
7
+ function toArray < T > ( maybeArr : T | T [ ] ) : T [ ] {
8
+ return Array . isArray ( maybeArr ) ? maybeArr : [ maybeArr ] ;
9
+ }
10
+
11
+ function checkJSON ( raw : any ) : any {
12
+ if ( typeof raw === "object" ) {
13
+ return raw ;
14
+ } else {
15
+ throw new JSONParseError ( "Failed to parse response body as JSON" , raw ) ;
16
+ }
17
+ }
18
+
8
19
export default class Client {
9
20
public config : Types . ClientConfig ;
10
21
@@ -47,29 +58,27 @@ export default class Client {
47
58
}
48
59
49
60
public getProfile ( userId : string ) : Promise < Types . Profile > {
50
- return this . get ( URL . profile ( userId ) ) . then ( this . checkJSON ) ;
61
+ return this . get ( URL . profile ( userId ) ) . then ( checkJSON ) ;
51
62
}
52
63
53
64
public getGroupMemberProfile (
54
65
groupId : string ,
55
66
userId : string ,
56
67
) : Promise < Types . Profile > {
57
- return this . get ( URL . groupMemberProfile ( groupId , userId ) ) . then (
58
- this . checkJSON ,
59
- ) ;
68
+ return this . get ( URL . groupMemberProfile ( groupId , userId ) ) . then ( checkJSON ) ;
60
69
}
61
70
62
71
public getRoomMemberProfile (
63
72
roomId : string ,
64
73
userId : string ,
65
74
) : Promise < Types . Profile > {
66
- return this . get ( URL . roomMemberProfile ( roomId , userId ) ) . then ( this . checkJSON ) ;
75
+ return this . get ( URL . roomMemberProfile ( roomId , userId ) ) . then ( checkJSON ) ;
67
76
}
68
77
69
78
public getGroupMemberIds ( groupId : string ) : Promise < string [ ] > {
70
79
const load = ( start ?: string ) : Promise < string [ ] > =>
71
80
this . get ( URL . groupMemberIds ( groupId , start ) )
72
- . then ( this . checkJSON )
81
+ . then ( checkJSON )
73
82
. then ( ( res : { memberIds : string [ ] ; next ?: string } ) => {
74
83
if ( ! res . next ) {
75
84
return res . memberIds ;
@@ -85,7 +94,7 @@ export default class Client {
85
94
public getRoomMemberIds ( roomId : string ) : Promise < string [ ] > {
86
95
const load = ( start ?: string ) : Promise < string [ ] > =>
87
96
this . get ( URL . roomMemberIds ( roomId , start ) )
88
- . then ( this . checkJSON )
97
+ . then ( checkJSON )
89
98
. then ( ( res : { memberIds : string [ ] ; next ?: string } ) => {
90
99
if ( ! res . next ) {
91
100
return res . memberIds ;
@@ -113,19 +122,19 @@ export default class Client {
113
122
public getRichMenu (
114
123
richMenuId : string ,
115
124
) : Promise < Types . RichMenuId & Types . RichMenu > {
116
- return this . get ( URL . richMenu ( richMenuId ) ) . then ( this . checkJSON ) ;
125
+ return this . get ( URL . richMenu ( richMenuId ) ) . then ( checkJSON ) ;
117
126
}
118
127
119
128
public createRichMenu ( richMenu : Types . RichMenu ) : Promise < Types . RichMenuId > {
120
- return this . post ( URL . richMenu ( ) , richMenu ) . then ( this . checkJSON ) ;
129
+ return this . post ( URL . richMenu ( ) , richMenu ) . then ( checkJSON ) ;
121
130
}
122
131
123
132
public deleteRichMenu ( richMenuId : string ) : Promise < any > {
124
133
return this . delete ( URL . richMenu ( richMenuId ) ) ;
125
134
}
126
135
127
136
public getUserRichMenuIds ( userId : string ) : Promise < Types . RichMenuId > {
128
- return this . get ( URL . userRichMenu ( userId ) ) . then ( this . checkJSON ) ;
137
+ return this . get ( URL . userRichMenu ( userId ) ) . then ( checkJSON ) ;
129
138
}
130
139
131
140
public linkRichMenuToUser ( userId : string , richMenuId : string ) : Promise < any > {
@@ -152,7 +161,7 @@ export default class Client {
152
161
}
153
162
154
163
public getRichMenuList ( ) : Promise < Array < Types . RichMenuId & Types . RichMenu > > {
155
- return this . get ( URL . richMenuList ( ) ) . then ( this . checkJSON ) ;
164
+ return this . get ( URL . richMenuList ( ) ) . then ( checkJSON ) ;
156
165
}
157
166
158
167
private authHeader ( ) : { [ key : string ] : string } {
@@ -182,12 +191,4 @@ export default class Client {
182
191
private stream ( url : string ) : Promise < Readable > {
183
192
return stream ( url , this . authHeader ( ) ) ;
184
193
}
185
-
186
- private checkJSON ( raw : any ) : any {
187
- if ( typeof raw === "object" ) {
188
- return raw ;
189
- } else {
190
- throw new JSONParseError ( "Failed to parse response body as JSON" , raw ) ;
191
- }
192
- }
193
194
}
0 commit comments