|
| 1 | +import type { HeadAttributeTypeMap, HeadAdapter, HeadElement } from './types'; |
| 2 | + |
| 3 | +export class HeadBuilder<TOutput = HeadElement[]> { |
| 4 | + private metadataBase?: URL; |
| 5 | + private elements: HeadElement[] = []; |
| 6 | + private adapter?: HeadAdapter<TOutput>; |
| 7 | + |
| 8 | + /** |
| 9 | + * Creates a new HeadBuilder instance with optional metadataBase and adapter configuration |
| 10 | + * |
| 11 | + * The metadataBase serves as the base path and origin for absolute URLs in various |
| 12 | + * metadata fields. When relative URLs (for Open Graph images, alternates, etc.) are used, |
| 13 | + * they are composed with this base. If not provided, relative URLs will be used as-is. |
| 14 | + * |
| 15 | + * The adapter can be injected to automatically transform the output when calling build(). |
| 16 | + * If provided, build() will return the adapted output; otherwise, it returns HeadElement[]. |
| 17 | + * |
| 18 | + * @param options - Configuration options |
| 19 | + * @param options.metadataBase - The base URL to use for resolving relative URLs in metadata |
| 20 | + * @param options.adapter - Optional adapter instance to transform the build output |
| 21 | + * |
| 22 | + * @example |
| 23 | + * // Without adapter - returns HeadElement[] |
| 24 | + * const elements = new HeadBuilder() |
| 25 | + * .addMeta({ name: 'description', content: 'My site' }) |
| 26 | + * .build(); |
| 27 | + * |
| 28 | + * @example |
| 29 | + * // With HTMLAdapter - returns string |
| 30 | + * const html = new HeadBuilder({ adapter: new HTMLAdapter() }) |
| 31 | + * .addMeta({ name: 'description', content: 'My site' }) |
| 32 | + * .build(); |
| 33 | + * |
| 34 | + * @example |
| 35 | + * // With metadataBase and ReactAdapter - returns ReactNode[] |
| 36 | + * const reactNodes = new HeadBuilder({ |
| 37 | + * metadataBase: new URL('https://devsantara.com'), |
| 38 | + * adapter: new ReactAdapter() |
| 39 | + * }) |
| 40 | + * .addMeta({ name: 'description', content: 'My site' }) |
| 41 | + * .build(); |
| 42 | + */ |
| 43 | + constructor(options?: { |
| 44 | + metadataBase?: URL; |
| 45 | + adapter?: HeadAdapter<TOutput>; |
| 46 | + }) { |
| 47 | + this.metadataBase = options?.metadataBase; |
| 48 | + this.adapter = options?.adapter; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Adds a head element to the builder's collection |
| 53 | + * |
| 54 | + * This private method is used internally to add metadata elements (meta, link, script, or style) |
| 55 | + * to the collection that will be used when building the final head configuration. |
| 56 | + * |
| 57 | + * @example |
| 58 | + * this.addElement('meta', { name: 'description', content: 'A description' }) |
| 59 | + * this.addElement('link', { rel: 'canonical', href: 'https://devsantara.com' }) |
| 60 | + */ |
| 61 | + private addElement<T extends keyof HeadAttributeTypeMap>( |
| 62 | + type: T, |
| 63 | + attributes: HeadAttributeTypeMap[T], |
| 64 | + ) { |
| 65 | + this.elements.push({ type, attributes }); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Gets the configured metadataBase URL |
| 71 | + */ |
| 72 | + getMetadataBase(): URL | undefined { |
| 73 | + return this.metadataBase; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Adds a meta element directly to the head configuration |
| 78 | + * |
| 79 | + * This is a general utility method for adding meta elements when a specific |
| 80 | + * helper method doesn't exist. It directly adds the element to the internal collection. |
| 81 | + * |
| 82 | + * @example |
| 83 | + * const head = new HeadBuilder() |
| 84 | + * .addMeta({ name: 'description', content: 'My site description' }) |
| 85 | + * .addMeta({ charSet: 'utf-8' }) |
| 86 | + * .build(); |
| 87 | + */ |
| 88 | + addMeta(attributes: HeadAttributeTypeMap['meta']) { |
| 89 | + return this.addElement('meta', attributes); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Adds a link element directly to the head configuration |
| 94 | + * |
| 95 | + * This is a general utility method for adding link elements when a specific |
| 96 | + * helper method doesn't exist. It directly adds the element to the internal collection. |
| 97 | + * |
| 98 | + * @example |
| 99 | + * const head = new HeadBuilder() |
| 100 | + * .addLink({ rel: 'canonical', href: 'https://devsantara.com' }) |
| 101 | + * .addLink({ rel: 'stylesheet', href: '/styles.css' }) |
| 102 | + * .build(); |
| 103 | + */ |
| 104 | + addLink(attributes: HeadAttributeTypeMap['link']) { |
| 105 | + return this.addElement('link', attributes); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Adds a script element directly to the head configuration |
| 110 | + * |
| 111 | + * This is a general utility method for adding script elements when a specific |
| 112 | + * helper method doesn't exist. It directly adds the element to the internal collection. |
| 113 | + * |
| 114 | + * @example |
| 115 | + * const head = new HeadBuilder() |
| 116 | + * .addScript({ src: '/analytics.js', async: true }) |
| 117 | + * .addScript({children: 'console.log("Hello World");'}); |
| 118 | + * .build(); |
| 119 | + */ |
| 120 | + addScript(attributes: HeadAttributeTypeMap['script']) { |
| 121 | + return this.addElement('script', attributes); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Adds a style element directly to the head configuration |
| 126 | + * |
| 127 | + * This is a general utility method for adding style elements when a specific |
| 128 | + * helper method doesn't exist. It directly adds the element to the internal collection. |
| 129 | + * |
| 130 | + * @example |
| 131 | + * const head = new HeadBuilder() |
| 132 | + * .addStyle({ |
| 133 | + * children: ` |
| 134 | + * .header { background: #333; color: white; padding: 20px; } |
| 135 | + * .hero { min-height: 100vh; display: flex; align-items: center; } |
| 136 | + * ` |
| 137 | + * }) |
| 138 | + * .build(); |
| 139 | + */ |
| 140 | + addStyle(attributes: HeadAttributeTypeMap['style']) { |
| 141 | + return this.addElement('style', attributes); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Builds and returns the head configuration |
| 146 | + * |
| 147 | + * If an adapter was provided in the constructor, returns the adapted output. |
| 148 | + * Otherwise, returns the raw HeadElement[] array. |
| 149 | + */ |
| 150 | + build(): TOutput { |
| 151 | + if (this.adapter) { |
| 152 | + return this.adapter.adapter(this.elements); |
| 153 | + } |
| 154 | + // oxlint-disable-next-line typescript/no-unsafe-type-assertion |
| 155 | + return this.elements as unknown as TOutput; |
| 156 | + } |
| 157 | +} |
0 commit comments