Skip to content

Commit 509cc1c

Browse files
committed
Better type DiffResult
`DiffResponse` is the response of the Bump.sh workspace API, we should not mix it with the doc_name. Hence the new type `DiffResult`, extension of the `DiffResponse` with the optional `doc_name` field. Thus, `DiffResponse` is not exported anymore to the outside (ie the GitHub action). While here, clarify parameter name `result` to `apiResponse` in `waitResult` function, as it can be either a `DiffResponse` or a `VersionResponse`.
1 parent 62ca185 commit 509cc1c

3 files changed

Lines changed: 19 additions & 17 deletions

File tree

src/api/models.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ export interface DiffRequest {
6666
export interface DiffResponse {
6767
breaking?: boolean
6868
details?: DiffItem[]
69-
doc_name?: string
7069
html?: string
7170
id: string
7271
markdown?: string

src/core/diff.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import {BumpApi} from '../api/index.js'
77
import {DiffRequest, DiffResponse, VersionRequest, VersionResponse, WithDiff} from '../api/models.js'
88
import {API} from '../definition.js'
99

10+
export interface DiffResult extends DiffResponse {
11+
doc_name?: string
12+
}
13+
1014
export class Diff {
1115
// 120 seconds = 2 minutes
1216
static readonly TIMEOUT = 120
@@ -108,7 +112,7 @@ export class Diff {
108112
return debug(`bump-cli:core:diff`)(formatter, ...args)
109113
}
110114

111-
extractDiff(versionWithDiff: VersionResponse & WithDiff): DiffResponse {
115+
extractDiff(versionWithDiff: VersionResponse & WithDiff): DiffResult {
112116
// TODO: return a real diff_id in the GET /version API
113117
return {
114118
breaking: versionWithDiff.diff_breaking,
@@ -145,7 +149,7 @@ export class Diff {
145149
expires?: string | undefined,
146150
overlays1?: string[] | undefined,
147151
overlays2?: string[] | undefined,
148-
): Promise<DiffResponse | undefined> {
152+
): Promise<DiffResult | undefined> {
149153
if (!this._config) this._config = await Config.load(resolve(import.meta.dirname, './../../'))
150154

151155
let diffVersion: DiffResponse | VersionResponse | undefined
@@ -183,13 +187,14 @@ export class Diff {
183187
}
184188

185189
async waitResult(
186-
result: DiffResponse | VersionResponse,
190+
apiResponse: DiffResponse | VersionResponse,
187191
token: string | undefined,
188192
opts: {format: string; timeout: number},
189-
): Promise<DiffResponse> {
190-
const pollingResponse = await (this.isVersion(result) && token
191-
? this.bumpClient.getVersion(result.id, token)
192-
: this.bumpClient.getDiff(result.id, opts.format))
193+
): Promise<DiffResult> {
194+
let diffResult: DiffResult = {id: apiResponse.id}
195+
const pollingResponse = await (this.isVersion(apiResponse) && token
196+
? this.bumpClient.getVersion(apiResponse.id, token)
197+
: this.bumpClient.getDiff(apiResponse.id, opts.format))
193198

194199
if (opts.timeout <= 0) {
195200
throw new CLIError(
@@ -199,30 +204,28 @@ export class Diff {
199204

200205
switch (pollingResponse.status) {
201206
case 200: {
202-
let diff: DiffResponse | (VersionResponse & WithDiff) = pollingResponse.data
207+
const diff: DiffResponse | (VersionResponse & WithDiff) = pollingResponse.data
203208

204-
if (this.isVersionWithDiff(diff)) {
205-
diff = this.extractDiff(diff)
206-
}
209+
diffResult = this.isVersionWithDiff(diff) ? this.extractDiff(diff) : diff
207210

208211
this.d('Received diff:')
209-
this.d(diff)
210-
return diff
212+
this.d(diffResult)
213+
return diffResult
211214
break
212215
}
213216

214217
case 202: {
215218
this.d('Waiting 1 sec before next poll')
216219
await this.pollingDelay()
217-
return this.waitResult(result, token, {
220+
return this.waitResult(apiResponse, token, {
218221
format: opts.format,
219222
timeout: opts.timeout - 1,
220223
})
221224
break
222225
}
223226
}
224227

225-
return {} as DiffResponse
228+
return diffResult
226229
}
227230

228231
private async delay(ms: number): Promise<void> {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const COMMANDS = {
1010
preview: Preview,
1111
}
1212

13-
export {DiffResponse, PreviewResponse, VersionResponse, WithDiff} from './api/models.js'
13+
export {PreviewResponse, VersionResponse, WithDiff} from './api/models.js'
1414

1515
export {default as Deploy} from './commands/deploy.js'
1616
export {default as Preview} from './commands/preview.js'

0 commit comments

Comments
 (0)