Skip to content

Commit 8e66474

Browse files
committed
chore(typescript): switching codebase to ts
1 parent 0b652d3 commit 8e66474

File tree

6 files changed

+284
-27
lines changed

6 files changed

+284
-27
lines changed

jest.config.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
collectCoverageFrom: [
5+
'src/*.{js,jsx}',
6+
'!**/node_modules/**',
7+
'!**/vendor/**',
8+
],
9+
};

package.json

+11-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": "1.0.17",
55
"description": "Small tool for wait that all xhr are finished in pupeteer",
66
"main": "lib/index.js",
7+
"types": "lib/index.d.ts",
78
"homepage": "https://github.com/jtassin/pending-xhr-puppeteer",
89
"repository": {
910
"type": "git",
@@ -24,22 +25,22 @@
2425
"e2e"
2526
],
2627
"devDependencies": {
28+
"@types/jest": "^24.0.11",
29+
"@types/puppeteer": "^1.12.3",
2730
"eslint": "5.15.3",
2831
"eslint-config-prettier": "3.6.0",
2932
"eslint-plugin-jest": "22.4.1",
3033
"husky": "1.3.1",
31-
"jest": "24.5.0",
34+
"jest": "^24.5.0",
3235
"lint-staged": "8.1.0",
3336
"prettier": "1.15.3",
34-
"puppeteer": "1.13.0",
35-
"rollup": "1.0.0"
36-
},
37-
"jest": {
38-
"collectCoverageFrom": [
39-
"src/*.{js,jsx}",
40-
"!**/node_modules/**",
41-
"!**/vendor/**"
42-
]
37+
"puppeteer": "1.11.0",
38+
"rollup": "1.0.0",
39+
"rollup-plugin-typescript": "^1.0.0",
40+
"ts-jest": "^24.0.0",
41+
"tsc": "^1.20150623.0",
42+
"tslib": "^1.9.3",
43+
"typescript": "^3.3.4000"
4344
},
4445
"scripts": {
4546
"prettier:write": "prettier --single-quote --trailing-comma es5 --write src/**/*",

src/__tests__/index.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ describe('PendingXHR', () => {
153153
});
154154
});
155155

156-
describe('waitForAllXhrFinished', async () => {
156+
describe('waitForAllXhrFinished', () => {
157157
it('returns immediatly if no xhr pending count', async () => {
158158
await startServerReturning(OK_NO_XHR);
159159
const page = await browser.newPage();

src/index.js src/index.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1+
import { Request, Page } from 'puppeteer';
2+
3+
interface ResolvableRequest extends Request {
4+
resolver: () => void;
5+
}
6+
17
class PendingXHR {
2-
constructor(page) {
8+
9+
page: Page;
10+
resourceType: string;
11+
pendingXhrs: Set<Request>;
12+
finishedWithSuccessXhrs: Set<Request>;
13+
finishedWithErrorsXhrs: Set<Request>;
14+
promisees: Array<Promise<void>>
15+
16+
constructor(page: Page) {
17+
this.promisees = [];
318
this.page = page;
419
this.resourceType = 'xhr';
520
// page.setRequestInterception(true);
621
this.pendingXhrs = new Set();
722
this.finishedWithSuccessXhrs = new Set();
823
this.finishedWithErrorsXhrs = new Set();
9-
this.promisees = [];
10-
page.on('request', request => {
24+
page.on('request', (request: ResolvableRequest) => {
1125
if (request.resourceType() === this.resourceType) {
1226
this.pendingXhrs.add(request);
1327
this.promisees.push(
@@ -17,7 +31,7 @@ class PendingXHR {
1731
);
1832
}
1933
});
20-
page.on('requestfailed', request => {
34+
page.on('requestfailed', (request: ResolvableRequest) => {
2135
if (request.resourceType() === this.resourceType) {
2236
this.pendingXhrs.delete(request);
2337
this.finishedWithErrorsXhrs.add(request);
@@ -27,7 +41,7 @@ class PendingXHR {
2741
}
2842
}
2943
});
30-
page.on('requestfinished', request => {
44+
page.on('requestfinished', (request: ResolvableRequest) => {
3145
if (request.resourceType() === this.resourceType) {
3246
this.pendingXhrs.delete(request);
3347
this.finishedWithSuccessXhrs.add(request);
@@ -41,7 +55,7 @@ class PendingXHR {
4155

4256
async waitForAllXhrFinished() {
4357
if (this.pendingXhrCount() === 0) {
44-
return true;
58+
return;
4559
}
4660
await Promise.all(this.promisees);
4761
}

tsconfig.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "build",
4+
"module": "commonjs",
5+
"target": "es5",
6+
"lib": ["es6"],
7+
"sourceMap": true,
8+
"allowJs": true,
9+
"moduleResolution": "node",
10+
"rootDir": "src",
11+
"noImplicitReturns": true,
12+
"noImplicitThis": true,
13+
"noImplicitAny": true,
14+
"strictNullChecks": true
15+
},
16+
"exclude": ["node_modules", "build"],
17+
"types": ["typePatches"]
18+
}

0 commit comments

Comments
 (0)