Skip to content

Commit

Permalink
feat: add typings
Browse files Browse the repository at this point in the history
test: 💍 add lab typescript tests

although it is giving issues that seem to be unrelated to the actual
typings and related to @types/node
  • Loading branch information
damusix authored and Danilo Alonso committed Feb 8, 2022
1 parent 0f03761 commit 6a8196c
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
"test": "lab -a @hapi/code -t 100 -L",
"test-cov-html": "lab -a @hapi/code -r html -o coverage.html"
},
"types": "./types.d.ts",
"author": "Gil Pedersen <[email protected]>",
"license": "BSD-2-Clause",
"devDependencies": {
"@hapi/code": "^8.0.1",
"@hapi/hapi": "^19.0.4",
"@hapi/lab": "^22.0.3"
"@hapi/lab": "^22.0.3",
"@types/hapi__hapi": "^20.0.10",
"typescript": "^4.5.5"
},
"dependencies": {
"@hapi/bounce": "^2.0.0",
Expand Down
29 changes: 29 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Server } from '@hapi/hapi';
import { ExitingManagerOptions, createManager, Manager, ProcessExitError } from '..';

(async () => {

const manager = createManager(new Server, {
exitTimeout: 30 * 1000
});


const x = new ProcessExitError();

if (x instanceof TypeError) {

console.log('error!');
}

await manager.start();
await manager.stop();

await manager.deactive();


const options: ExitingManagerOptions = {
exitTimeout: 30 * 1000
};

const manager2 = new Manager(new Server, options);
})()
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"typeRoots": [
"./node_modules/@types"
],
// "types": ["node"],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
},
"include": [
"./types.d.ts",
"./test"
]
}
64 changes: 64 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Server } from '@hapi/hapi';

export interface ExitingManagerOptions {
exitTimeout: number
}

export class Manager<S = Server | Server[], O = ExitingManagerOptions> {

exitTimeout: number;
servers: Server[];
state: 'starting' | 'started' | 'stopping' | 'prestopped' | 'stopped' | 'startAborted' | 'errored' | 'timeout';
exitTimer: number;
exitCode: number;
active: boolean

constructor(servers: S, options: O)

/**
* Starts the Hapi servers.
* Returns manager if the server starts succcessfully.
*/
start(): Promise<Manager | void>

/**
* Stops the Hapi servers.
* Throws if any server fails to stop.
*/
stop(): Promise<Error | void>

/**
* Removes process listeners and resets process exit
*/
deactive(): Promise<void>
}

/**
* Creates a new manager for given servers
* @param servers
* @param options
*/
export function createManager <
S = Server | Server[],
O = ExitingManagerOptions
>(
servers: S,
options: O
): Manager<S, O>

/**
* Console.error helper
* @param args log arguments
*/
export function log (...args: any[]): void;

/**
* Deactivates the existing manager
*/
export function reset(): void

/**
* Custom exiting error thrown when process.exit() is called
*/
export class ProcessExitError extends TypeError {}

0 comments on commit 6a8196c

Please sign in to comment.