Skip to content

Commit dfefed9

Browse files
committed
Fix JSDoc and automate TS type generation. Thanks @mirao
1 parent e43a528 commit dfefed9

File tree

4 files changed

+154
-150
lines changed

4 files changed

+154
-150
lines changed

index.d.ts

Lines changed: 85 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,93 @@
11
export = DbHelper;
2-
32
/**
43
* Database helper for CodeceptJS
54
*
65
* @author Thiago Delgado Pinto
76
*/
87
declare class DbHelper {
9-
10-
/**
11-
* Constructor
12-
*
13-
* @param {object} config CodeceptJS configuration.
14-
*/
15-
constructor(config: object);
16-
17-
/**
18-
* Connects to the database described by the given connection string.
19-
*
20-
* @param {string|number} key Identification for using in other commands.
21-
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
22-
* @param {object|undefined} [driver] [OPTIONAL] Driver object, used by `database-js`.
23-
*
24-
* @returns {Connection} DatabaseJS' connection
25-
*/
26-
connect(key: string | number, conn: string | object, driver?: object): any;
27-
28-
29-
/**
30-
* Disconnects and removes the database connection identified by the given key.
31-
*
32-
* @param {string|number} key Database identification key set in connect().
33-
*
34-
* @returns {Promise<boolean>} If it was successful.
35-
*/
36-
disconnect(key: string | number): Promise<boolean>;
37-
38-
/**
39-
* Disconnects and removes the database connection identified by the given key.
40-
*
41-
* @param {string|number} key Database identification key set in connect().
42-
*
43-
* @returns {Promise<boolean>} If it was successful.
44-
*/
45-
removeConnection(key: string | number): Promise<boolean>;
46-
47-
/**
48-
* Performs a query.
49-
*
50-
* @param {string|number} key Database identification key set in connect().
51-
* @param {string} command Query to run.
52-
* @param {...any[]|undefined} [params] [OPTIONAL] Query parameters.
53-
*
54-
* @returns {Promise<any[]>} Query results.
55-
*/
56-
query(key: string | number, command: string, ...params: any[]): Promise<any[]>;
57-
58-
59-
/**
60-
* Executes a command.
61-
*
62-
* @param {string|number} key Database identification key set in connect().
63-
* @param {string} command Command to run.
64-
* @param {any[]} [params] [OPTIONAL] Command parameters.
65-
*
66-
* @returns {Promise<any[]>} Command results.
67-
*/
68-
run(key: string | number, command: string, ...params: any[]): Promise<any[]>;
69-
70-
/**
71-
* Creates a database connection.
72-
*
73-
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
74-
* @param {object|undefined} [driver] [OPTIONAL] Driver object, used by `database-js`.
75-
*
76-
* @returns {Connection} DatabaseJS' connection
77-
*/
78-
createConnection(conn: string | object, driver?: object): any;
79-
80-
/**
81-
* Checks if there is a database connection with the given key.
82-
*
83-
* @param {string|number} key Database identification key set in connect().
84-
*
85-
* @returns {boolean}
86-
*/
87-
hasConnection(key: string | number): boolean;
88-
89-
/**
90-
* Gets the database connection with the given key.
91-
*
92-
* @param {string|number} key Database identification key set in connect().
93-
*
94-
* @returns {Connection} DatabaseJS' connection.
95-
*/
96-
getConnection(key: string | number): any;
97-
8+
/**
9+
* Constructor
10+
*
11+
* @param {object} config CodeceptJS configuration.
12+
*/
13+
constructor(config: object);
14+
options: any;
15+
connectionMap: {};
16+
/**
17+
* Connects to the database described by the given connection string.
18+
*
19+
* @param {string|number} key Identification for using in other commands.
20+
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
21+
* @param {object|undefined} [driver] [OPTIONAL] Driver object, used by `database-js`.
22+
*
23+
* @returns {Connection} DatabaseJS' connection
24+
*/
25+
connect(key: string | number, conn: string | object, driver?: object | undefined): any;
26+
/**
27+
* Disconnects and removes the database connection identified by the given key.
28+
*
29+
* @param {string|number} key Database identification key set in connect().
30+
*
31+
* @returns {Promise<boolean>} If it was successful.
32+
*/
33+
disconnect(key: string | number): Promise<boolean>;
34+
/**
35+
* Disconnects and removes the database connection identified by the given key.
36+
*
37+
* @param {string|number} key Database identification key set in connect().
38+
*
39+
* @returns {Promise<boolean>} If it was successful.
40+
*/
41+
removeConnection(key: string | number): Promise<boolean>;
42+
/**
43+
* Performs a query.
44+
*
45+
* @param {string|number} key Database identification key set in connect().
46+
* @param {string} command Query to run.
47+
* @param {...any[]|undefined} [params] [OPTIONAL] Query parameters.
48+
*
49+
* @returns {Promise<any[]>} Query results.
50+
*/
51+
query(key: string | number, command: string, ...params?: (any[] | undefined)[]): Promise<any[]>;
52+
/**
53+
* Executes a command.
54+
*
55+
* @param {string|number} key Database identification key set in connect().
56+
* @param {string} command Command to run.
57+
* @param {any[]} [params] [OPTIONAL] Command parameters.
58+
*
59+
* @returns {Promise<any[]>} Command results.
60+
*/
61+
run(key: string | number, command: string, ...params?: any[]): Promise<any[]>;
62+
/**
63+
* Creates a database connection.
64+
*
65+
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
66+
* @param {object|undefined} [driver] [OPTIONAL] Driver object, used by `database-js`.
67+
*
68+
* @returns {Connection} DatabaseJS' connection
69+
*/
70+
createConnection(conn: string | object, driver?: object | undefined): any;
71+
/**
72+
* Checks if there is a database connection with the given key.
73+
*
74+
* @param {string|number} key Database identification key set in connect().
75+
*
76+
* @returns {boolean}
77+
*/
78+
hasConnection(key: string | number): boolean;
79+
/**
80+
* Gets the database connection with the given key.
81+
*
82+
* @param {string|number} key Database identification key set in connect().
83+
*
84+
* @returns {Connection} DatabaseJS' connection.
85+
*/
86+
getConnection(key: string | number): any;
87+
/**
88+
* Creates an error that represents a database not found.
89+
*
90+
* @param {string|number} key Database identification key.
91+
*/
92+
_keyNotFoundError(key: string | number): Error;
9893
}

