Skip to content

Commit 689a2e4

Browse files
1 parent 19fd6c2 commit 689a2e4

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

types/psi/index.d.ts

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Type definitions for psi 4.0
2+
// Project: https://github.com/GoogleChromeLabs/psi#readme
3+
// Definitions by: Piotr Błażejewicz <https://github.com/peterblazejewicz>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
6+
/**
7+
* PageSpeed Insights with reporting
8+
*/
9+
declare function psi(url: string, options?: psi.Options): Promise<psi.ResponseData>;
10+
11+
declare namespace psi {
12+
/**
13+
* Output the formatted report to the terminal.
14+
*/
15+
function output(url: string, options?: Options): Promise<ResponseData>;
16+
17+
interface Options {
18+
/**
19+
* When using this module for a production-level build process,
20+
* registering for an API key from the Google Developer Console is recommended.
21+
*/
22+
key?: string;
23+
nokey?: string;
24+
/**
25+
* Strategy to use when analyzing the page.
26+
* @default 'mobile'
27+
*/
28+
strategy?: 'mobile' | 'desktop';
29+
/**
30+
* Locale results should be generated in.
31+
* @default 'en_US'
32+
*/
33+
locale?: string;
34+
/**
35+
* Threshold score to pass the PageSpeed test. Useful for setting a performance budget.
36+
* @default 70
37+
*/
38+
treshold?: number;
39+
}
40+
41+
interface Experience {
42+
id: string;
43+
metrics: {
44+
[key: string]: {
45+
percentile: number;
46+
distributions: Array<{
47+
min: number;
48+
max: number;
49+
proportion: number;
50+
}>;
51+
category: string;
52+
};
53+
};
54+
overall_category: string;
55+
initial_url: string;
56+
}
57+
58+
interface Environment {
59+
networkUserAgent: string;
60+
hostUserAgent: string;
61+
benchmarkIndex: number;
62+
}
63+
64+
interface ConfigSettings {
65+
emulatedFormFactor: string;
66+
locale: string;
67+
onlyCategories: object;
68+
}
69+
70+
interface Audit {
71+
id: string;
72+
title: string;
73+
description: string;
74+
score: object;
75+
displayValue: string;
76+
scoreDisplayMode: string;
77+
explanation: string;
78+
errorMessage: string;
79+
warnings: object;
80+
details: {
81+
[key: string]: object;
82+
};
83+
}
84+
85+
interface AuditRef {
86+
id: string;
87+
weight: number;
88+
group: string;
89+
}
90+
91+
interface Category {
92+
id: string;
93+
title: string;
94+
description: string;
95+
score: object;
96+
manualDescription: string;
97+
auditRefs: AuditRef[];
98+
}
99+
100+
interface CategoryGroup {
101+
title: string;
102+
description: string;
103+
}
104+
105+
interface RuntimeError {
106+
code: string;
107+
message: string;
108+
}
109+
110+
interface Timing {
111+
total: number;
112+
}
113+
114+
interface RendererFormattedStrings {
115+
varianceDisclaimer: string;
116+
opportunityResourceColumnLabel: string;
117+
opportunitySavingsColumnLabel: string;
118+
errorMissingAuditInfo: string;
119+
errorLabel: string;
120+
warningHeader: string;
121+
auditGroupExpandTooltip: string;
122+
passedAuditsGroupTitle: string;
123+
notApplicableAuditsGroupTitle: string;
124+
manualAuditsGroupTitle: string;
125+
toplevelWarningsMessage: string;
126+
scorescaleLabel: string;
127+
crcLongestDurationLabel: string;
128+
crcInitialNavigation: string;
129+
lsPerformanceCategoryDescription: string;
130+
labDataTitle: string;
131+
}
132+
133+
// tslint:disable-next-line:interface-name I18N is established convention
134+
interface I18N {
135+
rendererFormattedStrings: RendererFormattedStrings;
136+
}
137+
138+
interface Version {
139+
major: number;
140+
minor: number;
141+
}
142+
143+
interface ResponseData {
144+
data: Result;
145+
}
146+
147+
interface LighthouseResult {
148+
requestedUrl: string;
149+
finalUrl: string;
150+
lighthouseVersion: string;
151+
userAgent: string;
152+
fetchTime: string;
153+
environment: Environment;
154+
runWarnings: string[];
155+
configSettings: ConfigSettings;
156+
audits: {
157+
[key: string]: Audit;
158+
};
159+
categories: {
160+
[key: string]: Category;
161+
};
162+
categoryGroups: {
163+
[key: string]: CategoryGroup;
164+
};
165+
runtimeError: RuntimeError;
166+
timing: Timing;
167+
i18n: I18N;
168+
}
169+
interface Result {
170+
captchaResult: string;
171+
kind: string;
172+
id: string;
173+
loadingExperience: Experience;
174+
originLoadingExperience: Experience;
175+
lighthouseResult: LighthouseResult;
176+
analysisUTCTimestamp: string;
177+
version: Version;
178+
}
179+
}
180+
181+
export = psi;

types/psi/psi-tests.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference types="node" />
2+
import psi = require('psi');
3+
4+
(async () => {
5+
const { data } = await psi('https://theverge.com');
6+
console.log('Speed score:', data.lighthouseResult.categories.performance.score);
7+
await psi.output('https://theverge.com');
8+
console.log('Done');
9+
const data2 = await psi('https://theverge.com', {
10+
nokey: 'true',
11+
strategy: 'desktop',
12+
});
13+
console.log('Speed score:', data2.data.lighthouseResult.categories.performance.score);
14+
console.log(data.lighthouseResult.audits['screenshot-thumbnails'].details.items);
15+
})();
16+
psi('https://theverge.com').then(({ data }) => {
17+
console.log('Speed score:', data.lighthouseResult.categories.performance.score);
18+
});
19+
psi('https://theverge.com', {
20+
nokey: 'true',
21+
strategy: 'desktop',
22+
}).then(data2 => {
23+
console.log('Speed score:', data2.data.lighthouseResult.categories.performance.score);
24+
});

types/psi/tsconfig.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"baseUrl": "../",
12+
"typeRoots": [
13+
"../"
14+
],
15+
"types": [],
16+
"noEmit": true,
17+
"forceConsistentCasingInFileNames": true
18+
},
19+
"files": [
20+
"index.d.ts",
21+
"psi-tests.ts"
22+
]
23+
}

types/psi/tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "extends": "dtslint/dt.json" }

0 commit comments

Comments
 (0)