@@ -12,6 +12,14 @@ import ConfigStore from 'app/stores/configStore';
1212import MissingProjectWarningModal from './missingProjectWarningModal' ;
1313import { COLUMNS , PROMOTED_TAGS , SPECIAL_TAGS , HIDDEN_TAGS } from './data' ;
1414import { isValidAggregation } from './aggregations/utils' ;
15+ import {
16+ AggregationData ,
17+ Column ,
18+ Query ,
19+ Project ,
20+ Organization ,
21+ SnubaResult ,
22+ } from './types' ;
1523
1624const API_LIMIT = 10000 ;
1725
@@ -24,7 +32,7 @@ const DEFAULTS = {
2432 limit : 1000 ,
2533} ;
2634
27- function applyDefaults ( query ) {
35+ function applyDefaults ( query : any ) {
2836 Object . entries ( DEFAULTS ) . forEach ( ( [ key , value ] ) => {
2937 if ( ! ( key in query ) ) {
3038 query [ key ] = value ;
@@ -38,7 +46,7 @@ function applyDefaults(query) {
3846 * It applies sensible defaults if query parameters are not provided on
3947 * initialization.
4048 */
41- export default function createQueryBuilder ( initial = { } , organization ) {
49+ export default function createQueryBuilder ( initial = { } , organization : Organization ) {
4250 const api = new Client ( ) ;
4351 let query = applyDefaults ( initial ) ;
4452
@@ -58,7 +66,7 @@ export default function createQueryBuilder(initial = {}, organization) {
5866 ) ;
5967
6068 const columns = COLUMNS . map ( col => ( { ...col , isTag : false } ) ) ;
61- let tags = [ ] ;
69+ let tags : Column [ ] = [ ] ;
6270
6371 return {
6472 getInternal,
@@ -81,6 +89,10 @@ export default function createQueryBuilder(initial = {}, organization) {
8189 * @returns {Promise<Void> }
8290 */
8391 function load ( ) {
92+ type TagData = {
93+ tags_key : string ;
94+ } ;
95+
8496 return fetch ( {
8597 projects : projectsToFetchTags ,
8698 fields : [ 'tags_key' ] ,
@@ -89,16 +101,16 @@ export default function createQueryBuilder(initial = {}, organization) {
89101 range : '90d' ,
90102 turbo : true ,
91103 } )
92- . then ( res => {
104+ . then ( ( res : SnubaResult ) => {
93105 tags = res . data
94- . filter ( tag => ! HIDDEN_TAGS . includes ( tag . tags_key ) )
95- . map ( tag => {
96- const type = SPECIAL_TAGS [ tags . tags_key ] || 'string' ;
106+ . filter ( ( tag : TagData ) => ! HIDDEN_TAGS . includes ( tag . tags_key ) )
107+ . map ( ( tag : TagData ) => {
108+ const type = SPECIAL_TAGS [ tag . tags_key ] || 'string' ;
97109 return { name : tag . tags_key , type, isTag : true } ;
98110 } ) ;
99111 } )
100- . catch ( err => {
101- tags = PROMOTED_TAGS . map ( tag => {
112+ . catch ( ( ) => {
113+ tags = PROMOTED_TAGS . map ( ( tag : string ) => {
102114 const type = SPECIAL_TAGS [ tag ] || 'string' ;
103115 return { name : tag , type, isTag : true } ;
104116 } ) ;
@@ -160,11 +172,11 @@ export default function createQueryBuilder(initial = {}, organization) {
160172 * @param {* } value Value to update field to
161173 * @returns {Void }
162174 */
163- function updateField ( field , value ) {
175+ function updateField ( field : string , value : any ) {
164176 query [ field ] = value ;
165177
166178 // Ignore non valid aggregations (e.g. user halfway inputting data)
167- const validAggregations = query . aggregations . filter ( agg =>
179+ const validAggregations = query . aggregations . filter ( ( agg : AggregationData ) =>
168180 isValidAggregation ( agg , getColumns ( ) )
169181 ) ;
170182
@@ -173,7 +185,7 @@ export default function createQueryBuilder(initial = {}, organization) {
173185 getColumns ( ) . find ( f => f . name === orderbyField ) !== undefined ;
174186 const hasOrderFieldInSelectedFields = query . fields . includes ( orderbyField ) ;
175187 const hasOrderFieldInAggregations = query . aggregations . some (
176- agg => orderbyField === agg [ 2 ]
188+ ( agg : AggregationData ) => orderbyField === agg [ 2 ]
177189 ) ;
178190
179191 const hasInvalidOrderbyField = validAggregations . length
@@ -224,12 +236,12 @@ export default function createQueryBuilder(initial = {}, organization) {
224236 }
225237
226238 return api
227- . requestPromise ( endpoint , { includeAllArgs : true , method : 'POST' , data} )
239+ . requestPromise ( endpoint , { includeAllArgs : true , method : 'POST' , data} as any )
228240 . then ( ( [ responseData , _ , utils ] ) => {
229241 responseData . pageLinks = utils . getResponseHeader ( 'Link' ) ;
230242 return responseData ;
231243 } )
232- . catch ( err => {
244+ . catch ( ( ) => {
233245 throw new Error ( t ( 'An error occurred' ) ) ;
234246 } ) ;
235247 }
@@ -261,7 +273,7 @@ export default function createQueryBuilder(initial = {}, organization) {
261273 return Promise . reject ( new Error ( 'Start date cannot be after end date' ) ) ;
262274 }
263275
264- return api . requestPromise ( endpoint , { method : 'POST' , data} ) . catch ( ( ) => {
276+ return api . requestPromise ( endpoint , { method : 'POST' , data} as any ) . catch ( ( ) => {
265277 throw new Error ( t ( 'Error with query' ) ) ;
266278 } ) ;
267279 }
@@ -282,7 +294,7 @@ export default function createQueryBuilder(initial = {}, organization) {
282294 * @param {String } Type to fetch - currently either byDay or base
283295 * @returns {Object } Modified query to be run for that type
284296 */
285- function getQueryByType ( originalQuery , type ) {
297+ function getQueryByType ( originalQuery : Query , type : string ) {
286298 if ( type === 'byDayQuery' ) {
287299 return {
288300 ...originalQuery ,
@@ -296,7 +308,9 @@ export default function createQueryBuilder(initial = {}, organization) {
296308 // If id or issue.id is present in query fields, always fetch the project.id
297309 // so we can generate links
298310 if ( type === 'baseQuery' ) {
299- return originalQuery . fields . some ( field => field === 'id' || field === 'issue.id' )
311+ return ( originalQuery . fields || [ ] ) . some (
312+ field => field === 'id' || field === 'issue.id'
313+ )
300314 ? {
301315 ...originalQuery ,
302316 fields : uniq ( [ ...originalQuery . fields , 'project.id' ] ) ,
@@ -323,13 +337,13 @@ export default function createQueryBuilder(initial = {}, organization) {
323337 * @param {Object } [q] optional query to reset to
324338 * @returns {Void }
325339 */
326- function reset ( q = { } ) {
340+ function reset ( q : Query = { } ) {
327341 const [ validProjects , invalidProjects ] = partition ( q . projects || [ ] , project =>
328342 defaultProjectIds . includes ( project )
329343 ) ;
330344
331345 if ( invalidProjects . length ) {
332- openModal ( deps => (
346+ openModal ( ( deps : any ) => (
333347 < MissingProjectWarningModal
334348 organization = { organization }
335349 validProjects = { validProjects }
@@ -345,6 +359,6 @@ export default function createQueryBuilder(initial = {}, organization) {
345359 }
346360}
347361
348- function getProjectIds ( projects ) {
362+ function getProjectIds ( projects : Project [ ] ) {
349363 return projects . map ( project => parseInt ( project . id , 10 ) ) ;
350364}
0 commit comments