Skip to content

Commit 220b55a

Browse files
Merge pull request #6107 from Countly/tsconfig_types
First addition of typescript types to core files
2 parents f560908 + 57ed1ca commit 220b55a

File tree

9 files changed

+400
-111
lines changed

9 files changed

+400
-111
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ log/
4444
log/supervisord/
4545
plugins/plugins.json.*
4646
.sdk
47-
dump
47+
dump
48+
dist/

api/lib/countly.common.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
* @module "api/lib/countly.common"
44
*/
55

6+
/**
7+
* @typedef {import('moment-timezone')} MomentTimezone
8+
*/
9+
610
/** @lends module:api/lib/countly.common */
711
var countlyCommon = {},
12+
/**
13+
* Reference to momentjs
14+
* @type {MomentTimezone} moment
15+
*/
816
moment = require('moment-timezone'),
917
underscore = require('underscore');
1018

@@ -17,8 +25,8 @@ var _period = "hour",
1725

1826
/**
1927
* Returns array with unique ticks for period
20-
* @param {moment} startTimestamp - start of period
21-
* @param {moment} endTimestamp - end of period
28+
* @param {MomentTimezone} startTimestamp - start of period
29+
* @param {MomentTimezone} endTimestamp - end of period
2230
* @returns {array} unique array ticks for period
2331
**/
2432
/*function getTicksBetween(startTimestamp, endTimestamp) {
@@ -46,8 +54,8 @@ var _period = "hour",
4654

4755
/**
4856
* Returns array with more generalized unique ticks for period
49-
* @param {moment} startTimestamp - start of period
50-
* @param {moment} endTimestamp - end of period
57+
* @param {MomentTimezone} startTimestamp - start of period
58+
* @param {MomentTimezone} endTimestamp - end of period
5159
* @returns {array} unique array ticks for period
5260
**/
5361
/*function getTicksCheckBetween(startTimestamp, endTimestamp) {
@@ -355,7 +363,7 @@ function fixTimestampToMilliseconds(ts) {
355363

356364
/**
357365
* Returns a period object used by all time related data calculation functions
358-
* @param {moment} prmPeriod period to be calculated (optional)
366+
* @param {MomentTimezone} prmPeriod period to be calculated (optional)
359367
* @param {string} bucket - daily or monthly. If bucket is set, period will be modified to fit full months or days
360368
* @returns {timeObject} time object
361369
**/

api/utils/common.js

Lines changed: 88 additions & 95 deletions
Large diffs are not rendered by default.

api/utils/requestProcessor.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
* @module api/utils/requestProcessor
44
*/
55

6+
/**
7+
* @typedef {import('../../types/requestProcessor').Params} Params
8+
* @typedef {import('../../types/common').TimeObject} TimeObject
9+
*/
10+
611
const Promise = require('bluebird');
712
const url = require('url');
813
const common = require('./common.js');
@@ -65,7 +70,7 @@ const reloadConfig = function() {
6570
/**
6671
* Default request processing handler, which requires request context to operate. Check tcp_example.js
6772
* @static
68-
* @param {params} params - for request context. Minimum needed properties listed
73+
* @param {Params} params - for request context. Minimum needed properties listed
6974
* @param {object} params.req - Request object, should not be empty and should contain listed params
7075
* @param {string} params.req.url - Endpoint URL that you are calling. May contain query string.
7176
* @param {object} params.req.body - Parsed JSON object with data (same name params will overwrite query string if anything provided there)
@@ -103,9 +108,9 @@ const processRequest = (params) => {
103108
paths = urlParts.pathname.split("/");
104109
/**
105110
* Main request processing object containing all information shared through all the parts of the same request
106-
* @typedef params
107-
* @type {object}
108-
* @global
111+
* typedef params
112+
* type {object}
113+
* global
109114
* @property {string} href - full URL href
110115
* @property {http.ServerResponse} res - The HTTP response object
111116
* @property {http.IncomingMessage} req - The HTTP request object
@@ -182,7 +187,7 @@ const processRequest = (params) => {
182187
* @property {Object} [member.permission.d[app_id].allowed] - Object containing allowed delete permissions for the app
183188
* @property {Object} member.eventList - Object containing event collections with replaced app names
184189
* @property {Object} member.viewList - Object containing view collections with replaced app names
185-
* @property {timeObject} time - Time object for the request
190+
* @property {TimeObject} time - Time object for the request
186191
* @property {string} request_hash - Hash of the request data
187192
* @property {string} [previous_session] - ID of the user's previous session
188193
* @property {number} [previous_session_start] - Start timestamp of the user's previous session
@@ -3747,7 +3752,7 @@ function processUser(params, initiator, done, try_times) {
37473752
/**
37483753
* Function to fetch app user from db
37493754
* @param {object} params - params object
3750-
* @returns {promise} - user
3755+
* @returns {Promise} - user
37513756
*/
37523757
const fetchAppUser = (params) => {
37533758
return new Promise((resolve) => {

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"mocha": "10.8.2",
2626
"nyc": "17.1.0",
2727
"should": "13.2.3",
28-
"supertest": "7.1.0"
28+
"supertest": "7.1.0",
29+
"typescript": "^5.8.2"
2930
},
3031
"scripts": {
3132
"test": "grunt --verbose"

tsconfig.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// to set it up, you need to add typescript:
2+
// npm i typescript --save-dev
3+
//
4+
// to run the summary check, call:
5+
// npx tsc
6+
{
7+
"include": [
8+
//"plugins/push/**/*.ts",
9+
//"plugins/push/**/*.js"
10+
"api/utils/common.js",
11+
"api/utils/requestProcessor.js"
12+
],
13+
"exclude": [
14+
"node_modules"
15+
],
16+
"compilerOptions": {
17+
"outDir": "dist", // we're not building the app. but still necessary for, reasons...
18+
"allowJs": true,
19+
"checkJs": true,
20+
"resolveJsonModule": true,
21+
"strict": true
22+
}
23+
}

types/common.d.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Moment } from "moment-timezone";
2+
3+
export interface TimeObject {
4+
/** Momentjs instance for request's time in app's timezone */
5+
now: Moment;
6+
/** Momentjs instance for request's time in UTC */
7+
nowUTC: Moment;
8+
/** Momentjs instance for current time in app's timezone */
9+
nowWithoutTimestamp: Moment;
10+
/** Request's seconds timestamp */
11+
timestamp: number;
12+
/** Request's milliseconds timestamp */
13+
mstimestamp: number;
14+
/** Year of request time in app's timezone in YYYY format */
15+
yearly: string;
16+
/** Month of request time in app's timezone in YYYY.M format */
17+
monthly: string;
18+
/** Date of request time in app's timezone in YYYY.M.D format */
19+
daily: string;
20+
/** Hour of request time in app's timezone in YYYY.M.D.H format */
21+
hourly: string;
22+
/** Week of request time in app's timezone as result day of the year, divided by 7 */
23+
weekly: number;
24+
/** Week of request time in app's timezone according to ISO standard */
25+
weeklyISO: number;
26+
/** Month of request time in app's timezone in format M */
27+
month: string;
28+
/** Day of request time in app's timezone in format D */
29+
day: string;
30+
/** Hour of request time in app's timezone in format H */
31+
hour: string;
32+
}

0 commit comments

Comments
 (0)