feat(psi): new type definition (#45225)

- definition file for PageSpeedInsights v.4
- tests

https://github.com/GoogleChromeLabs/psi
https://github.com/GoogleChromeLabs/psi#usage
https://developers.google.com/speed/docs/insights/v5/about

Thanks!

/cc @astronaute77

Closes #45210
This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz) 2020-06-08 20:27:40 +02:00 committed by GitHub
parent 19fd6c2340
commit 689a2e444b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 229 additions and 0 deletions

181
types/psi/index.d.ts vendored Normal file
View File

@ -0,0 +1,181 @@
// Type definitions for psi 4.0
// Project: https://github.com/GoogleChromeLabs/psi#readme
// Definitions by: Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/**
* PageSpeed Insights with reporting
*/
declare function psi(url: string, options?: psi.Options): Promise<psi.ResponseData>;
declare namespace psi {
/**
* Output the formatted report to the terminal.
*/
function output(url: string, options?: Options): Promise<ResponseData>;
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;

24
types/psi/psi-tests.ts Normal file
View File

@ -0,0 +1,24 @@
/// <reference types="node" />
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);
});

23
types/psi/tsconfig.json Normal file
View File

@ -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"
]
}

1
types/psi/tslint.json Normal file
View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }