Skip to content

Commit

Permalink
Cleanup mw namespace
Browse files Browse the repository at this point in the history
Move some things from the `mw` namespace to sub-namespaces
- move `mw.CacheReport/LimitReport/LimitReportValue` to `mw.PageParseReport.Cache/Limit/LimitValue`
- move `mw.SearchIndexEntry` to `mw.SearchIndex.Entry`
- move `mw.TitleLike` to `mw.Title.Like`
- rename `mw.cookie.CookieOptions` to `mw.cookie.Options`
  • Loading branch information
Adrien LESÉNÉCHAL committed Oct 9, 2024
1 parent cee15b8 commit ec75f59
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 59 deletions.
8 changes: 4 additions & 4 deletions jquery/cookie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ declare global {
* current cookie keys and values.
* @param {string|null} [value] Cookie value to set. If `null`, this method will remove the cookie.
* If omited, this method will get and return the current value.
* @param {mw.cookie.CookieOptions} [options]
* @param {mw.cookie.Options} [options]
* @returns {string|Object} The current value (if getting a cookie), or an internal `document.cookie`
* expression (if setting or removing).
* @see https://doc.wikimedia.org/mediawiki-core/master/js/module-mediawiki.cookie.html#.$.cookie
*/
cookie(key: string, value: string | null, options?: mw.cookie.CookieOptions): string;
cookie(key: string, value: string | null, options?: mw.cookie.Options): string;
cookie(key: string): unknown;
cookie(): Record<string, unknown>;

Expand All @@ -32,11 +32,11 @@ declare global {
* } );
* ```
* @param {string} key
* @param {mw.cookie.CookieOptions} options
* @param {mw.cookie.Options} options
* @returns {boolean} True if the cookie previously existed
* @see https://doc.wikimedia.org/mediawiki-core/master/js/module-mediawiki.cookie.html#.$.removeCookie
*/
removeCookie(key: string, options: mw.cookie.CookieOptions): boolean;
removeCookie(key: string, options: mw.cookie.Options): boolean;
}
}

Expand Down
43 changes: 23 additions & 20 deletions mw/Api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ declare global {
* );
* ```
* @since 1.28
* @param {TitleLike} title Page title
* @param {Title.Like} title Page title
* @param {Api.Params.EditPage} params Edit API parameters
* @param {string} content Page content
* @returns {JQuery.Promise<Api.Response.Edit>} API response
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html#create
*/
create(
title: TitleLike,
title: Title.Like,
params: Api.Params.EditPage,
content: string
): JQuery.Promise<Api.Response.Edit>;
Expand Down Expand Up @@ -246,12 +246,15 @@ declare global {
* ```
*
* @since 1.28
* @param {TitleLike} title Page title
* @param {Title.Like} title Page title
* @param {Api.EditTransform} transform Callback that prepares the edit
* @returns {JQuery.Promise<Api.Response.Edit>} Edit API response
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html#edit
*/
edit(title: TitleLike, transform: Api.EditTransform): JQuery.Promise<Api.Response.Edit>;
edit(
title: Title.Like,
transform: Api.EditTransform
): JQuery.Promise<Api.Response.Edit>;

/**
* Perform API get request. See {@link ajax()} for details.
Expand All @@ -269,11 +272,11 @@ declare global {
/**
* Get the categories that a particular page on the wiki belongs to.
*
* @param {TitleLike} title
* @param {Title.Like} title
* @returns {JQuery.Promise<false|Title[]>} Promise that resolves with an array of category titles, or with false if the title was not found.
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html#getCategories
*/
getCategories(title: TitleLike): JQuery.Promise<false | Title[]>;
getCategories(title: Title.Like): JQuery.Promise<false | Title[]>;

/**
* Get a list of categories that match a certain prefix.
Expand Down Expand Up @@ -382,11 +385,11 @@ declare global {
/**
* Determine if a category exists.
*
* @param {TitleLike} title
* @param {Title.Like} title
* @returns {JQuery.Promise<boolean>} Promise that resolves with a boolean indicating whether the category exists.
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html#isCategory
*/
isCategory(title: TitleLike): JQuery.Promise<boolean>;
isCategory(title: Title.Like): JQuery.Promise<boolean>;

/**
* Load a set of messages and add them to {@link mw.messages}.
Expand Down Expand Up @@ -429,15 +432,15 @@ declare global {
/**
* Post a new section to the page.
*
* @param {TitleLike} title Target page
* @param {Title.Like} title Target page
* @param {string} header
* @param {string} message Wikitext message
* @param {Api.Params.EditPage} additionalParams Additional API parameters
* @returns {JQuery.Promise<Api.Response>} See {@link postWithEditToken}
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html#newSection
*/
newSection(
title: TitleLike,
title: Title.Like,
header: string,
message: string,
additionalParams?: Api.Params.EditPage
Expand All @@ -446,13 +449,13 @@ declare global {
/**
* Convenience method for `action=parse`.
*
* @param {TitleLike} content Content to parse, either as a wikitext string or a {@link mw.Title}
* @param {Title.Like} content Content to parse, either as a wikitext string or a {@link mw.Title}
* @param {Api.Params.Parse} [additionalParams] Parameters object to set custom settings, e.g.
* `redirects`, `sectionpreview`. `prop` should not be overridden.
* @returns {JQuery.Promise<string>} Promise that resolves with the parsed HTML of `wikitext`
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html#parse
*/
parse(content: TitleLike, additionalParams?: Api.Params.Parse): JQuery.Promise<string>;
parse(content: Title.Like, additionalParams?: Api.Params.Parse): JQuery.Promise<string>;

/**
* Perform API post request. See {@link ajax()} for details.
Expand Down Expand Up @@ -521,14 +524,14 @@ declare global {
* Convenience method for `action=rollback`.
*
* @since 1.28
* @param {TitleLike} page
* @param {Title.Like} page
* @param {string} user
* @param {Api.Params.Rollback} [params] Additional parameters
* @returns {JQuery.Promise<RollbackInfo>}
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html#rollback
*/
rollback(
page: TitleLike,
page: Title.Like,
user: string,
params?: Api.Params.Rollback
): JQuery.Promise<Api.Response.Rollback>;
Expand Down Expand Up @@ -568,17 +571,17 @@ declare global {
/**
* Convenience method for `action=watch&unwatch=1`.
*
* @param {TypeOrArray<TitleLike>} pages Full page name or instance of {@link mw.Title}, or an
* @param {TypeOrArray<Title.Like>} pages Full page name or instance of {@link mw.Title}, or an
* array thereof. If an array is passed, the return value passed to the promise will also be an
* array of appropriate objects.
* @returns {JQuery.Promise<TypeOrArray<Api.WatchedPage>>} A promise that resolves
* with an object (or array of objects) describing each page that was passed in and its
* current watched/unwatched status.
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html#unwatch
*/
unwatch<P extends TypeOrArray<TitleLike>>(
unwatch<P extends TypeOrArray<Title.Like>>(
pages: P
): JQuery.Promise<ReplaceValue<P, TitleLike, Api.WatchedPage>>;
): JQuery.Promise<ReplaceValue<P, Title.Like, Api.WatchedPage>>;

/**
* Upload a file to MediaWiki.
Expand Down Expand Up @@ -638,7 +641,7 @@ declare global {
* Convenience method for `action=watch`.
*
* @since 1.35 - expiry parameter can be passed when Watchlist Expiry is enabled.
* @param {TypeOrArray<TitleLike>} pages Full page name or instance of {@link mw.Title}, or an
* @param {TypeOrArray<Title.Like>} pages Full page name or instance of {@link mw.Title}, or an
* array thereof. If an array is passed, the return value passed to the promise will also be an
* array of appropriate objects.
* @param {string} [expiry] When the page should expire from the watchlist. If omitted, the
Expand All @@ -648,10 +651,10 @@ declare global {
* current watched/unwatched status.
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html#watch
*/
watch<P extends TypeOrArray<TitleLike>>(
watch<P extends TypeOrArray<Title.Like>>(
pages: P,
expiry?: string
): JQuery.Promise<ReplaceValue<P, TitleLike, Api.WatchedPage>>;
): JQuery.Promise<ReplaceValue<P, Title.Like, Api.WatchedPage>>;

/**
* Massage parameters from the nice format we accept into a format suitable for the API.
Expand Down
10 changes: 5 additions & 5 deletions mw/Title.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
declare global {
namespace mw {
type TitleLike = string | Title;

/**
* Library for constructing MediaWiki titles.
*
Expand Down Expand Up @@ -278,7 +276,7 @@ declare global {
* @throws {Error} If title is not a string or mw.Title
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Title.html#.exists
*/
static exists(title: TitleLike): boolean | null;
static exists(title: Title.Like): boolean | null;

/**
* Check if a given namespace is a talk namespace.
Expand Down Expand Up @@ -396,6 +394,8 @@ declare global {
}

namespace Title {
type Like = string | Title;

/**
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.Title.html#~TitleExistenceStore
*/
Expand Down Expand Up @@ -438,7 +438,7 @@ declare global {
}
}

/** @deprecated Use {@link mw.TitleLike} instead */
export type TitleLike = mw.TitleLike;
/** @deprecated Use {@link mw.Title.Like} instead */
export type TitleLike = mw.Title.Like;

export {};
44 changes: 23 additions & 21 deletions mw/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,31 +361,33 @@ declare global {
}>;

interface PageParseReport {
cachereport: CacheReport;
limitreport: LimitReport;
cachereport: PageParseReport.Cache;
limitreport: PageParseReport.Limit;
}

interface CacheReport {
timestamp: `${number}`;
transientcontent: boolean;
ttl: number;
}
namespace PageParseReport {
interface Cache {
timestamp: `${number}`;
transientcontent: boolean;
ttl: number;
}

interface LimitReport {
"cputime": `${number}`;
"expansiondepth": LimitReportValue;
"expensivefunctioncount": LimitReportValue;
"postexpandincludesize": LimitReportValue;
"ppvisitednodes": LimitReportValue;
"templateargumentsize": LimitReportValue;
"timingprofile": string[];
"unstrip-depth": LimitReportValue;
"unstrip-size": LimitReportValue;
}
interface Limit {
"cputime": `${number}`;
"expansiondepth": LimitValue;
"expensivefunctioncount": LimitValue;
"postexpandincludesize": LimitValue;
"ppvisitednodes": LimitValue;
"templateargumentsize": LimitValue;
"timingprofile": string[];
"unstrip-depth": LimitValue;
"unstrip-size": LimitValue;
}

interface LimitReportValue {
limit: number;
value: number;
interface LimitValue {
limit: number;
value: number;
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions mw/cookie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ declare global {
*
* @param {string} key
* @param {string|null} value Value of cookie. If `value` is `null` then this method will instead remove a cookie by name of `key`
* @param {CookieOptions|Date|number} [options] Options object, or expiry date
* @param {Options|Date|number} [options] Options object, or expiry date
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.cookie.html#.set
*/
// see https://stackoverflow.com/a/64932909 for <SS>
function set<SS extends string = SameSite>(
key: string,
value: string | null,
options?: CookieOptions<SS> | Date | number
options?: Options<SS> | Date | number
): void;

/**
* Custom scope for cookie key, must match the way it was set.
*
* @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.cookie.html#.CookieOptions
*/
interface CookieOptions<SS extends string = SameSite> {
interface Options<SS extends string = SameSite> {
/**
* Custom scope for cookie key. The domain attribute of the cookie.
* Defaults to wgCookieDomain.
Expand Down
14 changes: 8 additions & 6 deletions mw/hook.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,16 @@ declare global {
}

interface SearchIndex {
[k: string]: SearchIndexEntry[];
[k: string]: SearchIndex.Entry[];
}

interface SearchIndexEntry {
$field: JQuery;
$highlight: JQuery;
$tabPanel: JQuery;
$wrapper: JQuery;
namespace SearchIndex {
interface Entry {
$field: JQuery;
$highlight: JQuery;
$tabPanel: JQuery;
$wrapper: JQuery;
}
}

interface EditRecovery {
Expand Down

0 comments on commit ec75f59

Please sign in to comment.