Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
RevisionId,
SenseId,
} from '../types/entity.js'
import type { Url } from '../types/options.js'
import type { Url } from '../utils/build_url.js'

function isIdBuilder<T extends string> (regex: { readonly source: string, readonly flags: string }) {
return (id: string): id is T => typeof id === 'string' && new RegExp(regex.source, regex.flags).test(id)
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/sitelinks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { fixedEncodeURIComponent, isAKey, isOfType, rejectObsoleteInterface, replaceSpaceByUnderscores } from '../utils/utils.js'
import { languages } from './sitelinks_languages.js'
import { specialSites } from './special_sites.js'
import type { Url, WmLanguageCode } from '../types/options.js'
import type { WmLanguageCode } from '../types/options.js'
import type { Site } from '../types/sitelinks.js'
import type { Url } from '../utils/build_url.js'

const wikidataBase = 'https://www.wikidata.org/wiki/'

Expand Down
4 changes: 2 additions & 2 deletions src/queries/cirrus_search.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// See https://www.wikidata.org/w/api.php?action=help&modules=query%2Bsearch

import { isAKey, rejectObsoleteInterface } from '../utils/utils.js'
import type { Url, UrlResultFormat } from '../types/options.js'
import type { BuildUrlFunction } from '../utils/build_url.js'
import type { UrlResultFormat } from '../types/options.js'
import type { BuildUrlFunction, Url } from '../utils/build_url.js'

const namespacePattern = /^\d+[|\d]*$/

Expand Down
4 changes: 2 additions & 2 deletions src/queries/get_entities.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as validate from '../helpers/validate.js'
import { forceArray, rejectObsoleteInterface, shortLang } from '../utils/utils.js'
import type { EntityId } from '../types/entity.js'
import type { Props, Url, UrlResultFormat, WmLanguageCode } from '../types/options.js'
import type { Props, UrlResultFormat, WmLanguageCode } from '../types/options.js'
import type { WbGetEntities } from '../types/wbgetentities.js'
import type { BuildUrlFunction } from '../utils/build_url.js'
import type { BuildUrlFunction, Url } from '../utils/build_url.js'

export interface GetEntitiesOptions {
ids: EntityId | EntityId[]
Expand Down
4 changes: 2 additions & 2 deletions src/queries/get_entities_from_sitelinks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { languages } from '../helpers/sitelinks_languages.js'
import { forceArray, shortLang, rejectObsoleteInterface, isOfType } from '../utils/utils.js'
import type { Props, Url, UrlResultFormat, WmLanguageCode } from '../types/options.js'
import type { Props, UrlResultFormat, WmLanguageCode } from '../types/options.js'
import type { Site } from '../types/sitelinks.js'
import type { WbGetEntities } from '../types/wbgetentities.js'
import type { BuildUrlFunction } from '../utils/build_url.js'
import type { BuildUrlFunction, Url } from '../utils/build_url.js'

export interface GetEntitiesFromSitelinksOptions {
titles: string | string[]
Expand Down
2 changes: 1 addition & 1 deletion src/queries/get_entity_revision.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as validate from '../helpers/validate.js'
import { rejectObsoleteInterface } from '../utils/utils.js'
import type { EntityId, RevisionId } from '../types/entity.js'
import type { Url } from '../types/options.js'
import type { Url } from '../utils/build_url.js'

export interface GetEntityRevisionOptions {
id: EntityId
Expand Down
3 changes: 1 addition & 2 deletions src/queries/get_many_entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { rejectObsoleteInterface } from '../utils/utils.js'
import { getEntitiesFactory } from './get_entities.js'
import type { GetEntitiesOptions } from './get_entities.js'
import type { EntityId } from '../types/entity.js'
import type { Url } from '../types/options.js'
import type { BuildUrlFunction } from '../utils/build_url.js'
import type { BuildUrlFunction, Url } from '../utils/build_url.js'

export interface GetManyEntitiesOptions extends GetEntitiesOptions {
ids: EntityId[]
Expand Down
2 changes: 1 addition & 1 deletion src/queries/get_reverse_claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as validate from '../helpers/validate.js'
import { forceArray } from '../utils/utils.js'
import { sparqlQueryFactory } from './sparql_query.js'
import type { PropertyId } from '../types/entity.js'
import type { Url } from '../types/options.js'
import type { Url } from '../utils/build_url.js'

// Fiter-out properties. Can't be filtered by
// `?subject a wikibase:Item`, as those triples are omitted
Expand Down
4 changes: 2 additions & 2 deletions src/queries/get_revisions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as validate from '../helpers/validate.js'
import { forceArray, rejectObsoleteInterface } from '../utils/utils.js'
import type { EntityPageTitle } from '../types/entity.js'
import type { ApiQueryParameters, UrlResultFormat } from '../types/options.js'
import type { BuildUrlFunction } from '../utils/build_url.js'
import type { UrlResultFormat } from '../types/options.js'
import type { ApiQueryParameters, BuildUrlFunction } from '../utils/build_url.js'

// See https://www.wikidata.org/w/api.php?action=help&modules=query+revisions

Expand Down
4 changes: 2 additions & 2 deletions src/queries/search_entities.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EntityTypes } from '../types/entity.js'
import { isOfType, rejectObsoleteInterface } from '../utils/utils.js'
import type { EntityType } from '../types/entity.js'
import type { Url, UrlResultFormat } from '../types/options.js'
import type { BuildUrlFunction } from '../utils/build_url.js'
import type { UrlResultFormat } from '../types/options.js'
import type { BuildUrlFunction, Url } from '../utils/build_url.js'

export interface SearchEntitiesOptions {
search: string
Expand Down
2 changes: 1 addition & 1 deletion src/queries/sparql_query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fixedEncodeURIComponent } from '../utils/utils.js'
import type { Url } from '../types/options.js'
import type { Url } from '../utils/build_url.js'

export function sparqlQueryFactory (sparqlEndpoint: Url) {
return function sparqlQuery (sparql: string): Url {
Expand Down
5 changes: 0 additions & 5 deletions src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ export type Props = 'info' | 'sitelinks' | 'sitelinks/urls' | 'aliases' | 'label
export type UrlResultFormat = 'xml' | 'json'
export type WmLanguageCode = typeof languages[number]

export type ApiQueryParameters = Record<string, string | number | true>

// export type Url = `http${string}`
export type Url = string

export interface SimplifyEntityOptions extends SimplifySnaksOptions, SimplifySitelinkOptions {}

export interface SimplifySitelinkOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/types/sitelinks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ItemId } from './entity.js'
import type { Url } from './options.js'
import type { sites } from '../helpers/sitelinks_sites.js'
import type { specialSites } from '../helpers/special_sites.js'
import type { Url } from '../utils/build_url.js'

type ValueOf<T> = T[keyof T]
type SpecialSiteName = ValueOf<typeof specialSites>
Expand Down
16 changes: 10 additions & 6 deletions src/utils/build_url.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { ApiQueryParameters, Url } from '../types/options.js'

const isBrowser = typeof location !== 'undefined' && typeof document !== 'undefined'

type ApiQueryValue = string | number | true
export type ApiQueryParameters = Record<string, ApiQueryValue>

// export type Url = `http${string}`
export type Url = string

export type BuildUrlFunction = <T extends string>(options: Readonly<Partial<Record<T, ApiQueryValue>>>) => Url

export function buildUrlFactory (instanceApiEndpoint: Url): BuildUrlFunction {
return function (queryObj: ApiQueryParameters): Url {
return queryObj => {
// Request CORS headers if the request is made from a browser
// See https://www.wikidata.org/w/api.php ('origin' parameter)
if (isBrowser) queryObj.origin = '*'
if (isBrowser) queryObj = { ...queryObj, origin: '*' }

const queryEntries = Object.entries(queryObj)
// Remove null or undefined parameters
Expand All @@ -16,5 +22,3 @@ export function buildUrlFactory (instanceApiEndpoint: Url): BuildUrlFunction {
return instanceApiEndpoint + '?' + query
}
}

export type BuildUrlFunction = (options: ApiQueryParameters) => Url
4 changes: 2 additions & 2 deletions src/wikibase-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { getReverseClaimsFactory } from './queries/get_reverse_claims.js'
import { getRevisionsFactory } from './queries/get_revisions.js'
import { searchEntitiesFactory } from './queries/search_entities.js'
import { sparqlQueryFactory } from './queries/sparql_query.js'
import { buildUrlFactory } from './utils/build_url.js'
import { buildUrlFactory, type Url } from './utils/build_url.js'
import { isPlainObject } from './utils/utils.js'
import type { InstanceConfig, Url } from './types/options.js'
import type { InstanceConfig } from './types/options.js'

const tip = `Tip: if you just want to access functions that don't need an instance or a sparqlEndpoint,
those are also exposed directly on the module object. Exemple:
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs'
import type { Url } from '../../src/types/options.js'
import type { Url } from '../../src/utils/build_url.js'

export function readJsonFile (jsonFilePath: string) {
return JSON.parse(readFileSync(jsonFilePath, 'utf-8'))
Expand Down