Skip to content

Commit

Permalink
fix: Remove MimeType and change it to string
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Dec 23, 2022
1 parent 0e26ad2 commit 129a17a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
6 changes: 2 additions & 4 deletions src/http/get-content-type.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { MimeType } from '../serializers';

export const getContentType = (headers: Headers): MimeType | void => {
export const getContentType = (headers: Headers): string | void => {
const contentType = headers.get('Content-Type');
if (typeof contentType !== 'string') {
return;
}

return contentType.replace(/\s*;.*$/, '') as MimeType;
return contentType.replace(/\s*;.*$/, '');
};
18 changes: 11 additions & 7 deletions src/http/http-native-impl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MastoConfig } from '../config';
import type { CreateErrorParams } from '../errors';
import { createError, MastoUnexpectedError } from '../errors';
import type { MimeType, Serializer } from '../serializers';
import type { Serializer } from '../serializers';
import { mergeAbortSignals } from '../utils';
import { BaseHttp } from './base-http';
import { getContentType } from './get-content-type';
Expand All @@ -19,12 +19,10 @@ export class HttpNativeImpl extends BaseHttp implements Http {
async request(params: HttpRequestParams): Promise<HttpRequestResult> {
const { path, searchParams, requestInit } = params;

const url = this.config.resolveHttpPath(path);
url.search = this.serializer.serializeQueryString(searchParams);

const url = this.config.resolveHttpPath(path, searchParams);
const headers = this.config.createHeader(requestInit?.headers);
const reqType = headers.get('Content-Type') ?? 'application/json';
const body = this.serializer.serialize(reqType as MimeType, params.body);
const body = this.serializer.serialize(reqType, params.body);

if (body instanceof FormData) {
// As multipart form data should contain an arbitrary boundary,
Expand Down Expand Up @@ -63,14 +61,20 @@ export class HttpNativeImpl extends BaseHttp implements Http {

return {
headers: response.headers,
data: this.serializer.deserialize('application/json', text),
data: this.serializer.deserialize(
response.headers.get('Content-Type') ?? 'application/json',
text,
),
};
} catch (error) {
if (!(error instanceof Response)) {
throw error;
}

const data = await error.json();
const data = this.serializer.deserialize(
error.headers.get('Content-Type') ?? 'application/json',
await error.text(),
);

throw createError({
cause: error,
Expand Down
6 changes: 3 additions & 3 deletions src/serializers/serializer-native-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { camelCase, snakeCase } from 'change-case';
import { MastoDeserializeError } from '../errors';
import { flattenObject } from './form-data';
import { railsQueryString } from './rails-querystring';
import type { MimeType, Serializer } from './serializer';
import type { Serializer } from './serializer';
import { transformKeys } from './transform-keys';

export class SerializerNativeImpl implements Serializer {
serialize(type: MimeType, rawData: unknown): unknown {
serialize(type: string, rawData: unknown): unknown {
if (rawData == undefined) return;

const data = transformKeys(rawData, snakeCase);
Expand All @@ -34,7 +34,7 @@ export class SerializerNativeImpl implements Serializer {
return railsQueryString.stringify(data);
}

deserialize<T = Record<string, unknown>>(type: MimeType, data: string): T {
deserialize<T = Record<string, unknown>>(type: string, data: string): T {
switch (type) {
case 'application/json': {
try {
Expand Down
6 changes: 2 additions & 4 deletions src/serializers/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export type MimeType = 'application/json' | 'multipart/form-data';

export interface Serializer {
serialize(type: MimeType, data: unknown): unknown;
serialize(type: string, data: unknown): unknown;
serializeQueryString(data: unknown): string;
deserialize<T = Record<string, unknown>>(type: MimeType, data: unknown): T;
deserialize<T = Record<string, unknown>>(type: string, data: unknown): T;
}

0 comments on commit 129a17a

Please sign in to comment.