#!/usr/bin/env -S node --conditions=bun import { runGeneratorCli } from "@distilled.cloud/dev/codegen/cli"; /** * generate — turn the Smithy JSON models in .generated-specs into the * Datadog Effect SDK. * * Input: .generated-specs/{v1,v2}.json (written by scripts/convert.ts) * Output: src/services/{v1,v2}.ts + src/services/index.ts * * The smithy→SDK compiler and CLI pipeline live in * `@distilled.cloud/dev/codegen`; this script is Datadog's provider spec. * Datadog keeps the wire's snake_case member names on the TS surface, so no * member renaming or wire dictionaries appear here. */ import type { SdkSpec } from "@distilled.cloud/dev/codegen/generator"; const NULLABLE_TRAIT = "com.distilled.openapi#nullable"; const ERROR_MATCHERS_TRAIT = "com.distilled.openapi#errorMatchers"; const RAW_RESPONSE_TRAIT = "com.distilled.openapi#rawResponse"; const SENSITIVE_TRAIT = "smithy.api#sensitive"; /** Datadog's provider spec for the shared smithy→SDK compiler. */ const datadogSpec: SdkSpec = { nullableTrait: NULLABLE_TRAIT, errorMatchersTrait: ERROR_MATCHERS_TRAIT, extraBindings: [ { // Sole member of a synthesized wrapper for bare array/scalar response // bodies; as the response's only member, the response IS the payload. trait: RAW_RESPONSE_TRAIT, binding: "rawResponse", pipe: "T.RawResponse()", rootPipe: "T.RawResponseRoot()", }, ], // Sensitive strings (API keys, tokens): the schema member carries // T.SensitiveValue; the REST protocol delivers Redacted values and // accepts string | Redacted on input. memberTraitPipes: { [SENSITIVE_TRAIT]: "T.SensitiveValue", }, memberTsType: (m) => SENSITIVE_TRAIT in m.traits ? `string | Redacted.Redacted${m.nullable ? " | null" : ""}` : undefined, // Unions surface as TS type unions over an opaque schema — the REST // protocol passes union content through verbatim (wire names ARE the TS // names for Datadog), so no runtime case discrimination is needed. union: ({ name, caseTargets, tsRef }) => [ `export type ${name} = ${caseTargets.map(tsRef).join(" | ") || "unknown"};`, `export const ${name} = S.Unknown as any as S.Schema<${name}>;\n`, ], paginationProfiles: { cursor: { strategy: "paginateCursor", itemsFallback: "data", }, }, operationDecl: { contextType: "DatadogOpContext", commonErrorType: "DatadogOpError", serviceErrors: "DatadogServiceErrors", protocol: "DatadogProtocol", retry: "Retry.Retry", }, sourceNote: ".generated-specs (specs/spec-mirror-datadog)", // Sensitive member types reference Redacted; pull the import in when used. postProcess: (code) => code.includes("Redacted.Redacted<") ? code.replace( `import * as S from "@distilled.cloud/core/schema-lite";\n`, `import * as S from "@distilled.cloud/core/schema-lite";\nimport * as Redacted from "effect/Redacted";\n`, ) : code, }; runGeneratorCli({ description: "Generate the Datadog Effect SDK from the Smithy models", convert: () => import("./convert.ts"), root: `${import.meta.dirname}/..`, // patches/ holds OpenAPI-document patches consumed by scripts/convert.ts; // there is no smithy-model patch chain. patchesDir: false, spec: () => datadogSpec, });