All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
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
|