11import { Moment } from "moment-timezone" ;
22import { Collection , Db , ObjectId } from "mongodb" ;
33import { Params } from "./requestProcessor" ;
4+ import { PluginManager , Database } from "./pluginManager" ;
5+ import { Logger } from "./log" ;
6+ import { CountlyAPIConfig } from "./config" ;
7+
8+ /** Node.js Request object */
9+ export interface req {
10+ headers : { [ key : string ] : string | string [ ] } ;
11+ connection : {
12+ remoteAddress ?: string ;
13+ } ;
14+ socket ?: {
15+ remoteAddress ?: string ;
16+ } ;
17+ ip ?: string ;
18+ ips ?: string [ ] ;
19+ }
20+
21+ /** Generic output object for responses */
22+ export interface output {
23+ [ key : string ] : any ;
24+ }
425
526export interface TimeObject {
627 /** Momentjs instance for request's time in app's timezone */
@@ -68,6 +89,9 @@ export interface DbUserMap {
6889 platform : string ;
6990 platform_version : string ;
7091 app_version : string ;
92+ app_version_major : string ;
93+ app_version_minor : string ;
94+ app_version_patch : string ;
7195 last_begin_session_timestamp : string ;
7296 last_end_session_timestamp : string ;
7397 has_ongoing_session : string ;
@@ -101,48 +125,54 @@ export interface OsMapping {
101125export interface ValidationArgProperties {
102126 [ key : string ] : {
103127 /** should property be present in args */
104- required : boolean ;
128+ required ? : boolean ;
105129 /** what type should property be, possible values: String, Array, Number, URL, Boolean, Object, Email */
106- type : string ; // | string[] | object;
107- //array: boolean;
108- //discriminator: (value: any) => any;
130+ type ?: 'String' | 'Array' | 'Number' | 'URL' | 'Boolean' | 'Object' | 'Email' | string ;
109131 /** property should not be longer than provided value */
110- 'max-length' : string ; // number;
132+ 'max-length' ?: number ;
111133 /** property should not be shorter than provided value */
112- 'min-length' : string ; // number;
113- max : string ; // number;
114- min : string ; // number;
134+ 'min-length' ?: number ;
135+ max ?: number ;
136+ min ?: number ;
115137 /** should string property has any number in it */
116- 'has-number' : string ; // boolean;
138+ 'has-number' ?: boolean ;
117139 /** should string property has any latin character in it */
118- 'has-char' : string ; // boolean;
140+ 'has-char' ?: boolean ;
119141 /** should string property has any upper cased latin character in it */
120- 'has-upchar' : string ; // boolean;
142+ 'has-upchar' ?: boolean ;
121143 /** should string property has any none latin character in it */
122- 'has-special' : string ; //boolean;
123- //in?: string[] | (() => string[]);
124- //nonempty: boolean;
125- //custom: (value: any) => string | undefined;
126- //regex: string;
144+ 'has-special' ?: boolean ;
145+ /** allowed values for validation */
146+ in ?: string [ ] | ( ( ) => string [ ] ) ;
127147 /** should property be present in returned validated args object */
128- 'exclude-from-ret-obj' : string ; //boolean;
129- //trim: boolean;
130- //mods: string;
131- //multiple: boolean;
148+ 'exclude-from-ret-obj' ?: boolean ;
149+ /** custom validation function */
150+ custom ?: ( value : any ) => string | undefined ;
151+ /** regex pattern for validation */
152+ regex ?: string ;
153+ /** should value be non-empty */
154+ nonempty ?: boolean ;
155+ /** should trim whitespace */
156+ trim ?: boolean ;
157+ /** additional modifiers */
158+ mods ?: string ;
159+ /** allow multiple values */
160+ multiple ?: boolean ;
132161 } ;
133162}
134163
135164/** Validation result */
136165export interface ValidationResult {
137166 result : boolean ;
138167 errors ?: string [ ] ;
139- obj ?: any ;
168+ obj ?: Record < string , any > ;
140169}
141170
142171/** Date IDs */
143172export interface DateIds {
144173 zero : string ;
145174 month : string ;
175+ [ key : string ] : string ;
146176}
147177
148178/** Custom metric properties */
@@ -157,9 +187,7 @@ export interface CustomMetricProps {
157187 /** value to increment current metric for, default 1 */
158188 value ?: number ;
159189 /** object with segments to record data, key segment name and value segment value or array of segment values */
160- segments ?: {
161- [ key : string ] : string | string [ ] ;
162- } ;
190+ segments ?: Record < string , string | string [ ] | number | boolean > ;
163191 /** if metric should be treated as unique, and stored in 0 docs and be estimated on output */
164192 unique ?: boolean ;
165193 /** timestamp in seconds to be used to determine if unique metrics it unique for specific period */
@@ -205,15 +233,15 @@ export interface HTMLWhitelist {
205233 */
206234export interface Common {
207235 /** Reference to plugins */
208- plugins : any ;
236+ plugins : PluginManager ;
209237
210238 /**
211239 * Escape special characters in the given string of html.
212240 * @param {string } string - The string to escape for inserting into HTML
213241 * @param {boolean } more - if false, escapes only tags, if true escapes also quotes and ampersands
214242 * @returns {string } escaped string
215243 **/
216- escape_html : ( string : string , more : boolean ) => string ;
244+ escape_html : ( string : string , more ? : boolean ) => string ;
217245
218246 /**
219247 * Function to escape unicode characters
@@ -234,7 +262,7 @@ export interface Common {
234262 * @param {string } val - string that might be json encoded
235263 * @returns {object } with property data for parsed data and property valid to check if it was valid json encoded string or not
236264 **/
237- getJSON : ( val : string ) => object ;
265+ getJSON : ( val : string ) => { valid : boolean ; data ?: any } ;
238266
239267 /**
240268 * Logger object for creating module-specific logging
@@ -243,7 +271,7 @@ export interface Common {
243271 * const log = common.log('myplugin:api');
244272 * log.i('myPlugin got a request: %j', params.qstring);
245273 */
246- log : ( module : string ) => any ;
274+ log : ( module : string ) => Logger ;
247275
248276 /**
249277 * Mapping some common property names from longer understandable to shorter representation stored in database
@@ -266,15 +294,15 @@ export interface Common {
266294 /**
267295 * Default {@link countlyConfig} object for API server
268296 */
269- config : object ;
297+ config : CountlyAPIConfig ;
270298
271299 /** Reference to moment-timezone which combines moment.js with timezone support */
272- moment : Moment ;
300+ moment : typeof import ( 'moment-timezone' ) ;
273301
274302 /**
275303 * Reference to crypto module
276304 */
277- crypto : object ;
305+ crypto : typeof import ( 'crypto' ) ;
278306
279307 /**
280308 * Operating syste/platform mappings from what can be passed in metrics to shorter representations
@@ -292,7 +320,7 @@ export interface Common {
292320 dbPromise : ( collection : string , method : string , ...args : any [ ] ) => Promise < any > ;
293321
294322 /** Database reference */
295- db : Db ;
323+ db : Database ;
296324
297325 /**
298326 * Fetches nested property values from an obj.
@@ -329,7 +357,7 @@ export interface Common {
329357 * common.convertToType("test") //outputs "test"
330358 * common.convertToType("12345678901234567890") //outputs "12345678901234567890"
331359 */
332- convertToType : ( value : any , preventParsingToNumber : boolean ) => any ;
360+ convertToType : ( value : any , preventParsingToNumber ? : boolean ) => any ;
333361
334362 /**
335363 * Safe division between numbers providing 0 as result in cases when dividing by 0
@@ -417,7 +445,7 @@ export interface Common {
417445 * @param {string } reqTimestamp - timestamp in the request
418446 * @returns {TimeObject } Time object for current request
419447 */
420- initTimeObj : ( appTimezone : string , reqTimestamp : string ) => TimeObject ;
448+ initTimeObj : ( appTimezone : string , reqTimestamp : string | number ) => TimeObject ;
421449
422450 /**
423451 * Creates a Date object from provided seconds timestamp in provided timezone
@@ -939,8 +967,60 @@ export interface Common {
939967 */
940968 trimWhitespaceStartEnd : ( value : any ) => any ;
941969
970+
971+
972+ /** DataTable class for server-side processing */
973+ DataTable : any ;
974+
942975 /** Write batcher */
943976 writeBatcher : {
944977 add : ( collection : string , id : string , update : any ) => void ;
945978 } ;
979+
980+ /** Database connection for output */
981+ outDb ?: Database ;
982+
983+ /** Database connection for drill queries */
984+ drillDb ?: Database ;
985+
986+ /** Request processor function */
987+ processRequest ?: any ;
988+
989+ /** Read batcher */
990+ readBatcher ?: import ( './batcher' ) . ReadBatcher ;
991+
992+ /** Insert batcher */
993+ insertBatcher ?: any ;
994+
995+ /** Drill read batcher */
996+ drillReadBatcher ?: any ;
997+
998+ /** Job runners */
999+ runners ?: any ;
1000+
1001+ /** Cache instance */
1002+ cache ?: any ;
1003+
1004+ /** Cached schema */
1005+ cachedSchema ?: {
1006+ [ appId : string ] : {
1007+ loading : boolean ;
1008+ ts : number ;
1009+ } ;
1010+ } ;
1011+
1012+ /** Collection mapping */
1013+ collectionMap ?: {
1014+ [ hash : string ] : {
1015+ a : string ; // app id
1016+ e ?: string ; // event name (for events)
1017+ vs ?: string ; // view segment (for views)
1018+ name : string ; // display name
1019+ } ;
1020+ } ;
1021+
9461022}
1023+
1024+ /** Default export of common module */
1025+ declare const common : Common ;
1026+ export default common ;
0 commit comments