Skip to content

Commit

Permalink
fix(enterprise): fix api response shape
Browse files Browse the repository at this point in the history
This is a regression introduced in a previous commit but was never
released.
  • Loading branch information
eysi09 committed Mar 17, 2021
1 parent ace99fc commit 8e0ac89
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
13 changes: 7 additions & 6 deletions core/src/enterprise/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { IncomingHttpHeaders } from "http"

import { got, GotHeaders, GotHttpError } from "../util/http"
import { findProjectConfig } from "../config/base"
import { CommandError, EnterpriseApiError, RuntimeError } from "../exceptions"
Expand Down Expand Up @@ -42,9 +44,8 @@ export interface AuthTokenResponse {
tokenValidity: number
}

export interface ApiResponse<T> {
status: string
data: T
export type ApiFetchResponse<T> = T & {
headers: IncomingHttpHeaders
}

/**
Expand Down Expand Up @@ -293,7 +294,7 @@ export class EnterpriseApi {
}
}

private async apiFetch<T>(path: string, params: ApiFetchParams) {
private async apiFetch<T>(path: string, params: ApiFetchParams): Promise<ApiFetchResponse<T>> {
const { method, headers } = params
this.log.silly({ msg: `Calling enterprise API with ${method} ${path}` })
const token = await EnterpriseApi.getAuthToken(this.log)
Expand All @@ -307,7 +308,7 @@ export class EnterpriseApi {
json: params.body,
}

const res = await got<ApiResponse<T>>(`${this.domain}/${this.apiPrefix}/${path}`, {
const res = await got<T>(`${this.domain}/${this.apiPrefix}/${path}`, {
...requestObj,
responseType: "json",
})
Expand All @@ -320,7 +321,7 @@ export class EnterpriseApi {
}

return {
data: res.body.data,
...res.body,
headers: res.headers,
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/enterprise/get-secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function getSecrets({ log, environmentName, enterpriseApi }: GetSec
let secrets: StringMap = {}

try {
const res = await enterpriseApi.get<StringMap>(
const res = await enterpriseApi.get<{ status: string; data: StringMap }>(
`/secrets/projectUid/${enterpriseApi.projectId}/env/${environmentName}`
)
secrets = res.data
Expand Down
20 changes: 9 additions & 11 deletions core/src/enterprise/workflow-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { LogEntry } from "../logger/log-entry"
import { EnterpriseApiError } from "../exceptions"
import { gardenEnv } from "../constants"
import { Garden } from "../garden"
import { ApiFetchResponse } from "./api"

export interface RegisterWorkflowRunParams {
workflowConfig: WorkflowConfig
Expand Down Expand Up @@ -41,25 +42,22 @@ export async function registerWorkflowRun({
requestData["workflowRunUid"] = gardenEnv.GARDEN_WORKFLOW_RUN_UID
}
if (enterpriseApi) {
let res
// TODO: Use API types package here.
let res: ApiFetchResponse<{ workflowRunUid: string; status: string }>
try {
res = await enterpriseApi.post("workflow-runs", { body: requestData })
} catch (err) {
log.error(`An error occurred while registering workflow run: ${err.message}`)
throw err
}
const body = res.body

if (body && body["workflowRunUid"] && body["status"] === "success") {
return body["workflowRunUid"]
if (res?.workflowRunUid && res?.status === "success") {
return res.workflowRunUid
} else {
throw new EnterpriseApiError(
`Error while registering workflow run: Request failed with status ${body["status"]}`,
{
status: body["status"],
workflowRunUid: body["workflowRunUid"],
}
)
throw new EnterpriseApiError(`Error while registering workflow run: Request failed with status ${res?.status}`, {
status: res?.status,
workflowRunUid: res?.workflowRunUid,
})
}
}
throw new EnterpriseApiError("Error while registering workflow run: Couldn't initialize API.", {})
Expand Down

0 comments on commit 8e0ac89

Please sign in to comment.