@longzai-intelligence-open-alex/core 0.0.0-stage → 0.0.1
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 +8 -2
- package/dist/index.d.ts +372 -0
- package/dist/index.js +1 -0
- package/package.json +39 -4
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @longzai-intelligence-open-alex/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
开发工具类项目的核心包模板,承载项目核心类型定义与配置校验能力,为 CLI 与 Dashboard 应用提供共享的类型与校验函数。
|
|
4
|
+
|
|
5
|
+
定位为 dev-tool 项目模板 monorepo 的核心 packages 包,归属于 `packages/*` workspace,是 apps/cli 与 apps/dashboard 共同依赖的底层包。上游被同项目内的 cli 与 dashboard 通过 `workspace:*` 引用,自身依赖 zod 提供 Schema 校验能力,不反向依赖任何应用层包。
|
|
6
|
+
|
|
7
|
+
职能上,该包通过 src/index.ts 对外导出 CoreConfig 类型与 validateConfig 校验函数:CoreConfig 定义项目核心配置的数据结构,validateConfig 基于 zod 对配置进行运行时校验。构建链路为 lzi-builder 产出生产构建,导出路径通过 exports 字段限定为 ./dist/index.js 与对应的类型声明。
|
|
8
|
+
|
|
9
|
+
实现情况为脚手架模板初始状态,package.json 的脚本(build、build:prod、typecheck、lint、test、clean 等)与 exports 声明均已就绪,zod 依赖已声明。src/index.ts 已定义导出契约,但其引用的 ./types 与 ./validate 模块文件尚未在模板中提供,需在使用模板后由开发者按导出契约补齐具体实现。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { LziOpenAlexConfig } from "@longzai-intelligence-open-alex/config";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
/**
|
|
4
|
+
* fetch 注入签名,缺省全局 fetch,测试注入打桩用
|
|
5
|
+
*/
|
|
6
|
+
type FetchLike = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
7
|
+
/**
|
|
8
|
+
* sleep 注入签名,缺省真实定时等待,测试注入即时兑现并录制退避序列用
|
|
9
|
+
*/
|
|
10
|
+
type SleepImpl = (ms: number) => Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* 单请求失败类别枚举
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* 前四类可重试,client-error 为上游终态拒绝不重试
|
|
16
|
+
*/
|
|
17
|
+
type OpenAlexRequestFailureKind = 'network-error' | 'timeout-error' | 'rate-limited' | 'server-error' | 'client-error';
|
|
18
|
+
/**
|
|
19
|
+
* OpenAlex 请求错误上下文
|
|
20
|
+
*/
|
|
21
|
+
type OpenAlexRequestErrorDetails = {
|
|
22
|
+
/**
|
|
23
|
+
* 请求 URL
|
|
24
|
+
*/
|
|
25
|
+
url: string;
|
|
26
|
+
/**
|
|
27
|
+
* 失败类别
|
|
28
|
+
*/
|
|
29
|
+
kind: OpenAlexRequestFailureKind;
|
|
30
|
+
/**
|
|
31
|
+
* 上游状态码,无状态码时为 null
|
|
32
|
+
*/
|
|
33
|
+
status: number | null;
|
|
34
|
+
/**
|
|
35
|
+
* 已消耗尝试次数
|
|
36
|
+
*/
|
|
37
|
+
attempts: number;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* OpenAlex 请求错误
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* 重试耗尽与终态拒绝统一抛本错误,携带 URL 与失败类别与状态码与已耗尝试次数
|
|
44
|
+
*/
|
|
45
|
+
declare class OpenAlexRequestError extends Error {
|
|
46
|
+
/**
|
|
47
|
+
* 请求 URL
|
|
48
|
+
*/
|
|
49
|
+
readonly url: string;
|
|
50
|
+
/**
|
|
51
|
+
* 失败类别
|
|
52
|
+
*/
|
|
53
|
+
readonly kind: OpenAlexRequestFailureKind;
|
|
54
|
+
/**
|
|
55
|
+
* 上游状态码,网络与超时类失败无状态码为 null
|
|
56
|
+
*/
|
|
57
|
+
readonly status: number | null;
|
|
58
|
+
/**
|
|
59
|
+
* 已消耗尝试次数
|
|
60
|
+
*/
|
|
61
|
+
readonly attempts: number;
|
|
62
|
+
/**
|
|
63
|
+
* 构造 OpenAlex 请求错误
|
|
64
|
+
*
|
|
65
|
+
* @param message - 人读错误消息
|
|
66
|
+
* @param details - 错误上下文
|
|
67
|
+
*/
|
|
68
|
+
constructor(message: string, details: OpenAlexRequestErrorDetails);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 传输器工厂参数
|
|
72
|
+
*/
|
|
73
|
+
type OpenAlexTransportOptions = {
|
|
74
|
+
/**
|
|
75
|
+
* lzi-open-alex 配置,超时与重试与礼貌池取值来源
|
|
76
|
+
*/
|
|
77
|
+
config: LziOpenAlexConfig;
|
|
78
|
+
/**
|
|
79
|
+
* fetch 实现,缺省全局 fetch,测试注入用
|
|
80
|
+
*
|
|
81
|
+
* @default fetch
|
|
82
|
+
*/
|
|
83
|
+
fetchImpl?: FetchLike;
|
|
84
|
+
/**
|
|
85
|
+
* sleep 实现,缺省真实定时等待,测试注入用
|
|
86
|
+
*
|
|
87
|
+
* @default 真实定时等待
|
|
88
|
+
*/
|
|
89
|
+
sleepImpl?: SleepImpl;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* OpenAlex 传输器句柄
|
|
93
|
+
*/
|
|
94
|
+
type OpenAlexTransport = {
|
|
95
|
+
/**
|
|
96
|
+
* 请求 OpenAlex 端点并解析 JSON 响应
|
|
97
|
+
*
|
|
98
|
+
* @typeParam T - 预期响应体类型,结构校验由调用方承担
|
|
99
|
+
* @param path - 端点路径,以斜杠起始,如 `/works`
|
|
100
|
+
* @param params - 查询参数表,undefined 与空串值跳过
|
|
101
|
+
* @returns 解析后的响应体
|
|
102
|
+
* @throws {@link OpenAlexRequestError} 终态拒绝或重试耗尽时抛出
|
|
103
|
+
*/
|
|
104
|
+
requestJson<T>(path: string, params?: Record<string, string | number | undefined>): Promise<T>;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* 创建 OpenAlex 传输器
|
|
108
|
+
*
|
|
109
|
+
* @param options - 传输器工厂参数
|
|
110
|
+
* @returns 传输器句柄
|
|
111
|
+
*/
|
|
112
|
+
declare const createOpenAlexTransport: (options: OpenAlexTransportOptions) => OpenAlexTransport;
|
|
113
|
+
/**
|
|
114
|
+
* 有界并发工具
|
|
115
|
+
*
|
|
116
|
+
* @remarks
|
|
117
|
+
* 跨仓批量请求的负载天花板在本模块单点实现,在飞峰值可达上限但永不越界,
|
|
118
|
+
* 禁止任何调用方绕过本工具做无界全量派发
|
|
119
|
+
*/
|
|
120
|
+
/**
|
|
121
|
+
* 批量解析在飞请求上限显式常量
|
|
122
|
+
*
|
|
123
|
+
* @remarks
|
|
124
|
+
* 取自 config 包默认值保持单一真源,`resolveWorks` 未显式给定上限时以此兜底,
|
|
125
|
+
* 全程在飞峰值不超过本上限有全局测试守护
|
|
126
|
+
*/
|
|
127
|
+
declare const MAX_CONCURRENT_REQUESTS = 4;
|
|
128
|
+
/**
|
|
129
|
+
* 有界并发单条目执行体签名
|
|
130
|
+
*
|
|
131
|
+
* @typeParam TItem - 输入条目类型
|
|
132
|
+
* @typeParam TResult - 执行结果类型
|
|
133
|
+
*/
|
|
134
|
+
type ConcurrentWorker<TItem, TResult> = (item: TItem, index: number) => Promise<TResult>;
|
|
135
|
+
/**
|
|
136
|
+
* 有界并发 map 工具
|
|
137
|
+
*
|
|
138
|
+
* @remarks
|
|
139
|
+
* 以固定轨道数消费共享游标,结果按下标归位保持与输入同序,
|
|
140
|
+
* 上限按正整数下界钳制,轨道数超过条目数时按条目数收缩不空转,
|
|
141
|
+
* 单条目异常直接向上传播并等待全部在飞轨道收尾
|
|
142
|
+
* @typeParam TItem - 输入条目类型
|
|
143
|
+
* @typeParam TResult - 执行结果类型
|
|
144
|
+
* @param items - 输入条目清单,只读形态
|
|
145
|
+
* @param concurrencyLimit - 在飞上限,按正整数下界钳制
|
|
146
|
+
* @param worker - 单条目执行体,入参为条目与其下标
|
|
147
|
+
* @returns 与输入同序的结果清单
|
|
148
|
+
*/
|
|
149
|
+
declare const mapWithConcurrency: <TItem, TResult>(items: readonly TItem[], concurrencyLimit: number, worker: ConcurrentWorker<TItem, TResult>) => Promise<TResult[]>;
|
|
150
|
+
/**
|
|
151
|
+
* OpenAlex work 实体的 zod schema
|
|
152
|
+
*
|
|
153
|
+
* @remarks
|
|
154
|
+
* `id` 为唯一必填字段,其余字段按 OpenAlex 实际形态可空可缺省,未知字段保留
|
|
155
|
+
*/
|
|
156
|
+
declare const openAlexWorkSchema: z.ZodObject<{
|
|
157
|
+
id: z.ZodString;
|
|
158
|
+
doi: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
159
|
+
display_name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
160
|
+
publication_year: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
161
|
+
cited_by_count: z.ZodOptional<z.ZodNumber>;
|
|
162
|
+
referenced_works: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
163
|
+
type: z.ZodOptional<z.ZodString>;
|
|
164
|
+
authorships: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
165
|
+
author: z.ZodObject<{
|
|
166
|
+
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
167
|
+
display_name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
168
|
+
}, z.core.$loose>;
|
|
169
|
+
}, z.core.$loose>>>;
|
|
170
|
+
primary_location: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
171
|
+
source: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
172
|
+
display_name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
173
|
+
}, z.core.$loose>>>;
|
|
174
|
+
}, z.core.$loose>>>;
|
|
175
|
+
}, z.core.$loose>;
|
|
176
|
+
/**
|
|
177
|
+
* OpenAlex work 实体类型
|
|
178
|
+
*
|
|
179
|
+
* @remarks
|
|
180
|
+
* 由 `openAlexWorkSchema` 推导,未知字段以索引签名形态保留
|
|
181
|
+
*/
|
|
182
|
+
type OpenAlexWork = z.infer<typeof openAlexWorkSchema>;
|
|
183
|
+
/**
|
|
184
|
+
* OpenAlex works 列表响应的 zod schema
|
|
185
|
+
*
|
|
186
|
+
* @remarks
|
|
187
|
+
* 检索与被引列表端点共用同一响应外壳,meta 携带分页与 cursor 令牌
|
|
188
|
+
*/
|
|
189
|
+
declare const openAlexWorksResponseSchema: z.ZodObject<{
|
|
190
|
+
results: z.ZodArray<z.ZodObject<{
|
|
191
|
+
id: z.ZodString;
|
|
192
|
+
doi: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
193
|
+
display_name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
194
|
+
publication_year: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
195
|
+
cited_by_count: z.ZodOptional<z.ZodNumber>;
|
|
196
|
+
referenced_works: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
197
|
+
type: z.ZodOptional<z.ZodString>;
|
|
198
|
+
authorships: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
199
|
+
author: z.ZodObject<{
|
|
200
|
+
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
201
|
+
display_name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
202
|
+
}, z.core.$loose>;
|
|
203
|
+
}, z.core.$loose>>>;
|
|
204
|
+
primary_location: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
205
|
+
source: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
206
|
+
display_name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
207
|
+
}, z.core.$loose>>>;
|
|
208
|
+
}, z.core.$loose>>>;
|
|
209
|
+
}, z.core.$loose>>;
|
|
210
|
+
meta: z.ZodObject<{
|
|
211
|
+
count: z.ZodNumber;
|
|
212
|
+
page: z.ZodOptional<z.ZodNumber>;
|
|
213
|
+
per_page: z.ZodOptional<z.ZodNumber>;
|
|
214
|
+
next_cursor: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
215
|
+
}, z.core.$loose>;
|
|
216
|
+
}, z.core.$loose>;
|
|
217
|
+
/**
|
|
218
|
+
* OpenAlex works 列表响应类型
|
|
219
|
+
*/
|
|
220
|
+
type OpenAlexWorksResponse = z.infer<typeof openAlexWorksResponseSchema>;
|
|
221
|
+
/**
|
|
222
|
+
* works 分页结果的领域形态
|
|
223
|
+
*
|
|
224
|
+
* @remarks
|
|
225
|
+
* 页码分页与 cursor 分页统一收敛为本形态,cursor 终止判定即 `nextCursor` 为 null
|
|
226
|
+
*/
|
|
227
|
+
type OpenAlexWorksPage = {
|
|
228
|
+
/**
|
|
229
|
+
* 当前页 work 实体清单
|
|
230
|
+
*/
|
|
231
|
+
works: OpenAlexWork[];
|
|
232
|
+
/**
|
|
233
|
+
* 命中总量
|
|
234
|
+
*/
|
|
235
|
+
count: number;
|
|
236
|
+
/**
|
|
237
|
+
* 当前页码,上游未回传时为 null
|
|
238
|
+
*/
|
|
239
|
+
page: number | null;
|
|
240
|
+
/**
|
|
241
|
+
* 当前页大小,上游未回传时为 null
|
|
242
|
+
*/
|
|
243
|
+
perPage: number | null;
|
|
244
|
+
/**
|
|
245
|
+
* cursor 分页续页令牌,已到末页时为 null
|
|
246
|
+
*/
|
|
247
|
+
nextCursor: string | null;
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* works 检索取值输入
|
|
251
|
+
*/
|
|
252
|
+
type SearchWorksOptions = {
|
|
253
|
+
/**
|
|
254
|
+
* 检索词,下推为 search 查询参数
|
|
255
|
+
*/
|
|
256
|
+
query: string;
|
|
257
|
+
/**
|
|
258
|
+
* 页码,自 1 起
|
|
259
|
+
*/
|
|
260
|
+
page?: number;
|
|
261
|
+
/**
|
|
262
|
+
* 单页条数
|
|
263
|
+
*/
|
|
264
|
+
perPage?: number;
|
|
265
|
+
/**
|
|
266
|
+
* OpenAlex filter 表达式原文,如 `publication_year:2024`
|
|
267
|
+
*/
|
|
268
|
+
filter?: string;
|
|
269
|
+
/**
|
|
270
|
+
* 字段过滤清单,下推为 select 逗号串
|
|
271
|
+
*/
|
|
272
|
+
select?: string[];
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* 被引列表取值输入
|
|
276
|
+
*/
|
|
277
|
+
type ListCitingWorksOptions = {
|
|
278
|
+
/**
|
|
279
|
+
* cursor 分页续页令牌,首页缺省
|
|
280
|
+
*/
|
|
281
|
+
cursor?: string;
|
|
282
|
+
/**
|
|
283
|
+
* 单页条数
|
|
284
|
+
*/
|
|
285
|
+
perPage?: number;
|
|
286
|
+
/**
|
|
287
|
+
* 字段过滤清单,下推为 select 逗号串
|
|
288
|
+
*/
|
|
289
|
+
select?: string[];
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* API 客户端工厂参数
|
|
293
|
+
*/
|
|
294
|
+
type OpenAlexApiClientOptions = {
|
|
295
|
+
/**
|
|
296
|
+
* lzi-open-alex 配置,批量并发上限取自 `maxConcurrentRequests`
|
|
297
|
+
*/
|
|
298
|
+
config: LziOpenAlexConfig;
|
|
299
|
+
/**
|
|
300
|
+
* fetch 实现,缺省全局 fetch,透传传输层供测试注入
|
|
301
|
+
*
|
|
302
|
+
* @default fetch
|
|
303
|
+
*/
|
|
304
|
+
fetchImpl?: FetchLike;
|
|
305
|
+
/**
|
|
306
|
+
* sleep 实现,缺省真实定时等待,透传传输层供测试注入
|
|
307
|
+
*
|
|
308
|
+
* @default 真实定时等待
|
|
309
|
+
*/
|
|
310
|
+
sleepImpl?: SleepImpl;
|
|
311
|
+
};
|
|
312
|
+
/**
|
|
313
|
+
* OpenAlex API 客户端句柄
|
|
314
|
+
*/
|
|
315
|
+
type OpenAlexApiClient = {
|
|
316
|
+
/**
|
|
317
|
+
* 检索 works 实体
|
|
318
|
+
*
|
|
319
|
+
* @param options - 检索取值输入
|
|
320
|
+
* @returns 单页分页结果
|
|
321
|
+
* @throws {@link OpenAlexRequestError} 终态拒绝或重试耗尽时抛出
|
|
322
|
+
*/
|
|
323
|
+
searchWorks(options: SearchWorksOptions): Promise<OpenAlexWorksPage>;
|
|
324
|
+
/**
|
|
325
|
+
* 取单个 work 实体详情
|
|
326
|
+
*
|
|
327
|
+
* @param idOrDoi - OpenAlex ID 或 DOI URL 或裸 DOI 定位键
|
|
328
|
+
* @returns work 实体
|
|
329
|
+
* @throws {@link OpenAlexRequestError} 终态拒绝或重试耗尽时抛出
|
|
330
|
+
*/
|
|
331
|
+
getWork(idOrDoi: string): Promise<OpenAlexWork>;
|
|
332
|
+
/**
|
|
333
|
+
* 展开单个 work 的参考文献引用边
|
|
334
|
+
*
|
|
335
|
+
* @remarks
|
|
336
|
+
* 单请求取 work 记录的 `referenced_works` 清单,无引用边时返回空清单
|
|
337
|
+
* @param workId - OpenAlex ID 或 DOI URL 或裸 DOI 定位键
|
|
338
|
+
* @returns 参考文献 work ID 清单
|
|
339
|
+
* @throws {@link OpenAlexRequestError} 终态拒绝或重试耗尽时抛出
|
|
340
|
+
*/
|
|
341
|
+
getReferences(workId: string): Promise<string[]>;
|
|
342
|
+
/**
|
|
343
|
+
* 取单个 work 的被引列表
|
|
344
|
+
*
|
|
345
|
+
* @remarks
|
|
346
|
+
* 下推 `cites` 过滤条件,cursor 续页令牌透传,终止判定即回传 `nextCursor` 为 null
|
|
347
|
+
* @param workId - OpenAlex ID 或 DOI URL 或裸 DOI 定位键
|
|
348
|
+
* @param options - 被引列表取值输入,缺省取首页
|
|
349
|
+
* @returns 单页分页结果
|
|
350
|
+
* @throws {@link OpenAlexRequestError} 终态拒绝或重试耗尽时抛出
|
|
351
|
+
*/
|
|
352
|
+
listCitingWorks(workId: string, options?: ListCitingWorksOptions): Promise<OpenAlexWorksPage>;
|
|
353
|
+
/**
|
|
354
|
+
* 批量解析 work 实体
|
|
355
|
+
*
|
|
356
|
+
* @remarks
|
|
357
|
+
* 有界并发扇出,上限取配置 `maxConcurrentRequests`,在飞峰值可达上限但永不越界,
|
|
358
|
+
* 输入顺序与结果顺序一致,重复定位键按重复解析不折叠
|
|
359
|
+
* @param ids - 定位键清单,支持 OpenAlex ID 与 DOI URL 与裸 DOI 形态
|
|
360
|
+
* @returns 与输入同序的 work 实体清单
|
|
361
|
+
* @throws {@link OpenAlexRequestError} 任一条目终态拒绝或重试耗尽时抛出
|
|
362
|
+
*/
|
|
363
|
+
resolveWorks(ids: readonly string[]): Promise<OpenAlexWork[]>;
|
|
364
|
+
};
|
|
365
|
+
/**
|
|
366
|
+
* 创建 OpenAlex API 客户端
|
|
367
|
+
*
|
|
368
|
+
* @param options - API 客户端工厂参数
|
|
369
|
+
* @returns API 客户端句柄
|
|
370
|
+
*/
|
|
371
|
+
declare const createOpenAlexApiClient: (options: OpenAlexApiClientOptions) => OpenAlexApiClient;
|
|
372
|
+
export { type FetchLike, type ListCitingWorksOptions, MAX_CONCURRENT_REQUESTS, type OpenAlexApiClient, type OpenAlexApiClientOptions, OpenAlexRequestError, type OpenAlexRequestFailureKind, type OpenAlexTransport, type OpenAlexTransportOptions, type OpenAlexWork, type OpenAlexWorksPage, type OpenAlexWorksResponse, type SearchWorksOptions, type SleepImpl, createOpenAlexApiClient, createOpenAlexTransport, mapWithConcurrency, openAlexWorkSchema, openAlexWorksResponseSchema };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ensureError as e}from"@longzai-intelligence-error/utils";import{LZI_OPEN_ALEX_DEFAULT_MAX_CONCURRENT_REQUESTS as t}from"@longzai-intelligence-open-alex/config";import*as n from"zod";const r=/* @__PURE__ */ new Set([`network-error`,`timeout-error`,`rate-limited`,`server-error`]);var i=class extends Error{url;kind;status;attempts;constructor(e,t){super(e),this.name=`OpenAlexRequestError`,this.url=t.url,this.kind=t.kind,this.status=t.status,this.attempts=t.attempts}};const a=e=>e===429?`rate-limited`:e>=500?`server-error`:`client-error`,o=e=>e.name===`TimeoutError`||e.name===`AbortError`?`timeout-error`:`network-error`,s=(e,t,n,r)=>{let i=e.replace(/\/+$/,``),a=new URL(`${i}${t}`);for(let[e,t]of Object.entries(n))t!==void 0&&t!==``&&a.searchParams.set(e,String(t));return r!==void 0&&r!==``&&a.searchParams.set(`mailto`,r),a.toString()},c=e=>new Promise(t=>{setTimeout(t,e)}),l=t=>{let n=t.config,l=t.fetchImpl??fetch,u=t.sleepImpl??c;return{requestJson:async(t,c={})=>{let d=s(n.baseUrl,t,c,n.mailto),f=`network-error`,p=null;for(let t=1;t<=n.maxAttempts;t+=1){try{let e=await l(d,{signal:AbortSignal.timeout(n.timeoutMs)});if(e.ok)return await e.json();let o=a(e.status);if(!r.has(o))throw new i(`OpenAlex 请求被上游终态拒绝,状态码 ${e.status},URL ${d}`,{url:d,kind:o,status:e.status,attempts:t});f=o,p=e.status}catch(t){if(t instanceof i)throw t;let n=e(t);f=o(n),p=null}if(t<n.maxAttempts){let e=n.backoffBaseMs*2**(t-1);await u(e)}}throw new i(`OpenAlex 请求重试耗尽,共尝试 ${n.maxAttempts} 次,末次失败类别 ${f},末次状态码 ${p??`无`},URL ${d}`,{url:d,kind:f,status:p,attempts:n.maxAttempts})}}},u=t,d=async(e,t,n)=>{let r=Math.max(1,Math.floor(t)),i=[],a=0,o=Math.min(r,e.length),s=Array.from({length:o},async()=>{for(;;){let t=a;if(t>=e.length)return;a+=1;let r=e[t];if(r===void 0)return;i[t]=await n(r,t)}});return await Promise.all(s),i},f=n.looseObject({author:n.looseObject({id:n.string().nullable().optional(),display_name:n.string().nullable().optional()})}),p=n.looseObject({source:n.looseObject({display_name:n.string().nullable().optional()}).nullable().optional()}),m=n.looseObject({id:n.string(),doi:n.string().nullable().optional(),display_name:n.string().nullable().optional(),publication_year:n.number().nullable().optional(),cited_by_count:n.number().optional(),referenced_works:n.array(n.string()).optional(),type:n.string().optional(),authorships:n.array(f).optional(),primary_location:p.nullable().optional()}),h=n.looseObject({results:n.array(m),meta:n.looseObject({count:n.number(),page:n.number().optional(),per_page:n.number().optional(),next_cursor:n.string().nullable().optional()})}),g=/^[Ww]\d+$/,_=/^https?:\/\/openalex\.org\/([Ww]\d+)$/i,v=/^10\.\d{4,9}\/\S+$/,y=/^doi:/i,b=/^https?:\/\/doi\.org\/(.+)$/i,x=e=>{let t=e.trim(),n=t.match(_);if(n!==null)return(n[1]??t).toUpperCase();if(g.test(t))return t.toUpperCase();let r=t.replace(y,``),i=r.match(b);return i===null?v.test(r)?`https://doi.org/${r}`:t:`https://doi.org/${i[1]??r}`},S=e=>e.map(e=>`${e.path.join(`.`)}: ${e.message}`).join(`,`),C=e=>{let t=h.safeParse(e);if(!t.success)throw Error(`OpenAlex 响应结构不合法,字段问题 ${S(t.error.issues)}`);return t.data},w=e=>{let t=m.safeParse(e);if(!t.success)throw Error(`OpenAlex 响应结构不合法,字段问题 ${S(t.error.issues)}`);return t.data},T=e=>({works:e.results,count:e.meta.count,page:e.meta.page??null,perPage:e.meta.per_page??null,nextCursor:e.meta.next_cursor??null}),E=e=>{let t=l({config:e.config,fetchImpl:e.fetchImpl,sleepImpl:e.sleepImpl}),n=e.config.maxConcurrentRequests??u,r=async e=>{let n=x(e),r=await t.requestJson(`/works/${n}`);return w(r)};return{async searchWorks(e){let n=await t.requestJson(`/works`,{search:e.query,page:e.page,"per-page":e.perPage,filter:e.filter,select:e.select?.join(`,`)});return T(C(n))},getWork:r,async getReferences(e){return(await r(e)).referenced_works??[]},async listCitingWorks(e,n){let r=await t.requestJson(`/works`,{filter:`cites:${x(e)}`,cursor:n?.cursor,"per-page":n?.perPage,select:n?.select?.join(`,`)});return T(C(r))},async resolveWorks(e){return d(e,n,e=>r(e))}}};export{u as MAX_CONCURRENT_REQUESTS,i as OpenAlexRequestError,E as createOpenAlexApiClient,l as createOpenAlexTransport,d as mapWithConcurrency,m as openAlexWorkSchema,h as openAlexWorksResponseSchema};
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@longzai-intelligence-open-alex/core",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Longzai Intelligence OpenAlex 核心获取层",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"core",
|
|
8
|
+
"domain-model",
|
|
9
|
+
"longzai-intelligence"
|
|
10
|
+
],
|
|
11
|
+
"license": "UNLICENSED",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "lzi-builder",
|
|
26
|
+
"build:prod": "NODE_ENV=production lzi-builder",
|
|
27
|
+
"prepublishOnly": "bun run build:prod",
|
|
28
|
+
"typecheck": "lzi-typecheck",
|
|
29
|
+
"lint": "lzi-lint lint",
|
|
30
|
+
"lint:fix": "lzi-lint fix",
|
|
31
|
+
"test": "lzi-bun test",
|
|
32
|
+
"test:watch": "lzi-bun test --watch",
|
|
33
|
+
"test:coverage": "lzi-bun test --coverage",
|
|
34
|
+
"clean": "lzi-clean"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@longzai-intelligence-error/utils": "^0.2.0",
|
|
38
|
+
"@longzai-intelligence-open-alex/config": "0.0.1",
|
|
39
|
+
"zod": "^4.6.7"
|
|
40
|
+
}
|
|
41
|
+
}
|