Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Factor out handling of response content type handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Tejas Kumar committed Aug 8, 2018
1 parent 77962df commit cdf1bf0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Get.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from "react";
import RestfulReactProvider, { RestfulReactConsumer, RestfulReactProviderProps } from "./Context";
import { processResponse } from "./util/processResponse";

/**
* A function that resolves returned data from
Expand Down Expand Up @@ -171,8 +172,7 @@ class ContextlessGet<TData, TError> extends React.Component<
const request = new Request(`${base}${requestPath || path || ""}`, this.getRequestOptions(thisRequestOptions));
const response = await fetch(request);

const data =
response.headers.get("content-type") === "application/json" ? await response.json() : await response.text();
const data = await processResponse(response);

if (!response.ok) {
this.setState({
Expand Down Expand Up @@ -208,7 +208,7 @@ class ContextlessGet<TData, TError> extends React.Component<
* in order to provide new `base` props that contain
* a segment of the path, creating composable URLs.
*/
function Get<TData = {}, TError = {}>(props: GetProps<TData, TError>) {
function Get<TData = any, TError = any>(props: GetProps<TData, TError>) {
return (
<RestfulReactConsumer>
{contextProps => (
Expand Down
2 changes: 1 addition & 1 deletion src/Mutate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class ContextlessMutate<TData, TError> extends React.Component<MutateProps<TData
* in order to provide new `base` props that contain
* a segment of the path, creating composable URLs.
*/
function Mutate<TError = {}, TData = {}>(props: MutateProps<TData, TError>) {
function Mutate<TError = any, TData = any>(props: MutateProps<TData, TError>) {
return (
<RestfulReactConsumer>
{contextProps => (
Expand Down
6 changes: 3 additions & 3 deletions src/Poll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import equal from "react-fast-compare";

import { RestfulReactConsumer } from "./Context";
import { GetProps, GetState, Meta as GetComponentMeta } from "./Get";
import { processResponse } from "./util/processResponse";

/**
* Meta information returned from the poll.
Expand Down Expand Up @@ -207,8 +208,7 @@ class ContextlessPoll<TData, TError> extends React.Component<
});
const response = await fetch(request);

const responseBody =
response.headers.get("content-type") === "application/json" ? await response.json() : await response.text();
const responseBody = processResponse(response);

if (!this.isResponseOk(response)) {
const error = { message: `${response.status} ${response.statusText}`, data: responseBody };
Expand Down Expand Up @@ -284,7 +284,7 @@ class ContextlessPoll<TData, TError> extends React.Component<
}
}

function Poll<TData = {}, TError = {}>(props: PollProps<TData, TError>) {
function Poll<TData = any, TError = any>(props: PollProps<TData, TError>) {
// Compose Contexts to allow for URL nesting
return (
<RestfulReactConsumer>
Expand Down
7 changes: 7 additions & 0 deletions src/util/processResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const processResponse = (response: Response) => {
if ((response.headers.get("content-type") || "").includes("application/json")) {
return response.json();
} else {
return response.text();
}
};

0 comments on commit cdf1bf0

Please sign in to comment.