Skip to content

Commit 746a6ba

Browse files
committed
refactor: allow for custom ApiQueryParameters
1 parent 5fa1e50 commit 746a6ba

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

src/queries/get_revisions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as validate from '../helpers/validate.js'
22
import { forceArray, rejectObsoleteInterface } from '../utils/utils.js'
33
import type { EntityPageTitle } from '../types/entity.js'
4-
import type { ApiQueryParameters, UrlResultFormat } from '../types/options.js'
5-
import type { BuildUrlFunction } from '../utils/build_url.js'
4+
import type { UrlResultFormat } from '../types/options.js'
5+
import type { ApiQueryParameters, BuildUrlFunction } from '../utils/build_url.js'
66

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

src/types/options.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ export type Props = 'info' | 'sitelinks' | 'sitelinks/urls' | 'aliases' | 'label
1111
export type UrlResultFormat = 'xml' | 'json'
1212
export type WmLanguageCode = typeof languages[number]
1313

14-
export type ApiQueryParameters = Record<string, string | number | true>
15-
1614
// export type Url = `http${string}`
1715
export type Url = string
1816

src/types/wbgetentities.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type { UrlResultFormat } from './options.js'
22

3-
// TODO: not sure why this fails as an interface
4-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
5-
export type WbGetEntities = {
3+
export interface WbGetEntities {
64
action: 'wbgetentities'
75
titles?: string
86
sites?: string

src/utils/build_url.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import type { ApiQueryParameters, Url } from '../types/options.js'
1+
import type { Url } from '../types/options.js'
22

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

5+
type ApiQueryValue = string | number | true
6+
export type ApiQueryParameters = Record<string, ApiQueryValue>
7+
8+
export type BuildUrlFunction = <T extends string>(options: Readonly<Partial<Record<T, ApiQueryValue>>>) => Url
9+
510
export function buildUrlFactory (instanceApiEndpoint: Url): BuildUrlFunction {
6-
return function (queryObj: ApiQueryParameters): Url {
11+
return queryObj => {
712
// Request CORS headers if the request is made from a browser
813
// See https://www.wikidata.org/w/api.php ('origin' parameter)
9-
if (isBrowser) queryObj.origin = '*'
14+
if (isBrowser) queryObj = { ...queryObj, origin: '*' }
1015

1116
const queryEntries = Object.entries(queryObj)
1217
// Remove null or undefined parameters
@@ -16,5 +21,3 @@ export function buildUrlFactory (instanceApiEndpoint: Url): BuildUrlFunction {
1621
return instanceApiEndpoint + '?' + query
1722
}
1823
}
19-
20-
export type BuildUrlFunction = (options: ApiQueryParameters) => Url

0 commit comments

Comments
 (0)