Skip to content

Commit dab1dc1

Browse files
committed
Parse V4 response types on Containers API requests
1 parent 3ff30e8 commit dab1dc1

File tree

1 file changed

+41
-7
lines changed
  • packages/wrangler/src/cloudchamber/client/core

1 file changed

+41
-7
lines changed

packages/wrangler/src/cloudchamber/client/core/request.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ import type { ApiRequestOptions } from "./ApiRequestOptions";
99
import type { ApiResult } from "./ApiResult";
1010
import type { OnCancel } from "./CancelablePromise";
1111

12+
interface FetchError {
13+
code: number;
14+
documentation_url?: string;
15+
message: string;
16+
error_chain?: FetchError[];
17+
}
18+
19+
interface FetchResult<ResponseType = unknown> {
20+
success: boolean;
21+
result: ResponseType;
22+
errors: FetchError[];
23+
messages?: string[];
24+
result_info?: unknown;
25+
}
26+
1227
const isDefined = <T>(
1328
value: T | null | undefined
1429
): value is Exclude<T, null | undefined> => {
@@ -197,6 +212,13 @@ const getRequestBody = (options: ApiRequestOptions): any => {
197212
return undefined;
198213
};
199214

215+
const isResponseSchemaV4 = (
216+
config: OpenAPIConfig,
217+
_options: ApiRequestOptions
218+
): boolean => {
219+
return config.BASE.endsWith("/containers");
220+
};
221+
200222
export const sendRequest = async (
201223
config: OpenAPIConfig,
202224
options: ApiRequestOptions,
@@ -319,16 +341,28 @@ export const request = <T>(
319341
options.responseHeader
320342
);
321343

322-
const result: ApiResult = {
323-
url,
324-
ok: response.ok,
325-
status: response.status,
326-
statusText: response.statusText,
327-
body: responseHeader ?? responseBody,
344+
const parseResponseSchemaV4 = (body: any): ApiResult => {
345+
const fetchResult = body as FetchResult<T>;
346+
return {
347+
url,
348+
ok: response.ok && fetchResult.success,
349+
status: response.status,
350+
statusText: response.statusText,
351+
body: responseHeader ?? fetchResult.result,
352+
};
328353
};
329354

330-
catchErrorCodes(options, result);
355+
const result: ApiResult = isResponseSchemaV4(config, options)
356+
? parseResponseSchemaV4(responseBody)
357+
: {
358+
url,
359+
ok: response.ok,
360+
status: response.status,
361+
statusText: response.statusText,
362+
body: responseHeader ?? responseBody,
363+
};
331364

365+
catchErrorCodes(options, result);
332366
resolve(result.body);
333367
}
334368
} catch (error) {

0 commit comments

Comments
 (0)