tldraw/apps/www/pages/api/create.ts
Gwenaël Gallon e0e1373468
Chore: clean up sort imports with prettier (#870)
* Update prettier to latest

* Add format command

* Create .prettierignore

* Add prettier plugin sort imports

* Update prettier config

* Update prettier config

* Update .prettierignore

* Fix @babel/parser conflict

https://github.com/trivago/prettier-plugin-sort-imports/issues/156

* Revert "Update .prettierignore"

This reverts commit 282e5b838376f16b3df7f4c1f99f1106baaffea4.

* Revert change for apps/www/pages/v/[id].tsx

* Sort imports

Moves the third party imports to the top, "~" imports in middle, and "./" at last

* Sorting of the specifiers

in an import declarations

* [www] use path vs  "../"

* [core] use path "~" vs "../"

* [tldraw] use path "~" vs "../.../"

* [tldraw] use path "~" vs "../"

* [tldraw] Cleanup

* Update prettier config

* Last use path "~" vs "../.../"

* [www] Fix order of the third party imports

* Clean prettier config
2022-08-02 14:56:12 +01:00

72 lines
1.9 KiB
TypeScript

import { Utils } from '@tldraw/core'
import { TDDocument } from '@tldraw/tldraw'
import { NextApiRequest, NextApiResponse } from 'next'
type RequestBody = {
pageId: string
document: TDDocument
}
export default async function CreateMultiplayerRoom(req: NextApiRequest, res: NextApiResponse) {
try {
// 1. Get an authentication token from Liveblocks
const { token } = await fetch('https://liveblocks.io/api/authorize', {
headers: {
Authorization: `Bearer ${process.env.LIVEBLOCKS_SECRET_KEY}`,
'Content-Type': 'application/json',
},
}).then((d) => d.json())
// 2. Create the Liveblocks storage JSON
const { pageId, document } = JSON.parse(req.body) as RequestBody
const storageJson = {
liveblocksType: 'LiveObject',
data: {
version: 2.1,
shapes: {
liveblocksType: 'LiveMap',
data: {},
},
bindings: {
liveblocksType: 'LiveMap',
data: {},
},
assets: {
liveblocksType: 'LiveMap',
data: {},
},
},
}
const page = document.pages[pageId]
storageJson.data.shapes.data = page.shapes ?? {}
storageJson.data.bindings.data = page.bindings ?? {}
storageJson.data.assets.data = document.assets ?? {}
// 3. Post the JSON and token to Liveblocks
const roomId = Utils.uniqueId()
const result = await fetch(`https://liveblocks.net/api/v1/room/${roomId}/storage`, {
method: 'POST',
body: JSON.stringify(storageJson),
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
})
if (result.status === 200) {
// If success, send back the url for the new multiplayer project
res.send({ status: 'success', message: result.statusText, url: '/r/' + roomId })
} else {
throw Error(result.statusText)
}
} catch (e) {
res.send({ status: 'error', message: e.message })
}
}