From 7548c54676f9cc7e0ceaae11169bfd418d0cc7b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9C=9E=E9=A3=9B?= Date: Mon, 4 Mar 2024 10:54:03 +0800 Subject: [PATCH] feat: support fetch API produced in Koishi v4.17.0 (#138) * fix: encode data * chore: use Object * fix: use the best pratice one --------- Co-authored-by: Maiko Tan --- packages/pixiv/src/index.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/pixiv/src/index.ts b/packages/pixiv/src/index.ts index fee272963..5f07d458d 100644 --- a/packages/pixiv/src/index.ts +++ b/packages/pixiv/src/index.ts @@ -1,4 +1,4 @@ -import { Context, Schema, trimSlash } from 'koishi' +import { Context, Quester, Schema, trimSlash } from 'koishi' import { ImageSource } from 'koishi-plugin-booru' import { PixivAppApi } from './types' @@ -73,6 +73,9 @@ class PixivImageSource extends ImageSource { } }) } catch (err) { + if (Quester.Error.is(err)) { + throw new Error('get pixiv image failed: ' + `${err.message} (${err.response?.status})`) + } return } } @@ -81,17 +84,15 @@ class PixivImageSource extends ImageSource { const endpoint = 'https://oauth.secure.pixiv.net/' // OAuth Endpoint const url = trimSlash(endpoint) + '/auth/token' - const data = { - get_secure_url: true, + const data = new URLSearchParams({ + get_secure_url: 'true', client_id: CLIENT_ID, client_secret: CLIENT_SECRET, grant_type: 'refresh_token', refresh_token: this.refreshToken, - } + }) - const resp = await this.ctx.http.axios(url, { - method: 'POST', - data, + const resp = await this.ctx.http.post(url, data, { headers: { ...this._getHeaders(), 'Content-Type': 'application/x-www-form-urlencoded',