@@ -3,6 +3,7 @@ import { get, post, stream, del, postBinary } from "./http";
3
3
import * as Types from "./types" ;
4
4
import * as URL from "./urls" ;
5
5
import { toArray , detectContentType } from "./util" ;
6
+ import { JSONParseError } from "./exceptions" ;
6
7
7
8
export default class Client {
8
9
public config : Types . ClientConfig ;
@@ -22,7 +23,7 @@ export default class Client {
22
23
return this . post ( URL . push , {
23
24
messages : toArray ( messages ) ,
24
25
to,
25
- } ) ;
26
+ } ) /*.then(this.checkJSON)*/ ;
26
27
}
27
28
28
29
public replyMessage (
@@ -32,7 +33,7 @@ export default class Client {
32
33
return this . post ( URL . reply , {
33
34
messages : toArray ( messages ) ,
34
35
replyToken,
35
- } ) ;
36
+ } ) . then ( this . checkJSON ) ;
36
37
}
37
38
38
39
public multicast (
@@ -42,52 +43,58 @@ export default class Client {
42
43
return this . post ( URL . multicast , {
43
44
messages : toArray ( messages ) ,
44
45
to,
45
- } ) ;
46
+ } ) . then ( this . checkJSON ) ;
46
47
}
47
48
48
49
public getProfile ( userId : string ) : Promise < Types . Profile > {
49
- return this . get ( URL . profile ( userId ) ) ;
50
+ return this . get ( URL . profile ( userId ) ) . then ( this . checkJSON ) ;
50
51
}
51
52
52
53
public getGroupMemberProfile (
53
54
groupId : string ,
54
55
userId : string ,
55
56
) : Promise < Types . Profile > {
56
- return this . get ( URL . groupMemberProfile ( groupId , userId ) ) ;
57
+ return this . get ( URL . groupMemberProfile ( groupId , userId ) ) . then (
58
+ this . checkJSON ,
59
+ ) ;
57
60
}
58
61
59
62
public getRoomMemberProfile (
60
63
roomId : string ,
61
64
userId : string ,
62
65
) : Promise < Types . Profile > {
63
- return this . get ( URL . roomMemberProfile ( roomId , userId ) ) ;
66
+ return this . get ( URL . roomMemberProfile ( roomId , userId ) ) . then ( this . checkJSON ) ;
64
67
}
65
68
66
69
public getGroupMemberIds ( groupId : string ) : Promise < string [ ] > {
67
70
const load = ( start ?: string ) : Promise < string [ ] > =>
68
- this . get (
69
- URL . groupMemberIds ( groupId , start ) ,
70
- ) . then ( ( res : { memberIds : string [ ] ; next ?: string } ) => {
71
- if ( ! res . next ) {
72
- return res . memberIds ;
73
- }
74
-
75
- return load ( res . next ) . then ( extraIds => res . memberIds . concat ( extraIds ) ) ;
76
- } ) ;
71
+ this . get ( URL . groupMemberIds ( groupId , start ) )
72
+ . then ( this . checkJSON )
73
+ . then ( ( res : { memberIds : string [ ] ; next ?: string } ) => {
74
+ if ( ! res . next ) {
75
+ return res . memberIds ;
76
+ }
77
+
78
+ return load ( res . next ) . then ( extraIds =>
79
+ res . memberIds . concat ( extraIds ) ,
80
+ ) ;
81
+ } ) ;
77
82
return load ( ) ;
78
83
}
79
84
80
85
public getRoomMemberIds ( roomId : string ) : Promise < string [ ] > {
81
86
const load = ( start ?: string ) : Promise < string [ ] > =>
82
- this . get (
83
- URL . roomMemberIds ( roomId , start ) ,
84
- ) . then ( ( res : { memberIds : string [ ] ; next ?: string } ) => {
85
- if ( ! res . next ) {
86
- return res . memberIds ;
87
- }
88
-
89
- return load ( res . next ) . then ( extraIds => res . memberIds . concat ( extraIds ) ) ;
90
- } ) ;
87
+ this . get ( URL . roomMemberIds ( roomId , start ) )
88
+ . then ( this . checkJSON )
89
+ . then ( ( res : { memberIds : string [ ] ; next ?: string } ) => {
90
+ if ( ! res . next ) {
91
+ return res . memberIds ;
92
+ }
93
+
94
+ return load ( res . next ) . then ( extraIds =>
95
+ res . memberIds . concat ( extraIds ) ,
96
+ ) ;
97
+ } ) ;
91
98
return load ( ) ;
92
99
}
93
100
@@ -96,32 +103,31 @@ export default class Client {
96
103
}
97
104
98
105
public leaveGroup ( groupId : string ) : Promise < any > {
99
- return this . post ( URL . leaveGroup ( groupId ) ) ;
106
+ return this . post ( URL . leaveGroup ( groupId ) ) . then ( this . checkJSON ) ;
100
107
}
101
108
102
109
public leaveRoom ( roomId : string ) : Promise < any > {
103
- return this . post ( URL . leaveRoom ( roomId ) ) ;
110
+ return this . post ( URL . leaveRoom ( roomId ) ) . then ( this . checkJSON ) ;
104
111
}
105
112
106
113
public getRichMenu (
107
114
richMenuId : string ,
108
115
) : Promise < Types . RichMenuId & Types . RichMenu > {
109
- return this . get ( URL . richMenu ( richMenuId ) ) ;
116
+ return this . get ( URL . richMenu ( richMenuId ) ) . then ( this . checkJSON ) ;
110
117
}
111
118
112
119
public createRichMenu ( richMenu : Types . RichMenu ) : Promise < Types . RichMenuId > {
113
- return this . post ( URL . richMenu ( ) , richMenu ) ;
120
+ return this . post ( URL . richMenu ( ) , richMenu ) . then ( this . checkJSON ) ;
114
121
}
115
122
116
123
public deleteRichMenu ( richMenuId : string ) : Promise < any > {
117
124
return this . delete ( URL . richMenu ( richMenuId ) ) ;
118
125
}
119
126
120
127
public getUserRichMenuIds ( userId : string ) : Promise < Types . RichMenuId > {
121
- return this . get ( URL . userRichMenu ( userId ) ) ;
128
+ return this . get ( URL . userRichMenu ( userId ) ) . then ( this . checkJSON ) ;
122
129
}
123
130
124
- // TODO: change return type to appropriate one
125
131
public linkRichMenuToUser ( userId : string , richMenuId : string ) : Promise < any > {
126
132
return this . post ( URL . userRichMenu ( userId , richMenuId ) ) ;
127
133
}
@@ -146,7 +152,7 @@ export default class Client {
146
152
}
147
153
148
154
public getRichMenuList ( ) : Promise < Array < Types . RichMenuId & Types . RichMenu > > {
149
- return this . get ( URL . richMenuList ( ) ) ;
155
+ return this . get ( URL . richMenuList ( ) ) . then ( this . checkJSON ) ;
150
156
}
151
157
152
158
private authHeader ( ) : { [ key : string ] : string } {
@@ -176,4 +182,12 @@ export default class Client {
176
182
private stream ( url : string ) : Promise < Readable > {
177
183
return stream ( url , this . authHeader ( ) ) ;
178
184
}
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
+ }
179
193
}
0 commit comments