Niedzielski has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/378198 )
Change subject: Chore: improve page unmarshaller typing ...................................................................... Chore: improve page unmarshaller typing Bug: T173323 Change-Id: I4b632874cc3f022440ecb8ac2b72e3bda0391e36 --- M docs/development.md M src/common/marshallers/page-unmarshaller.ts A src/common/types/json.d.ts 3 files changed, 48 insertions(+), 44 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/marvin refs/changes/98/378198/1 diff --git a/docs/development.md b/docs/development.md index 065e35a..41111d2 100644 --- a/docs/development.md +++ b/docs/development.md @@ -67,7 +67,8 @@ The reasoning is that the brevity of inferred code is preferred -as long as the compiler doesn't complain- but APIs are seams and their types should be both fixed and documented. We have set up the TypeScript compiler with the -strict settings so no implicit `any` or `null`/`undefined` should slip in. +strict settings so no implicit `any` or `null` / `undefined` should slip in. +When in doubt, treat typing as logical assertions. The following examples are possible module implementations but the second or third are preferred: diff --git a/src/common/marshallers/page-unmarshaller.ts b/src/common/marshallers/page-unmarshaller.ts index 48f5976..c90bcb4 100644 --- a/src/common/marshallers/page-unmarshaller.ts +++ b/src/common/marshallers/page-unmarshaller.ts @@ -4,53 +4,46 @@ PageSummary, PageThumbnail } from "../models/page"; +import { JSONObject } from "../types/json"; -const unmarshalPageThumbnailRESTBase = ( - json: RESTBase.PageThumbnail -): PageThumbnail => ({ - URL: json.source, - originalURL: json.original, - width: json.width, - height: json.height -}); -export const unmarshalPageThumbnail = (json: any): PageThumbnail => - unmarshalPageThumbnailRESTBase(json); +export const unmarshalPageThumbnail = (json: JSONObject): PageThumbnail => { + const type: RESTBase.PageThumbnail = json as any; + return { + URL: type.source, + originalURL: type.original, + width: type.width, + height: type.height + }; +}; -const unmarshalPageImageRESTBase = (json: RESTBase.PageImage): PageImage => ({ - URL: json.source, - width: json.width, - height: json.height -}); -export const unmarshalPageImage = (json: any): PageImage => - unmarshalPageImageRESTBase(json); +export const unmarshalPageImage = (json: JSONObject): PageImage => { + const type: RESTBase.PageImage = json as any; + return { URL: type.source, width: type.width, height: type.height }; +}; -const unmarshalPageGeolocationRESTBase = ( - json: RESTBase.PageGeolocation -): PageGeolocation => ({ - latitude: json.lat, - longitude: json.lon -}); -export const unmarshalPageGeolocation = (json: any): PageGeolocation => - unmarshalPageGeolocationRESTBase(json); +export const unmarshalPageGeolocation = (json: JSONObject): PageGeolocation => { + const type: RESTBase.PageGeolocation = json as any; + return { latitude: type.lat, longitude: type.lon }; +}; -const unmarshalPageSummaryRESTBase = ( - json: RESTBase.PageSummary -): PageSummary => ({ - wikiLanguageCode: json.lang, - localeDirection: json.dir, - pageID: json.pageid, - lastModified: new Date(json.timestamp), - titleText: json.title, - titleHTML: json.displaytitle, - descriptionText: json.description, - extractText: json.extract, - extractHTML: json.extract_html, - thumbnail: json.thumbnail && unmarshalPageThumbnail(json.thumbnail), - image: json.originalimage && unmarshalPageImage(json.originalimage), - geolocation: json.coordinates && unmarshalPageGeolocation(json.coordinates) -}); -export const unmarshalPageSummary = (json: any): PageSummary => - unmarshalPageSummaryRESTBase(json); +export const unmarshalPageSummary = (json: JSONObject): PageSummary => { + const type: RESTBase.PageSummary = json as any; + return { + wikiLanguageCode: type.lang, + localeDirection: type.dir, + pageID: type.pageid, + lastModified: new Date(type.timestamp), + titleText: type.title, + titleHTML: type.displaytitle, + descriptionText: type.description, + extractText: type.extract, + extractHTML: type.extract_html, + thumbnail: type.thumbnail && unmarshalPageThumbnail(type.thumbnail as {}), + image: type.originalimage && unmarshalPageImage(type.originalimage as {}), + geolocation: + type.coordinates && unmarshalPageGeolocation(type.coordinates as {}) + }; +}; namespace RESTBase { export interface PageThumbnail { diff --git a/src/common/types/json.d.ts b/src/common/types/json.d.ts new file mode 100644 index 0000000..e3f0f5c --- /dev/null +++ b/src/common/types/json.d.ts @@ -0,0 +1,10 @@ +// https://github.com/Microsoft/TypeScript/issues/1897 + +export type JSONPrimitive = string | number | boolean | null; + +// eslint-disable-next-line no-use-before-define +export type JSONValue = JSONPrimitive | JSONObject | JSONArray; + +export type JSONObject = { [member: string]: JSONValue }; + +export interface JSONArray extends Array<JSONValue> {} -- To view, visit https://gerrit.wikimedia.org/r/378198 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4b632874cc3f022440ecb8ac2b72e3bda0391e36 Gerrit-PatchSet: 1 Gerrit-Project: marvin Gerrit-Branch: master Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org> Gerrit-Reviewer: Sniedzielski <sniedziel...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits