hana-music-api 7.0.0 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -30
- package/dist/src/core/call-context.js +0 -1
- package/dist/src/core/errors.js +2 -2
- package/dist/src/core/option-builder.d.ts +0 -1
- package/dist/src/core/option-builder.js +0 -1
- package/dist/src/core/options.js +1 -1
- package/dist/src/core/request.js +1 -1
- package/dist/src/core/service-metadata.js +1 -1
- package/dist/src/core/transport.js +2 -23
- package/dist/src/modules/cloud.d.ts +3 -0
- package/dist/src/modules/cloud.js +9 -15
- package/dist/src/modules/login_qr_create.d.ts +0 -2
- package/dist/src/modules/login_qr_create.js +4 -19
- package/dist/src/modules/verify_getQr.d.ts +0 -1
- package/dist/src/modules/verify_getQr.js +2 -12
- package/dist/src/types/request.d.ts +0 -1
- package/dist/src/types/sdk.d.ts +1 -2
- package/package.json +3 -18
- package/dist/src/core/optional-dependency.js +0 -16
package/README.md
CHANGED
|
@@ -1,27 +1,41 @@
|
|
|
1
1
|
# hana-music-api
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
基于 TypeScript、Effect 与 Hono 构建的第三方网易云音乐 API 实现。既可作为 ESM TypeScript SDK 在 Node.js 与 Bun 应用中调用,也可从仓库源码启动独立的 HTTP 服务。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 核心特性
|
|
6
|
+
|
|
7
|
+
- **按需导入与全量客户端**:支持通过 `./api/*` 与 `createClient` 按需组合接口,也提供包含全部 352 个接口的 `createHanaMusicApi`。
|
|
8
|
+
- **端到端类型与契约校验**:提供完整的 TypeScript 输入类型提示,并为已建模返回体提供 Standard Schema v1 校验器与类型推导。
|
|
9
|
+
- **结构化并发与防重入**:基于 Effect 统一管理全链路超时时间、`AbortSignal` 取消、并发请求去重与实例级读缓存。
|
|
10
|
+
- **开箱即用的 HTTP 服务**:通过 Bun 与 Hono 启动 HTTP 服务,内置入口限流、健康检查、静态文档托管与可视化调试台。
|
|
11
|
+
|
|
12
|
+
## 安装与环境要求
|
|
6
13
|
|
|
7
14
|
```bash
|
|
8
15
|
npm install hana-music-api
|
|
9
16
|
```
|
|
10
17
|
|
|
11
|
-
SDK 需要 Node.js 24
|
|
12
|
-
|
|
13
|
-
|
|
18
|
+
- **运行时要求**:SDK 需要 Node.js `>=24` 或 Bun,仅支持 ESM 模块。
|
|
19
|
+
- **极简依赖边界**:默认安装仅依赖 `effect`,不包含 `peerDependencies` 与额外多媒体或网络代理依赖。
|
|
20
|
+
- **传输扩展方式**:网络代理、连接池管理与日志埋点统一通过 `config.fetcher` 注入实现,详见 [代理与自定义 fetcher](website/guide/proxy-and-fetcher.md)。
|
|
21
|
+
- **浏览器 Watchman 支持**:需要在浏览器端获取风控 Watchman 令牌时,可使用独立子路径 `hana-music-api/watchman`,详见 [浏览器 Watchman](website/guide/watchman.md)。
|
|
14
22
|
|
|
15
|
-
##
|
|
23
|
+
## SDK 调用
|
|
16
24
|
|
|
17
|
-
|
|
25
|
+
推荐使用 `createClient` 搭配按需子路径导入接口,只打包实际使用的模块并共享实例级缓存与默认配置:
|
|
18
26
|
|
|
19
27
|
```ts
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
import { search } from 'hana-music-api/api/search';
|
|
29
|
+
import { songDetail } from 'hana-music-api/api/song_detail';
|
|
30
|
+
import { createClient } from 'hana-music-api/core';
|
|
31
|
+
|
|
32
|
+
const hana = createClient(
|
|
33
|
+
{ search, songDetail },
|
|
34
|
+
{
|
|
35
|
+
cookie: 'MUSIC_U=your-cookie',
|
|
36
|
+
cache: { enabled: true },
|
|
37
|
+
},
|
|
38
|
+
);
|
|
25
39
|
|
|
26
40
|
const result = await hana.search({
|
|
27
41
|
keywords: '周杰伦',
|
|
@@ -31,41 +45,55 @@ const result = await hana.search({
|
|
|
31
45
|
console.log(result.body);
|
|
32
46
|
```
|
|
33
47
|
|
|
34
|
-
|
|
48
|
+
快速脚本或需要完整接口集合时,也可直接从根入口创建全量客户端:
|
|
35
49
|
|
|
36
|
-
|
|
50
|
+
```ts
|
|
51
|
+
import { createHanaMusicApi } from 'hana-music-api';
|
|
37
52
|
|
|
38
|
-
|
|
53
|
+
const hana = createHanaMusicApi({
|
|
54
|
+
cookie: 'MUSIC_U=your-cookie',
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const detail = await hana.songDetail({ ids: [347230] });
|
|
58
|
+
console.log(detail.body);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
- **统一返回结构**:所有接口返回 Promise,成功结果包含 `status`、`body` 与 `cookie` 字段,失败时抛出包含 `failure` 分类字段的响应对象。
|
|
62
|
+
- **多种调用方式**:除上述客户端方式外,还可直接调用独立具名函数或使用 `invokeModule` 按模块名动态调用,详见 [SDK 调用方式](website/guide/sdk-usage.md)。
|
|
39
63
|
|
|
40
64
|
## 启动 HTTP 服务
|
|
41
65
|
|
|
42
|
-
安装 Bun
|
|
66
|
+
安装 Bun 后,在仓库根目录启动 HTTP 服务:
|
|
43
67
|
|
|
44
68
|
```bash
|
|
45
69
|
bun install --frozen-lockfile
|
|
46
70
|
bun start
|
|
47
71
|
```
|
|
48
72
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
- 服务首页:`http://127.0.0.1:3021/`
|
|
52
|
-
- 健康检查:`http://127.0.0.1:3021/health`
|
|
53
|
-
- 文档:`http://127.0.0.1:3021/docs`,需先运行 `bun run docs:build`
|
|
73
|
+
- **默认监听地址**:服务默认监听 `0.0.0.0:3021`,可通过 `HOST` 与 `PORT` 环境变量覆盖。
|
|
74
|
+
- **基础端点**:`/` 为服务欢迎页,`/health` 为健康检查接口,`/demo` 为内置交互调试台,`/docs` 提供本地静态文档(需先运行 `bun run docs:build`)。
|
|
54
75
|
|
|
55
|
-
|
|
76
|
+
通过 `curl` 验证搜索接口:
|
|
56
77
|
|
|
57
78
|
```bash
|
|
58
79
|
curl 'http://127.0.0.1:3021/search?keywords=hello&limit=5'
|
|
59
80
|
```
|
|
60
81
|
|
|
61
|
-
HTTP
|
|
82
|
+
HTTP 路由仅接受业务参数以及 `cookie`、`noCookie` 控制字段。超时时间、传输层与重试策略须在服务端设置,详见 [HTTP 调用约定](website/guide/http-conventions.md) 与 [部署 HTTP 服务](website/guide/http-deployment.md)。
|
|
83
|
+
|
|
84
|
+
## 文档导航
|
|
85
|
+
|
|
86
|
+
在线文档站:[https://hana-music-api.netlify.app/](https://hana-music-api.netlify.app/)
|
|
62
87
|
|
|
63
|
-
|
|
88
|
+
- **入门与概览**:阅读 [项目简介](website/guide/introduction.md) 与 [快速开始](website/guide/getting-started.md)。
|
|
89
|
+
- **SDK 开发**:查看 [SDK 调用方式](website/guide/sdk-usage.md)、[包导出与子路径](website/guide/package-exports.md) 与 [认证机制](website/guide/authentication.md)。
|
|
90
|
+
- **配置与容错**:查看 [错误处理](website/guide/error-handling.md)、[配置参考](website/guide/config.md) 与 [重试、超时与连接策略](website/guide/resilience.md)。
|
|
91
|
+
- **HTTP 服务**:查看 [HTTP 调用约定](website/guide/http-conventions.md) 与 [部署 HTTP 服务](website/guide/http-deployment.md)。
|
|
92
|
+
- **架构与迁移**:查看 [架构设计与技术演进](website/guide/architecture.md)、[从 7.x 升级到 8.x](website/guide/migrating-to-v8.md) 与 [从 6.x 升级到 7.x](website/guide/migrating-to-v7.md)。
|
|
93
|
+
- **接口参考**:浏览 [API 参考目录](website/api/index.md) 与 [公共数据结构](website/api/shared-types.md)。
|
|
64
94
|
|
|
65
|
-
|
|
95
|
+
## 使用边界与频率控制
|
|
66
96
|
|
|
67
|
-
-
|
|
68
|
-
- [
|
|
69
|
-
-
|
|
70
|
-
- [调用约定](website/guide/request-convention.md)
|
|
71
|
-
- [API 参考](website/api/index.md)
|
|
97
|
+
- **无内置出口限速**:SDK 不在本地限制或排队控制请求发送频率,每次调用都会立即发送请求至上游。
|
|
98
|
+
- **限流应对方式**:调用方须自行控制并发与请求频率。上游返回 HTTP 429 或业务限流时,调用会以状态 `429` 失败并附带建议等待秒数 `body.retryAfter`(不会自动重试),详见 [重试、超时与连接策略](website/guide/resilience.md)。
|
|
99
|
+
- **第三方声明**:本项目为第三方 API 实现,仅供技术学习与研究使用,请遵守相关平台服务协议。
|
|
@@ -3,7 +3,6 @@ import * as Context from "effect/Context";
|
|
|
3
3
|
var ExecutionScope = class extends Context.Service()("hana-music-api/core/call-context/ExecutionScope") {};
|
|
4
4
|
const resolveExecutionScope = (options, execution, startedAt, timeoutMs, stageTimeoutMs) => Object.freeze({
|
|
5
5
|
fetcher: options.fetcher ?? execution?.fetcher,
|
|
6
|
-
proxy: options.proxy,
|
|
7
6
|
connectionStrategy: options.connectionStrategy,
|
|
8
7
|
onRequestEvent: options.onRequestEvent ?? execution?.onRequestEvent,
|
|
9
8
|
entropy: execution?.entropy,
|
package/dist/src/core/errors.js
CHANGED
|
@@ -92,5 +92,5 @@ var PartialUpload = class extends Schema.TaggedError()("PartialUpload", {
|
|
|
92
92
|
completedStages: Schema.Array(Schema.String),
|
|
93
93
|
cause: Schema.optional(Schema.Unknown)
|
|
94
94
|
}) {};
|
|
95
|
-
|
|
96
|
-
export { DeadlineExceeded, InvalidModuleInput, InvalidRequest,
|
|
95
|
+
Schema.TaggedError()("ModuleInvariantFailed", { message: Schema.String });
|
|
96
|
+
export { DeadlineExceeded, InvalidModuleInput, InvalidRequest, PartialUpload, ProtocolFailed, ResponseDecodeFailed, TargetRejected, TransportFailed, UnexpectedUpstreamShape, UpstreamBusinessFailed, UpstreamRateLimited };
|
|
@@ -12,7 +12,6 @@ interface OptionSource {
|
|
|
12
12
|
readonly headers?: CreateRequestOptions['headers'];
|
|
13
13
|
readonly ip?: unknown;
|
|
14
14
|
readonly onRequestEvent?: CreateRequestOptions['onRequestEvent'];
|
|
15
|
-
readonly proxy?: unknown;
|
|
16
15
|
readonly realIP?: unknown;
|
|
17
16
|
readonly retry?: CreateRequestOptions['retry'];
|
|
18
17
|
readonly signal?: AbortSignal;
|
|
@@ -11,7 +11,6 @@ const createOption = (query, crypto = "") => ({
|
|
|
11
11
|
headers: query.headers,
|
|
12
12
|
ip: toOptionalString(query.ip),
|
|
13
13
|
onRequestEvent: query.onRequestEvent,
|
|
14
|
-
proxy: toOptionalString(query.proxy),
|
|
15
14
|
realIP: toOptionalString(query.realIP),
|
|
16
15
|
retry: query.retry,
|
|
17
16
|
signal: query.signal,
|
package/dist/src/core/options.js
CHANGED
|
@@ -8,7 +8,6 @@ const moduleConfigKeys = [
|
|
|
8
8
|
"fetcher",
|
|
9
9
|
"ip",
|
|
10
10
|
"onRequestEvent",
|
|
11
|
-
"proxy",
|
|
12
11
|
"realIP",
|
|
13
12
|
"retry",
|
|
14
13
|
"signal",
|
|
@@ -17,6 +16,7 @@ const moduleConfigKeys = [
|
|
|
17
16
|
"checkToken"
|
|
18
17
|
];
|
|
19
18
|
const removedModuleConfigKeys = [
|
|
19
|
+
"proxy",
|
|
20
20
|
"crypto",
|
|
21
21
|
"ua",
|
|
22
22
|
"domain",
|
package/dist/src/core/request.js
CHANGED
|
@@ -189,7 +189,6 @@ const requestEffect = (request) => withRequestDeadline(Effect.gen(function* () {
|
|
|
189
189
|
},
|
|
190
190
|
identity: plan.identity,
|
|
191
191
|
method: plan.method,
|
|
192
|
-
proxy: scope.proxy,
|
|
193
192
|
fetcher: scope.fetcher
|
|
194
193
|
}, (response) => {
|
|
195
194
|
if (plan.protocol !== "plain") return interpretResponse(response, plan.protocol, plan.encryptResponse, plan.headers["x-aeapi"] === "true");
|
|
@@ -352,6 +351,7 @@ const runRequestAtEdge = async (input, options, execution) => {
|
|
|
352
351
|
};
|
|
353
352
|
const createRequest = async (uri, data, options = {}) => {
|
|
354
353
|
if (Object.hasOwn(options, "checkToken")) return InvalidRequest.make({ message: "checkToken is a module option; raw requests use explicit data/headers" }).pipe(Effect.fail, runPublicEffect);
|
|
354
|
+
if (Object.hasOwn(options, "proxy")) return InvalidRequest.make({ message: "proxy option has been removed; configure a custom fetcher instead" }).pipe(Effect.fail, runPublicEffect);
|
|
355
355
|
const response = await runRequestAtEdge({
|
|
356
356
|
target: uri,
|
|
357
357
|
protocol: options.crypto || requestProfiles.eapi.protocol,
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isMissingOptionalDependencyError, loadOptionalDependency } from "./optional-dependency.js";
|
|
1
|
+
import { ProtocolFailed, ResponseDecodeFailed, TransportFailed, UpstreamRateLimited } from "./errors.js";
|
|
3
2
|
import { classifyConsumedRateLimit, classifyHeaderRateLimit } from "./response.js";
|
|
4
3
|
import * as Clock from "effect/Clock";
|
|
5
4
|
import * as Effect from "effect/Effect";
|
|
6
5
|
import * as Result from "effect/Result";
|
|
7
6
|
// src/core/transport.ts
|
|
8
7
|
const transportEffect = (url, options, interpret) => {
|
|
9
|
-
if (options.proxy && options.fetcher) return Effect.fail(InvalidRequest.make({ message: "proxy cannot be combined with a custom fetcher" }));
|
|
10
8
|
const host = new URL(url).host;
|
|
11
9
|
const identity = options.identity ?? "anonymous";
|
|
12
10
|
return Effect.scoped(Effect.gen(function* () {
|
|
@@ -14,15 +12,6 @@ const transportEffect = (url, options, interpret) => {
|
|
|
14
12
|
const controller = yield* Effect.acquireRelease(Effect.sync(() => new AbortController()), (resource) => Effect.sync(() => {
|
|
15
13
|
if (!bodyConsumed) resource.abort();
|
|
16
14
|
}));
|
|
17
|
-
const proxyUrl = options.proxy && typeof Bun === "undefined" ? options.proxy : void 0;
|
|
18
|
-
const undici = proxyUrl ? yield* Effect.tryPromise({
|
|
19
|
-
try: () => loadOptionalDependency("undici"),
|
|
20
|
-
catch: (error) => isMissingOptionalDependencyError(error, "undici") ? InvalidRequest.make({ message: "Node.js proxy support requires installing optional peer dependency 'undici'" }) : TransportFailed.make({
|
|
21
|
-
message: error instanceof Error ? error.message : String(error),
|
|
22
|
-
cause: error
|
|
23
|
-
})
|
|
24
|
-
}) : void 0;
|
|
25
|
-
const agent = proxyUrl && undici ? yield* Effect.acquireRelease(Effect.sync(() => new undici.ProxyAgent(proxyUrl)), (resource) => Effect.promise(() => resource.destroy())) : void 0;
|
|
26
15
|
const response = yield* Effect.tryPromise({
|
|
27
16
|
try: (signal) => {
|
|
28
17
|
const init = {
|
|
@@ -36,17 +25,7 @@ const transportEffect = (url, options, interpret) => {
|
|
|
36
25
|
]),
|
|
37
26
|
redirect: "manual"
|
|
38
27
|
};
|
|
39
|
-
|
|
40
|
-
const undiciInit = {
|
|
41
|
-
...init,
|
|
42
|
-
dispatcher: agent
|
|
43
|
-
};
|
|
44
|
-
return undici.fetch(url, undiciInit);
|
|
45
|
-
}
|
|
46
|
-
return (options.fetcher ?? fetch)(url, options.proxy ? {
|
|
47
|
-
...init,
|
|
48
|
-
proxy: options.proxy
|
|
49
|
-
} : init);
|
|
28
|
+
return (options.fetcher ?? fetch)(url, init);
|
|
50
29
|
},
|
|
51
30
|
catch: (error) => TransportFailed.make({
|
|
52
31
|
message: error instanceof Error ? error.message : String(error),
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { InvalidModuleInput, UnexpectedUpstreamShape } from "../core/errors.js";
|
|
2
2
|
import { UploadedFile, decodeModuleInput as decodeModuleInput$1 } from "../core/module-input.js";
|
|
3
|
-
import { loadOptionalDependency } from "../core/optional-dependency.js";
|
|
4
3
|
import { uploadWork } from "../core/upload-work.js";
|
|
5
4
|
import { isRecord } from "../core/utils.js";
|
|
6
5
|
import song_upload_default, { songUploadActions } from "../plugins/song-upload.js";
|
|
@@ -39,7 +38,12 @@ const moduleActions = {
|
|
|
39
38
|
retry: "never"
|
|
40
39
|
}
|
|
41
40
|
};
|
|
42
|
-
const inputSchema = Schema.Struct({
|
|
41
|
+
const inputSchema = Schema.Struct({
|
|
42
|
+
album: Schema.optional(Schema.String),
|
|
43
|
+
artist: Schema.optional(Schema.String),
|
|
44
|
+
song: Schema.optional(Schema.String),
|
|
45
|
+
songFile: Schema.optional(UploadedFile)
|
|
46
|
+
});
|
|
43
47
|
const decodeModuleInput = (input) => decodeModuleInput$1(inputSchema, input);
|
|
44
48
|
const cloud = (input, request) => uploadWork("cloud", request, (stage) => Effect.gen(function* () {
|
|
45
49
|
if (!input.songFile) return yield* InvalidModuleInput.make({
|
|
@@ -73,16 +77,6 @@ const cloud = (input, request) => uploadWork("cloud", request, (stage) => Effect
|
|
|
73
77
|
expected: "needUpload and songId",
|
|
74
78
|
actual: typeof checked.body
|
|
75
79
|
})));
|
|
76
|
-
const tags = yield* Effect.tryPromise({
|
|
77
|
-
try: async () => {
|
|
78
|
-
return (await loadOptionalDependency("music-metadata")).parseBuffer(bytes, songFile.mimetype);
|
|
79
|
-
},
|
|
80
|
-
catch: () => InvalidModuleInput.make({ message: "无法读取音乐文件元数据" })
|
|
81
|
-
}).pipe(Effect.map((info) => info.common), Effect.orElseSucceed(() => ({
|
|
82
|
-
title: "",
|
|
83
|
-
album: "",
|
|
84
|
-
artist: ""
|
|
85
|
-
})));
|
|
86
80
|
const allocation = yield* stage("allocate", { data: {
|
|
87
81
|
bucket: "",
|
|
88
82
|
ext,
|
|
@@ -106,9 +100,9 @@ const cloud = (input, request) => uploadWork("cloud", request, (stage) => Effect
|
|
|
106
100
|
md5: songFile.md5,
|
|
107
101
|
songid: check.songId,
|
|
108
102
|
filename: songFile.name,
|
|
109
|
-
song:
|
|
110
|
-
album:
|
|
111
|
-
artist:
|
|
103
|
+
song: input.song || filename,
|
|
104
|
+
album: input.album || "未知专辑",
|
|
105
|
+
artist: input.artist || "未知艺术家",
|
|
112
106
|
bitrate: "999000",
|
|
113
107
|
resourceId: token.resourceId
|
|
114
108
|
} });
|
|
@@ -6,13 +6,11 @@ import "effect/Effect";
|
|
|
6
6
|
export type ModuleInput = {
|
|
7
7
|
key: string;
|
|
8
8
|
platform?: 'pc' | 'web';
|
|
9
|
-
qrimg?: boolean;
|
|
10
9
|
};
|
|
11
10
|
export declare const ModuleBody: BodySchema<{
|
|
12
11
|
readonly code: 200;
|
|
13
12
|
readonly data: {
|
|
14
13
|
readonly qrurl: string;
|
|
15
|
-
readonly qrimg: string;
|
|
16
14
|
};
|
|
17
15
|
}>;
|
|
18
16
|
export type ModuleBody = BodyType<typeof ModuleBody>;
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { Call } from "../core/call-context.js";
|
|
2
|
-
import {
|
|
3
|
-
import { BooleanValue, decodeModuleInput as decodeModuleInput$1 } from "../core/module-input.js";
|
|
4
|
-
import { isMissingOptionalDependencyError, loadOptionalDependency } from "../core/optional-dependency.js";
|
|
2
|
+
import { decodeModuleInput as decodeModuleInput$1 } from "../core/module-input.js";
|
|
5
3
|
import { defineModuleBody } from "../core/upstream-body.js";
|
|
6
4
|
import { generateChainId } from "../core/utils.js";
|
|
7
5
|
import * as Effect from "effect/Effect";
|
|
@@ -10,35 +8,22 @@ import * as Schema from "effect/Schema";
|
|
|
10
8
|
const moduleActions = {};
|
|
11
9
|
const inputSchema = Schema.Struct({
|
|
12
10
|
key: Schema.String,
|
|
13
|
-
platform: Schema.optional(Schema.Literals(["pc", "web"]))
|
|
14
|
-
qrimg: Schema.optional(BooleanValue)
|
|
11
|
+
platform: Schema.optional(Schema.Literals(["pc", "web"]))
|
|
15
12
|
});
|
|
16
13
|
const decodeModuleInput = (input) => decodeModuleInput$1(inputSchema, input);
|
|
17
14
|
const ModuleBody = /* @__PURE__ */ defineModuleBody(Schema.Struct({
|
|
18
15
|
code: Schema.Literal(200),
|
|
19
|
-
data: Schema.Struct({
|
|
20
|
-
qrurl: Schema.String,
|
|
21
|
-
qrimg: Schema.String
|
|
22
|
-
})
|
|
16
|
+
data: Schema.Struct({ qrurl: Schema.String })
|
|
23
17
|
}));
|
|
24
18
|
const loginQrCreate = (query) => Effect.gen(function* () {
|
|
25
19
|
const call = yield* Call;
|
|
26
20
|
let url = `https://music.163.com/login?codekey=${query.key}`;
|
|
27
21
|
if ((query.platform || "pc") === "web") url += `&chainId=${generateChainId(call.identity.cookie)}`;
|
|
28
|
-
const qrimg = query.qrimg ? yield* Effect.tryPromise({
|
|
29
|
-
try: async () => {
|
|
30
|
-
return (await loadOptionalDependency("qrcode")).toDataURL(url);
|
|
31
|
-
},
|
|
32
|
-
catch: (error) => ModuleInvariantFailed.make({ message: isMissingOptionalDependencyError(error, "qrcode") ? "Generating QR code images requires installing optional peer dependency 'qrcode'" : String(error) })
|
|
33
|
-
}) : "";
|
|
34
22
|
return {
|
|
35
23
|
status: 200,
|
|
36
24
|
body: {
|
|
37
25
|
code: 200,
|
|
38
|
-
data: {
|
|
39
|
-
qrurl: url,
|
|
40
|
-
qrimg
|
|
41
|
-
}
|
|
26
|
+
data: { qrurl: url }
|
|
42
27
|
},
|
|
43
28
|
cookie: []
|
|
44
29
|
};
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { ModuleInvariantFailed } from "../core/errors.js";
|
|
2
1
|
import { IntegerValue, decodeModuleInput as decodeModuleInput$1 } from "../core/module-input.js";
|
|
3
|
-
import { isMissingOptionalDependencyError, loadOptionalDependency } from "../core/optional-dependency.js";
|
|
4
2
|
import { UpstreamObject, decodeUpstreamBody, defineModuleBody } from "../core/upstream-body.js";
|
|
5
3
|
import * as Effect from "effect/Effect";
|
|
6
4
|
import * as Schema from "effect/Schema";
|
|
@@ -25,8 +23,7 @@ const ModuleBody = /* @__PURE__ */ defineModuleBody(Schema.Struct({
|
|
|
25
23
|
code: Schema.Literal(200),
|
|
26
24
|
data: Schema.Struct({
|
|
27
25
|
qrCode: Schema.String,
|
|
28
|
-
qrurl: Schema.String
|
|
29
|
-
qrimg: Schema.String
|
|
26
|
+
qrurl: Schema.String
|
|
30
27
|
})
|
|
31
28
|
}));
|
|
32
29
|
const verifyGetQr = (query, request) => Effect.gen(function* () {
|
|
@@ -42,7 +39,6 @@ const verifyGetQr = (query, request) => Effect.gen(function* () {
|
|
|
42
39
|
size: 150
|
|
43
40
|
} });
|
|
44
41
|
const { qrCode } = (yield* decodeUpstreamBody("verify_getQr", upstreamSchema, response)).data;
|
|
45
|
-
const qrurl = `https://st.music.163.com/encrypt-pages?qrCode=${qrCode}&verifyToken=${query.token}&verifyId=${query.vid}&verifyType=${query.type}¶ms=${params}`;
|
|
46
42
|
return {
|
|
47
43
|
status: 200,
|
|
48
44
|
cookie: [],
|
|
@@ -50,13 +46,7 @@ const verifyGetQr = (query, request) => Effect.gen(function* () {
|
|
|
50
46
|
code: 200,
|
|
51
47
|
data: {
|
|
52
48
|
qrCode,
|
|
53
|
-
qrurl
|
|
54
|
-
qrimg: yield* Effect.tryPromise({
|
|
55
|
-
try: async () => {
|
|
56
|
-
return (await loadOptionalDependency("qrcode")).toDataURL(qrurl);
|
|
57
|
-
},
|
|
58
|
-
catch: (error) => ModuleInvariantFailed.make({ message: isMissingOptionalDependencyError(error, "qrcode") ? "Generating QR code images requires installing optional peer dependency 'qrcode'" : String(error) })
|
|
59
|
-
})
|
|
49
|
+
qrurl: `https://st.music.163.com/encrypt-pages?qrCode=${qrCode}&verifyToken=${query.token}&verifyId=${query.vid}&verifyType=${query.type}¶ms=${params}`
|
|
60
50
|
}
|
|
61
51
|
}
|
|
62
52
|
};
|
|
@@ -31,7 +31,6 @@ export interface CreateRequestOptions {
|
|
|
31
31
|
readonly headers?: Record<string, string>;
|
|
32
32
|
readonly ip?: string;
|
|
33
33
|
readonly onRequestEvent?: (event: RequestDebugEvent) => void;
|
|
34
|
-
readonly proxy?: string;
|
|
35
34
|
readonly realIP?: string;
|
|
36
35
|
readonly retry?: RequestRetryOptions;
|
|
37
36
|
readonly signal?: AbortSignal;
|
package/dist/src/types/sdk.d.ts
CHANGED
|
@@ -7,7 +7,6 @@ export interface ModuleCallConfig {
|
|
|
7
7
|
readonly fetcher?: CreateRequestOptions['fetcher'];
|
|
8
8
|
readonly ip?: string;
|
|
9
9
|
readonly onRequestEvent?: CreateRequestOptions['onRequestEvent'];
|
|
10
|
-
readonly proxy?: string;
|
|
11
10
|
readonly realIP?: string;
|
|
12
11
|
readonly retry?: CreateRequestOptions['retry'];
|
|
13
12
|
readonly signal?: AbortSignal;
|
|
@@ -26,7 +25,7 @@ export interface CreateHanaMusicApiConfig extends ModuleCallConfig {
|
|
|
26
25
|
readonly cache?: SdkCacheConfig;
|
|
27
26
|
readonly identityPool?: IdentityPoolConfig;
|
|
28
27
|
}
|
|
29
|
-
export type DisallowExecutionKeys = { [K in keyof ModuleCallConfig | 'cache' | 'identityPool' | 'crypto' | 'ua' | 'domain' | 'e_r' | 'acceptGzip' | 'headers' | 'deviceId']?: never; };
|
|
28
|
+
export type DisallowExecutionKeys = { [K in keyof ModuleCallConfig | 'proxy' | 'cache' | 'identityPool' | 'crypto' | 'ua' | 'domain' | 'e_r' | 'acceptGzip' | 'headers' | 'deviceId']?: never; };
|
|
30
29
|
type RequiredKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? never : K; }[keyof T];
|
|
31
30
|
export type SdkQueryOf<K extends ModuleIdentifier> = ModuleInputOf<K> & DisallowExecutionKeys;
|
|
32
31
|
export type SdkModuleInvokerFor<Input, Response extends ModuleResponse<unknown> = ModuleResponse> = RequiredKeys<Input & DisallowExecutionKeys> extends never ? (query?: Input & DisallowExecutionKeys, config?: ModuleCallConfig) => Promise<Response> : (query: Input & DisallowExecutionKeys, config?: ModuleCallConfig) => Promise<Response>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hana-music-api",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Modern NCM API library for Bun & Node.js.",
|
|
6
6
|
"keywords": [
|
|
@@ -66,6 +66,8 @@
|
|
|
66
66
|
"changeset": "changeset",
|
|
67
67
|
"check:dist": "publint --strict && attw --pack . --profile esm-only",
|
|
68
68
|
"dev": "bun --watch src/app/cli.ts",
|
|
69
|
+
"docs:api:check": "bun scripts/sync-api-docs.mts --check",
|
|
70
|
+
"docs:api:sync": "bun scripts/sync-api-docs.mts",
|
|
69
71
|
"docs:build": "vitepress build website",
|
|
70
72
|
"docs:dev": "vitepress dev website",
|
|
71
73
|
"docs:preview": "vitepress preview website",
|
|
@@ -112,7 +114,6 @@
|
|
|
112
114
|
"cspell": "^10.4.0",
|
|
113
115
|
"hono": "^4.13.16",
|
|
114
116
|
"mermaid": "^12.3.0",
|
|
115
|
-
"music-metadata": "^12.3.4",
|
|
116
117
|
"oxfmt": "^0.76.0",
|
|
117
118
|
"oxlint": "^1.91.0",
|
|
118
119
|
"oxlint-tsgolint": "^7.0.2005",
|
|
@@ -125,22 +126,6 @@
|
|
|
125
126
|
"vitepress": "^1.6.4",
|
|
126
127
|
"vitepress-plugin-mermaid": "^2.0.17"
|
|
127
128
|
},
|
|
128
|
-
"peerDependencies": {
|
|
129
|
-
"music-metadata": "^12.3.4",
|
|
130
|
-
"qrcode": "^1.5.4",
|
|
131
|
-
"undici": "^8.12.3"
|
|
132
|
-
},
|
|
133
|
-
"peerDependenciesMeta": {
|
|
134
|
-
"music-metadata": {
|
|
135
|
-
"optional": true
|
|
136
|
-
},
|
|
137
|
-
"qrcode": {
|
|
138
|
-
"optional": true
|
|
139
|
-
},
|
|
140
|
-
"undici": {
|
|
141
|
-
"optional": true
|
|
142
|
-
}
|
|
143
|
-
},
|
|
144
129
|
"engines": {
|
|
145
130
|
"node": ">=24"
|
|
146
131
|
},
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// src/core/optional-dependency.ts
|
|
2
|
-
const MISSING_MODULE_CODES = /* @__PURE__ */ new Set(["ERR_MODULE_NOT_FOUND", "MODULE_NOT_FOUND"]);
|
|
3
|
-
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
4
|
-
const isMissingOptionalDependencyError = (error, packageName) => {
|
|
5
|
-
if (typeof error !== "object" || error === null) return false;
|
|
6
|
-
const message = Reflect.get(error, "message");
|
|
7
|
-
if (typeof message !== "string") return false;
|
|
8
|
-
const code = Reflect.get(error, "code");
|
|
9
|
-
const name = Reflect.get(error, "name");
|
|
10
|
-
if (!(typeof code === "string" && MISSING_MODULE_CODES.has(code) || name === "ResolveMessage" || /(?:Cannot find (?:package|module)|Failed to resolve)/u.test(message))) return false;
|
|
11
|
-
return new RegExp(`(?:Cannot find (?:package|module)|Failed to resolve(?: import)?)\\s+['"]${escapeRegExp(packageName)}['"]`, "u").test(message);
|
|
12
|
-
};
|
|
13
|
-
const loadOptionalDependency = async (packageName) => {
|
|
14
|
-
return await import(packageName);
|
|
15
|
-
};
|
|
16
|
-
export { isMissingOptionalDependencyError, loadOptionalDependency };
|