| 13 | import type { AxisRegistry, AxisRegistryDownload } from './types'; |
| 14 | |
| 15 | export const updateVariableList = async (env: Env, ctx: ExecutionContext) => { |
| 16 | const resp = await fetch(VARIABLE_URL); |
| 17 | if (!resp.ok) { |
| 18 | const text = await resp.text(); |
| 19 | throw new StatusError( |
| 20 | resp.status, |
| 21 | `Failed to fetch variable metadata list. ${text}`, |
| 22 | ); |
| 23 | } |
| 24 | const data = (await resp.json()) as Record< |
| 25 | string, |
| 26 | VariableMetadataWithVariants |
| 27 | >; |
| 28 | |
| 29 | const respIcons = await fetch(VARIABLE_ICONS_URL); |
| 30 | if (!respIcons.ok) { |
| 31 | const text = await respIcons.text(); |
| 32 | throw new StatusError( |
| 33 | respIcons.status, |
| 34 | `Failed to fetch variable icons metadata list. ${text}`, |
| 35 | ); |
| 36 | } |
| 37 | const dataIcons = (await respIcons.json()) as Record< |
| 38 | string, |
| 39 | VariableMetadataWithVariants |
| 40 | >; |
| 41 | |
| 42 | const dataMerged = { ...data, ...dataIcons }; |
| 43 | |
| 44 | // Remove variants property from all fonts |
| 45 | const noVariants: Record<string, VariableMetadata> = {}; |
| 46 | for (const [key, value] of Object.entries(dataMerged)) { |
| 47 | const { axes, family } = value; |
| 48 | noVariants[key] = { axes, family }; |
| 49 | } |
| 50 | |
| 51 | // Save entire metadata into KV first |
| 52 | ctx.waitUntil( |
| 53 | env.METADATA.put(METADATA_KEYS.variable_list, JSON.stringify(noVariants)), |
| 54 | ); |
| 55 | |
| 56 | return noVariants; |
| 57 | }; |
| 58 | |
| 59 | export const updateAxisRegistry = async (env: Env, ctx: ExecutionContext) => { |
| 60 | const resp = await fetch(AXIS_REGISTRY_URL); |