Open History to see the last commit
1import * as Schema from "effect/Schema";
2import * as SchemaAST from "effect/SchemaAST";
3
4import { TrimmedString } from "./baseSchemas.ts";
5import { SecretReference } from "./secretReference.ts";
6
7/**
8 * A credential setting: the literal, which is kept in the secret store and redacted for
9 * clients, or the secret reference to read it from, which is not itself a secret and is
10 * stored and shown as written. Typing a settings field with it is the whole declaration:
11 * forms offer a secret provider for it and the server reads the secret for its consumer.
12 */
13export const SettingsSecretValue = Schema.Union([TrimmedString, SecretReference]).pipe(
14 Schema.annotateKey({ settingsSecret: true, settingsSecretReference: true }),
15);
16export type SettingsSecretValue = typeof SettingsSecretValue.Type;
17
18export const isSecretReference = Schema.is(SecretReference);
19
20/** The literal a credential setting holds, or `""` when it names a secret instead. */
21export function settingsSecretLiteral(value: SettingsSecretValue | undefined): string {
22 return typeof value === "string" ? value : "";
23}
24
25export function acceptsSecretReference(schema: Schema.Top): boolean {
26 return Schema.resolveAnnotationsKey(schema)?.settingsSecretReference === true;
27}
28
29/** Fields of a settings struct typed `SettingsSecretValue`; any other schema has none. */
30function secretReferenceSettingsFields(schema: Schema.Top): ReadonlyArray<string> {
31 if (!SchemaAST.isObjects(schema.ast)) return [];
32 return schema.ast.propertySignatures.flatMap((property) =>
33 property.type.context?.annotations?.settingsSecretReference === true &&
34 typeof property.name === "string"
35 ? [property.name]
36 : [],
37 );
38}
39
40/** Each declared reference field of `settings` that holds a reference, in field order. */
41export function secretReferencesInSettings(
42 schema: Schema.Top,
43 settings: unknown,
44): ReadonlyArray<{ readonly field: string; readonly reference: SecretReference }> {
45 if (settings === null || typeof settings !== "object") return [];
46 const values = settings as Readonly<Record<string, unknown>>;
47 return secretReferenceSettingsFields(schema).flatMap((field) => {
48 const value = values[field];
49 return isSecretReference(value) ? [{ field, reference: value }] : [];
50 });
51}