Skip to content

Commit

Permalink
fix: update servers to parse responses better
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Sep 16, 2024
1 parent 70de21a commit bc17169
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/servers/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class BunServer extends Server {
);
} else
resolve(
new Response(JSON.stringify(response.body), {
new Response(response.body ? JSON.stringify(response.body) : null, {
status: response.status || 200,
headers: {
...((response.headers || {}) as Record<string, string>),
Expand Down
2 changes: 1 addition & 1 deletion src/servers/cfworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class CloudflareWorkerServer extends Server {
);
} else
resolve(
new Response(JSON.stringify(response.body), {
new Response(response.body ? JSON.stringify(response.body) : null, {
status: response.status || 200,
headers: {
...((response.headers || {}) as Record<string, string>),
Expand Down
43 changes: 23 additions & 20 deletions src/servers/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,29 @@ export class ExpressServer extends Server {

/** @private */
createEndpoint(path: string, handler: ServerRequestHandler) {
this.app.post(path, (req: any, res: any) =>
handler(
{
headers: req.headers,
body: req.body,
request: req,
response: res
},
async (response) => {
res.status(response.status || 200);
if (response.headers) for (const key in response.headers) res.set(key, response.headers[key]);
if (response.files) {
const data = new MultipartData();
res.set('Content-Type', 'multipart/form-data; boundary=' + data.boundary);
for (const i in response.files) data.attach(`files[${i}]`, response.files[i].file, response.files[i].name);
data.attach('payload_json', JSON.stringify(response.body));
res.send(Buffer.concat(data.finish()));
} else res.send(response.body);
}
)
this.app.post(
path,
(req: any, res: any) =>
void handler(
{
headers: req.headers,
body: req.body,
request: req,
response: res
},
async (response) => {
res.status(response.status || 200);
if (response.headers) for (const key in response.headers) res.set(key, response.headers[key]);
if (response.files) {
const data = new MultipartData();
res.set('Content-Type', 'multipart/form-data; boundary=' + data.boundary);
for (const i in response.files)
data.attach(`files[${i}]`, response.files[i].file, response.files[i].name);
data.attach('payload_json', JSON.stringify(response.body));
res.send(Buffer.concat(data.finish()));
} else res.send(response.body);
}
)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/servers/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class AWSLambdaServer extends Server {
callback(null, {
statusCode: response.status || 200,
headers: responseHeaders,
body: JSON.stringify(response.body)
body: response.body ? JSON.stringify(response.body) : undefined
});
}
}
Expand Down

0 comments on commit bc17169

Please sign in to comment.