Tldraw/apps/dotcom-worker/src/lib/routes/getReadonlySlug.ts

31 wiersze
854 B
TypeScript
Czysty Zwykły widok Historia

2024-04-22 12:48:06 +00:00
import { GetReadonlySlugResponseBody } from '@tldraw/dotcom-shared'
import { lns } from '@tldraw/utils'
2024-03-15 12:34:59 +00:00
import { IRequest } from 'itty-router'
import { Environment } from '../types'
2024-03-15 12:34:59 +00:00
// Return a URL to a readonly version of the room
export async function getReadonlySlug(request: IRequest, env: Environment): Promise<Response> {
2024-03-15 12:34:59 +00:00
const roomId = request.params.roomId
2024-04-18 14:55:12 +00:00
if (!roomId) {
return new Response('Bad request', {
status: 400,
})
}
2024-03-15 12:34:59 +00:00
let slug = await env.SLUG_TO_READONLY_SLUG.get(roomId)
2024-04-22 12:48:06 +00:00
let isLegacy = false
if (!slug) {
// For all newly created rooms we add the readonly slug to the KV store.
// If it does not exist there it means we are trying to get a slug for an old room.
slug = lns(roomId)
2024-04-22 12:48:06 +00:00
isLegacy = true
}
2024-03-15 12:34:59 +00:00
return new Response(
JSON.stringify({
slug,
2024-04-22 12:48:06 +00:00
isLegacy,
} satisfies GetReadonlySlugResponseBody)
2024-03-15 12:34:59 +00:00
)
}