File size: 8,332 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
import type {
AlternateLinkDescriptor,
ResolvedAlternateURLs,
} from '../types/alternative-urls-types'
import type {
Metadata,
ResolvedMetadata,
Viewport,
} from '../types/metadata-interface'
import type { ResolvedVerification } from '../types/metadata-types'
import type {
FieldResolver,
AsyncFieldResolverExtraArgs,
MetadataContext,
} from '../types/resolvers'
import { resolveAsArrayOrUndefined } from '../generate/utils'
import { resolveAbsoluteUrlWithPathname } from './resolve-url'
function resolveAlternateUrl(
url: string | URL,
metadataBase: URL | null,
pathname: string,
metadataContext: MetadataContext
) {
// If alter native url is an URL instance,
// we treat it as a URL base and resolve with current pathname
if (url instanceof URL) {
const newUrl = new URL(pathname, url)
url.searchParams.forEach((value, key) =>
newUrl.searchParams.set(key, value)
)
url = newUrl
}
return resolveAbsoluteUrlWithPathname(
url,
metadataBase,
pathname,
metadataContext
)
}
export const resolveThemeColor: FieldResolver<'themeColor', Viewport> = (
themeColor
) => {
if (!themeColor) return null
const themeColorDescriptors: Viewport['themeColor'] = []
resolveAsArrayOrUndefined(themeColor)?.forEach((descriptor) => {
if (typeof descriptor === 'string')
themeColorDescriptors.push({ color: descriptor })
else if (typeof descriptor === 'object')
themeColorDescriptors.push({
color: descriptor.color,
media: descriptor.media,
})
})
return themeColorDescriptors
}
async function resolveUrlValuesOfObject(
obj:
| Record<
string,
string | URL | AlternateLinkDescriptor[] | null | undefined
>
| null
| undefined,
metadataBase: ResolvedMetadata['metadataBase'],
pathname: Promise<string>,
metadataContext: MetadataContext
): Promise<null | Record<string, AlternateLinkDescriptor[]>> {
if (!obj) return null
const result: Record<string, AlternateLinkDescriptor[]> = {}
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'string' || value instanceof URL) {
const pathnameForUrl = await pathname
result[key] = [
{
url: resolveAlternateUrl(
value,
metadataBase,
pathnameForUrl,
metadataContext
),
},
]
} else if (value && value.length) {
result[key] = []
const pathnameForUrl = await pathname
value.forEach((item, index) => {
const url = resolveAlternateUrl(
item.url,
metadataBase,
pathnameForUrl,
metadataContext
)
result[key][index] = {
url,
title: item.title,
}
})
}
}
return result
}
async function resolveCanonicalUrl(
urlOrDescriptor: string | URL | null | AlternateLinkDescriptor | undefined,
metadataBase: URL | null,
pathname: Promise<string>,
metadataContext: MetadataContext
): Promise<null | AlternateLinkDescriptor> {
if (!urlOrDescriptor) return null
const url =
typeof urlOrDescriptor === 'string' || urlOrDescriptor instanceof URL
? urlOrDescriptor
: urlOrDescriptor.url
const pathnameForUrl = await pathname
// Return string url because structureClone can't handle URL instance
return {
url: resolveAlternateUrl(
url,
metadataBase,
pathnameForUrl,
metadataContext
),
}
}
export const resolveAlternates: AsyncFieldResolverExtraArgs<
'alternates',
[ResolvedMetadata['metadataBase'], Promise<string>, MetadataContext]
> = async (alternates, metadataBase, pathname, context) => {
if (!alternates) return null
const canonical = await resolveCanonicalUrl(
alternates.canonical,
metadataBase,
pathname,
context
)
const languages = await resolveUrlValuesOfObject(
alternates.languages,
metadataBase,
pathname,
context
)
const media = await resolveUrlValuesOfObject(
alternates.media,
metadataBase,
pathname,
context
)
const types = await resolveUrlValuesOfObject(
alternates.types,
metadataBase,
pathname,
context
)
const result: ResolvedAlternateURLs = {
canonical,
languages,
media,
types,
}
return result
}
const robotsKeys = [
'noarchive',
'nosnippet',
'noimageindex',
'nocache',
'notranslate',
'indexifembedded',
'nositelinkssearchbox',
'unavailable_after',
'max-video-preview',
'max-image-preview',
'max-snippet',
] as const
const resolveRobotsValue: (robots: Metadata['robots']) => string | null = (
robots
) => {
if (!robots) return null
if (typeof robots === 'string') return robots
const values: string[] = []
if (robots.index) values.push('index')
else if (typeof robots.index === 'boolean') values.push('noindex')
if (robots.follow) values.push('follow')
else if (typeof robots.follow === 'boolean') values.push('nofollow')
for (const key of robotsKeys) {
const value = robots[key]
if (typeof value !== 'undefined' && value !== false) {
values.push(typeof value === 'boolean' ? key : `${key}:${value}`)
}
}
return values.join(', ')
}
export const resolveRobots: FieldResolver<'robots'> = (robots) => {
if (!robots) return null
return {
basic: resolveRobotsValue(robots),
googleBot:
typeof robots !== 'string' ? resolveRobotsValue(robots.googleBot) : null,
}
}
const VerificationKeys = ['google', 'yahoo', 'yandex', 'me', 'other'] as const
export const resolveVerification: FieldResolver<'verification'> = (
verification
) => {
if (!verification) return null
const res: ResolvedVerification = {}
for (const key of VerificationKeys) {
const value = verification[key]
if (value) {
if (key === 'other') {
res.other = {}
for (const otherKey in verification.other) {
const otherValue = resolveAsArrayOrUndefined(
verification.other[otherKey]
)
if (otherValue) res.other[otherKey] = otherValue
}
} else res[key] = resolveAsArrayOrUndefined(value) as (string | number)[]
}
}
return res
}
export const resolveAppleWebApp: FieldResolver<'appleWebApp'> = (appWebApp) => {
if (!appWebApp) return null
if (appWebApp === true) {
return {
capable: true,
}
}
const startupImages = appWebApp.startupImage
? resolveAsArrayOrUndefined(appWebApp.startupImage)?.map((item) =>
typeof item === 'string' ? { url: item } : item
)
: null
return {
capable: 'capable' in appWebApp ? !!appWebApp.capable : true,
title: appWebApp.title || null,
startupImage: startupImages,
statusBarStyle: appWebApp.statusBarStyle || 'default',
}
}
export const resolveAppLinks: FieldResolver<'appLinks'> = (appLinks) => {
if (!appLinks) return null
for (const key in appLinks) {
// @ts-ignore // TODO: type infer
appLinks[key] = resolveAsArrayOrUndefined(appLinks[key])
}
return appLinks as ResolvedMetadata['appLinks']
}
export const resolveItunes: AsyncFieldResolverExtraArgs<
'itunes',
[ResolvedMetadata['metadataBase'], Promise<string>, MetadataContext]
> = async (itunes, metadataBase, pathname, context) => {
if (!itunes) return null
return {
appId: itunes.appId,
appArgument: itunes.appArgument
? resolveAlternateUrl(
itunes.appArgument,
metadataBase,
await pathname,
context
)
: undefined,
}
}
export const resolveFacebook: FieldResolver<'facebook'> = (facebook) => {
if (!facebook) return null
return {
appId: facebook.appId,
admins: resolveAsArrayOrUndefined(facebook.admins),
}
}
export const resolvePagination: AsyncFieldResolverExtraArgs<
'pagination',
[ResolvedMetadata['metadataBase'], Promise<string>, MetadataContext]
> = async (pagination, metadataBase, pathname, context) => {
return {
previous: pagination?.previous
? resolveAlternateUrl(
pagination.previous,
metadataBase,
await pathname,
context
)
: null,
next: pagination?.next
? resolveAlternateUrl(
pagination.next,
metadataBase,
await pathname,
context
)
: null,
}
}
|