wiki-entity 0.7.0 → 1.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +172 -0
  2. package/LICENSE +15 -0
  3. package/README.md +287 -89
  4. package/dist/index.cjs +1312 -0
  5. package/dist/index.d.cts +370 -0
  6. package/dist/index.d.ts +370 -0
  7. package/dist/index.js +1296 -0
  8. package/package.json +59 -24
  9. package/data/countries.json +0 -1
  10. package/data/entity_types.json +0 -1
  11. package/lib/index.js +0 -129
  12. package/lib/request.js +0 -37
  13. package/lib/simpleEntity/convertToSimpleEntity.js +0 -53
  14. package/lib/simpleEntity/getEntityCountry.js +0 -25
  15. package/lib/simpleEntity/getEntityData.js +0 -27
  16. package/lib/simpleEntity/getEntityInstanceType.js +0 -32
  17. package/lib/simpleEntity/getEntityType.js +0 -62
  18. package/lib/simpleEntity/getEntityTypeByExtract.js +0 -28
  19. package/lib/simpleEntity/index.js +0 -7
  20. package/lib/simpleEntity/simpleEntity.js +0 -20
  21. package/lib/types.js +0 -13
  22. package/lib/utils.js +0 -15
  23. package/lib/wikidata/api.js +0 -106
  24. package/lib/wikidata/get_entity_types.js +0 -60
  25. package/lib/wikidata/index.js +0 -112
  26. package/lib/wikidata/simplify_claims.js +0 -100
  27. package/lib/wikidata/simplify_entity.js +0 -76
  28. package/lib/wikipedia/api.js +0 -146
  29. package/types/index.d.ts +0 -9
  30. package/types/request.d.ts +0 -5
  31. package/types/simpleEntity/convertToSimpleEntity.d.ts +0 -6
  32. package/types/simpleEntity/getEntityCountry.d.ts +0 -2
  33. package/types/simpleEntity/getEntityData.d.ts +0 -3
  34. package/types/simpleEntity/getEntityInstanceType.d.ts +0 -3
  35. package/types/simpleEntity/getEntityType.d.ts +0 -3
  36. package/types/simpleEntity/getEntityTypeByExtract.d.ts +0 -2
  37. package/types/simpleEntity/index.d.ts +0 -2
  38. package/types/simpleEntity/simpleEntity.d.ts +0 -29
  39. package/types/types.d.ts +0 -68
  40. package/types/utils.d.ts +0 -8
  41. package/types/wikidata/api.d.ts +0 -4
  42. package/types/wikidata/get_entity_types.d.ts +0 -1
  43. package/types/wikidata/index.d.ts +0 -3
  44. package/types/wikidata/simplify_claims.d.ts +0 -4
  45. package/types/wikidata/simplify_entity.d.ts +0 -21
  46. package/types/wikipedia/api.d.ts +0 -31
