11import z from "zod" ;
2- import { extractSocialData , getSocialIcon } from "./socials.ts" ;
3- import type { SOCIAL_TYPES as INNER_SOCIAL_TYPES } from "./social-links.ts" ;
2+ import {
3+ createExtractSocialData ,
4+ extractSocialData ,
5+ getSocialIcon ,
6+ } from "./socials.ts" ;
7+ import {
8+ createSocialLinks ,
9+ type CreateSocialLinksConfig ,
10+ type DomainShortcuts ,
11+ type SOCIAL_TYPES as INNER_SOCIAL_TYPES ,
12+ } from "./social-links.ts" ;
413
514export const SocialsSchema = z . union ( [
615 z . string ( ) . url ( ) ,
@@ -14,26 +23,47 @@ export const SocialsSchema = z.union([
1423
1524export type SocialsSchema = z . infer < typeof SocialsSchema > ;
1625
17- export const transformSocial = ( social : SocialsSchema ) => {
18- if ( typeof social == "string" ) {
19- return extractSocialData ( { url : social } ) ;
20- }
21- const { icon, url, platform, username } = social ;
22- const data = extractSocialData ( { url } ) ;
23-
24- return {
25- icon :
26- icon ??
27- ( platform === undefined
28- ? data . icon
29- : getSocialIcon ( platform as INNER_SOCIAL_TYPES ) ) ,
30- url : url ?? data . url ,
31- platform : platform ?? data . platform ,
32- username : username ?? data . username ,
26+ export type SOCIAL_TYPES = INNER_SOCIAL_TYPES ;
27+ export type { DomainShortcuts } ;
28+
29+ export type CreateSocialsConfig = CreateSocialLinksConfig ;
30+
31+ const buildTransformSocial =
32+ ( extractor : ReturnType < typeof createExtractSocialData > ) =>
33+ ( social : SocialsSchema ) => {
34+ if ( typeof social == "string" ) {
35+ return extractor ( { url : social } ) ;
36+ }
37+ const { icon, url, platform, username } = social ;
38+ const data = extractor ( { url } ) ;
39+
40+ return {
41+ icon :
42+ icon ??
43+ ( platform === undefined
44+ ? data . icon
45+ : getSocialIcon ( platform as INNER_SOCIAL_TYPES ) ) ,
46+ url : url ?? data . url ,
47+ platform : platform ?? data . platform ,
48+ username : username ?? data . username ,
49+ } ;
3350 } ;
51+
52+ export const createSocialsTransformer = ( config : CreateSocialsConfig = { } ) => {
53+ const socialLinks = createSocialLinks ( config ) ;
54+ const extractor = createExtractSocialData ( socialLinks ) ;
55+ const transformSocial = buildTransformSocial ( extractor ) ;
56+
57+ const SocialLinks = z
58+ . array ( SocialsSchema )
59+ . default ( [ ] )
60+ . transform ( ( socialUrls ) => socialUrls . map ( transformSocial ) ) ;
61+
62+ return { SocialsSchema, transformSocial, SocialLinks, socialLinks } ;
3463} ;
3564
36- export type SOCIAL_TYPES = INNER_SOCIAL_TYPES ;
65+ // Export a default transformer
66+ export const transformSocial = buildTransformSocial ( extractSocialData ) ;
3767
3868export const SocialLinks = z
3969 . array ( SocialsSchema )
0 commit comments