From 209a86c25c6e590762eab4c8fa6664136b1f1a75 Mon Sep 17 00:00:00 2001 From: rai Date: Sat, 3 Feb 2024 11:11:28 +0900 Subject: [PATCH 1/4] Improve typescript oEmbed type --- src/types.ts | 63 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/src/types.ts b/src/types.ts index 0a8628b..e3ca81e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -24,25 +24,7 @@ export type Metadata = { author?: string; theme_color?: string; canonical_url?: string; - oEmbed?: { - type: "photo" | "video" | "link" | "rich"; - width?: number; - height?: number; - version?: string; - title?: string; - author_name?: string; - author_url?: string; - provider_name?: string; - provider_url?: string; - cache_age?: number; - thumbnails?: [ - { - url?: string; - width?: number; - height?: number; - } - ]; - }; + oEmbed?: OEmbedPhoto | OEmbedVideo | OEmbedLink | OEmbedRich; twitter_card: { card: string; site?: string; @@ -117,3 +99,46 @@ export type Metadata = { }; }; }; + +type OEmbedBase = { + type: "photo" | "video" | "link" | "rich"; + version: string; + title?: string; + author_name?: string; + author_url?: string; + provider_name?: string; + provider_url?: string; + cache_age?: number; + thumbnails?: [ + { + url?: string; + width?: number; + height?: number; + } + ]; +} + +type OEmbedPhoto = OEmbedBase & { + type: "photo"; + url: string; + width: number; + height: number; +} + +type OEmbedVideo = OEmbedBase & { + type: "video"; + html: string; + width: number; + height: number; +} + +type OEmbedLink = OEmbedBase & { + type: "link"; +} + +type OEmbedRich = OEmbedBase & { + type: "rich"; + html: string; + width: number; + height: number; +} From b4457916c8570c61a644034fd88644f32299eacc Mon Sep 17 00:00:00 2001 From: rai Date: Sat, 3 Feb 2024 12:00:34 +0900 Subject: [PATCH 2/4] Update `Metadata` type in README --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8dc4cdd..49b5ec0 100644 --- a/README.md +++ b/README.md @@ -58,21 +58,7 @@ type Metadata = { author?: string theme_color?: string canonical_url?: string - oEmbed?: { - type: 'photo' | 'video' | 'link' | 'rich' - version?: string - title?: string - author_name?: string - author_url?: string - provider_name?: string - provider_url?: string - cache_age?: number - thumbnails?: [{ - url?: string - width?: number - height?: number - }] - } + oEmbed?: OEmbedPhoto | OEmbedVideo | OEmbedLink | OEmbedRich twitter_card: { card: string site?: string @@ -147,6 +133,49 @@ type Metadata = { } } } + +type OEmbedBase = { + type: "photo" | "video" | "link" | "rich" + version: string + title?: string + author_name?: string + author_url?: string + provider_name?: string + provider_url?: string + cache_age?: number + thumbnails?: [ + { + url?: string + width?: number + height?: number + } + ] +} + +type OEmbedPhoto = OEmbedBase & { + type: "photo" + url: string + width: number + height: number +} + +type OEmbedVideo = OEmbedBase & { + type: "video" + html: string + width: number + height: number +} + +type OEmbedLink = OEmbedBase & { + type: "link" +} + +type OEmbedRich = OEmbedBase & { + type: "rich" + html: string + width: number + height: number +} ``` ## The who 💖 From fcb32664db2cd721fb70df498f1313a9ad6067e8 Mon Sep 17 00:00:00 2001 From: jack <8209433+jacktuck@users.noreply.github.com> Date: Mon, 12 Feb 2024 01:02:34 +0000 Subject: [PATCH 3/4] lint --- src/types.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/types.ts b/src/types.ts index e3ca81e..af5db5a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -116,29 +116,29 @@ type OEmbedBase = { height?: number; } ]; -} +}; type OEmbedPhoto = OEmbedBase & { type: "photo"; url: string; width: number; height: number; -} +}; type OEmbedVideo = OEmbedBase & { type: "video"; html: string; width: number; height: number; -} +}; type OEmbedLink = OEmbedBase & { type: "link"; -} +}; type OEmbedRich = OEmbedBase & { type: "rich"; html: string; width: number; height: number; -} +}; From 13b2cc4c7838431e9291e56eb42783968f7faca8 Mon Sep 17 00:00:00 2001 From: rai Date: Tue, 13 Feb 2024 10:05:04 +0900 Subject: [PATCH 4/4] fix: add type guard to oEmbed test --- test/oembed/test.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/test/oembed/test.ts b/test/oembed/test.ts index caf3f76..144f4a5 100644 --- a/test/oembed/test.ts +++ b/test/oembed/test.ts @@ -32,11 +32,15 @@ test("width/height should be numbers", async () => { const result = await unfurl("http://localhost/html/oembed"); - expect(result.oEmbed.width).toEqual(640); - expect(result.oEmbed.height).toEqual(640); + expect(result.oEmbed?.type).toEqual("video"); + const oEmbed = + result.oEmbed?.type === "video" ? result.oEmbed : (result.oEmbed as never); - expect(result.oEmbed.thumbnails[0].width).toEqual(200); - expect(result.oEmbed.thumbnails[0].height).toEqual(200); + expect(oEmbed.width).toEqual(640); + expect(oEmbed.height).toEqual(640); + + expect(oEmbed.thumbnails?.[0].width).toEqual(200); + expect(oEmbed.thumbnails?.[0].height).toEqual(200); }); test("should decode entities in OEmbed URL", async () => { @@ -54,11 +58,15 @@ test("should decode entities in OEmbed URL", async () => { const result = await unfurl("http://localhost/html/oembed"); - expect(result.oEmbed.width).toEqual(640); - expect(result.oEmbed.height).toEqual(640); + expect(result.oEmbed?.type).toEqual("video"); + const oEmbed = + result.oEmbed?.type === "video" ? result.oEmbed : (result.oEmbed as never); + + expect(oEmbed.width).toEqual(640); + expect(oEmbed.height).toEqual(640); - expect(result.oEmbed.thumbnails[0].width).toEqual(200); - expect(result.oEmbed.thumbnails[0].height).toEqual(200); + expect(oEmbed.thumbnails?.[0].width).toEqual(200); + expect(oEmbed.thumbnails?.[0].height).toEqual(200); }); test("should prefer fetching JSON oEmbed", async () => { @@ -118,7 +126,7 @@ test("should upgrade to HTTPS if needed", async () => { const result = await unfurl("http://localhost/html/oembed-http"); - expect(result.oEmbed.version).toEqual("1.0"); + expect(result.oEmbed?.version).toEqual("1.0"); }); test("should build oEmbed from JSON", async () => {