zhaoyingbo 09e352a9c1
All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
feat: 抽象网络请求类 & 内容转为ctx向内传递
2024-08-16 09:12:11 +00:00

59 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { LarkServer } from "../../types"
import LarkBaseService from "./base"
class LarkDriveService extends LarkBaseService {
/**
* 批量获取文档元数据。
*
* @param {string[]} docTokens - 文档令牌数组。
* @param {string} [doc_type="doc"] - 文档类型,默认为 "doc"。
* @param {string} [user_id_type="user_id"] - 用户ID类型默认为 "user_id"。
* @returns {Promise<{ code: number, data: { metas: any[], failed_list: any[] }, message: string }>} 包含元数据和失败列表的响应对象。
*/
async batchGetMeta(
docTokens: string[],
doc_type = "doc",
user_id_type = "user_id"
) {
const path = "/drive/v1/metas/batch_query"
// 如果docTokens长度超出150需要分批请求
const docTokensLen = docTokens.length
const maxLen = 150
const requestMap = Array.from(
{ length: Math.ceil(docTokensLen / maxLen) },
(_, index) => {
const start = index * maxLen
const docTokensSlice = docTokens.slice(start, start + maxLen)
const data = {
request_docs: docTokensSlice.map((id) => ({
doc_token: id,
doc_type,
})),
}
return this.post<LarkServer.BatchDocMetaRes>(path, data, {
user_id_type,
})
}
)
const responses = await Promise.all(requestMap)
const metas = responses.flatMap((res) => {
return res.data?.metas || []
})
const failed_list = responses.flatMap((res) => {
return res.data?.failed_list || []
})
return {
code: 0,
data: {
metas,
failed_list,
},
message: "success",
}
}
}
export default LarkDriveService