Skip to content

Commit a102ed8

Browse files
committed
fix:api method body response fix
1 parent 8d0c4e6 commit a102ed8

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-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, sanitizeFetchResponseHeader } 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, sanitizeFetchResponseHeader);
7268
} catch (err: any) {
7369
if (err.response) {
7470
return new Response(err.response?.data, {

src/utils/utils.ts

Lines changed: 53 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,55 @@ export const fetchToAxiosConfig = (
8585

8686
return axiosConfig;
8787
};
88+
89+
export function axiosToFetchResponse(
90+
axiosRes: AxiosResponse,
91+
sanitizeHeadersFn?: (headers: Record<string, string>) => Record<string, string>
92+
): Response {
93+
const { data, status, statusText, headers: rawHeaders } = axiosRes;
94+
95+
let body: BodyInit;
96+
let contentType = 'application/json';
97+
98+
if (data instanceof Blob || typeof data === 'string' || data instanceof ArrayBuffer) {
99+
body = data;
100+
contentType = rawHeaders['content-type'] || 'application/octet-stream';
101+
} else {
102+
body = JSON.stringify(data);
103+
}
104+
105+
const headersObj = sanitizeHeadersFn
106+
? sanitizeHeadersFn(
107+
Object.fromEntries(
108+
Object.entries(rawHeaders).map(([key, value]) => [key, String(value)])
109+
)
110+
)
111+
: Object.fromEntries(
112+
Object.entries(rawHeaders).map(([key, value]) => [key, String(value)])
113+
);
114+
115+
if (!headersObj['content-type']) {
116+
headersObj['content-type'] = contentType;
117+
}
118+
119+
const responseInit: ResponseInit = {
120+
status,
121+
statusText,
122+
headers: new Headers(headersObj),
123+
};
124+
125+
return new Response(body, responseInit);
126+
}
127+
128+
export function sanitizeFetchResponseHeader(headers: Record<string, string>): Record<string, string> {
129+
const blocked = ['authorization', 'cookie', 'set-cookie'];
130+
const sanitized: Record<string, string> = {};
131+
132+
for (const [key, value] of Object.entries(headers)) {
133+
if (!blocked.includes(key.toLowerCase())) {
134+
sanitized[key] = value;
135+
}
136+
}
137+
138+
return sanitized;
139+
}

0 commit comments

Comments
 (0)