wiki-saikou
Version:
The library provides the out of box accessing to MediaWiki API in both browsers & Node.js, and the syntax is very similar to vanilla `new mw.Api()`. TypeScript definition included~
89 lines (88 loc) • 4.11 kB
TypeScript
import { FexiosConfigs, FexiosRequestOptions, FexiosFinalContext } from 'fexios';
import { createFexiosSaikou, FexiosSaikou } from './models/FexiosSaikou.js';
import { MwApiParams, MwApiResponse, MwTokenName } from './types.js';
export interface WikiSaikouConfig {
/**
* @default undefined // required in Node.js environment
* @default `${wgServer}${wgScriptPath}/api.php` // in MW pages
* @description The MediaWiki API endpoint, e.g. "https://www.mediawiki.org/w/api.php"
* @optional In real MediaWiki browser pages, `baseURL` can be omitted and will be inferred automatically based on `window.mw`
*/
baseURL: string;
/**
* Transport/runtime options passed to the underlying Fexios instance (headers, fetch, credentials, etc.).
* @default { responseType: 'json' }
*/
fexiosConfigs: Partial<FexiosConfigs>;
/**
* Default query parameters merged into every request.
* @default { action: 'query' }
*/
defaultParams: MwApiParams;
/**
* When true, responses whose JSON body contains `error`/`errors` will throw `MediaWikiApiError` even if HTTP status is 2xx.
* @default false
*/
throwOnApiError: boolean;
}
export type WikiSaikouInitConfig = Omit<Partial<WikiSaikouConfig>, 'baseURL'> & {
baseURL: string;
};
/**
* WikiSaikou Core class
* @internal You **SHOULD NOT** use this class directly, instead, use the `MediaWikiApi` class.
* @author dragon-fish <dragon-fish@qq.com>
* @license MIT
*/
export declare class WikiSaikouCore {
readonly config: Required<WikiSaikouConfig>;
readonly version: any;
readonly request: FexiosSaikou;
static INIT_DEFAULT_PARAMS: MwApiParams;
static DEFAULT_CONFIGS: Required<WikiSaikouConfig>;
/** @deprecated Use `new MediaWikiApi(config)` instead */
constructor(baseURL?: string, defaultOptions?: Partial<FexiosConfigs>, defaultParams?: MwApiParams);
constructor(config?: WikiSaikouInitConfig);
setBaseURL(baseURL: string): this;
/** Base methods encapsulation */
get<T = any>(query: MwApiParams, options?: Partial<FexiosRequestOptions>): Promise<FexiosFinalContext<MwApiResponse<T>>>;
post<T = any>(data: MwApiParams | URLSearchParams | FormData, options?: Partial<FexiosRequestOptions>): Promise<FexiosFinalContext<MwApiResponse<T>>>;
/**
* Wrap a request to map non-2xx responses containing MediaWiki API error bodies
* into MediaWikiApiError when throwOnApiError=true, and then pass 2xx responses
* through handleApiResponse for unified processing.
*/
private runRequestWithApiErrorMapping;
private throwIfApiError;
private handleApiResponse;
/** Token Handler */
get tokens(): Map<string, string>;
fetchTokens(types?: MwTokenName[]): Promise<Map<string, string>>;
getToken(type?: MwTokenName, noCache?: boolean): Promise<string>;
badToken(type: MwTokenName): Map<string, string>;
postWithToken<T = any>(tokenType: MwTokenName, body: MwApiParams, options?: {
tokenName?: string;
retry?: number;
noCache?: boolean;
fexiosOptions?: Partial<FexiosRequestOptions>;
}): Promise<FexiosFinalContext<MwApiResponse<T>>>;
postWithEditToken<T = any>(body: MwApiParams): Promise<FexiosFinalContext<MwApiResponse<T>>>;
/** @deprecated Use `this.config.baseURL` instead */
get baseURL(): string;
/** @deprecated Use `this.config.defaultParams` instead */
get defaultParams(): MwApiParams;
/** @deprecated Use `this.config.fexiosConfigs` instead */
get defaultOptions(): Partial<FexiosConfigs>;
/** @deprecated Use `createFexiosSaikou` instead */
static readonly createRequestHandler: typeof createFexiosSaikou;
/** @deprecated Use `this.getToken` instead */
token: (type?: MwTokenName, noCache?: boolean) => Promise<string>;
}
export * from './models/errors.js';
export * from './models/FexiosSaikou.js';
export * from './models/MwParamNormalizer.js';
export * from './types.js';
export * from 'fexios';
export {
/** @deprecated Use `WikiSaikou` instead */
WikiSaikouCore as MwApiBase, };