| 58 | |
| 59 | // This updates the fontlist dataset for a given key |
| 60 | const updateList = async ( |
| 61 | key: FontlistQueries, |
| 62 | env: Env, |
| 63 | ctx: ExecutionContext, |
| 64 | ) => { |
| 65 | const response = await fetch(METADATA_URL); |
| 66 | const data = (await response.json()) as FontsourceMetadata; |
| 67 | |
| 68 | // Depending on key, generate a fontlist object with respective values |
| 69 | const list: Fontlist = {}; |
| 70 | |
| 71 | // Rewrite variable object to boolean state |
| 72 | for (const value of Object.values(data)) { |
| 73 | list[value.id] = key === 'variable' ? Boolean(value.variable) : value[key]; |
| 74 | } |
| 75 | |
| 76 | // Store the list in KV |
| 77 | ctx.waitUntil( |
| 78 | env.METADATA.put(METADATA_KEYS.fontlist(key), JSON.stringify(list)), |
| 79 | ); |
| 80 | return list; |
| 81 | }; |
| 82 | |
| 83 | export { updateList, updateMetadata }; |