Skip to content

Commit

Permalink
Better types
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Mar 19, 2024
1 parent baf22e4 commit c4fb36a
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/rest-client-utils/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export enum LogLevel {
BODY_AND_HEADERS = 3,
}

type Job = {
type: 'job';
id: string;
};

type JobResponse = {
data: Job;
};

type RequestOptions = {
baseUrl: string;
fetchJobResult: (jobId: string) => Promise<JobResult>;
Expand Down Expand Up @@ -260,19 +269,19 @@ export async function request<T>(options: RequestOptions): Promise<T> {
if (logLevel >= LogLevel.BASIC) {
log(`[${requestId}] Status: ${response.status} (${response.statusText})`);
if (logLevel >= LogLevel.BODY_AND_HEADERS) {
[
for (const key of [
'x-api-version',
'x-environment',
'x-queue-time',
'x-ratelimit-remaining',
'x-request-id',
'cf-ray',
].forEach((key) => {
]) {
const value = response.headers.get(key);
if (value) {
log(`[${requestId}] ${key}: ${value}`);
}
});
}
}
}

Expand All @@ -284,7 +293,9 @@ export async function request<T>(options: RequestOptions): Promise<T> {
}

if (response.status === 202) {
const jobResult = await options.fetchJobResult(responseBody.data.id);
const jobResult = await options.fetchJobResult(
(responseBody as JobResponse).data.id,
);

if (jobResult.status < 200 || jobResult.status >= 300) {
throw new ApiError(
Expand All @@ -304,7 +315,7 @@ export async function request<T>(options: RequestOptions): Promise<T> {
}

if (response.status >= 200 && response.status < 300) {
return responseBody;
return responseBody as T;
}

const error = new ApiError(
Expand Down

0 comments on commit c4fb36a

Please sign in to comment.