Skip to content

Commit

Permalink
fix(api): include variable icons metadata to variable api (#906)
Browse files Browse the repository at this point in the history
* chore: upgrade deps

* fix(api): add variable icons metadata to variable api

* Create popular-windows-try.md
  • Loading branch information
ayuhito committed Dec 10, 2023
1 parent ab71aeb commit 35d51de
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 915 deletions.
8 changes: 8 additions & 0 deletions .changeset/popular-windows-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"cdn": patch
"common-api": patch
"api": patch
"upload": patch
---

fix(api): include variable icons metadata to variable api
6 changes: 3 additions & 3 deletions api/cdn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
},
"devDependencies": {
"@ayuhito/eslint-config": "^0.6.2",
"@cloudflare/workers-types": "^4.20230904.0",
"@cloudflare/workers-types": "^4.20231121.0",
"eslint": "^8.50.0",
"itty-router": "^4.0.22",
"typescript": "^5.2.2",
"typescript": "^5.3.3",
"vitest-environment-miniflare": "^2.14.1",
"wrangler": "^3.7.0"
"wrangler": "^3.19.0"
},
"dependencies": {
"@fontsource-utils/cli": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion api/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"deploy": "echo 'Common Package'"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230914.0",
"@cloudflare/workers-types": "^4.20231121.0",
"itty-router": "^4.0.22"
}
}
6 changes: 3 additions & 3 deletions api/metadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
},
"devDependencies": {
"@ayuhito/eslint-config": "^0.6.2",
"@cloudflare/workers-types": "^4.20230914.0",
"@cloudflare/workers-types": "^4.20231121.0",
"eslint": "^8.50.0",
"itty-router": "^4.0.23",
"typescript": "^5.2.2",
"typescript": "^5.3.3",
"vitest": "^0.34.4",
"vitest-environment-miniflare": "^2.14.1",
"wrangler": "^3.8.0"
"wrangler": "^3.19.0"
},
"dependencies": {
"common-api": "workspace:*"
Expand Down
3 changes: 3 additions & 0 deletions api/metadata/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export const METADATA_URL =
export const VARIABLE_URL =
'https://raw.githubusercontent.com/fontsource/font-files/main/metadata/variable.json';

export const VARIABLE_ICONS_URL =
'https://raw.githubusercontent.com/fontsource/font-files/main/metadata/icons-variable.json';

export const AXIS_REGISTRY_URL =
'https://raw.githubusercontent.com/fontsource/font-files/main/metadata/axis-registry.json';

Expand Down
52 changes: 36 additions & 16 deletions api/metadata/src/variable/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import {
} from 'common-api/types';
import { StatusError } from 'itty-router';

import { AXIS_REGISTRY_URL, KV_TTL, VARIABLE_URL } from '../utils';
import {
AXIS_REGISTRY_URL,
KV_TTL,
VARIABLE_ICONS_URL,
VARIABLE_URL,
} from '../utils';
import { type AxisRegistry, type AxisRegistryDownload } from './types';

export const updateVariableList = async (env: Env, ctx: ExecutionContext) => {
Expand All @@ -13,14 +18,32 @@ export const updateVariableList = async (env: Env, ctx: ExecutionContext) => {
const text = await resp.text();
throw new StatusError(
resp.status,
`Failed to fetch variable metadata. ${text}`,
`Failed to fetch variable metadata list. ${text}`,
);
}
const data = await resp.json<Record<string, VariableMetadataWithVariants>>();
const data = (await resp.json()) as Record<
string,
VariableMetadataWithVariants
>;

const respIcons = await fetch(VARIABLE_ICONS_URL);
if (!respIcons.ok) {
const text = await respIcons.text();
throw new StatusError(
respIcons.status,
`Failed to fetch variable icons metadata list. ${text}`,
);
}
const dataIcons = (await respIcons.json()) as Record<
string,
VariableMetadataWithVariants
>;

const dataMerged = { ...data, ...dataIcons };

// Remove variants property from all fonts
const noVariants: Record<string, VariableMetadata> = {};
for (const [key, value] of Object.entries(data)) {
for (const [key, value] of Object.entries(dataMerged)) {
const { axes, family } = value;
noVariants[key] = { axes, family };
}
Expand All @@ -43,20 +66,17 @@ export const updateVariable = async (
env: Env,
ctx: ExecutionContext,
) => {
const resp = await fetch(VARIABLE_URL);
if (!resp.ok) {
const text = await resp.text();
throw new StatusError(
resp.status,
`Failed to fetch variable item metadata. ${text}`,
);
// Fetch from KV store
let data = await env.VARIABLE_LIST.get<Record<string, VariableMetadata>>(id, {
type: 'json',
});
if (!data) {
data = await updateVariableList(env, ctx);
}
const data = await resp.json<Record<string, VariableMetadataWithVariants>>();
const dataId = data[id];

const dataId = data[id];
if (!dataId) {
// eslint-disable-next-line unicorn/no-null
return null;
throw new StatusError(404, `Variable ${id} not found.`);
}

const noVariants: VariableMetadata = {
Expand Down Expand Up @@ -86,7 +106,7 @@ export const updateAxisRegistry = async (env: Env, ctx: ExecutionContext) => {
`Failed to fetch axis registry metadata. ${text}`,
);
}
const data = await resp.json<AxisRegistryDownload>();
const data = (await resp.json()) as AxisRegistryDownload;

const registry: AxisRegistry = {};
// Remove tag property from all fonts and use it as a key
Expand Down
6 changes: 3 additions & 3 deletions api/upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
},
"devDependencies": {
"@ayuhito/eslint-config": "^0.6.2",
"@cloudflare/workers-types": "^4.20230914.0",
"@cloudflare/workers-types": "^4.20231121.0",
"eslint": "^8.50.0",
"itty-router": "^4.0.23",
"typescript": "^5.0.4",
"typescript": "^5.3.3",
"vitest": "^0.34.4",
"vitest-environment-miniflare": "^2.14.1",
"wrangler": "^3.8.0"
"wrangler": "^3.19.0"
}
}

0 comments on commit 35d51de

Please sign in to comment.