( raw: string )
| 33 | * OPENAI_DEFAULT_HEADERS='{"HTTP-Referer":"https://example.com","X-Title":"My App"}' |
| 34 | */ |
| 35 | export const parseDefaultHeaders = ( |
| 36 | raw: string |
| 37 | ): Record<string, string> | undefined => { |
| 38 | if (raw === "") { |
| 39 | return undefined; |
| 40 | } |
| 41 | |
| 42 | let parsed: unknown; |
| 43 | |
| 44 | try { |
| 45 | parsed = JSON.parse(raw); |
| 46 | } catch { |
| 47 | warnInvalid("is not valid JSON"); |
| 48 | |
| 49 | return undefined; |
| 50 | } |
| 51 | |
| 52 | if (!isStringRecord(parsed)) { |
| 53 | warnInvalid("is not a JSON object"); |
| 54 | |
| 55 | return undefined; |
| 56 | } |
| 57 | |
| 58 | const entries = collectStringEntries(parsed); |
| 59 | |
| 60 | return Object.keys(entries).length > 0 ? entries : undefined; |
| 61 | }; |
no test coverage detected