( existing: readonly T[], incoming: readonly T[], )
| 128 | }; |
| 129 | |
| 130 | export const mergeAuthTemplates = <T extends { readonly slug: string }>( |
| 131 | existing: readonly T[], |
| 132 | incoming: readonly T[], |
| 133 | ): readonly T[] => { |
| 134 | const result: T[] = existing.map((entry: T) => entry); |
| 135 | const taken = new Set<string>(result.map((entry: T) => String(entry.slug))); |
| 136 | for (const entry of incoming) { |
| 137 | // `slug` may be branded-required in the plugin's schema, but JSON callers |
| 138 | // can submit it empty/blank — read defensively and backfill so every |
| 139 | // stored template has a stable slug. |
| 140 | const rawSlug = (entry as { readonly slug?: unknown }).slug; |
| 141 | const requested = typeof rawSlug === "string" ? rawSlug.trim() : ""; |
| 142 | const existingIndex = result.findIndex((current: T) => String(current.slug) === requested); |
| 143 | if (requested.length > 0 && existingIndex >= 0) { |
| 144 | result[existingIndex] = entry; |
| 145 | continue; |
| 146 | } |
| 147 | const slug = |
| 148 | requested.length > 0 && !taken.has(requested) ? requested : freshCustomAuthSlug(taken); |
| 149 | taken.add(slug); |
| 150 | result.push({ ...entry, slug } as T); |
| 151 | } |
| 152 | return result; |
| 153 | }; |
| 154 | |
| 155 | /** |
| 156 | * A durable change to the integration catalog, emitted through |
no test coverage detected