diff --git a/types/psi/index.d.ts b/types/psi/index.d.ts new file mode 100644 index 0000000000..75f27a50ca --- /dev/null +++ b/types/psi/index.d.ts @@ -0,0 +1,181 @@ +// Type definitions for psi 4.0 +// Project: https://github.com/GoogleChromeLabs/psi#readme +// Definitions by: Piotr Błażejewicz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * PageSpeed Insights with reporting + */ +declare function psi(url: string, options?: psi.Options): Promise; + +declare namespace psi { + /** + * Output the formatted report to the terminal. + */ + function output(url: string, options?: Options): Promise; + + interface Options { + /** + * When using this module for a production-level build process, + * registering for an API key from the Google Developer Console is recommended. + */ + key?: string; + nokey?: string; + /** + * Strategy to use when analyzing the page. + * @default 'mobile' + */ + strategy?: 'mobile' | 'desktop'; + /** + * Locale results should be generated in. + * @default 'en_US' + */ + locale?: string; + /** + * Threshold score to pass the PageSpeed test. Useful for setting a performance budget. + * @default 70 + */ + treshold?: number; + } + + interface Experience { + id: string; + metrics: { + [key: string]: { + percentile: number; + distributions: Array<{ + min: number; + max: number; + proportion: number; + }>; + category: string; + }; + }; + overall_category: string; + initial_url: string; + } + + interface Environment { + networkUserAgent: string; + hostUserAgent: string; + benchmarkIndex: number; + } + + interface ConfigSettings { + emulatedFormFactor: string; + locale: string; + onlyCategories: object; + } + + interface Audit { + id: string; + title: string; + description: string; + score: object; + displayValue: string; + scoreDisplayMode: string; + explanation: string; + errorMessage: string; + warnings: object; + details: { + [key: string]: object; + }; + } + + interface AuditRef { + id: string; + weight: number; + group: string; + } + + interface Category { + id: string; + title: string; + description: string; + score: object; + manualDescription: string; + auditRefs: AuditRef[]; + } + + interface CategoryGroup { + title: string; + description: string; + } + + interface RuntimeError { + code: string; + message: string; + } + + interface Timing { + total: number; + } + + interface RendererFormattedStrings { + varianceDisclaimer: string; + opportunityResourceColumnLabel: string; + opportunitySavingsColumnLabel: string; + errorMissingAuditInfo: string; + errorLabel: string; + warningHeader: string; + auditGroupExpandTooltip: string; + passedAuditsGroupTitle: string; + notApplicableAuditsGroupTitle: string; + manualAuditsGroupTitle: string; + toplevelWarningsMessage: string; + scorescaleLabel: string; + crcLongestDurationLabel: string; + crcInitialNavigation: string; + lsPerformanceCategoryDescription: string; + labDataTitle: string; + } + + // tslint:disable-next-line:interface-name I18N is established convention + interface I18N { + rendererFormattedStrings: RendererFormattedStrings; + } + + interface Version { + major: number; + minor: number; + } + + interface ResponseData { + data: Result; + } + + interface LighthouseResult { + requestedUrl: string; + finalUrl: string; + lighthouseVersion: string; + userAgent: string; + fetchTime: string; + environment: Environment; + runWarnings: string[]; + configSettings: ConfigSettings; + audits: { + [key: string]: Audit; + }; + categories: { + [key: string]: Category; + }; + categoryGroups: { + [key: string]: CategoryGroup; + }; + runtimeError: RuntimeError; + timing: Timing; + i18n: I18N; + } + interface Result { + captchaResult: string; + kind: string; + id: string; + loadingExperience: Experience; + originLoadingExperience: Experience; + lighthouseResult: LighthouseResult; + analysisUTCTimestamp: string; + version: Version; + } +} + +export = psi; diff --git a/types/psi/psi-tests.ts b/types/psi/psi-tests.ts new file mode 100644 index 0000000000..4b16ef054c --- /dev/null +++ b/types/psi/psi-tests.ts @@ -0,0 +1,24 @@ +/// +import psi = require('psi'); + +(async () => { + const { data } = await psi('https://theverge.com'); + console.log('Speed score:', data.lighthouseResult.categories.performance.score); + await psi.output('https://theverge.com'); + console.log('Done'); + const data2 = await psi('https://theverge.com', { + nokey: 'true', + strategy: 'desktop', + }); + console.log('Speed score:', data2.data.lighthouseResult.categories.performance.score); + console.log(data.lighthouseResult.audits['screenshot-thumbnails'].details.items); +})(); +psi('https://theverge.com').then(({ data }) => { + console.log('Speed score:', data.lighthouseResult.categories.performance.score); +}); +psi('https://theverge.com', { + nokey: 'true', + strategy: 'desktop', +}).then(data2 => { + console.log('Speed score:', data2.data.lighthouseResult.categories.performance.score); +}); diff --git a/types/psi/tsconfig.json b/types/psi/tsconfig.json new file mode 100644 index 0000000000..4175835c56 --- /dev/null +++ b/types/psi/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "psi-tests.ts" + ] +} diff --git a/types/psi/tslint.json b/types/psi/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/psi/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }