Skip to content

Commit

Permalink
fix: post login request broken in #138 (#139)
Browse files Browse the repository at this point in the history
* fix: post login request broken im #138

* fix: redundant .data

* fix: add error reporting
  • Loading branch information
MaikoTan authored Mar 4, 2024
1 parent 7548c54 commit 4dec198
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions packages/pixiv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ class PixivImageSource extends ImageSource<PixivImageSource.Config> {
} catch (err) {
if (Quester.Error.is(err)) {
throw new Error('get pixiv image failed: ' + `${err.message} (${err.response?.status})`)
} else {
throw new Error('get pixiv image failed: ' + err)
}
return
}
}

Expand All @@ -92,27 +93,30 @@ class PixivImageSource extends ImageSource<PixivImageSource.Config> {
refresh_token: this.refreshToken,
})

const resp = await this.ctx.http.post(url, data, {
headers: {
...this._getHeaders(),
'Content-Type': 'application/x-www-form-urlencoded',
'host': 'oauth.secure.pixiv.net',
},
validateStatus: () => true,
})

const SUCCESS_STATUS = [200, 301, 302]
if (!SUCCESS_STATUS.includes(resp.status)) {
throw new Error('Login failed with status code ' + resp.status + JSON.stringify((resp as any).data))
}
try {
const resp = await this.ctx.http.post(url, data, {
headers: {
...this._getHeaders(),
'Content-Type': 'application/x-www-form-urlencoded',
'host': 'oauth.secure.pixiv.net',
},
validateStatus: (status) => [200, 301, 302].includes(status),
})

this.userId = resp.data.user.id
this.accessToken = resp.data.access_token
this.refreshToken = resp.data.refresh_token
if (this.refreshTime) clearTimeout(this.refreshTime)
this.refreshTime = setTimeout(() => (this.accessToken = undefined), resp.data.expires_in * 1000)
this.userId = resp.user.id
this.accessToken = resp.access_token
this.refreshToken = resp.refresh_token
if (this.refreshTime) clearTimeout(this.refreshTime)
this.refreshTime = setTimeout(() => (this.accessToken = undefined), resp.expires_in * 1000)

return this.accessToken
return this.accessToken
} catch (err) {
if (Quester.Error.is(err)) {
throw new Error('Login failed with status code ' + err.response?.status + '\n' + JSON.stringify(err.response))
} else {
throw new Error('Login failed with unknown error: ' + err.message)
}
}
}

_getHeaders() {
Expand Down

0 comments on commit 4dec198

Please sign in to comment.