index.js

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ if ( ! Helper ) {
1010
*/
1111
class DbHelper extends Helper {
1212

13-
/**
14-
* Constructor
15-
*
16-
* @param {object} config CodeceptJS configuration
17-
*/
13+
/**
14+
* Constructor
15+
*
16+
* @param {object} config CodeceptJS configuration.
17+
*/
1818
constructor( config ) {
1919
super( config );
2020
const defaultOptions = {};
@@ -23,26 +23,26 @@ class DbHelper extends Helper {
2323
this.connectionMap = {};
2424
}
2525

26-
/**
27-
* Connects to the database described by the given connection string.
28-
*
29-
* @param {string|number} key Identification for using in other commands.
30-
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
31-
* @param {object|undefined} driver [OPTIONAL] Driver object, used by `database-js`.
26+
/**
27+
* Connects to the database described by the given connection string.
28+
*
29+
* @param {string|number} key Identification for using in other commands.
30+
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
31+
* @param {object|undefined} [driver] [OPTIONAL] Driver object, used by `database-js`.
3232
*
3333
* @returns {Connection} DatabaseJS' connection
34-
*/
34+
*/
3535
connect( key, conn, driver ) {
3636
return this.connectionMap[ key ] = this.createConnection( conn, driver );
3737
}
3838

39-
/**
40-
* Disconnects from the database identified by the given key.
41-
*
42-
* @param {string|number} key Database identification key set in connect().
39+
/**
40+
* Disconnects and removes the database connection identified by the given key.
41+
*
42+
* @param {string|number} key Database identification key set in connect().
4343
*
4444
* @returns {Promise<boolean>} If it was successful.
45-
*/
45+
*/
4646
async disconnect( key ) {
4747
let conn = this.connectionMap[ key ];
4848
if ( ! conn ) {
@@ -51,13 +51,13 @@ class DbHelper extends Helper {
5151
return conn.close();
5252
}
5353

54-
/**
55-
* Disconnects and removes the database connection identified by the given key.
56-
*
57-
* @param {string|number} key Database identification key set in connect().
54+
/**
55+
* Disconnects and removes the database connection identified by the given key.
5856
*
59-
* @returns {boolean} If it was successful.
60-
*/
57+
* @param {string|number} key Database identification key set in connect().
58+
*
59+
* @returns {Promise<boolean>} If it was successful.
60+
*/
6161
async removeConnection( key ) {
6262
if ( ! this.connectionMap[ key ] ) {
6363
return true;
@@ -72,15 +72,15 @@ class DbHelper extends Helper {
7272
return couldClose;
7373
}
7474

75-
/**
76-
* Performs a query.
77-
*
78-
* @param {string|number} key Database identification key set in connect().
79-
* @param {string} command Query to run.
80-
* @param {any[]} params [OPTIONAL] Query parameters.
81-
*
82-
* @returns {Promise<any[]>} Query results.
83-
*/
75+
/**
76+
* Performs a query.
77+
*
78+
* @param {string|number} key Database identification key set in connect().
79+
* @param {string} command Query to run.
80+
* @param {...any[]|undefined} [params] [OPTIONAL] Query parameters.
81+
*
82+
* @returns {Promise<any[]>} Query results.
83+
*/
8484
async query( key, command, ... params ) {
8585
let conn = this.connectionMap[ key ];
8686
if ( ! conn ) {
@@ -93,15 +93,15 @@ class DbHelper extends Helper {
9393
return stmt.query( ... params );
9494
}
9595

96-
/**
97-
* Executes a command.
98-
*
99-
* @param {string|number} key Database identification key set in connect().
100-
* @param {string} command Command to run.
101-
* @param {any[]} params [OPTIONAL] Command parameters.
102-
*
103-
* @returns {Promise<any[]>} Command results.
104-
*/
96+
/**
97+
* Executes a command.
98+
*
99+
* @param {string|number} key Database identification key set in connect().
100+
* @param {string} command Command to run.
101+
* @param {any[]} [params] [OPTIONAL] Command parameters.
102+
*
103+
* @returns {Promise<any[]>} Command results.
104+
*/
105105
async run( key, command, ... params ) {
106106
let conn = this.connectionMap[ key ];
107107
if ( ! conn ) {
@@ -114,36 +114,37 @@ class DbHelper extends Helper {
114114
return stmt.execute( ... params );
115115
}
116116

117-
/**
118-
* Creates a database connection.
119-
*
120-
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
121-
* @param {object|undefined} driver [OPTIONAL] Driver object, used by `database-js`.
117+
/**
118+
* Creates a database connection.
122119
*
123-
* @returns {Connection} DatabaseJS' connection.
124-
*/
120+
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
121+
* @param {object|undefined} [driver] [OPTIONAL] Driver object, used by `database-js`.
122+
*
123+
* @returns {Connection} DatabaseJS' connection
124+
*/
125125
createConnection( conn, driver ) {
126126
return new dbjs.Connection( conn, driver );
127127
}
128128

129-
/**
130-
* Checks if there is a database connection with the given key.
131-
*
132-
* @param {string|number} key Database identification key set in connect().
129+
130+
/**
131+
* Checks if there is a database connection with the given key.
132+
*
133+
* @param {string|number} key Database identification key set in connect().
133134
*
134135
* @returns {boolean}
135-
*/
136+
*/
136137
hasConnection( key ) {
137138
return !! this.connectionMap[ key ];
138139
}
139140

140-
/**
141-
* Gets the database connection with the given key.
142-
*
143-
* @param {string|number} key Database identification key set in connect().
141+
/**
142+
* Gets the database connection with the given key.
143+
*
144+
* @param {string|number} key Database identification key set in connect().
144145
*
145146
* @returns {Connection} DatabaseJS' connection.
146-
*/
147+
*/
147148
getConnection( key ) {
148149
return this.connectionMap[ key ];
149150
}

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@
2525
"test:v2": "cd codeceptjs_check/v2 && npx codeceptjs run",
2626
"test:v3": "cd codeceptjs_check/v3 && npx codeceptjs run",
2727
"test:all": "npm run test && npm run test:v2 && npm run test:v3",
28-
"preversion": "npm run test"
28+
"doc": "tsc index.js --declaration --allowJs --emitDeclarationOnly",
29+
"preversion": "npm run doc && npm run test"
2930
},
3031
"dependencies": {
3132
"database-js": "^3.0.11"
3233
},
3334
"devDependencies": {
3435
"codeceptjs": "^3.1.2",
3536
"database-js-sqlite": "^1.3.0",
36-
"mocha": "^8.4.0"
37+
"mocha": "^8.4.0",
38+
"typescript": "^4.4.3"
3739
}
3840
}

0 commit comments

Comments
 (0)