1
- import rp , { RequestPromiseOptions } from 'request-promise' ;
1
+ import fetch , { RequestInit } from 'node-fetch' ;
2
+ import FormData from 'form-data' ;
2
3
import path from 'path' ;
3
4
import { ReadStream } from 'fs' ;
4
5
@@ -8,32 +9,49 @@ export type AuthObject = {
8
9
deploymentId ?: string
9
10
} ;
10
11
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
+
11
23
export class CubeCloudClient {
12
24
public constructor (
13
25
protected readonly auth ?: AuthObject ,
14
26
protected readonly livePreview ?: boolean
15
27
) {
16
28
}
17
29
18
- private async request ( options : {
30
+ private async request < T > ( options : {
19
31
url : ( deploymentId : string ) => string ,
20
32
auth ?: AuthObject ,
21
- } & RequestPromiseOptions ) {
33
+ } & RequestInit ) : Promise < T > {
22
34
const { url, auth, ...restOptions } = options ;
23
35
24
36
const authorization = auth || this . auth ;
25
37
if ( ! authorization ) {
26
38
throw new Error ( 'Auth isn\'t set' ) ;
27
39
}
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
+ }
28
53
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 > ;
37
55
}
38
56
39
57
public getDeploymentsList ( { auth } : { auth ?: AuthObject } = { } ) {
@@ -45,99 +63,105 @@ export class CubeCloudClient {
45
63
}
46
64
47
65
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 } )
57
72
}
58
- } ) ;
73
+ ) ;
59
74
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' ) ;
62
83
}
63
84
64
85
return res . jwt ;
65
86
}
66
87
67
88
private extendRequestByLivePreview ( ) {
68
- return this . livePreview ? { qs : { live : ' true' } } : { } ;
89
+ return this . livePreview ? '?live= true' : '' ;
69
90
}
70
91
71
- public getUpstreamHashes ( { auth } : { auth ?: AuthObject } = { } ) {
92
+ public getUpstreamHashes ( { auth } : { auth ?: AuthObject } = { } ) : Promise < UpstreamHashesResponse > {
72
93
return this . request ( {
73
- url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /files` ,
94
+ url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /files${ this . extendRequestByLivePreview ( ) } ` ,
74
95
method : 'GET' ,
75
96
auth,
76
- ...this . extendRequestByLivePreview ( )
77
97
} ) ;
78
98
}
79
99
80
- public startUpload ( { auth } : { auth ?: AuthObject } = { } ) {
100
+ public startUpload ( { auth } : { auth ?: AuthObject } = { } ) : Promise < StartUploadResponse > {
81
101
return this . request ( {
82
- url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /start-upload` ,
102
+ url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /start-upload${ this . extendRequestByLivePreview ( ) } ` ,
83
103
method : 'POST' ,
84
104
auth,
85
- ...this . extendRequestByLivePreview ( )
86
105
} ) ;
87
106
}
88
107
89
108
public uploadFile (
90
109
{ transaction, fileName, data, auth } :
91
110
{ transaction : any , fileName : string , data : ReadStream , auth ?: AuthObject }
92
111
) {
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
+
93
127
return this . request ( {
94
- url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /upload-file` ,
128
+ url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /upload-file${ this . extendRequestByLivePreview ( ) } ` ,
95
129
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 ,
106
133
} ,
107
134
auth,
108
- ...this . extendRequestByLivePreview ( )
109
135
} ) ;
110
136
}
111
137
112
138
public finishUpload ( { transaction, files, auth } :
113
139
{ transaction : any , files : any , auth ?: AuthObject } ) {
114
140
return this . request ( {
115
- url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /finish-upload` ,
141
+ url : ( deploymentId : string ) => `build/deploy/${ deploymentId } /finish-upload${ this . extendRequestByLivePreview ( ) } ` ,
116
142
method : 'POST' ,
117
- body : {
118
- transaction,
119
- files
120
- } ,
143
+ body : JSON . stringify ( { transaction, files } ) ,
121
144
auth,
122
- ...this . extendRequestByLivePreview ( )
123
145
} ) ;
124
146
}
125
147
126
148
public setEnvVars ( { envVariables, auth } : { envVariables : any , auth ?: AuthObject } ) {
127
149
return this . request ( {
128
150
url : ( deploymentId ) => `build/deploy/${ deploymentId } /set-env` ,
129
151
method : 'POST' ,
130
- body : {
131
- envVariables : JSON . stringify ( envVariables ) ,
132
- } ,
152
+ body : JSON . stringify ( { envVariables } ) ,
133
153
auth
134
154
} ) ;
135
155
}
136
156
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
+
138
163
return this . request ( {
139
- url : ( deploymentId ) => `devmode/${ deploymentId } /status` ,
140
- qs : { lastHash } ,
164
+ url : ( deploymentId ) => `devmode/${ deploymentId } /status?${ params . toString ( ) } ` ,
141
165
method : 'GET' ,
142
166
auth
143
167
} ) ;
@@ -147,7 +171,7 @@ export class CubeCloudClient {
147
171
return this . request ( {
148
172
url : ( deploymentId ) => `devmode/${ deploymentId } /token` ,
149
173
method : 'POST' ,
150
- body : payload ,
174
+ body : JSON . stringify ( payload ) ,
151
175
auth
152
176
} ) ;
153
177
}
0 commit comments