#!/usr/bin/env -S node --conditions=bun
/**
* generate — turn the Smithy JSON models in .generated-specs into an Effect
* SDK.
*
* Input: .generated-specs/.json (written by scripts/convert.ts)
* Output: src/services/.ts + src/services/index.ts
*
* Rivet is plain bearer-REST with cursor pagination. The RFC-6902 patches in
* patches//.json apply to the Smithy models via finalizeConvert
* in scripts/convert.ts — never here.
*/
import { runGeneratorCli } from "@distilled.cloud/dev/codegen/cli";
import { type SdkSpec } from "@distilled.cloud/dev/codegen/generator";
import {
ERROR_MATCHERS_TRAIT,
NULLABLE_TRAIT,
RAW_RESPONSE_TRAIT,
} from "@distilled.cloud/dev/codegen/openapi";
const SENSITIVE_TRAIT = "smithy.api#sensitive";
const camel = (slug: string): string =>
slug
.split("_")
.filter(Boolean)
.map((s, i) => (i === 0 ? s : s.charAt(0).toUpperCase() + s.slice(1)))
.join("");
const spec: SdkSpec = {
nullableTrait: NULLABLE_TRAIT,
errorMatchersTrait: ERROR_MATCHERS_TRAIT,
extraBindings: [
{
// Sole member of a synthesized wrapper for bare array/scalar response
// bodies — as a response's sole member, the response IS the payload.
trait: RAW_RESPONSE_TRAIT,
binding: "rawResponse",
pipe: "T.RawResponse()",
rootPipe: "T.RawResponseRoot()",
},
],
// Sensitive strings (the JWT `createAuthToken` answers with): Redacted on
// the way out, `string | Redacted` accepted on the way in.
memberTraitPipes: {
[SENSITIVE_TRAIT]: "T.SensitiveValue",
},
memberTsType: (m) =>
SENSITIVE_TRAIT in m.traits
? `string | Redacted.Redacted${m.nullable ? " | null" : ""}`
: undefined,
// The spec's `oneOf`s (an auth grant's target: `"any"` or `{ id }`) are
// plain value alternatives — the TS type is the case union, the schema
// stays opaque.
union: ({ name, caseTargets, tsRef }) => [
`export type ${name} = ${caseTargets.map(tsRef).join(" | ") || "unknown"};`,
`export const ${name} = /*@__PURE__*/ S.Unknown as any as S.Schema<${name}>;\n`,
],
paginationProfiles: {
cursor: {
strategy: "paginateCursor",
itemsFallback: "",
},
},
sourceNote: ".generated-specs (specs/spec-mirror-rivet/specs/openapi.json)",
operationDecl: {
contextType: "RivetOpContext",
commonErrorType: "RivetOpError",
serviceErrors: "RivetServiceErrors",
protocol: "RivetProtocol",
retry: "Retry.Retry",
},
// `effect/Redacted` is only referenced by modules with sensitive members —
// import it exactly there.
postProcess: (code) =>
code.includes("Redacted.Redacted<")
? code.replace(
`import * as S from "@distilled.cloud/core/schema-lite";`,
`import * as S from "@distilled.cloud/core/schema-lite";\nimport * as Redacted from "effect/Redacted";`,
)
: code,
};
runGeneratorCli({
description: "Generate the Rivet Effect SDK from the Smithy models",
convert: () => import("./convert.ts"),
root: `${import.meta.dirname}/..`,
patchesDir: false,
barrelExportName: camel,
spec: () => spec,
});