Skip to content

Commit 4d77140

Browse files
okineadevdanielroe
andauthored
refactor: use JSON-LD schema types from the schema-dts package (#751)
Co-authored-by: Daniel Roe <daniel@roe.dev>
1 parent 2ea2019 commit 4d77140

File tree

3 files changed

+13
-92
lines changed

3 files changed

+13
-92
lines changed

app/composables/useJsonLd.ts

Lines changed: 4 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,9 @@
1-
// JSON-LD Schema Types
2-
interface JsonLdBase {
3-
'@context': 'https://schema.org'
4-
'@type': string
5-
}
6-
7-
interface WebSiteSchema extends JsonLdBase {
8-
'@type': 'WebSite'
9-
'name': string
10-
'url': string
11-
'description'?: string
12-
'potentialAction'?: SearchActionSchema
13-
}
14-
15-
interface SearchActionSchema {
16-
'@type': 'SearchAction'
17-
'target': {
18-
'@type': 'EntryPoint'
19-
'urlTemplate': string
20-
}
21-
'query-input': string
22-
}
23-
24-
interface SoftwareApplicationSchema extends JsonLdBase {
25-
'@type': 'SoftwareApplication'
26-
'name': string
27-
'description'?: string
28-
'applicationCategory': 'DeveloperApplication'
29-
'operatingSystem': 'Cross-platform'
30-
'url': string
31-
'softwareVersion'?: string
32-
'dateModified'?: string
33-
'datePublished'?: string
34-
'license'?: string
35-
'author'?: PersonSchema | OrganizationSchema | (PersonSchema | OrganizationSchema)[]
36-
'maintainer'?: PersonSchema | OrganizationSchema | (PersonSchema | OrganizationSchema)[]
37-
'offers'?: OfferSchema
38-
'downloadUrl'?: string
39-
'codeRepository'?: string
40-
'keywords'?: string[]
41-
}
42-
43-
interface PersonSchema extends JsonLdBase {
44-
'@type': 'Person'
45-
'name': string
46-
'url'?: string
47-
}
48-
49-
interface OrganizationSchema extends JsonLdBase {
50-
'@type': 'Organization'
51-
'name': string
52-
'url'?: string
53-
'logo'?: string
54-
'description'?: string
55-
'sameAs'?: string[]
56-
}
57-
58-
interface OfferSchema {
59-
'@type': 'Offer'
60-
'price': string
61-
'priceCurrency': string
62-
}
63-
64-
interface BreadcrumbListSchema extends JsonLdBase {
65-
'@type': 'BreadcrumbList'
66-
'itemListElement': BreadcrumbItemSchema[]
67-
}
68-
69-
interface BreadcrumbItemSchema {
70-
'@type': 'ListItem'
71-
'position': number
72-
'name': string
73-
'item'?: string
74-
}
75-
76-
interface ProfilePageSchema extends JsonLdBase {
77-
'@type': 'ProfilePage'
78-
'name': string
79-
'url': string
80-
'mainEntity': PersonSchema | OrganizationSchema
81-
}
82-
83-
type JsonLdSchema =
84-
| WebSiteSchema
85-
| SoftwareApplicationSchema
86-
| PersonSchema
87-
| OrganizationSchema
88-
| BreadcrumbListSchema
89-
| ProfilePageSchema
1+
import type { Thing, WebSite, WithContext } from 'schema-dts'
902

913
/**
924
* Inject JSON-LD script into head
935
*/
94-
export function setJsonLd(schema: JsonLdSchema | JsonLdSchema[]) {
6+
export function setJsonLd(schema: WithContext<Thing> | WithContext<Thing>[]): void {
957
const schemas = Array.isArray(schema) ? schema : [schema]
968

979
useHead({
@@ -109,7 +21,7 @@ export function setJsonLd(schema: JsonLdSchema | JsonLdSchema[]) {
10921
export function createWebSiteSchema(options?: {
11022
name?: string
11123
description?: string
112-
}): WebSiteSchema {
24+
}): WithContext<WebSite> {
11325
const siteUrl = 'https://npmx.dev'
11426
return {
11527
'@context': 'https://schema.org',
@@ -123,7 +35,7 @@ export function createWebSiteSchema(options?: {
12335
'@type': 'EntryPoint',
12436
'urlTemplate': `${siteUrl}/search?q={search_term_string}`,
12537
},
126-
'query-input': 'required name=search_term_string',
38+
'query': 'required name=search_term_string',
12739
},
12840
}
12941
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"knip": "5.82.1",
109109
"lint-staged": "16.2.7",
110110
"playwright-core": "1.58.0",
111+
"schema-dts": "1.1.5",
111112
"simple-git-hooks": "2.13.1",
112113
"typescript": "5.9.3",
113114
"vitest": "npm:@voidzero-dev/vite-plus-test@0.0.0-833c515fa25cef20905a7f9affb156dfa6f151ab",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)