server/utils/response.ts

59 lines
1.8 KiB
TypeScript
Raw Normal View History

import type { APActivity, APObject } from "activitypub-types";
import type { NodeObject } from "jsonld";
2023-09-13 01:08:46 +02:00
2023-10-02 02:07:29 +02:00
export const jsonResponse = (
2024-04-07 07:30:49 +02:00
data: object,
status = 200,
headers: Record<string, string> = {},
2023-10-02 02:07:29 +02:00
) => {
2024-04-07 07:30:49 +02:00
return new Response(JSON.stringify(data), {
headers: {
"Content-Type": "application/json",
"X-Frame-Options": "DENY",
"X-Permitted-Cross-Domain-Policies": "none",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers":
"Authorization,Content-Type,Idempotency-Key",
"Access-Control-Allow-Methods": "POST,PUT,DELETE,GET,PATCH,OPTIONS",
"Access-Control-Allow-Origin": "*",
"Access-Control-Expose-Headers":
"Link,X-RateLimit-Reset,X-RateLimit-Limit,X-RateLimit-Remaining,X-Request-Id,Idempotency-Key",
2024-04-07 13:51:41 +02:00
// CSP should follow Content Security Policy directive: "connect-src 'self' blob: https: wss:".
"Content-Security-Policy":
"default-src 'self'; connect-src 'self' blob: https: wss:; frame-ancestors 'none';",
2024-04-07 07:30:49 +02:00
...headers,
},
status,
});
2023-09-13 02:29:13 +02:00
};
2023-09-12 22:48:10 +02:00
2023-09-13 07:33:08 +02:00
export const xmlResponse = (data: string, status = 200) => {
2024-04-07 07:30:49 +02:00
return new Response(data, {
headers: {
"Content-Type": "application/xml",
},
status,
});
2023-09-13 07:33:08 +02:00
};
2023-09-13 05:06:47 +02:00
export const jsonLdResponse = (
2024-04-07 07:30:49 +02:00
data: NodeObject | APActivity | APObject,
status = 200,
2023-09-13 05:06:47 +02:00
) => {
2024-04-07 07:30:49 +02:00
return new Response(JSON.stringify(data), {
headers: {
"Content-Type": "application/activity+json",
},
status,
});
2023-09-13 01:08:46 +02:00
};
2023-09-12 22:48:10 +02:00
export const errorResponse = (error: string, status = 500) => {
2024-04-07 07:30:49 +02:00
return jsonResponse(
{
error: error,
},
status,
);
2023-09-13 02:29:13 +02:00
};