mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-26 22:11:20 -07:00
json only
This commit is contained in:
@@ -67,9 +67,8 @@ describe("pages/api/widgets/customapi", () => {
|
|||||||
headers: { "X-Test": "1", Authorization: `Basic ${Buffer.from("u:p").toString("base64")}` },
|
headers: { "X-Test": "1", Authorization: `Basic ${Buffer.from("u:p").toString("base64")}` },
|
||||||
body: '{"foo":"bar"}',
|
body: '{"foo":"bar"}',
|
||||||
});
|
});
|
||||||
expect(res.setHeader).toHaveBeenCalledWith("Content-Type", "application/json");
|
|
||||||
expect(res.statusCode).toBe(200);
|
expect(res.statusCode).toBe(200);
|
||||||
expect(res.body.toString()).toBe('{"a":1}');
|
expect(res.body).toEqual({ a: 1 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("defaults to GET and passes string bodies through", async () => {
|
it("defaults to GET and passes string bodies through", async () => {
|
||||||
|
|||||||
@@ -31,14 +31,16 @@ export default async function handler(req, res) {
|
|||||||
params.body = typeof options.requestBody === "object" ? JSON.stringify(options.requestBody) : options.requestBody;
|
params.body = typeof options.requestBody === "object" ? JSON.stringify(options.requestBody) : options.requestBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [status, contentType, data] = await httpProxy(url, params);
|
const [status, , data] = await httpProxy(url, params);
|
||||||
|
|
||||||
if (status >= 400) {
|
if (status >= 400) {
|
||||||
logger.debug("HTTP Error %d calling %s//%s%s", status, url.protocol, url.host, url.pathname);
|
logger.debug("HTTP Error %d calling %s//%s%s", status, url.protocol, url.host, url.pathname);
|
||||||
return res.status(status).json({ error: { message: "HTTP Error", url: sanitizeErrorURL(url) } });
|
return res.status(status).json({ error: { message: "HTTP Error", url: sanitizeErrorURL(url) } });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contentType) res.setHeader("Content-Type", contentType);
|
try {
|
||||||
|
return res.status(status).json(JSON.parse(Buffer.from(data).toString()));
|
||||||
return res.status(status).send(data);
|
} catch {
|
||||||
|
return res.status(500).json({ error: { message: "Invalid JSON", url: sanitizeErrorURL(url) } });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user