|
| 1 | +import "server-only"; |
| 2 | + |
| 3 | +import { fetchCredentials } from "@/lib/credential-fetcher"; |
| 4 | +import { type StepInput, withStepLogging } from "@/lib/steps/step-handler"; |
| 5 | +import { getErrorMessage } from "@/lib/utils"; |
| 6 | +import type { BrandfetchCredentials } from "../credentials"; |
| 7 | + |
| 8 | +const BRANDFETCH_API_URL = "https://api.brandfetch.io/v2"; |
| 9 | + |
| 10 | +type BrandfetchLogo = { |
| 11 | + type: string; |
| 12 | + theme: string; |
| 13 | + formats: Array<{ |
| 14 | + src: string; |
| 15 | + format: string; |
| 16 | + }>; |
| 17 | +}; |
| 18 | + |
| 19 | +type BrandfetchColor = { |
| 20 | + hex: string; |
| 21 | + type: string; |
| 22 | + brightness: number; |
| 23 | +}; |
| 24 | + |
| 25 | +type BrandfetchLink = { |
| 26 | + name: string; |
| 27 | + url: string; |
| 28 | +}; |
| 29 | + |
| 30 | +type BrandfetchIndustry = { |
| 31 | + name: string; |
| 32 | + slug: string; |
| 33 | + score: number; |
| 34 | +}; |
| 35 | + |
| 36 | +type BrandfetchResponse = { |
| 37 | + id: string; |
| 38 | + name: string; |
| 39 | + domain: string; |
| 40 | + description?: string; |
| 41 | + longDescription?: string; |
| 42 | + logos?: BrandfetchLogo[]; |
| 43 | + colors?: BrandfetchColor[]; |
| 44 | + links?: BrandfetchLink[]; |
| 45 | + company?: { |
| 46 | + industries?: BrandfetchIndustry[]; |
| 47 | + }; |
| 48 | +}; |
| 49 | + |
| 50 | +type GetBrandResult = |
| 51 | + | { |
| 52 | + success: true; |
| 53 | + name: string; |
| 54 | + domain: string; |
| 55 | + description: string; |
| 56 | + logoUrl: string; |
| 57 | + iconUrl: string; |
| 58 | + colors: string[]; |
| 59 | + links: Array<{ name: string; url: string }>; |
| 60 | + industry: string; |
| 61 | + } |
| 62 | + | { success: false; error: string }; |
| 63 | + |
| 64 | +export type GetBrandCoreInput = { |
| 65 | + identifierType: "domain" | "ticker" | "isin"; |
| 66 | + identifier: string; |
| 67 | +}; |
| 68 | + |
| 69 | +export type GetBrandInput = StepInput & |
| 70 | + GetBrandCoreInput & { |
| 71 | + integrationId?: string; |
| 72 | + }; |
| 73 | + |
| 74 | +function findLogoUrl(logos: BrandfetchLogo[] | undefined, type: string): string { |
| 75 | + if (!logos) return ""; |
| 76 | + |
| 77 | + const logo = logos.find((l) => l.type === type); |
| 78 | + if (!logo || !logo.formats || logo.formats.length === 0) return ""; |
| 79 | + |
| 80 | + // Prefer PNG or SVG |
| 81 | + const pngFormat = logo.formats.find((f) => f.format === "png"); |
| 82 | + if (pngFormat) return pngFormat.src; |
| 83 | + |
| 84 | + const svgFormat = logo.formats.find((f) => f.format === "svg"); |
| 85 | + if (svgFormat) return svgFormat.src; |
| 86 | + |
| 87 | + return logo.formats[0].src; |
| 88 | +} |
| 89 | + |
| 90 | +async function stepHandler( |
| 91 | + input: GetBrandCoreInput, |
| 92 | + credentials: BrandfetchCredentials |
| 93 | +): Promise<GetBrandResult> { |
| 94 | + const apiKey = credentials.BRANDFETCH_API_KEY; |
| 95 | + |
| 96 | + if (!apiKey) { |
| 97 | + return { |
| 98 | + success: false, |
| 99 | + error: |
| 100 | + "BRANDFETCH_API_KEY is not configured. Please add it in Project Integrations.", |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + if (!input.identifier) { |
| 105 | + return { |
| 106 | + success: false, |
| 107 | + error: "Identifier is required", |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + // Validate identifier format based on type |
| 112 | + const identifier = input.identifier.trim(); |
| 113 | + const identifierType = input.identifierType || "domain"; |
| 114 | + |
| 115 | + if (identifierType === "domain") { |
| 116 | + if (!identifier.includes(".")) { |
| 117 | + return { |
| 118 | + success: false, |
| 119 | + error: "Invalid domain format. Expected format: example.com", |
| 120 | + }; |
| 121 | + } |
| 122 | + } else if (identifierType === "ticker") { |
| 123 | + if (!/^[A-Z]{1,5}$/.test(identifier.toUpperCase())) { |
| 124 | + return { |
| 125 | + success: false, |
| 126 | + error: "Invalid ticker format. Expected 1-5 uppercase letters (e.g., NKE)", |
| 127 | + }; |
| 128 | + } |
| 129 | + } else if (identifierType === "isin") { |
| 130 | + if (!/^[A-Z]{2}[A-Z0-9]{10}$/.test(identifier.toUpperCase())) { |
| 131 | + return { |
| 132 | + success: false, |
| 133 | + error: "Invalid ISIN format. Expected 12 characters starting with country code (e.g., US6541061031)", |
| 134 | + }; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + try { |
| 139 | + const response = await fetch( |
| 140 | + `${BRANDFETCH_API_URL}/brands/${encodeURIComponent(identifier)}`, |
| 141 | + { |
| 142 | + method: "GET", |
| 143 | + headers: { |
| 144 | + Authorization: `Bearer ${apiKey}`, |
| 145 | + }, |
| 146 | + } |
| 147 | + ); |
| 148 | + |
| 149 | + if (!response.ok) { |
| 150 | + if (response.status === 404) { |
| 151 | + return { |
| 152 | + success: false, |
| 153 | + error: `Brand not found for: ${input.identifier}`, |
| 154 | + }; |
| 155 | + } |
| 156 | + const errorData = (await response.json().catch(() => ({}))) as { |
| 157 | + message?: string; |
| 158 | + }; |
| 159 | + return { |
| 160 | + success: false, |
| 161 | + error: errorData.message || `HTTP ${response.status}`, |
| 162 | + }; |
| 163 | + } |
| 164 | + |
| 165 | + const brand = (await response.json()) as BrandfetchResponse; |
| 166 | + |
| 167 | + // Extract primary logo and icon URLs |
| 168 | + const logoUrl = findLogoUrl(brand.logos, "logo"); |
| 169 | + const iconUrl = findLogoUrl(brand.logos, "icon") || findLogoUrl(brand.logos, "symbol"); |
| 170 | + |
| 171 | + // Extract colors (just hex values) |
| 172 | + const colors = brand.colors?.map((c) => c.hex) || []; |
| 173 | + |
| 174 | + // Extract links |
| 175 | + const links = brand.links?.map((l) => ({ name: l.name, url: l.url })) || []; |
| 176 | + |
| 177 | + // Get primary industry |
| 178 | + const industry = brand.company?.industries?.[0]?.name || ""; |
| 179 | + |
| 180 | + return { |
| 181 | + success: true, |
| 182 | + name: brand.name || "", |
| 183 | + domain: brand.domain || "", |
| 184 | + description: brand.description || brand.longDescription || "", |
| 185 | + logoUrl, |
| 186 | + iconUrl, |
| 187 | + colors, |
| 188 | + links, |
| 189 | + industry, |
| 190 | + }; |
| 191 | + } catch (error) { |
| 192 | + return { |
| 193 | + success: false, |
| 194 | + error: `Failed to get brand: ${getErrorMessage(error)}`, |
| 195 | + }; |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +export async function getBrandStep( |
| 200 | + input: GetBrandInput |
| 201 | +): Promise<GetBrandResult> { |
| 202 | + "use step"; |
| 203 | + |
| 204 | + const credentials = input.integrationId |
| 205 | + ? await fetchCredentials(input.integrationId) |
| 206 | + : {}; |
| 207 | + |
| 208 | + return withStepLogging(input, () => stepHandler(input, credentials)); |
| 209 | +} |
| 210 | +getBrandStep.maxRetries = 0; |
| 211 | + |
| 212 | +export const _integrationType = "brandfetch"; |
0 commit comments