2024-10-08 12:32:47 -04:00
|
|
|
import { UnitLength_type } from '@kittycad/lib/dist/types/src/models'
|
2024-10-10 18:55:31 -04:00
|
|
|
import { CREATE_FILE_URL_PARAM } from './constants'
|
2024-10-08 12:32:47 -04:00
|
|
|
import { stringToBase64 } from './base64'
|
2025-01-08 16:56:48 -05:00
|
|
|
import { ZOO_STUDIO_PROTOCOL } from './link'
|
2024-10-08 14:52:38 -04:00
|
|
|
export interface FileLinkParams {
|
2024-10-10 18:55:31 -04:00
|
|
|
code: string
|
|
|
|
name: string
|
|
|
|
units: UnitLength_type
|
2024-10-08 14:52:38 -04:00
|
|
|
}
|
|
|
|
|
2024-10-08 12:32:47 -04:00
|
|
|
/**
|
|
|
|
* Given a file's code, name, and units, creates shareable link
|
2025-01-08 16:56:48 -05:00
|
|
|
* TODO: update the return type to use TS library after its updated
|
2024-10-08 12:32:47 -04:00
|
|
|
*/
|
2025-01-08 12:00:19 -05:00
|
|
|
export async function createFileLink(
|
|
|
|
token: string,
|
|
|
|
{ code, name, units }: FileLinkParams
|
2025-01-08 16:56:48 -05:00
|
|
|
): Promise<Error | { key: string; url: string }> {
|
2024-10-19 00:12:58 -04:00
|
|
|
// During development, the "handler" needs to first be the web app version,
|
|
|
|
// which exists on localhost:3000 typically.
|
|
|
|
let origin = 'http://localhost:3000'
|
2024-10-17 18:55:20 -04:00
|
|
|
|
2024-10-19 00:12:58 -04:00
|
|
|
let urlFileToShare = new URL(
|
2025-01-08 16:56:48 -05:00
|
|
|
`?${CREATE_FILE_URL_PARAM}&name=${encodeURIComponent(
|
2024-10-08 12:32:47 -04:00
|
|
|
name
|
|
|
|
)}&units=${units}&code=${encodeURIComponent(stringToBase64(code))}`,
|
2025-01-08 12:00:19 -05:00
|
|
|
origin
|
2024-10-17 18:55:20 -04:00
|
|
|
).toString()
|
|
|
|
|
2025-01-08 16:56:48 -05:00
|
|
|
/**
|
|
|
|
* We don't use our `withBaseURL` function here because
|
|
|
|
* there is no URL shortener service in the dev API.
|
|
|
|
*/
|
|
|
|
const response = await fetch('https://api.zoo.dev/user/shortlinks', {
|
2024-10-19 00:12:58 -04:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-type': 'application/json',
|
2025-01-08 12:00:19 -05:00
|
|
|
Authorization: `Bearer ${token}`,
|
2024-10-19 00:12:58 -04:00
|
|
|
},
|
2025-01-08 16:56:48 -05:00
|
|
|
body: JSON.stringify({
|
|
|
|
url: urlFileToShare,
|
|
|
|
// In future we can support org-scoped and password-protected shortlinks here
|
|
|
|
// https://zoo.dev/docs/api/shortlinks/create-a-shortlink-for-a-user?lang=typescript
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
console.log('response', response)
|
|
|
|
if (!response.ok) {
|
|
|
|
const error = await response.json()
|
|
|
|
return new Error(`Failed to create shortlink: ${error.message}`)
|
|
|
|
} else {
|
|
|
|
return response.json()
|
|
|
|
}
|
2024-10-08 12:32:47 -04:00
|
|
|
}
|