Skip to content

Commit

Permalink
fix: HttpNativeImpl.request()'s return type
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jun 18, 2022
1 parent 281cfcb commit 5536100
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/http/http-native-impl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { headerCase } from 'change-case';

import { MastoConfig } from '../config';
import { createError, CreateErrorParams } from '../errors';
import { MimeType, Serializer } from '../serializers';
import { BaseHttp } from './base-http';
import { Http, Request, Response } from './http';
import { Headers, Http, Request, Response } from './http';

export class HttpNativeImpl extends BaseHttp implements Http {
constructor(readonly config: MastoConfig, readonly serializer: Serializer) {
Expand Down Expand Up @@ -37,10 +39,14 @@ export class HttpNativeImpl extends BaseHttp implements Http {
body: body as string,
});
const text = await response.text();
const contentType =
this.getContentType(response.headers) ?? 'application/json';

return this.serializer.deserialize(contentType, text);
return {
headers: HttpNativeImpl.toHeaders(response.headers),
data: this.serializer.deserialize(
this.getContentType(response.headers) ?? 'application/json',
text,
),
};
} catch (e) {
if (!(e instanceof Response)) {
throw e;
Expand All @@ -59,4 +65,12 @@ export class HttpNativeImpl extends BaseHttp implements Http {
} as CreateErrorParams);
}
}

private static toHeaders(headers: globalThis.Headers): Headers {
const result: Record<string, unknown> = {};
headers.forEach((value, key) => {
result[headerCase(key)] = value;
});
return result;
}
}

0 comments on commit 5536100

Please sign in to comment.