Skip to content

Commit e95d897

Browse files
authored
Merge pull request #146 from contentstack/staging
Staging
2 parents 034debe + 7336f3c commit e95d897

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/app-sdk",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"types": "dist/src/index.d.ts",
55
"description": "The Contentstack App SDK allows you to customize your Contentstack applications.",
66
"main": "dist/index.js",

src/utils/adapter.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
AxiosResponse,
77
} from "axios";
88

9-
import { fetchToAxiosConfig } from "./utils";
9+
import { axiosToFetchResponse, fetchToAxiosConfig } from "./utils";
1010

1111
/**
1212
* Dispatches a request using PostRobot.
@@ -60,15 +60,11 @@ export const dispatchApiRequest = async (
6060
): Promise<Response> => {
6161
try {
6262
const config = fetchToAxiosConfig(url, options);
63-
const response = (await dispatchAdapter(PostRobot)(
63+
const axiosResponse = (await dispatchAdapter(PostRobot)(
6464
config
6565
)) as AxiosResponse;
6666

67-
return new Response(response?.data, {
68-
status: response.status,
69-
statusText: response.statusText,
70-
headers: response.config.headers,
71-
});
67+
return axiosToFetchResponse(axiosResponse);
7268
} catch (err: any) {
7369
if (err.response) {
7470
return new Response(err.response?.data, {

src/utils/utils.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AxiosHeaders, AxiosRequestConfig } from "axios";
1+
import { AxiosHeaders, AxiosRequestConfig, AxiosResponse } from "axios";
22

33
import { Region, RegionType } from "../types";
44

@@ -85,3 +85,33 @@ export const fetchToAxiosConfig = (
8585

8686
return axiosConfig;
8787
};
88+
89+
export function axiosToFetchResponse(axiosRes: AxiosResponse): Response {
90+
const { data, status, statusText, config } = axiosRes;
91+
92+
let body: BodyInit;
93+
let contentType = "application/json";
94+
95+
if (
96+
data instanceof Blob ||
97+
typeof data === "string" ||
98+
data instanceof ArrayBuffer
99+
) {
100+
body = data;
101+
contentType = config.headers["content-type"] || "application/octet-stream";
102+
} else {
103+
body = JSON.stringify(data);
104+
}
105+
106+
if (!config.headers["content-type"]) {
107+
config.headers["content-type"] = contentType;
108+
}
109+
110+
const responseInit: ResponseInit = {
111+
status,
112+
statusText,
113+
headers: config.headers,
114+
};
115+
116+
return new Response(body, responseInit);
117+
}

0 commit comments

Comments
 (0)