@@ -52,22 +52,21 @@ export class TraktClient extends BaseTraktClient {
52
52
/**
53
53
* Exchanges an authorization code or refresh token for an access token.
54
54
*
55
- * @private
56
- *
57
55
* @param request - The request object containing the code or refresh token.
58
56
*
59
57
* @returns A promise resolving to the updated Trakt authentication information.
60
58
*
61
59
* @throws Error Throws an error if the exchange fails or an error is received from the server.
62
60
*
61
+ * @private
62
+ *
63
63
* @see handleError
64
- * @memberof TraktClient
65
64
*/
66
65
private async _exchange ( request : Pick < TraktAuthenticationCodeRequest , 'code' > | Pick < TraktAuthenticationRefreshRequest , 'refresh_token' > ) {
67
66
const _request : TraktAuthenticationBaseRequest = {
68
- client_id : this . _settings . client_id ,
69
- client_secret : this . _settings . client_secret ,
70
- redirect_uri : this . _settings . redirect_uri ,
67
+ client_id : this . settings . client_id ,
68
+ client_secret : this . settings . client_secret ,
69
+ redirect_uri : this . settings . redirect_uri ,
71
70
grant_type : 'code' in request ? 'authorization_code' : 'refresh_token' ,
72
71
...request ,
73
72
} ;
@@ -98,24 +97,23 @@ export class TraktClient extends BaseTraktClient {
98
97
/**
99
98
* Revokes the current authentication by invalidating the access token.
100
99
*
101
- * @private
102
- *
103
100
* @paramrequest - Additional parameters for revoking authentication.
104
101
*
105
102
* @returns A promise resolving when the authentication is successfully revoked.
106
103
*
107
104
* @throws Error Throws an error if no access token is found.
108
105
*
106
+ * @private
107
+ *
109
108
* @see isResponseOk
110
- * @memberof TraktClient
111
109
*/
112
110
private async _revoke ( request : Partial < TraktAuthenticationRevokeRequest > = { } ) {
113
111
if ( ! request && ! this . auth . access_token ) throw new Error ( 'No access token found.' ) ;
114
112
115
113
const _request : TraktAuthenticationRevokeRequest = {
116
114
token : this . auth . access_token ! ,
117
- client_id : this . _settings . client_id ,
118
- client_secret : this . _settings . client_secret ,
115
+ client_id : this . settings . client_id ,
116
+ client_secret : this . settings . client_secret ,
119
117
...request ,
120
118
} ;
121
119
@@ -129,8 +127,6 @@ export class TraktClient extends BaseTraktClient {
129
127
/**
130
128
* Initiates device authentication and retrieves the device code.
131
129
*
132
- * @private
133
- *
134
130
* @template T - The type of the authentication information to be returned (string means auth token, null means codes).
135
131
*
136
132
* @param {T extends string | null } code - The device code (if polling) or null to initiate a new device authentication.
@@ -139,21 +135,22 @@ export class TraktClient extends BaseTraktClient {
139
135
*
140
136
* @throws Error Throws an error if the device authentication fails.
141
137
*
138
+ * @private
139
+ *
142
140
* @see handleError
143
- * @memberof TraktClient
144
141
*/
145
142
private async _device < T extends string | null > ( code : T ) : Promise < T extends null ? TraktDeviceAuthentication : TraktAuthentication > {
146
143
try {
147
144
let response : TraktApiResponse < TraktAuthentication | TraktDeviceAuthentication > ;
148
145
if ( code ) {
149
146
response = await this . authentication . device . token ( {
150
- client_id : this . _settings . client_id ,
151
- client_secret : this . _settings . client_secret ,
147
+ client_id : this . settings . client_id ,
148
+ client_secret : this . settings . client_secret ,
152
149
code,
153
150
} ) ;
154
151
} else {
155
152
response = await this . authentication . device . code ( {
156
- client_id : this . _settings . client_id ,
153
+ client_id : this . settings . client_id ,
157
154
} ) ;
158
155
}
159
156
return ( await response . json ( ) ) as T extends null ? TraktDeviceAuthentication : TraktAuthentication ;
@@ -162,6 +159,18 @@ export class TraktClient extends BaseTraktClient {
162
159
}
163
160
}
164
161
162
+ /**
163
+ * Polls the device authentication endpoint to complete the authentication.
164
+ * If the timeout is reached, the polling is cancelled and an error is thrown.
165
+ * If the authentication is successful, the polling is cancelled and the authentication information is returned.
166
+ *
167
+ * @param poll - The device authentication information.
168
+ * @param timeout - The timeout in milliseconds.
169
+ *
170
+ * @returns A promise resolving to the authentication information if successful
171
+ *
172
+ * @private
173
+ */
165
174
private async _devicePolling ( poll : TraktDeviceAuthentication , timeout : number ) {
166
175
if ( timeout <= Date . now ( ) ) {
167
176
clearInterval ( this . polling ) ;
@@ -198,8 +207,6 @@ export class TraktClient extends BaseTraktClient {
198
207
* The code should then be used in conjunction with the {@link pollWithDeviceCode} method to finish authentication.
199
208
*
200
209
* @returns A promise resolving to the device authentication information.
201
- *
202
- * @memberof TraktClient
203
210
*/
204
211
getDeviceCode ( ) {
205
212
return this . _device ( null ) ;
@@ -211,8 +218,6 @@ export class TraktClient extends BaseTraktClient {
211
218
* @param poll - The device authentication information.
212
219
*
213
220
* @returns A promise resolving to the completed authentication information or `undefined`.
214
- *
215
- * @memberof TraktClient
216
221
*/
217
222
pollWithDeviceCode ( poll : TraktDeviceAuthentication ) {
218
223
if ( this . polling ) {
@@ -244,8 +249,6 @@ export class TraktClient extends BaseTraktClient {
244
249
* @returns A promise resolving to the response from the Trakt website.
245
250
*
246
251
* @see [authorize]{@link https://trakt.docs.apiary.io/#reference/authentication-oauth/authorize}
247
- *
248
- * @memberof TraktClient
249
252
*/
250
253
redirectToAuthentication ( request : Pick < TraktAuthenticationAuthorizeRequest , 'state' | 'signup' | 'prompt' > & { redirect ?: RequestRedirect } = { } ) {
251
254
this . updateAuth ( auth => ( { ...auth , state : request . state ?? randomHex ( ) } ) ) ;
@@ -257,8 +260,8 @@ export class TraktClient extends BaseTraktClient {
257
260
return this . authentication . oAuth . authorize (
258
261
{
259
262
response_type : 'code' ,
260
- client_id : this . _settings . client_id ,
261
- redirect_uri : this . _settings . redirect_uri ,
263
+ client_id : this . settings . client_id ,
264
+ redirect_uri : this . settings . redirect_uri ,
262
265
state : this . auth . state ,
263
266
...request ,
264
267
} ,
@@ -275,8 +278,6 @@ export class TraktClient extends BaseTraktClient {
275
278
* @returns A promise resolving to the Trakt authentication information.
276
279
*
277
280
* @throws Error Throws an error if the CSRF token is invalid.
278
- *
279
- * @memberof TraktClient
280
281
*/
281
282
exchangeCodeForToken ( code : string , state ?: string ) {
282
283
if ( state && state !== this . auth . state ) throw Error ( 'Invalid CSRF (State)' ) ;
@@ -289,8 +290,6 @@ export class TraktClient extends BaseTraktClient {
289
290
* @returns A promise resolving to the updated Trakt authentication information.
290
291
*
291
292
* @throws Error Throws an error if no refresh token is found.
292
- *
293
- * @memberof TraktClient
294
293
*/
295
294
refreshToken ( ) {
296
295
if ( ! this . auth . refresh_token ) {
@@ -305,8 +304,6 @@ export class TraktClient extends BaseTraktClient {
305
304
* @returns A promise resolving when the authentication is successfully revoked.
306
305
*
307
306
* @throws Error Throws an error if no access token is found.
308
- *
309
- * @memberof TraktClient
310
307
*/
311
308
async revokeAuthentication ( ) : Promise < void > {
312
309
if ( this . auth . access_token ) {
@@ -322,8 +319,6 @@ export class TraktClient extends BaseTraktClient {
322
319
* @param auth - The Trakt authentication information to import.
323
320
*
324
321
* @returns A promise resolving to the imported Trakt authentication information.
325
- *
326
- * @memberof TraktClient
327
322
*/
328
323
async importAuthentication ( auth : TraktClientAuthentication ) : Promise < TraktClientAuthentication > {
329
324
this . updateAuth ( auth ) ;
0 commit comments