Files
modeling-app/src/lib/createFileLink.ts

54 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-10-08 12:32:47 -04:00
import { UnitLength_type } from '@kittycad/lib/dist/types/src/models'
import { CREATE_FILE_URL_PARAM } from './constants'
2024-10-08 12:32:47 -04:00
import { stringToBase64 } from './base64'
import { ZOO_STUDIO_PROTOCOL } from './link'
export interface FileLinkParams {
code: string
name: string
units: UnitLength_type
}
2024-10-08 12:32:47 -04:00
/**
* Given a file's code, name, and units, creates shareable link
* TODO: update the return type to use TS library after its updated
2024-10-08 12:32:47 -04:00
*/
export async function createFileLink(
token: string,
{ code, name, units }: FileLinkParams
): Promise<Error | { key: string; url: string }> {
// 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
let urlFileToShare = new URL(
`?${CREATE_FILE_URL_PARAM}&name=${encodeURIComponent(
2024-10-08 12:32:47 -04:00
name
)}&units=${units}&code=${encodeURIComponent(stringToBase64(code))}`,
origin
2024-10-17 18:55:20 -04:00
).toString()
/**
* 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', {
method: 'POST',
headers: {
'Content-type': 'application/json',
Authorization: `Bearer ${token}`,
},
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
}