#!/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 Notion * Effect SDK. * * Input: .generated-specs/.json (one model per Notion API tag, * written by scripts/convert.ts) * Output: src/services/.ts + src/services/index.ts * * The smithy→SDK compiler and CLI pipeline live in * `@distilled.cloud/dev/codegen`; this script is Notion's provider spec: * the `com.distilled.openapi` trait vocabulary (nullable members, bare-body * responses), a passthrough union style, the cursor pagination profile, and * the protocol/retry/error names. * * Notion keeps its wire member names verbatim (snake_case — `start_cursor`, * `has_more`, `parent`) on the TS surface, the spelling its own docs use, so * there is no memberName mapping. Module files are named after the tag slug * (`data_sources.ts`) while the barrel exports them camelCased * (`dataSources.queryDataSource`). */ 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"; /** `data_sources` → `dataSources` (the barrel's export name). */ const camel = (slug: string): string => slug .split("_") .filter(Boolean) .map((s, i) => (i === 0 ? s : s.charAt(0).toUpperCase() + s.slice(1))) .join(""); /** Notion's provider spec for the shared smithy→SDK compiler. */ 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: Redacted on the way out, `string | Redacted` // accepted on the way in (the REST protocol unwraps). memberTraitPipes: { [SENSITIVE_TRAIT]: "T.SensitiveValue", }, memberTsType: (m) => SENSITIVE_TRAIT in m.traits ? `string | Redacted.Redacted${m.nullable ? " | null" : ""}` : undefined, // Notion's `oneOf` unions (block types, property values, parents) are // decoded passthrough: the TS type is the case union, the schema stays // opaque. Wire names equal the TS names, so no runtime case // discrimination is needed. 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`, ], // One pagination profile: `start_cursor` in, `next_cursor` out (`null` on // the last page), traversed by core's paginateCursor. The trait is // stamped in scripts/convert.ts — see `paginationFor` there. paginationProfiles: { cursor: { strategy: "paginateCursor", itemsFallback: "results", }, }, sourceNote: ".generated-specs (specs/spec-mirror-notion/specs/openapi.json)", operationDecl: { contextType: "NotionOpContext", commonErrorType: "NotionOpError", serviceErrors: "NotionServiceErrors", protocol: "NotionProtocol", 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 Notion Effect SDK from the Smithy models", convert: () => import("./convert.ts"), root: `${import.meta.dirname}/..`, // RFC-6902 patches apply to the Smithy models via finalizeConvert in // scripts/convert.ts — never here. patchesDir: false, barrelExportName: camel, spec: () => spec, });