Skip to content

Commit 4daa950

Browse files
authored
Merge pull request #11 from bravo-kernel/json-ld-webapplication
feat: extend structured data with type WebApplication
2 parents 640f1f6 + bf88b4a commit 4daa950

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type { WebApplication } from "schema-dts"
2+
import { webApp } from "./web-app"
3+
4+
describe("webApp", () => {
5+
it("should return the correct structured data", () => {
6+
const data: WebApplication = {
7+
"@type": "WebApplication",
8+
name: "Example Web App",
9+
url: "https://example.com",
10+
browserRequirements: "Chrome 90+, Firefox 88+",
11+
offers: {
12+
"@type": "Offer",
13+
price: "0.00",
14+
priceCurrency: "USD",
15+
},
16+
}
17+
18+
expect(webApp(data)).toEqual({
19+
"@context": "https://schema.org",
20+
...data,
21+
})
22+
})
23+
24+
it("should return the correct structured data with an image", () => {
25+
const data: WebApplication = {
26+
"@type": "WebApplication",
27+
name: "Example Web App",
28+
url: "https://example.com",
29+
browserRequirements: "Chrome 90+, Firefox 88+",
30+
offers: {
31+
"@type": "Offer",
32+
price: "0.00",
33+
priceCurrency: "USD",
34+
},
35+
image: "https://example.com/example-app.jpg",
36+
}
37+
38+
expect(webApp(data)).toEqual({
39+
"@context": "https://schema.org",
40+
...data,
41+
})
42+
})
43+
44+
it("should return the correct structured data with a description", () => {
45+
const data: WebApplication = {
46+
"@type": "WebApplication",
47+
name: "Example Web App",
48+
url: "https://example.com",
49+
browserRequirements: "Chrome 90+, Firefox 88+",
50+
offers: {
51+
"@type": "Offer",
52+
price: "0.00",
53+
priceCurrency: "USD",
54+
},
55+
description: "An example web application description.",
56+
}
57+
58+
expect(webApp(data)).toEqual({
59+
"@context": "https://schema.org",
60+
...data,
61+
})
62+
})
63+
})

src/structured-data/web-app.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { WebApplication, WithContext } from "schema-dts"
2+
3+
/**
4+
* This helper is used to generate a WebApplication ld+json structured data object.
5+
* @param webApp WebApplication object
6+
* @see https://schema.org/WebApplication
7+
* @returns Returns a WebApplication object to be used in head via json-ld script tag
8+
*/
9+
export const webApp = (webApp: WebApplication): WithContext<WebApplication> => {
10+
return {
11+
"@context": "https://schema.org",
12+
...webApp,
13+
}
14+
}
15+
16+
export type { WebApplication }

0 commit comments

Comments
 (0)