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

Commit

Permalink
Merge pull request #22 from contiamo/fix/content-type
Browse files Browse the repository at this point in the history
Factor out handling of response content type handler
  • Loading branch information
Tejas Kumar authored Aug 8, 2018
2 parents 77962df + 7742532 commit 59b0d47
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
7 changes: 3 additions & 4 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 @@ -170,9 +171,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 +207,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
16 changes: 8 additions & 8 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 @@ -205,22 +206,21 @@ class ContextlessPoll<TData, TError> extends React.Component<
...requestOptions.headers,
},
});
const response = await fetch(request);

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

if (!this.isResponseOk(response)) {
const error = { message: `${response.status} ${response.statusText}`, data: responseBody };
this.setState({ loading: false, lastResponse: response, data: responseBody, error });
const error = { message: `${response.status} ${response.statusText}`, data };
this.setState({ loading: false, lastResponse: response, data, error });
throw new Error(`Failed to Poll: ${error}`);
}

if (this.isModified(response, responseBody)) {
if (this.isModified(response, data)) {
this.setState(() => ({
loading: false,
lastResponse: response,
data: resolve ? resolve(responseBody) : responseBody,
data: resolve ? resolve(data) : data,
lastPollIndex: response.headers.get("x-polling-index") || undefined,
}));
}
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 59b0d47

Please sign in to comment.