From bf88b4ad1fd6dfb5d2893fa0b4c685ed4bebc55f Mon Sep 17 00:00:00 2001 From: bravo-kernel Date: Sat, 24 May 2025 10:22:22 +0200 Subject: [PATCH] feat: add type WebApplication to Structured Data --- src/structured-data/web-app.test.ts | 63 +++++++++++++++++++++++++++++ src/structured-data/web-app.ts | 16 ++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/structured-data/web-app.test.ts create mode 100644 src/structured-data/web-app.ts diff --git a/src/structured-data/web-app.test.ts b/src/structured-data/web-app.test.ts new file mode 100644 index 0000000..5f0bec3 --- /dev/null +++ b/src/structured-data/web-app.test.ts @@ -0,0 +1,63 @@ +import type { WebApplication } from "schema-dts" +import { webApp } from "./web-app" + +describe("webApp", () => { + it("should return the correct structured data", () => { + const data: WebApplication = { + "@type": "WebApplication", + name: "Example Web App", + url: "https://example.com", + browserRequirements: "Chrome 90+, Firefox 88+", + offers: { + "@type": "Offer", + price: "0.00", + priceCurrency: "USD", + }, + } + + expect(webApp(data)).toEqual({ + "@context": "https://schema.org", + ...data, + }) + }) + + it("should return the correct structured data with an image", () => { + const data: WebApplication = { + "@type": "WebApplication", + name: "Example Web App", + url: "https://example.com", + browserRequirements: "Chrome 90+, Firefox 88+", + offers: { + "@type": "Offer", + price: "0.00", + priceCurrency: "USD", + }, + image: "https://example.com/example-app.jpg", + } + + expect(webApp(data)).toEqual({ + "@context": "https://schema.org", + ...data, + }) + }) + + it("should return the correct structured data with a description", () => { + const data: WebApplication = { + "@type": "WebApplication", + name: "Example Web App", + url: "https://example.com", + browserRequirements: "Chrome 90+, Firefox 88+", + offers: { + "@type": "Offer", + price: "0.00", + priceCurrency: "USD", + }, + description: "An example web application description.", + } + + expect(webApp(data)).toEqual({ + "@context": "https://schema.org", + ...data, + }) + }) +}) diff --git a/src/structured-data/web-app.ts b/src/structured-data/web-app.ts new file mode 100644 index 0000000..8025d9c --- /dev/null +++ b/src/structured-data/web-app.ts @@ -0,0 +1,16 @@ +import type { WebApplication, WithContext } from "schema-dts" + +/** + * This helper is used to generate a WebApplication ld+json structured data object. + * @param webApp WebApplication object + * @see https://schema.org/WebApplication + * @returns Returns a WebApplication object to be used in head via json-ld script tag + */ +export const webApp = (webApp: WebApplication): WithContext => { + return { + "@context": "https://schema.org", + ...webApp, + } +} + +export type { WebApplication }