@@ -0,0 +1,370 @@
1
+ /** Statement rank as modelled by Wikibase. */
2
+ type WikidataRank = "preferred" | "normal" | "deprecated";
3
+ /**
4
+ * A globe coordinate claim value.
5
+ * @see https://www.wikidata.org/wiki/Help:Data_type#Geographic_coordinates
6
+ */
7
+ interface WikidataGlobeCoordinateValue {
8
+ latitude: number;
9
+ longitude: number;
10
+ precision?: number | null;
11
+ globe?: string;
12
+ altitude?: number | null;
13
+ }
14
+ /**
15
+ * A point in time claim value. `time` uses a signed, zero padded year, so
16
+ * BCE dates start with `-`.
17
+ * @see https://www.wikidata.org/wiki/Help:Dates
18
+ */
19
+ interface WikidataTimeValue {
20
+ time: string;
21
+ precision: number;
22
+ timezone?: number;
23
+ before?: number;
24
+ after?: number;
25
+ calendarmodel?: string;
26
+ }
27
+ /** A quantity claim value. Bounds are absent for exact quantities. */
28
+ interface WikidataQuantityValue {
29
+ amount: string;
30
+ unit?: string;
31
+ upperBound?: string;
32
+ lowerBound?: string;
33
+ }
34
+ interface WikidataMonolingualTextValue {
35
+ text: string;
36
+ language: string;
37
+ }
38
+ /**
39
+ * The simplified value of a claim. `value` is a scalar for the common data
40
+ * types and the raw Wikibase object for structured ones (time, quantity,
41
+ * globe-coordinate); `value_string` then carries a readable rendering.
42
+ */
43
+ interface WikidataPropertyValue {
44
+ datatype: string;
45
+ value: string | number | WikidataTimeValue | WikidataQuantityValue | WikidataGlobeCoordinateValue;
46
+ rank?: WikidataRank;
47
+ pageid?: number;
48
+ value_string?: string;
49
+ label?: string;
50
+ description?: string;
51
+ qualifiers?: WikidataEntityClaims | null;
52
+ }
53
+ interface WikidataBaseEntity {
54
+ id: string;
55
+ label?: string;
56
+ description?: string;
57
+ [index: string]: unknown;
58
+ }
59
+ interface WikidataProperty extends WikidataBaseEntity {
60
+ values: WikidataPropertyValue[];
61
+ }
62
+ type WikidataEntityClaims = Record<string, WikidataProperty>;
63
+ interface WikidataEntity extends WikidataBaseEntity {
64
+ pageid?: number;
65
+ /** Wikibase datatype, present on property (`P…`) entities only. */
66
+ datatype?: string;
67
+ aliases?: string[];
68
+ sitelinks?: Record<string, string>;
69
+ claims?: WikidataEntityClaims;
70
+ labels?: Record<string, string>;
71
+ descriptions?: Record<string, string>;
72
+ redirectsToId?: string;
73
+ redirectsFromId?: string;
74
+ }
75
+ type WikidataEntities = Record<string, WikidataEntity>;
76
+ interface WikiEntity extends WikidataEntity {
77
+ extract?: string;
78
+ types?: string[];
79
+ redirects?: string[];
80
+ categories?: string[];
81
+ }
82
+ type WikiEntities = Record<string, WikiEntity>;
83
+ type ParamClaimsType = "none" | "all" | "item" | "property";
84
+ /** Entity props the Wikibase API can return. */
85
+ declare const WIKIDATA_PROPS: readonly ["info", "sitelinks", "aliases", "labels", "descriptions", "claims", "datatype"];
86
+ type WikidataProp = (typeof WIKIDATA_PROPS)[number];
87
+ interface WikidataEntitiesParams {
88
+ /** Wikidata entity ids, e.g. `["Q42"]`. Max 500. */
89
+ ids?: string[];
90
+ /** Wikipedia article titles, resolved against `language`. Max 500. */
91
+ titles?: string[];
92
+ /** Which props to fetch. Defaults to all of them. */
93
+ props?: WikidataProp[];
94
+ /** Language of `titles` and of the resulting `label`/`description`. */
95
+ language?: string;
96
+ /**
97
+ * Resolve Wikidata entity redirects to their target. Default: `true`.
98
+ *
99
+ * This is about *Wikidata items* that were merged into another item. For
100
+ * Wikipedia redirect titles see {@link WikiEntitiesParams.redirects}.
101
+ */
102
+ followEntityRedirects?: boolean;
103
+ /** How deeply to resolve claim values. Default: `"none"`. */
104
+ claims?: ParamClaimsType;
105
+ /** Per request timeout in milliseconds. */
106
+ httpTimeout?: number;
107
+ /** Extra languages to populate `WikiEntity.labels` with. */
108
+ languages?: string[];
109
+ /** Cancellation signal propagated to every underlying request. */
110
+ signal?: AbortSignal;
111
+ }
112
+ interface WikiEntitiesParams extends WikidataEntitiesParams {
113
+ /** Number of sentences in the Wikipedia extract. `0`/absent: no extract. */
114
+ extract?: number;
115
+ /**
116
+ * `true` to resolve DBpedia ontology types, or an array of prefixes to keep.
117
+ * Known prefixes: `dbo`, `schema`, `foaf`, `owl`, `dul`, `geo`, `wikidata`.
118
+ */
119
+ types?: boolean | string[];
120
+ /**
121
+ * Fetch the titles of *Wikipedia articles* that redirect to this entity.
122
+ * Unrelated to {@link WikidataEntitiesParams.followEntityRedirects}.
123
+ */
124
+ redirects?: boolean;
125
+ /** Fetch Wikipedia article categories. */
126
+ categories?: boolean;
127
+ /** Fetch the Wikipedia `pageid`. Default: `true`. */
128
+ wikiPageId?: boolean;
129
+ }
130
+
131
+ /** Coarse entity category, encoded as a single letter. */
132
+ declare enum SimpleEntityType {
133
+ EVENT = "E",
134
+ ORG = "O",
135
+ PERSON = "H",
136
+ PLACE = "P",
137
+ PRODUCT = "R",
138
+ WORK = "W"
139
+ }
140
+ /** Claim values keyed by property id, e.g. `{ P31: ["Q5"] }`. */
141
+ type SimpleEntityData = {
142
+ [prop: string]: string[];
143
+ };
144
+ type SimpleEntity = {
145
+ /** Language the entity was resolved in. Always set. */
146
+ lang: string;
147
+ /** Wikidata item id. Always set. */
148
+ wikiDataId: string;
149
+ name?: string;
150
+ description?: string;
151
+ about?: string;
152
+ wikiPageId?: number;
153
+ wikiPageTitle?: string;
154
+ type?: SimpleEntityType;
155
+ types?: string[];
156
+ countryCodes?: string[];
157
+ data?: SimpleEntityData;
158
+ categories?: string[];
159
+ redirectsToId?: string;
160
+ redirectsFromId?: string;
161
+ };
162
+
163
+ type ConvertToSimpleEntityOptions = {
164
+ /** Used when no type could be detected. */
165
+ defaultType?: SimpleEntityType;
166
+ };
167
+ /**
168
+ * Flatten a {@link WikiEntity} into the compact {@link SimpleEntity} shape.
169
+ *
170
+ * The entity type is resolved from the DBpedia ontology types first, then from
171
+ * the `P31` claims, and finally from the extract's opening sentence.
172
+ */
173
+ declare function convertToSimpleEntity(wikiEntity: WikiEntity, lang: string, options?: ConvertToSimpleEntityOptions): SimpleEntity;
174
+
175
+ type SimplifyEntityOptionsType = {
176
+ labels?: boolean;
177
+ descriptions?: boolean;
178
+ aliases?: boolean;
179
+ sitelinks?: boolean;
180
+ claims?: boolean;
181
+ /** Keep statements Wikidata marks as deprecated. Default: `false`. */
182
+ keepDeprecatedClaims?: boolean;
183
+ };
184
+ /**
185
+ * One entity exactly as `action=wbgetentities` returns it. Every field is
186
+ * optional: the API only sends the props that were asked for.
187
+ */
188
+ interface WikibaseEntityJson {
189
+ id?: string;
190
+ pageid?: number;
191
+ /** Present on property (`P…`) entities only. */
192
+ datatype?: string;
193
+ labels?: Record<string, {
194
+ value?: string;
195
+ language?: string;
196
+ }>;
197
+ descriptions?: Record<string, {
198
+ value?: string;
199
+ language?: string;
200
+ }>;
201
+ aliases?: Record<string, {
202
+ value?: string;
203
+ language?: string;
204
+ }[]>;
205
+ sitelinks?: Record<string, {
206
+ title?: string;
207
+ badges?: string[];
208
+ }>;
209
+ claims?: Record<string, unknown[]>;
210
+ redirectsToId?: string;
211
+ redirectsFromId?: string;
212
+ }
213
+ declare function simplifyEntity(lang: string, data: WikibaseEntityJson, options?: SimplifyEntityOptionsType): WikidataEntity;
214
+
215
+ /** One Wikipedia article, with whatever extras were requested. */
216
+ type WikipediaPage = {
217
+ pageid: number;
218
+ title: string;
219
+ /** Lead-section summary, when `extract` was requested. */
220
+ extract?: string;
221
+ /** Non-hidden category titles, when `categories` was requested. */
222
+ categories?: string[];
223
+ /** Titles of articles redirecting here, when `redirects` was requested. */
224
+ redirects?: string[];
225
+ };
226
+ interface QueryPagesOptions {
227
+ /** Wikipedia language code, e.g. `"en"`. */
228
+ lang: string;
229
+ titles: readonly string[];
230
+ /** Number of sentences of the lead section to extract. */
231
+ extract?: number;
232
+ /** Include the titles of articles redirecting to each page. */
233
+ redirects?: boolean;
234
+ /** Include the article's (non hidden) categories. */
235
+ categories?: boolean;
236
+ /** Follow redirects, so `titles` pointing at a redirect resolve to its target. */
237
+ followRedirects?: boolean;
238
+ httpTimeout?: number;
239
+ signal?: AbortSignal;
240
+ }
241
+ interface ResolvedTitle {
242
+ /** The final page title, after normalisation and redirect resolution. */
243
+ title: string;
244
+ /** `true` when at least one hop was an actual page redirect. */
245
+ redirected: boolean;
246
+ }
247
+ interface QueryPagesResult {
248
+ pages: WikipediaPage[];
249
+ /** Requested title -> where it ended up. */
250
+ resolved: Map<string, ResolvedTitle>;
251
+ /**
252
+ * Maps each *resolved* page title back to the title that was requested, after
253
+ * MediaWiki normalisation and redirect resolution.
254
+ */
255
+ requestedTitleOf: Map<string, string>;
256
+ }
257
+ /**
258
+ * Query Wikipedia pages by title, transparently batching, paging through
259
+ * `continue` responses and mapping the results back to the requested titles.
260
+ */
261
+ declare function queryPages(options: QueryPagesOptions): Promise<QueryPagesResult>;
262
+
263
+ /**
264
+ * Point type lookups at a different SPARQL endpoint (a DBpedia mirror, or a
265
+ * local Virtuoso instance).
266
+ */
267
+ declare function setDbpediaEndpoint(url: string): void;
268
+ declare function getDbpediaEndpoint(): string;
269
+ interface EntityTypesOptions {
270
+ /** Keep only these prefixes, e.g. `["dbo", "schema"]`. Default: all of them. */
271
+ prefixes?: string[] | undefined;
272
+ httpTimeout?: number;
273
+ signal?: AbortSignal;
274
+ }
275
+ /**
276
+ * Look up ontology types for several English Wikipedia titles at once.
277
+ *
278
+ * Resolving them one request per entity floods the public DBpedia endpoint, so
279
+ * the titles are batched into a single `VALUES` query per group.
280
+ *
281
+ * @returns A map keyed by the passed-in names. Names DBpedia knows nothing
282
+ * about are absent from the map.
283
+ */
284
+ declare function getEntityTypesByNames(names: readonly string[], prefixesOrOptions?: string[] | EntityTypesOptions): Promise<Map<string, string[]>>;
285
+
286
+ /**
287
+ * Override the `User-Agent` sent with every request. Wikimedia asks clients to
288
+ * identify themselves with an app name and a contact URL or e-mail.
289
+ *
290
+ * @example
291
+ * setUserAgent("MyApp/1.0 (https://myapp.example; me@myapp.example)");
292
+ */
293
+ declare function setUserAgent(ua: string): void;
294
+ /** The `User-Agent` currently sent with every request. */
295
+ declare function getUserAgent(): string;
296
+
297
+ /** Base class for every error thrown by this package. */
298
+ declare class WikiEntityError extends Error {
299
+ constructor(message: string, options?: {
300
+ cause?: unknown;
301
+ });
302
+ }
303
+ /** A transport level failure: non 2xx status, network error or timeout. */
304
+ declare class HttpError extends WikiEntityError {
305
+ readonly url: string;
306
+ readonly status?: number | undefined;
307
+ /** Milliseconds requested by a `Retry-After` response header, if any. */
308
+ readonly retryAfter: number | undefined;
309
+ constructor(message: string, url: string, status?: number | undefined, options?: {
310
+ cause?: unknown;
311
+ retryAfter?: number;
312
+ });
313
+ /**
314
+ * `true` when retrying the very same request may succeed: rate limiting,
315
+ * server errors and transport failures.
316
+ */
317
+ get retryable(): boolean;
318
+ }
319
+ /** The request succeeded but the MediaWiki/SPARQL endpoint reported an error. */
320
+ declare class ApiError extends WikiEntityError {
321
+ readonly code?: string | undefined;
322
+ readonly url?: string | undefined;
323
+ constructor(message: string, code?: string | undefined, url?: string | undefined);
324
+ }
325
+
326
+ /**
327
+ * Fetch entities from Wikidata, optionally enriched with data from Wikipedia
328
+ * (page id, extract, redirects, categories) and DBpedia (ontology types).
329
+ *
330
+ * Results follow the order of the requested `ids`/`titles`; entities that do
331
+ * not exist are omitted.
332
+ *
333
+ * @example
334
+ * const [europe] = await getEntities({ language: "en", titles: ["Europe"] });
335
+ */
336
+ declare function getEntities(params: WikiEntitiesParams): Promise<WikiEntity[]>;
337
+ /**
338
+ * {@link getEntities} followed by {@link convertToSimpleEntity}, in one call.
339
+ *
340
+ * The language is taken from `params`, so it cannot drift from the one the
341
+ * entities were fetched in — passing a different language to
342
+ * `convertToSimpleEntity` by hand silently mixes languages, because `name` and
343
+ * `about` come from the fetch while `wikiPageTitle` is read from the sitelinks
344
+ * of whatever language you passed.
345
+ *
346
+ * @example
347
+ * const entities = await getSimpleEntities({
348
+ * language: "en",
349
+ * ids: ["Q937"],
350
+ * types: true,
351
+ * extract: 2
352
+ * });
353
+ */
354
+ declare function getSimpleEntities(params: WikiEntitiesParams, options?: ConvertToSimpleEntityOptions): Promise<SimpleEntity[]>;
355
+ interface MapRedirectsOptions {
356
+ httpTimeout?: number;
357
+ signal?: AbortSignal;
358
+ }
359
+ /**
360
+ * Resolve Wikipedia redirect titles to the articles they point at.
361
+ *
362
+ * @returns A map of `redirectTitle -> targetTitle`, containing only the titles
363
+ * that actually are redirects.
364
+ *
365
+ * @example
366
+ * await mapRedirects(["Brashov"], "ro"); // { Brashov: "Brașov" }
367
+ */
368
+ declare function mapRedirects(titles: string[], lang: string, options?: MapRedirectsOptions): Promise<Record<string, string>>;
369
+
370
+ export { ApiError, type ConvertToSimpleEntityOptions, type EntityTypesOptions, HttpError, type MapRedirectsOptions, type ParamClaimsType, type QueryPagesOptions, type QueryPagesResult, type ResolvedTitle, type SimpleEntity, type SimpleEntityData, SimpleEntityType, type SimplifyEntityOptionsType, type WikiEntities, type WikiEntitiesParams, type WikiEntity, WikiEntityError, type WikibaseEntityJson, type WikidataBaseEntity, type WikidataEntities, type WikidataEntitiesParams, type WikidataEntity, type WikidataEntityClaims, type WikidataGlobeCoordinateValue, type WikidataMonolingualTextValue, type WikidataProp, type WikidataProperty, type WikidataPropertyValue, type WikidataQuantityValue, type WikidataRank, type WikidataTimeValue, type WikipediaPage, convertToSimpleEntity, getDbpediaEndpoint, getEntities, getEntityTypesByNames, getSimpleEntities, getUserAgent, mapRedirects, queryPages, setDbpediaEndpoint, setUserAgent, simplifyEntity };