1- import { ApiClient , TableDataRequest , BugSplatResponse , TableDataResponse } from '@common' ;
1+ import {
2+ ApiClient ,
3+ TableDataRequest ,
4+ BugSplatResponse ,
5+ TableDataResponse ,
6+ } from '@common' ;
27import { TableDataFormDataBuilder } from '../table-data-form-data-builder/table-data-form-data-builder' ;
38
49export class TableDataClient {
5-
6- constructor ( private _apiClient : ApiClient , private _url : string ) { }
10+ constructor ( private _apiClient : ApiClient , private _url : string ) { }
711
812 // We use POST to get data in most cases because it supports longer queries
9- async postGetData < T , U = ( Record < string , unknown > | undefined ) > ( request : TableDataRequest , formParts : Record < string , string > = { } ) : Promise < BugSplatResponse < TableDataResponse < T , U > > > {
13+ async postGetData < T , U = Record < string , unknown > | undefined > (
14+ request : TableDataRequest ,
15+ formParts : Record < string , string > = { }
16+ ) : Promise < BugSplatResponse < TableDataResponse < T , U > > > {
1017 const factory = ( ) => this . _apiClient . createFormData ( ) ;
1118 const formData = new TableDataFormDataBuilder ( factory , formParts )
1219 . withDatabase ( request . database )
@@ -23,12 +30,14 @@ export class TableDataClient {
2330 cache : 'no-cache' ,
2431 credentials : 'include' ,
2532 redirect : 'follow' ,
26- duplex : 'half'
33+ duplex : 'half' ,
2734 } as RequestInit ;
2835 return this . makeRequest < T , U > ( this . _url , requestInit ) ;
2936 }
3037
31- async getData < T , U = ( Record < string , unknown > | undefined ) > ( request : TableDataRequest ) : Promise < BugSplatResponse < TableDataResponse < T , U > > > {
38+ async getData < T , U = Record < string , unknown > | undefined > (
39+ request : TableDataRequest
40+ ) : Promise < BugSplatResponse < TableDataResponse < T , U > > > {
3241 const factory = ( ) => this . _apiClient . createFormData ( ) ;
3342 const formData = new TableDataFormDataBuilder ( factory )
3443 . withDatabase ( request . database )
@@ -43,17 +52,30 @@ export class TableDataClient {
4352 method : 'GET' ,
4453 cache : 'no-cache' ,
4554 credentials : 'include' ,
46- redirect : 'follow'
55+ redirect : 'follow' ,
4756 } as RequestInit ;
4857 const queryParams = new URLSearchParams ( formData ) . toString ( ) ;
4958 return this . makeRequest < T , U > ( `${ this . _url } ?${ queryParams } ` , requestInit ) ;
5059 }
5160
52- private async makeRequest < T , U = unknown > ( url : string , init : RequestInit ) : Promise < BugSplatResponse < TableDataResponse < T , U > > > {
53- const response = await this . _apiClient . fetch < RawResponse < TableDataResponse < T , U > > > ( url , init ) ;
61+ private async makeRequest < T , U = unknown > (
62+ url : string ,
63+ init : RequestInit
64+ ) : Promise < BugSplatResponse < TableDataResponse < T , U > > > {
65+ const response = await this . _apiClient . fetch <
66+ RawResponse < TableDataResponse < T , U > > | TableDataResponse < T , U >
67+ > ( url , init ) ;
5468 const responseData = await response . json ( ) ;
55- const rows = responseData ? responseData [ 0 ] ?. Rows : [ ] ;
56- const pageData = responseData ? responseData [ 0 ] ?. PageData : { } ;
69+
70+ // Handle legacy and new API responses until we can upgrade legacy APIs
71+ const rows =
72+ ( responseData as TableDataResponse < T , U > ) ?. rows ??
73+ responseData ?. [ 0 ] ?. Rows ??
74+ [ ] ;
75+ const pageData =
76+ ( responseData as TableDataResponse < T , U > ) ?. pageData ??
77+ responseData ?. [ 0 ] ?. PageData ??
78+ { } ;
5779
5880 const status = response . status ;
5981 const body = response . body ;
@@ -64,13 +86,12 @@ export class TableDataClient {
6486 status,
6587 body,
6688 json,
67- text
89+ text,
6890 } ;
6991 }
7092}
7193
7294// https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#key-remapping-via-as
7395export type RawResponse < T > = Array < {
74- [ Property in keyof T as Capitalize < string & Property > ] : T [ Property ]
75- } >
76-
96+ [ Property in keyof T as Capitalize < string & Property > ] : T [ Property ] ;
97+ } > ;
0 commit comments