Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

client.ts

94 lines3.4 KB

1/**
2 * The client half of a provider package.
3 *
4 * Every provider package exports a `ProviderClientDefinition` from its
5 * `./client` entry point. Clients render settings, labels, and badges from
6 * these definitions plus the server's `ServerProvider` snapshots, so a new
7 * provider needs no per-driver branches in web or mobile.
8 *
9 * Definitions are browser- and React Native-safe: plain data and Effect
10 * schemas, no platform UI imports.
11 *
12 * @module provider-core/client
13 */
14import type { ProviderDriverKind } from "@t3tools/contracts";
15import type * as Schema from "effect/Schema";
16​
17/**
18 * A provider's instance settings schema. Fields carry `providerSettingsForm`
19 * annotations, which the settings form renders generically.
20 */
21export type ProviderSettingsSchema = {
22 readonly fields: Readonly<Record<string, Schema.Top>>;
23} & Schema.Top;
24​
25/** An environment variable the settings UI offers for every instance of a driver. */
26export interface ProviderEnvironmentField {
27 readonly name: string;
28 readonly label: string;
29 readonly description?: string;
30 readonly placeholder?: string;
31 readonly sensitive?: boolean;
33​
34/**
35 * A provider glyph as plain data, so web and mobile can each draw it with
36 * their own SVG renderer. Colors are per theme; a path without a fill uses
37 * the icon's `fill`.
38 */
39export interface ProviderIcon {
40 readonly viewBox: string;
41 readonly fill: { readonly light: string; readonly dark: string };
42 readonly paths: ReadonlyArray<{
43 readonly d: string;
44 readonly fillRule?: "evenodd" | "nonzero";
45 readonly fill?: { readonly light: string; readonly dark: string };
46 }>;
48​
49export interface ProviderClientDefinition {
50 readonly driverKind: ProviderDriverKind;
51 readonly label: string;
52 /** Omitted for drivers whose glyph a client still draws itself. */
53 readonly icon?: ProviderIcon;
54 readonly settingsSchema: ProviderSettingsSchema;
55 readonly environmentFields?: ReadonlyArray<ProviderEnvironmentField>;
56 /** Whether the driver has a default instance backed by legacy `providers.<kind>` settings. */
57 readonly hasDefaultInstance?: boolean;
58 /**
59 * Short warning badge beside every instance of the driver, such as "Beta".
60 * It marks the driver kind, so built-in and custom instances show it alike.
61 */
62 readonly badgeLabel?: string;
64​
65export function defineProviderClient<const Definition extends ProviderClientDefinition>(
66 definition: Definition,
67): Definition {
68 return definition;
70​
71/** The provider definitions a client loaded, in presentation order. */
72export interface ProviderClientRegistry {
73 readonly definitions: ReadonlyArray<ProviderClientDefinition>;
74 /** `undefined` for drivers this client does not ship, such as a fork's driver. */
75 readonly get: (
76 driverKind: ProviderDriverKind | undefined,
77 ) => ProviderClientDefinition | undefined;
79​
80export function makeProviderClientRegistry(
81 definitions: ReadonlyArray<ProviderClientDefinition>,
82): ProviderClientRegistry {
83 const byDriverKind = new Map<ProviderDriverKind, ProviderClientDefinition>();
84 for (const definition of definitions) {
85 if (byDriverKind.has(definition.driverKind)) {
86 throw new Error(`Provider driver '${definition.driverKind}' is defined more than once.`);
87 }
88 byDriverKind.set(definition.driverKind, definition);
89 }
90 return {
91 definitions,
92 get: (driverKind) => (driverKind === undefined ? undefined : byDriverKind.get(driverKind)),
93 };