MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / get

Function get

pages/carddav.ts:5–73  ·  view source on GitHub ↗
({ request, user, match }: RequestHandlerParams)

Source from the content-addressed store, hash-verified

3import { AppConfig } from '/lib/config.ts';
4
5async function get({ request, user, match }: RequestHandlerParams): Promise<Response> {
6 const contactsConfig = await AppConfig.getContactsConfig();
7
8 if (!contactsConfig.enableCardDavServer) {
9 return new Response('Not Found', { status: 404 });
10 }
11
12 if (!user) {
13 return new Response('Unauthorized', {
14 status: 401,
15 headers: { 'www-authenticate': 'Basic realm="bewCloud", charset="UTF-8"' },
16 });
17 }
18
19 let { path } = match.pathname.groups;
20
21 if (!path) {
22 path = '/';
23 }
24
25 const userId = user.id;
26
27 try {
28 const requestBodyText = await request.clone().text();
29
30 // Remove the `/carddav/` prefix from the hrefs in the request
31 let parsedRequestBodyText: string | undefined = requestBodyText.replaceAll('<href>/carddav/', `<href>/`).replaceAll(
32 ':href>/carddav/',
33 `:href>/`,
34 );
35
36 // The spec doesn't allow a body for GET or HEAD requests (and Deno fails if you try)
37 if (request.method === 'GET' || request.method === 'HEAD') {
38 parsedRequestBodyText = undefined;
39 }
40
41 const originalRequestHeaders = { ...Object.fromEntries(request.headers.entries()) };
42
43 const response = await fetch(`${contactsConfig.cardDavUrl}/${path}`, {
44 headers: {
45 ...originalRequestHeaders,
46 'X-Remote-User': `${userId}`,
47 },
48 method: request.method,
49 body: parsedRequestBodyText,
50 });
51
52 if (response.status === 204) {
53 return new Response(null, { status: 204 });
54 }
55
56 const responseBodyText = await response.clone().text();
57
58 // Add the `/carddav/` prefix to the hrefs in the response
59 const parsedBodyResponseText = responseBodyText.replaceAll('<href>/', `<href>/carddav/`).replaceAll(
60 ':href>/',
61 `:href>/carddav/`,
62 );

Callers

nothing calls this directly

Calls 3

fetchFunction · 0.85
getContactsConfigMethod · 0.80
cloneMethod · 0.80

Tested by

no test coverage detected