server/utils/response.ts

56 lines
1.3 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 = (
data: object,
status = 200,
headers: Record<string, string> = {}
) => {
2023-09-11 05:31:08 +02:00
return new Response(JSON.stringify(data), {
headers: {
2023-09-13 02:29:13 +02:00
"Content-Type": "application/json",
2023-10-02 02:07:29 +02:00
"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",
...headers,
2023-09-11 05:31:08 +02:00
},
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) => {
return new Response(data, {
headers: {
"Content-Type": "application/xml",
},
status,
});
};
2023-09-13 05:06:47 +02:00
export const jsonLdResponse = (
data: NodeObject | APActivity | APObject,
status = 200
) => {
2023-09-13 01:08:46 +02:00
return new Response(JSON.stringify(data), {
headers: {
"Content-Type": "application/activity+json",
},
status,
});
};
2023-09-12 22:48:10 +02:00
export const errorResponse = (error: string, status = 500) => {
2023-09-13 02:29:13 +02:00
return jsonResponse(
{
error: error,
},
status
);
};