From e81dfba70a83d9b17dc977a7fa3d3eb3329603f4 Mon Sep 17 00:00:00 2001 From: Last2014 Date: Sat, 24 Jan 2026 11:19:19 +0900 Subject: [PATCH] v1.1.0 --- README.md | 14 +- package.json | 2 +- src/1.6.8/parser.ts | 85 +++++++++++ src/1.6.8/types/api/ueuse/create.d.ts | 4 +- src/1.6.8/types/modules/ueuse.d.ts | 206 ++++---------------------- src/index.ts | 39 ++--- 6 files changed, 145 insertions(+), 205 deletions(-) create mode 100644 src/1.6.8/parser.ts diff --git a/README.md b/README.md index 91a265b..82dad2e 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,13 @@ ## インストール ```bash # npm -npm install git+https://gitea.last2014.com/last2014/better-uwuzu-sdk.git#1.0.3 +npm install git+https://gitea.last2014.com/last2014/better-uwuzu-sdk.git#1.1.0 # yarn -yarn add git+https://gitea.last2014.com/last2014/better-uwuzu-sdk.git#1.0.3 +yarn add git+https://gitea.last2014.com/last2014/better-uwuzu-sdk.git#1.1.0 # pnpm -pnpm add git+https://gitea.last2014.com/last2014/better-uwuzu-sdk.git#1.0.3 +pnpm add git+https://gitea.last2014.com/last2014/better-uwuzu-sdk.git#1.1.0 ``` > **NOTE** > このSDKはnpmリポジトリがありません。Gitリポジトリを使用しています。 @@ -22,10 +22,12 @@ pnpm add git+https://gitea.last2014.com/last2014/better-uwuzu-sdk.git#1.0.3 ## 初期化 ```ts import uwuzu from "better-uwuzu-sdk"; -import type ApiMap from "better-uwuzu-sdk/1.6.11/map"; +import type ApiMap from "better-uwuzu-sdk/1.6.8/map"; +import Parser from "better-uwuzu-sdk/1.6.8/parser"; const client = new uwuzu({ origin: "https://uwuzu.net", + parser: Parser, }); try { @@ -39,10 +41,12 @@ try { ## APIリクエスト ```ts import uwuzu from "better-uwuzu-sdk"; -import type ApiMap from "better-uwuzu-sdk/1.6.11/map"; +import type ApiMap from "better-uwuzu-sdk/1.6.8/map"; +import Parser from "better-uwuzu-sdk/1.6.8/parser"; const client = new uwuzu({ origin: "https://uwuzu.net", + parser: Parser, }); try { diff --git a/package.json b/package.json index 9b3adb2..639245e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "better-uwuzu-sdk", - "version": "1.0.3", + "version": "1.1.0", "description": "A better uwuzu SDK.", "main": "dist/index.js", "types": "dist/types/index.d.ts", diff --git a/src/1.6.8/parser.ts b/src/1.6.8/parser.ts new file mode 100644 index 0000000..c418a78 --- /dev/null +++ b/src/1.6.8/parser.ts @@ -0,0 +1,85 @@ +import type { parserType } from "../index"; + +const mediaToObj = (ueuse: any) => { + const media = { + photo: [] as string[], + video: [] as string[], + }; + + if (ueuse.photo1 !== "" || ueuse.video1 !== "") { + if (ueuse.photo1 !== "") { + media.photo.push(ueuse.photo1); + delete ueuse.photo1; + } + if (ueuse.photo2 !== "") { + media.photo.push(ueuse.photo2); + delete ueuse.photo2; + } + if (ueuse.photo3 !== "") { + media.photo.push(ueuse.photo3); + delete ueuse.photo3; + } + if (ueuse.photo4 !== "") { + media.photo.push(ueuse.photo4); + delete ueuse.photo4; + } + if (ueuse.video1 !== "") { + media.video.push(ueuse.video1); + delete ueuse.video1; + } + } + + ueuse.media = media; + + return ueuse; +} + +const Parser: parserType = (data, type, endpoint) => { + let result = data; + + if (type === "request") { + if (result.media && result.text && endpoint === "ueuse/create") { + for (let i = 0; i < result.media.photo.length; i++) { + result[`photo${i + 1}`] = result.media.photo[i]; + } + + for (let i = 0; i < result.media.video.length; i++) { + result[`video${i + 1}`] = result.media.video[i]; + } + } + + return result; + } else { + if (result["0"] !== undefined && result.success === true) { + delete result.success; + + const objData: any[] = Object.values(result); + + for (let i = 0; i < objData.length; i++) { + if (endpoint.indexOf("ueuse") !== -1) { + objData[i] = mediaToObj(objData[i]); + } + } + + return { + success: true, + data: objData, + }; + } + + if (result.favorite_list !== undefined && result.success === true) { + delete result.success; + + const list = result.favorite_list.split(","); + + return { + success: true, + favorite_list: list, + }; + } + + return result; + } +} + +export default Parser; \ No newline at end of file diff --git a/src/1.6.8/types/api/ueuse/create.d.ts b/src/1.6.8/types/api/ueuse/create.d.ts index 0d1f10c..c0ef622 100644 --- a/src/1.6.8/types/api/ueuse/create.d.ts +++ b/src/1.6.8/types/api/ueuse/create.d.ts @@ -3,10 +3,12 @@ import AuthError from "../../modules/error/auth"; import { Media } from "../../modules/ueuse"; export default interface UeuseCreate { - "ueuse/get": { + "ueuse/create": { body: { /** 本文 */ text: string; + /** メディア */ + media: Media; /** NSFWにするかどうか */ nsfw?: boolean; /** 返信先ユーズのユニークID */ diff --git a/src/1.6.8/types/modules/ueuse.d.ts b/src/1.6.8/types/modules/ueuse.d.ts index 5852eb1..e5e8183 100644 --- a/src/1.6.8/types/modules/ueuse.d.ts +++ b/src/1.6.8/types/modules/ueuse.d.ts @@ -1,111 +1,23 @@ -type Abi = { - /** - * 追記 - * 追記がない場合は空文字列です。 - */ - abi: string; - /** 追記時刻 */ - abidatetime: string; -} | { - /** - * 追記 - * 追記が入力できないため常に空文字列です。 - */ - abi: ""; - /** - * 追記時刻 - * 追記が入力できないため常に0000-00-00 00:00:00です。 - */ - abidatetime: "0000-00-00 00:00:00"; -} +export interface Media { + /** 画像URL */ + photo: string[]; + /** 動画URL */ + video: string[]; +}; -export type Media = - | { - /** - * 画像1URL - * 画像1がないため常に空文字列です。 - */ - photo1?: undefined; - /** - * 画像2URL - * 画像2がないため常に空文字列です。 - * 画像2は画像1が入力されていることの内包条件です。 - */ - photo2?: undefined; - /** - * 画像3URL - * 画像3がないため常に空文字列です。 - * 画像3は画像2が入力されていることの内包条件です。 - */ - photo3?: undefined; - /** - * 画像4URL - * 画像4がないため常に空文字列です。 - * 画像4は画像3が入力されていることの内包条件です。 - */ - photo4?: undefined; - /** - * 動画URL - * 動画がないため常に空文字列です。 - */ - video1?: undefined; - } | { - /** 画像1URL */ - photo1: string; - /** - * 画像2URL - * 画像2がない場合は空文字列です。 - * 画像2は画像1が入力されていることの内包条件です。 - */ - photo2?: string; - /** - * 画像3URL - * 画像3がない場合は空文字列です。 - * 画像3は画像2が入力されていることの内包条件です。 - */ - photo3?: string; - /** - * 画像4URL - * 画像4がない場合は空文字列です。 - * 画像4は画像3が入力されていることの内包条件です。 - */ - photo4?: string; - /** - * 動画URL - * 動画がないため常に空文字列です。 - */ - video1?: undefined; - } | { - /** 動画URL */ - video1: string; - /** - * 画像1URL - * 画像1がないため常に空文字列です。 - */ - photo1?: undefined; - /** - * 画像2URL - * 画像2がないため常に空文字列です。 - * 画像2は画像1が入力されていることの内包条件です。 - */ - photo2?: undefined; - /** - * 画像3URL - * 画像3がないため常に空文字列です。 - * 画像3は画像1が入力されていることの内包条件です。 - */ - photo3?: undefined; - /** - * 画像4URL - * 画像4がないため常に空文字列です。 - * 画像4は画像1が入力されていることの内包条件です。 - */ - photo4?: undefined; - }; - -interface ueuseBase { +export default interface ueuseModule { /** ユニークID */ uniqid: string; + /** + * 返信先ID + * 返信でない場合は空文字列です。 + */ + replyid: string | ""; + /** + * リユーズ/引用元ID + * リユーズ/引用でない場合は空文字列です。 + */ + reuseid: string | ""; /** 送信者 */ account: { /** ユーザー名 */ @@ -121,86 +33,30 @@ interface ueuseBase { }; /** いいねしたユーザーID */ favorite: string[]; + /** メディア */ + media: Media; /** いいね数 */ favorite_cnt: number; /** 返信数 */ reply_cnt: number; /** リユーズ+引用数 */ reuse_cnt: number; - /** 送信時刻 */ + /** + * 送信時刻 + * YYYY-MM-DD HH:MM:SS形式です。 + */ datetime: string; - /** NSFW(自主設定)かどうか */ - nsfw: boolean; -} - -interface NormalUeuse extends ueuseBase { - /** 本文 */ - text: string; - /** - * 返信先ID - * 返信でないため空文字列です。 - */ - replyid: ""; - /** - * リユーズ/引用元ID - * リユーズ/引用でないため空文字列です。 - */ - reuseid: ""; -} - -interface ReplyUeuse extends ueuseBase { - /** 本文 */ - text: string; - /** 返信先ID */ - replyid: string; - /** - * リユーズ/引用元ID - * リユーズ/引用でないため空文字列です。 - */ - reuseid: ""; -} - -interface Reuse extends ueuseBase { - /** - * 本文 - * リユーズであるため常に空文字列です。 - */ - text: ""; - /** - * 返信先ID - * 返信でないため空文字列です。 - */ - replyid: ""; - /** リユーズ/引用元ID */ - reuseid: string; /** * 追記 - * リユーズであるため常に空文字列です。 + * 追記がない場合は空文字列です。 */ - abi: ""; + abi: string | ""; /** * 追記時刻 - * リユーズであるため常に0000-00-00 00:00:00です。 + * 追記がない場合は0000-00-00 00:00:00です。 + * YYYY-MM-DD HH:MM:SS形式です。 */ - abidatetime: "0000-00-00 00:00:00"; -} - -interface QuoteReuse extends ueuseBase { - /** 本文 */ - text: string; - /** - * 返信先ID - * 返信でないため空文字列です。 - */ - replyid: ""; - /** 引用元ID */ - reuseid: string; -} - -type Ext = Abi & Media; - -export type ueuseModule = - | (NormalUeuse & Ext) - | (ReplyUeuse & Ext) - | (QuoteReuse & Ext) - | Reuse; \ No newline at end of file + abidatetime: string | "0000-00-00 00:00:00"; + /** NSFW(自主設定)かどうか */ + nsfw: boolean; +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index d961c58..4900374 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,19 @@ import uwuzuError from "./lib/error"; import uwuzuFetch from "./lib/fetch"; +export type parserType = ( + data: any, + type: "request" | "response", + endpoint: string, +) => any; + interface sdkOptions { /** uwuzuサーバーのorigin */ origin: string; + /** + * リクエスト/レスポンスのパーサー + */ + parser: parserType; /** * 通信に失敗した際の再試行回数です。 * 全て失敗した場合はエラーを発生します。 @@ -29,15 +39,18 @@ export default class uwuzu< M extends { [K in keyof M]: { body?: any; response: any } } > { readonly origin: string; + readonly parser: parserType; readonly retry: number; readonly waiting: number; private _token: string | null = null; constructor(options: sdkOptions) { this.origin = options.origin; + this.parser = options.parser; this.retry = options.retry ?? 5; this.waiting = options.waiting ?? 500; + if (!this.parser) throw new uwuzuError("Invalid parser."); if (this.retry < 1) throw new uwuzuError("Invalid retry count."); if (this.waiting < 1) throw new uwuzuError("Invalid base waiting time."); if (options.origin !== new URL(options.origin).origin) @@ -73,6 +86,8 @@ export default class uwuzu< let bodyParsed: any = args[0] ?? {}; if (typeof bodyParsed === "object") { + bodyParsed = this.parser(bodyParsed, "request", endpoint); + bodyParsed = { ...bodyParsed, token: this._token, @@ -93,29 +108,7 @@ export default class uwuzu< } ); - let res = await req.json(); - - if (res["0"] !== undefined && res.success === true) { - res.success = undefined; - - const data = Object.values(res).filter(Boolean); - - return { - success: true, - data, - }; - } - - if (res.favorite_list !== undefined && res.success === true) { - res.success = undefined; - - const list = res.favorite_list.split(","); - - return { - success: true, - favorite_list: list, - }; - } + const res = this.parser(await req.json(), "response", endpoint); return res; }