1- import rp , { RequestPromiseOptions } from 'request-promise' ;
1+ import fetch , { RequestInit } from 'node-fetch' ;
2+ import FormData from 'form-data' ;
23import path from 'path' ;
34import { ReadStream } from 'fs' ;
45
@@ -8,32 +9,49 @@ export type AuthObject = {
89 deploymentId ?: string
910} ;
1011
12+ export interface StartUploadResponse {
13+ transaction : string ;
14+ deploymentName : string ;
15+ }
16+
17+ export interface UpstreamHashesResponse {
18+ [ key : string ] : {
19+ hash : string ;
20+ } ;
21+ }
22+
1123export class CubeCloudClient {
1224 public constructor (
1325 protected readonly auth ?: AuthObject ,
1426 protected readonly livePreview ?: boolean
1527 ) {
1628 }
1729
18- private async request ( options : {
30+ private async request < T > ( options : {
1931 url : ( deploymentId : string ) => string ,
2032 auth ?: AuthObject ,
21- } & RequestPromiseOptions ) {
33+ } & RequestInit ) : Promise < T > {
2234 const { url, auth, ...restOptions } = options ;
2335
2436 const authorization = auth || this . auth ;
2537 if ( ! authorization ) {
2638 throw new Error ( 'Auth isn\'t set' ) ;
2739 }
40+ // Ensure headers object exists in restOptions
41+ restOptions . headers = restOptions . headers || { } ;
42+ // Add authorization to headers
43+ ( restOptions . headers as any ) . authorization = authorization . auth ;
44+
45+ const response = await fetch (
46+ `${ authorization . url } /${ url ( authorization . deploymentId || '' ) } ` ,
47+ restOptions ,
48+ ) ;
49+
50+ if ( ! response . ok ) {
51+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
52+ }
2853
29- return rp ( {
30- headers : {
31- authorization : authorization . auth
32- } ,
33- ...restOptions ,
34- url : `${ authorization . url } /${ url ( authorization . deploymentId || '' ) } ` ,
35- json : true
36- } ) ;
54+ return await response . json ( ) as Promise < T > ;
3755 }
3856
3957 public getDeploymentsList ( { auth } : { auth ?: AuthObject } = { } ) {
@@ -45,99 +63,105 @@ export class CubeCloudClient {
4563 }
4664
4765 public async getDeploymentToken ( authToken : string ) {
48- const res = await rp ( {
49- url : `${ process . env . CUBE_CLOUD_HOST || 'https://cubecloud.dev' } /v1/token` ,
50- method : 'POST' ,
51- headers : {
52- 'Content-type' : 'application/json'
53- } ,
54- json : true ,
55- body : {
56- token : authToken
66+ const response = await fetch (
67+ `${ process . env . CUBE_CLOUD_HOST || 'https://cubecloud.dev' } /v1/token` ,
68+ {
69+ method : 'POST' ,
70+ headers : { 'Content-type' : 'application/json' } ,
71+ body : JSON . stringify ( { token : authToken } )
5772 }
58- } ) ;
73+ ) ;
5974
60- if ( res && res . error ) {
61- throw res . error ;
75+ if ( ! response . ok ) {
76+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
77+ }
78+
79+ const res = await response . json ( ) as any ;
80+
81+ if ( ! res . jwt ) {
82+ throw new Error ( 'JWT token is not present in the response' ) ;
6283 }
6384
6485 return res . jwt ;
6586 }
6687
6788 private extendRequestByLivePreview ( ) {
68- return this . livePreview ? { qs : { live : ' true' } } : { } ;
89+ return this . livePreview ? '?live= true' : '' ;
6990 }
7091
71- public getUpstreamHashes ( { auth } : { auth ?: AuthObject } = { } ) {
92+ public getUpstreamHashes ( { auth } : { auth ?: AuthObject } = { } ) : Promise < UpstreamHashesResponse > {
7293 return this . request ( {
73- url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /files` ,
94+ url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /files${ this . extendRequestByLivePreview ( ) } ` ,
7495 method : 'GET' ,
7596 auth,
76- ...this . extendRequestByLivePreview ( )
7797 } ) ;
7898 }
7999
80- public startUpload ( { auth } : { auth ?: AuthObject } = { } ) {
100+ public startUpload ( { auth } : { auth ?: AuthObject } = { } ) : Promise < StartUploadResponse > {
81101 return this . request ( {
82- url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /start-upload` ,
102+ url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /start-upload${ this . extendRequestByLivePreview ( ) } ` ,
83103 method : 'POST' ,
84104 auth,
85- ...this . extendRequestByLivePreview ( )
86105 } ) ;
87106 }
88107
89108 public uploadFile (
90109 { transaction, fileName, data, auth } :
91110 { transaction : any , fileName : string , data : ReadStream , auth ?: AuthObject }
92111 ) {
112+ const formData = new FormData ( ) ;
113+ formData . append ( 'transaction' , JSON . stringify ( transaction ) ) ;
114+ formData . append ( 'fileName' , fileName ) ;
115+ formData . append ( 'file' , {
116+ value : data ,
117+ options : {
118+ filename : path . basename ( fileName ) ,
119+ contentType : 'application/octet-stream'
120+ }
121+ } ) ;
122+
123+ // Get the form data buffer and headers
124+ const formDataBuffer = formData . getBuffer ( ) ;
125+ const formDataHeaders = formData . getHeaders ( ) ;
126+
93127 return this . request ( {
94- url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /upload-file` ,
128+ url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /upload-file${ this . extendRequestByLivePreview ( ) } ` ,
95129 method : 'POST' ,
96- formData : {
97- transaction : JSON . stringify ( transaction ) ,
98- fileName,
99- file : {
100- value : data ,
101- options : {
102- filename : path . basename ( fileName ) ,
103- contentType : 'application/octet-stream'
104- }
105- }
130+ body : formDataBuffer ,
131+ headers : {
132+ ...formDataHeaders ,
106133 } ,
107134 auth,
108- ...this . extendRequestByLivePreview ( )
109135 } ) ;
110136 }
111137
112138 public finishUpload ( { transaction, files, auth } :
113139 { transaction : any , files : any , auth ?: AuthObject } ) {
114140 return this . request ( {
115- url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /finish-upload` ,
141+ url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /finish-upload${ this . extendRequestByLivePreview ( ) } ` ,
116142 method : 'POST' ,
117- body : {
118- transaction,
119- files
120- } ,
143+ body : JSON . stringify ( { transaction, files } ) ,
121144 auth,
122- ...this . extendRequestByLivePreview ( )
123145 } ) ;
124146 }
125147
126148 public setEnvVars ( { envVariables, auth } : { envVariables : any , auth ?: AuthObject } ) {
127149 return this . request ( {
128150 url : ( deploymentId ) => `build/deploy/${ deploymentId } /set-env` ,
129151 method : 'POST' ,
130- body : {
131- envVariables : JSON . stringify ( envVariables ) ,
132- } ,
152+ body : JSON . stringify ( { envVariables } ) ,
133153 auth
134154 } ) ;
135155 }
136156
137- public getStatusDevMode ( { auth, lastHash } : { auth ?: AuthObject , lastHash ?: string } = { } ) {
157+ public getStatusDevMode ( { auth, lastHash } : { auth ?: AuthObject , lastHash ?: string } = { } ) : Promise < { [ key : string ] : any } > {
158+ const params = new URLSearchParams ( ) ;
159+ if ( lastHash ) {
160+ params . append ( 'lastHash' , lastHash ) ;
161+ }
162+
138163 return this . request ( {
139- url : ( deploymentId ) => `devmode/${ deploymentId } /status` ,
140- qs : { lastHash } ,
164+ url : ( deploymentId ) => `devmode/${ deploymentId } /status?${ params . toString ( ) } ` ,
141165 method : 'GET' ,
142166 auth
143167 } ) ;
@@ -147,7 +171,7 @@ export class CubeCloudClient {
147171 return this . request ( {
148172 url : ( deploymentId ) => `devmode/${ deploymentId } /token` ,
149173 method : 'POST' ,
150- body : payload ,
174+ body : JSON . stringify ( payload ) ,
151175 auth
152176 } ) ;
153177 }
0 commit comments