Skip to content

Commit 4b731fa

Browse files
remojansengergelyke
authored andcommitted
feat(types): add typescript support
1 parent 6194b3e commit 4b731fa

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
6+
"types": "./typings/index.d.ts",
67
"scripts": {
7-
"test": "mocha lib/**/*.spec.js",
8+
"test-typings": "tsc --lib ES2015 --noEmit typings/*.ts",
9+
"test": "mocha lib/**/*.spec.js && npm run test-typings",
810
"eslint": "eslint-godaddy -c .eslintrc lib example index.js",
911
"coverage": "nyc mocha lib/**/*.spec.js",
1012
"publish": "npm run eslint && npm test",
@@ -22,6 +24,9 @@
2224
"stoppable": "^1.0.5"
2325
},
2426
"devDependencies": {
27+
"@types/express": "^4.0.39",
28+
"@types/koa": "^2.0.42",
29+
"@types/node": "^8.0.58",
2530
"chai": "^4.1.2",
2631
"eslint": "^4.4.1",
2732
"eslint-config-godaddy": "^2.0.0",
@@ -31,7 +36,8 @@
3136
"mocha": "^4.0.1",
3237
"node-fetch": "^1.7.3",
3338
"nyc": "^11.3.0",
39+
"semantic-release": "^8.2.0",
3440
"supertest": "^3.0.0",
35-
"semantic-release": "^8.2.0"
41+
"typescript": "^2.6.2"
3642
}
3743
}

typings/express.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as http from "http";
2+
import * as terminus from "@godaddy/terminus";
3+
import * as express from "express";
4+
5+
const app = express();
6+
7+
app.get("/", (req, res) => res.send("ok"));
8+
9+
const server = http.createServer(app);
10+
11+
function onHealthCheck() {
12+
return Promise.resolve();
13+
}
14+
15+
terminus(http.createServer(app), {
16+
logger: console.log,
17+
healthChecks: {
18+
"/healthcheck": () => Promise.resolve()
19+
}
20+
}).listen(3000);

typings/index.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
declare module "@godaddy/terminus" {
2+
3+
type HealthCheck = () => Promise<any>;
4+
5+
interface HealthCheckMap {
6+
[key: string]: HealthCheck;
7+
}
8+
9+
interface TerminusOptions {
10+
healthChecks?: HealthCheckMap;
11+
timeout?: number;
12+
onSigterm?: () => Promise<any>;
13+
onShutdown?: () => Promise<any>;
14+
logger?: (msg: string, err: Error) => void;
15+
}
16+
17+
type Terminus = <T>(server: T, options?: TerminusOptions) => T;
18+
19+
const terminus: Terminus;
20+
21+
export = terminus;
22+
}

typings/index.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as http from "http";
2+
import * as terminus from "@godaddy/terminus";
3+
4+
async function onSigterm () {
5+
console.log('server is starting cleanup');
6+
return Promise.all([
7+
// your clean logic, like closing database connections
8+
]);
9+
}
10+
11+
async function onShutdown () {
12+
console.log('cleanup finished, server is shutting down');
13+
return Promise.resolve();
14+
}
15+
16+
const server = http.createServer((request, response) => {
17+
response.end('<html><body><h1>Hello, World!</h1></body></html>');
18+
})
19+
20+
terminus(server, {
21+
healthChecks: {
22+
"/healthcheck": () => Promise.resolve()
23+
},
24+
timeout: 1000,
25+
onSigterm,
26+
onShutdown,
27+
logger: console.log
28+
});
29+
30+
server.listen(3000);

typings/koa.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as http from "http";
2+
import * as terminus from "@godaddy/terminus";
3+
import * as Koa from "koa";
4+
5+
const app = new Koa();
6+
7+
const server = http.createServer(app.callback());
8+
9+
function onHealthCheck() {
10+
return Promise.resolve();
11+
}
12+
13+
terminus(server, {
14+
logger: console.log,
15+
healthChecks: {
16+
"/healthcheck": () => Promise.resolve()
17+
}
18+
});
19+
20+
server.listen(3000);

0 commit comments

Comments
 (0)