Skip to content

Commit

Permalink
strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
YieldRay committed Apr 7, 2022
1 parent eabcbee commit 987a627
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 115 deletions.
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

蓝奏云 API node.js 版本,目前实现了大部分功能,推荐 node.js >= 14

typescript 编写,生成 d.ts 方便开发和访问接口
理论上程序能够容易地转换为 Deno 版本和浏览器版本(浏览器需解除 fetch 限制)

仅供测试,勿用于非法行为
typescript 编写,仅供学习,勿用于非法行为

# 构建

```sh
$ tsc
$ npm install --production
$ tsc # 需要自行安装typescript

$ npm run doc # 生成文档
```
Expand All @@ -22,7 +22,7 @@ $ npm run doc # 生成文档

```js
import LanzouAPI from "./lib/lanzou.js";
LanzouAPI.queryShareLink("https://xxxxxx.lanzouj.com/xxxxxx", "passwd").then(console.log);
LanzouAPI.queryShareFileInfoWithPassword("https://upload.lanzouj.com/i95j302pbxeb").then(console.log);
const lanzou = LanzouAPI.of(cookie); // new LanzouAPI(cookie)
lanzou.getFolders().then(({ zt, info, text }) => {
if (zt === 1) {
Expand All @@ -36,28 +36,26 @@ lanzou.getFolders().then(({ zt, info, text }) => {

蓝奏云的接口有点混乱,但一般来说 info 表示操作结果或返回值,text 提供额外信息或 null

./src/types.ts 已经对所有返回值进行了描述

操作成功(即 zt == 1)时返回值符合接口,失败时不符合,参见以下说明
`./src/types.ts` 已经对所有返回值进行了描述

**正常情况下所有的 API 都不会抛出异常且返回以下接口**

```ts
interface {
zt: number; // 状态码: 0失败 1成功 等等
info; // 成功时为操作结果或返回值,失败时返回失败信息
text; // 成功时可能返回额外信息,失败时返回Error对象(本程序扩展
interface<Info> {
zt: number; // 状态码: 0 失败 1 成功 等等
info: Info | Error; // 成功时为操作结果或返回值,失败时返回 Error 对象
text string | null; // 成功时可能返回额外信息或 null,失败时失败信息(字符串
}
```

失败时可以通过检测 text 来获取程序捕获的异常
失败时可以通过检测 `info` 来获取程序捕获的异常

```js
const { zt, info, text } = await lanzou.getFolders();
if (zt === 1) {
// success
} else {
if (text instanceof Error) {
if (info instanceof Error) {
// ...
}
}
Expand Down
37 changes: 18 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"node-fetch": "^3.2.3"
},
"devDependencies": {
"@types/node": "^17.0.21",
"typedoc": "^0.22.13"
"@types/node": "^16.11.26",
"typedoc": "^0.22.13",
"typescript": "^4.6.3"
},
"repository": {
"type": "git",
Expand All @@ -28,4 +29,4 @@
"url": "https://github.com/YieldRay/lanzou-api/issues"
},
"homepage": "https://github.com/YieldRay/lanzou-api#readme"
}
}
18 changes: 10 additions & 8 deletions src/lanzou.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ class LanzouAPI {
private async fetchLanzou(bodyObj: object): Promise<anyResp> {
try {
const json = await fetchJSON("https://pc.woozooo.com/doupload.php", {
headers: {
accept: "application/json, text/javascript, */*; q=0.01",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
cookie: this.cookie,
...headersObj,
},
headers: Object.assign(
{
accept: "application/json, text/javascript, */*; q=0.01",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
cookie: this.cookie,
},
headersObj
),
body: objToURL(bodyObj),
method: "POST",
});
Expand Down Expand Up @@ -333,7 +335,7 @@ class LanzouAPI {
}
if (!folder_id) folder_id = -1;
const filename = getBasename(filepath);
if (!LanzouAPI.allowList.includes(filename.split(".").pop())) {
if (!filename.includes(".") || !LanzouAPI.allowList.includes((filename.split(".") as any[]).pop())) {
throw new LanzouAPIError("文件类型不允许上传");
}
const fd = new FormData();
Expand Down
Loading

0 comments on commit 987a627

Please sign in to comment.