11import { RestClient , HttpMethods , RequestOptions } from '../../common.ts' ;
2+ import { JsonParsingTransformer , ReadLineTransformer } from "../../stream-transformers.ts" ;
23
34function join ( ...args : string [ ] ) {
45 return args . join ( '/' ) ;
@@ -22,7 +23,7 @@ function join(...args: string[]) {
2223export class InClusterUnstableRestClient implements RestClient {
2324 readonly baseUrl : string ;
2425 readonly secretsPath : string ;
25- readonly namespace : string ;
26+ readonly defaultNamespace : string ;
2627 readonly #httpClient: Deno . HttpClient ;
2728 readonly #token: string ;
2829
@@ -33,34 +34,43 @@ export class InClusterUnstableRestClient implements RestClient {
3334 this . baseUrl = baseUrl ;
3435 this . secretsPath = secretsPath ;
3536
36- this . namespace = Deno . readTextFileSync ( join ( secretsPath , 'namespace' ) ) ;
37+ this . defaultNamespace = Deno . readTextFileSync ( join ( secretsPath , 'namespace' ) ) ;
3738 this . #httpClient = Deno . createHttpClient ( { caFile : join ( secretsPath , `ca.crt` ) } ) ;
3839 this . #token = Deno . readTextFileSync ( join ( secretsPath , 'token' ) ) ;
3940 }
4041
41- async performRequest ( method : HttpMethods , opts : RequestOptions = { } ) : Promise < any > {
42+ async performRequest ( opts : RequestOptions ) : Promise < any > {
4243 let path = opts . path || '/' ;
4344 if ( opts . querystring ) {
4445 path += `?${ opts . querystring } ` ;
4546 }
46- console . error ( method . toUpperCase ( ) , path ) ;
47+ console . error ( opts . method , path ) ;
4748
4849 const resp = await fetch ( this . baseUrl + path , {
49- method : method ,
50- body : opts . bodyStream ?? JSON . stringify ( opts . body ) ,
50+ method : opts . method ,
51+ body : opts . bodyStream ?? opts . bodyRaw ?? JSON . stringify ( opts . bodyJson ) ,
5152 redirect : 'error' ,
5253 signal : opts . abortSignal ,
5354 headers : {
5455 'Authorization' : `Bearer ${ this . #token} ` ,
55- 'Accept' : opts . accept ?? 'application/octet-stream' ,
56+ 'Accept' : opts . accept ?? ( opts . expectJson ? 'application/json' : 'application/ octet-stream') ,
5657 } ,
5758 client : this . #httpClient,
5859 } ) ;
5960
60- if ( opts . streaming ) {
61- return resp . body ;
62- } else if ( opts . accept === 'application/json' ) {
61+ if ( opts . expectStream ) {
62+ if ( ! resp . body ) return new ReadableStream ( ) ;
63+ if ( opts . expectJson ) {
64+ return resp . body
65+ . pipeThrough ( new ReadLineTransformer ( 'utf-8' ) )
66+ . pipeThrough ( new JsonParsingTransformer ( ) ) ;
67+ } else {
68+ return resp . body ;
69+ }
70+
71+ } else if ( opts . expectJson ) {
6372 return resp . json ( ) ;
73+
6474 } else {
6575 return new Uint8Array ( await resp . arrayBuffer ( ) ) ;
6676 }
0 commit comments