@distilled.cloud/core 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Sensitive data schemas for handling credentials and secrets.
3
+ *
4
+ * This module provides schemas that wrap sensitive data in Effect's Redacted type,
5
+ * preventing accidental logging of secrets like passwords and tokens.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { SensitiveString } from "@distilled.cloud/sdk-core/sensitive";
10
+ *
11
+ * const Password = Schema.Struct({ plain_text: SensitiveString });
12
+ *
13
+ * // Users can pass raw strings (convenient):
14
+ * API.createPassword({ plain_text: "my-secret" })
15
+ *
16
+ * // Or Redacted values (explicit):
17
+ * API.createPassword({ plain_text: Redacted.make("my-secret") })
18
+ *
19
+ * // Response values are always Redacted (safe):
20
+ * console.log(result.plain_text); // logs "<redacted>"
21
+ * ```
22
+ */
23
+ import * as Redacted from "effect/Redacted";
24
+ import * as S from "effect/Schema";
25
+ import * as SchemaTransformation from "effect/SchemaTransformation";
26
+
27
+ /**
28
+ * Sensitive - Marks data as sensitive, wrapping in Effect's Redacted type.
29
+ *
30
+ * This schema provides a convenient way to handle sensitive data:
31
+ * - TypeScript type: `A | Redacted.Redacted<A>` (accepts both for inputs)
32
+ * - Decode (responses): Always wraps wire value in Redacted
33
+ * - Encode (requests): Accepts BOTH raw values and Redacted, extracts the raw value
34
+ *
35
+ * The union type allows users to conveniently pass plain values for inputs while
36
+ * still getting proper Redacted types for outputs. Response values will always
37
+ * be Redacted, which prevents accidental logging.
38
+ */
39
+ export const Sensitive = <A>(
40
+ schema: S.Schema<A>,
41
+ ): S.Schema<A | Redacted.Redacted<A>> =>
42
+ schema
43
+ .pipe(
44
+ S.decodeTo(
45
+ S.Union([S.toType(schema), S.Redacted(S.toType(schema))]),
46
+ SchemaTransformation.transform({
47
+ // Decode: wire format -> always wrap in Redacted
48
+ decode: (a) => Redacted.make(a) as any,
49
+ // Encode: accept both raw and Redacted -> extract raw value
50
+ encode: (v) => (Redacted.isRedacted(v) ? Redacted.value(v) : v),
51
+ }),
52
+ ),
53
+ )
54
+ .annotate({
55
+ identifier: `Sensitive<${schema.ast.annotations?.identifier ?? "unknown"}>`,
56
+ });
57
+
58
+ /**
59
+ * Sensitive string - a string marked as sensitive.
60
+ * Wire format is plain string, TypeScript type is string | Redacted<string>.
61
+ * At runtime, decoded values are always Redacted<string>.
62
+ */
63
+ export const SensitiveString = Sensitive(S.String).annotate({
64
+ identifier: "SensitiveString",
65
+ });
66
+
67
+ /**
68
+ * Sensitive nullable string - a nullable string marked as sensitive.
69
+ * Wire format is plain string | null, TypeScript type is string | null | Redacted<string>.
70
+ * At runtime, decoded non-null values are always Redacted<string>.
71
+ */
72
+ export const SensitiveNullableString = S.NullOr(SensitiveString).annotate({
73
+ identifier: "SensitiveNullableString",
74
+ });
package/src/traits.ts ADDED
@@ -0,0 +1,627 @@
1
+ /**
2
+ * Annotation-based traits for declarative operation definitions.
3
+ *
4
+ * This module provides a type-safe annotation system for defining HTTP operations
5
+ * using Schema annotations. Traits can be applied via `.pipe()` or composed with `all()`.
6
+ *
7
+ * The annotation system is shared across all SDKs. Individual SDKs can extend it
8
+ * with provider-specific traits (e.g., AWS Smithy traits, Cloudflare-specific traits).
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import * as T from "@distilled.cloud/sdk-core/traits";
13
+ *
14
+ * const GetDatabaseInput = Schema.Struct({
15
+ * organization: Schema.String.pipe(T.PathParam()),
16
+ * database: Schema.String.pipe(T.PathParam()),
17
+ * }).pipe(
18
+ * T.Http({ method: "GET", path: "/organizations/{organization}/databases/{database}" })
19
+ * );
20
+ * ```
21
+ */
22
+ import * as Schema from "effect/Schema";
23
+ import * as AST from "effect/SchemaAST";
24
+
25
+ // ============================================================================
26
+ // Annotation Primitives
27
+ // ============================================================================
28
+
29
+ /**
30
+ * Internal symbol for annotation metadata storage.
31
+ */
32
+ const annotationMetaSymbol = Symbol.for("@distilled.cloud/annotation-meta");
33
+
34
+ /**
35
+ * Any type that has an .annotate() method returning itself.
36
+ * This includes Schema.Schema and Schema.PropertySignature.
37
+ */
38
+ type Annotatable = {
39
+ annotate(annotations: unknown): Annotatable;
40
+ };
41
+
42
+ /**
43
+ * An Annotation is a callable that can be used with .pipe() AND
44
+ * has symbol properties so it works directly with Schema.Struct/Class.
45
+ *
46
+ * The index signatures allow TypeScript to accept this as a valid annotations object.
47
+ */
48
+ export interface Annotation {
49
+ <A extends Annotatable>(schema: A): A;
50
+ readonly [annotationMetaSymbol]: Array<{ symbol: symbol; value: unknown }>;
51
+ // Index signatures for compatibility with Schema.Annotations
52
+ readonly [key: symbol]: unknown;
53
+ readonly [key: string]: unknown;
54
+ }
55
+
56
+ /**
57
+ * Create an annotation builder for a given symbol and value.
58
+ * This is the core primitive used to build all trait annotations.
59
+ */
60
+ export function makeAnnotation<T>(sym: symbol, value: T): Annotation {
61
+ const fn = <A extends Annotatable>(schema: A): A =>
62
+ schema.annotate({ [sym]: value }) as A;
63
+
64
+ (fn as any)[annotationMetaSymbol] = [{ symbol: sym, value }];
65
+ (fn as any)[sym] = value;
66
+
67
+ return fn as Annotation;
68
+ }
69
+
70
+ /**
71
+ * Combine multiple annotations into one.
72
+ * Use when you need multiple annotations on the same schema.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const MyInput = Schema.Struct({
77
+ * id: Schema.String.pipe(T.all(T.PathParam(), T.Required())),
78
+ * });
79
+ * ```
80
+ */
81
+ export function all(...annotations: Annotation[]): Annotation {
82
+ const entries: Array<{ symbol: symbol; value: unknown }> = [];
83
+ const raw: Record<symbol, unknown> = {};
84
+
85
+ for (const a of annotations) {
86
+ for (const entry of a[annotationMetaSymbol]) {
87
+ entries.push(entry);
88
+ raw[entry.symbol] = entry.value;
89
+ }
90
+ }
91
+
92
+ const fn = <A extends Annotatable>(schema: A): A => schema.annotate(raw) as A;
93
+
94
+ (fn as any)[annotationMetaSymbol] = entries;
95
+
96
+ for (const { symbol, value } of entries) {
97
+ (fn as any)[symbol] = value;
98
+ }
99
+
100
+ return fn as Annotation;
101
+ }
102
+
103
+ // =============================================================================
104
+ // HTTP Operation Traits
105
+ // =============================================================================
106
+
107
+ /** Symbol for HTTP operation metadata (method + path template) */
108
+ export const httpSymbol = Symbol.for("@distilled.cloud/http");
109
+
110
+ /** HTTP method type */
111
+ export type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD";
112
+
113
+ /** HTTP trait configuration */
114
+ export interface HttpTrait {
115
+ /** HTTP method */
116
+ method: HttpMethod;
117
+ /** Path template with {param} placeholders */
118
+ path: string;
119
+ /** Content type override (e.g., "multipart") */
120
+ contentType?: string;
121
+ /** Whether the request has a body (used by GCP generator) */
122
+ hasBody?: boolean;
123
+ }
124
+
125
+ /**
126
+ * Http trait - defines the HTTP method and path template for an operation.
127
+ * Path parameters are specified using {paramName} syntax.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * const GetDatabaseInput = Schema.Struct({
132
+ * organization: Schema.String.pipe(T.PathParam()),
133
+ * database: Schema.String.pipe(T.PathParam()),
134
+ * }).pipe(
135
+ * T.Http({ method: "GET", path: "/organizations/{organization}/databases/{database}" })
136
+ * );
137
+ * ```
138
+ */
139
+ export const Http = (trait: HttpTrait) => makeAnnotation(httpSymbol, trait);
140
+
141
+ // =============================================================================
142
+ // Path Parameter Traits
143
+ // =============================================================================
144
+
145
+ /** Symbol for path parameter annotation */
146
+ export const pathParamSymbol = Symbol.for("@distilled.cloud/path-param");
147
+
148
+ /**
149
+ * PathParam trait - marks a field as a path parameter.
150
+ * The field name is used as the placeholder name in the path template.
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * const Input = Schema.Struct({
155
+ * organization: Schema.String.pipe(T.PathParam()),
156
+ * }).pipe(
157
+ * T.Http({ method: "GET", path: "/organizations/{organization}" })
158
+ * );
159
+ * ```
160
+ */
161
+ export const PathParam = () => makeAnnotation(pathParamSymbol, true);
162
+
163
+ // =============================================================================
164
+ // Query Parameter Traits
165
+ // =============================================================================
166
+
167
+ /** Symbol for query parameter annotation */
168
+ export const queryParamSymbol = Symbol.for("@distilled.cloud/query-param");
169
+
170
+ /**
171
+ * QueryParam trait - marks a field as a query parameter.
172
+ * Optionally specify a different wire name.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * const Input = Schema.Struct({
177
+ * perPage: Schema.optional(Schema.Number).pipe(T.QueryParam("per_page")),
178
+ * });
179
+ * ```
180
+ */
181
+ export const QueryParam = (name?: string) =>
182
+ makeAnnotation(queryParamSymbol, name ?? true);
183
+
184
+ // =============================================================================
185
+ // Header Parameter Traits
186
+ // =============================================================================
187
+
188
+ /** Symbol for header parameter annotation */
189
+ export const headerParamSymbol = Symbol.for("@distilled.cloud/header-param");
190
+
191
+ /**
192
+ * HeaderParam trait - marks a field as a header parameter.
193
+ * Specify the header name.
194
+ *
195
+ * @example
196
+ * ```ts
197
+ * const Input = Schema.Struct({
198
+ * apiToken: Schema.String.pipe(T.HeaderParam("X-API-Token")),
199
+ * });
200
+ * ```
201
+ */
202
+ export const HeaderParam = (name: string) =>
203
+ makeAnnotation(headerParamSymbol, name);
204
+
205
+ // =============================================================================
206
+ // Convenience Aliases (used by Cloudflare/GCP generators)
207
+ // =============================================================================
208
+
209
+ /**
210
+ * HttpPath - alias for PathParam that also carries the wire name.
211
+ * Used in generated code: `Schema.String.pipe(T.HttpPath("account_id"))`
212
+ */
213
+ export const HttpPath = (name: string) => makeAnnotation(pathParamSymbol, name);
214
+
215
+ /**
216
+ * HttpQuery - alias for QueryParam with an explicit wire name.
217
+ * Used in generated code: `Schema.optional(Schema.String).pipe(T.HttpQuery("per_page"))`
218
+ */
219
+ export const HttpQuery = (name: string) =>
220
+ makeAnnotation(queryParamSymbol, name);
221
+
222
+ /**
223
+ * HttpHeader - alias for HeaderParam.
224
+ * Used in generated code: `Schema.String.pipe(T.HttpHeader("X-Custom-Header"))`
225
+ */
226
+ export const HttpHeader = (name: string) =>
227
+ makeAnnotation(headerParamSymbol, name);
228
+
229
+ /** Symbol for HTTP body annotation */
230
+ export const httpBodySymbol = Symbol.for("@distilled.cloud/http-body");
231
+
232
+ /**
233
+ * HttpBody - marks a field as the raw HTTP body.
234
+ * Used for operations where a field IS the entire request body
235
+ * (not a named field within a JSON body).
236
+ */
237
+ export const HttpBody = () => makeAnnotation(httpBodySymbol, true);
238
+
239
+ /** Symbol for form data file annotation */
240
+ export const httpFormDataFileSymbol = Symbol.for(
241
+ "@distilled.cloud/http-form-data-file",
242
+ );
243
+
244
+ /**
245
+ * HttpFormDataFile - marks a field as a file upload in multipart form data.
246
+ */
247
+ export const HttpFormDataFile = () =>
248
+ makeAnnotation(httpFormDataFileSymbol, true);
249
+
250
+ // =============================================================================
251
+ // API Error Code Trait
252
+ // =============================================================================
253
+
254
+ /** Symbol for API error code mapping */
255
+ export const apiErrorCodeSymbol = Symbol.for("@distilled.cloud/api-error-code");
256
+
257
+ /**
258
+ * ApiErrorCode trait - maps an error class to an API error code.
259
+ * Used to match API error responses to typed error classes.
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * class NotFoundError extends Schema.TaggedErrorClass<NotFoundError>()(
264
+ * "NotFoundError",
265
+ * { message: Schema.String },
266
+ * ).pipe(T.ApiErrorCode("not_found")) {}
267
+ * ```
268
+ */
269
+ export const ApiErrorCode = (code: string) =>
270
+ makeAnnotation(apiErrorCodeSymbol, code);
271
+
272
+ // =============================================================================
273
+ // Service Metadata Trait
274
+ // =============================================================================
275
+
276
+ /** Symbol for service metadata */
277
+ export const serviceSymbol = Symbol.for("@distilled.cloud/service");
278
+
279
+ /** Service metadata */
280
+ export interface ServiceTrait {
281
+ name: string;
282
+ version?: string;
283
+ baseUrl?: string;
284
+ /** GCP-specific: Root URL for the service */
285
+ rootUrl?: string;
286
+ /** GCP-specific: Service path appended to root URL */
287
+ servicePath?: string;
288
+ /** Allow additional properties for provider-specific metadata */
289
+ [key: string]: unknown;
290
+ }
291
+
292
+ /**
293
+ * Service trait - attaches service metadata to a schema.
294
+ */
295
+ export const Service = (trait: ServiceTrait) =>
296
+ makeAnnotation(serviceSymbol, trait);
297
+
298
+ // =============================================================================
299
+ // Annotation Retrieval Helpers
300
+ // =============================================================================
301
+
302
+ /**
303
+ * Get annotation value from an AST node, following encoding chain if needed.
304
+ */
305
+ export const getAnnotation = <T>(
306
+ ast: AST.AST,
307
+ symbol: symbol,
308
+ ): T | undefined => {
309
+ // Direct annotation
310
+ const annotations = ast.annotations as Record<symbol, unknown> | undefined;
311
+ const direct = annotations?.[symbol] as T | undefined;
312
+ if (direct !== undefined) return direct;
313
+
314
+ // Follow encoding chain (replaces v3 Transformation handling)
315
+ if (ast.encoding && ast.encoding.length > 0) {
316
+ return getAnnotation<T>(ast.encoding[0].to, symbol);
317
+ }
318
+
319
+ return undefined;
320
+ };
321
+
322
+ /**
323
+ * Get HTTP trait from a schema's AST.
324
+ */
325
+ export const getHttpTrait = (ast: AST.AST): HttpTrait | undefined =>
326
+ getAnnotation<HttpTrait>(ast, httpSymbol);
327
+
328
+ /**
329
+ * Check if a PropertySignature has the pathParam annotation.
330
+ * Works for both PathParam() (annotation value = true) and HttpPath("wire_name") (annotation value = string).
331
+ */
332
+ export const isPathParam = (prop: AST.PropertySignature): boolean => {
333
+ const value = getAnnotation<string | boolean>(prop.type, pathParamSymbol);
334
+ return value !== undefined;
335
+ };
336
+
337
+ /**
338
+ * Get query param name from a PropertySignature (returns true if unnamed, string if named).
339
+ */
340
+ export const getQueryParam = (
341
+ prop: AST.PropertySignature,
342
+ ): string | boolean | undefined => {
343
+ return getAnnotation<string | boolean>(prop.type, queryParamSymbol);
344
+ };
345
+
346
+ /**
347
+ * Get header param name from a PropertySignature.
348
+ */
349
+ export const getHeaderParam = (
350
+ prop: AST.PropertySignature,
351
+ ): string | undefined => {
352
+ return getAnnotation<string>(prop.type, headerParamSymbol);
353
+ };
354
+
355
+ /**
356
+ * Get API error code from an error class AST.
357
+ */
358
+ export const getApiErrorCode = (ast: AST.AST): string | undefined =>
359
+ getAnnotation<string>(ast, apiErrorCodeSymbol);
360
+
361
+ /**
362
+ * Get service metadata from a schema's AST.
363
+ */
364
+ export const getServiceTrait = (ast: AST.AST): ServiceTrait | undefined =>
365
+ getAnnotation<ServiceTrait>(ast, serviceSymbol);
366
+
367
+ /**
368
+ * Extract path parameters from a schema's struct properties.
369
+ * Returns an array of field names that have the PathParam annotation.
370
+ */
371
+ export const getPathParams = (ast: AST.AST): string[] => {
372
+ // Handle Objects (struct) - v4 renamed from TypeLiteral
373
+ if (ast._tag === "Objects") {
374
+ return ast.propertySignatures
375
+ .filter((prop) => isPathParam(prop))
376
+ .map((prop) => String(prop.name));
377
+ }
378
+
379
+ // Follow encoding chain (replaces v3 Transformation handling)
380
+ if (ast.encoding && ast.encoding.length > 0) {
381
+ return getPathParams(ast.encoding[0].to);
382
+ }
383
+
384
+ return [];
385
+ };
386
+
387
+ /**
388
+ * Build the request path by substituting path parameters into the template.
389
+ * Simple version that assumes input keys match template placeholders.
390
+ * For schema-aware path building (with camelCase → wire_name mapping), use buildPathFromSchema.
391
+ */
392
+ export const buildPath = (
393
+ template: string,
394
+ input: Record<string, unknown>,
395
+ ): string => {
396
+ return template.replace(/\{(\w+)\}/g, (_, name) => {
397
+ const value = input[name];
398
+ if (value === undefined || value === null) {
399
+ throw new Error(`Missing path parameter: ${name}`);
400
+ }
401
+ return encodeURIComponent(String(value));
402
+ });
403
+ };
404
+
405
+ /**
406
+ * Extract AST property signatures from a schema AST, following encoding chain and suspends.
407
+ */
408
+ export const getStructProps = (ast: AST.AST): AST.PropertySignature[] => {
409
+ if (ast.encoding && ast.encoding.length > 0) {
410
+ return getStructProps(ast.encoding[0].to);
411
+ }
412
+ if (ast._tag === "Suspend") {
413
+ return getStructProps(ast.thunk());
414
+ }
415
+ if (ast._tag === "Objects") {
416
+ return [...ast.propertySignatures];
417
+ }
418
+ return [];
419
+ };
420
+
421
+ /**
422
+ * Get the path parameter wire name from a PropertySignature.
423
+ * - For HttpPath("wire_name"), returns the wire name string.
424
+ * - For PathParam(), returns the property name (since annotation is `true`).
425
+ * - Returns undefined if not a path param.
426
+ */
427
+ export const getPathParamWireName = (
428
+ prop: AST.PropertySignature,
429
+ ): string | undefined => {
430
+ const value = getAnnotation<string | boolean>(prop.type, pathParamSymbol);
431
+ if (value === undefined) return undefined;
432
+ if (typeof value === "string") return value;
433
+ // PathParam() stores `true` — use property name as wire name
434
+ return String(prop.name);
435
+ };
436
+
437
+ /**
438
+ * Result of categorizing a schema's input properties by their HTTP binding.
439
+ */
440
+ export interface RequestParts {
441
+ /** Resolved path with all parameters substituted */
442
+ path: string;
443
+ /** Query parameters: wire_name → string value */
444
+ query: Record<string, string | string[]>;
445
+ /** Header parameters: header-name → string value */
446
+ headers: Record<string, string>;
447
+ /** Body: remaining non-path/query/header properties, with wire-name keys where applicable */
448
+ body: Record<string, unknown> | undefined;
449
+ /** Whether the body should use multipart/form-data */
450
+ isMultipart: boolean;
451
+ }
452
+
453
+ /**
454
+ * Schema-aware request builder. Categorizes input properties into path, query, header,
455
+ * and body parts using annotations on the schema AST.
456
+ *
457
+ * Handles camelCase → wire_name mapping for path params (HttpPath), query params (HttpQuery),
458
+ * and header params (HttpHeader).
459
+ *
460
+ * When `inputSchema` is provided, uses `Schema.encodeSync` to encode the input through the
461
+ * schema's encoding pipeline (e.g., `encodeKeys` for camelCase → snake_case mapping).
462
+ * The encoded output is used for body construction, ensuring wire-format key names.
463
+ */
464
+ export const buildRequestParts = (
465
+ ast: AST.AST,
466
+ httpTrait: HttpTrait,
467
+ input: Record<string, unknown>,
468
+ // biome-ignore lint: using any for generic schema parameter
469
+ inputSchema?: any,
470
+ ): RequestParts => {
471
+ let path = httpTrait.path;
472
+ const query: Record<string, string | string[]> = {};
473
+ const headers: Record<string, string> = {};
474
+ let rawBody: unknown = undefined;
475
+ let hasRawBody = false;
476
+ const isMultipart = httpTrait.contentType === "multipart";
477
+
478
+ // Track which TS property names are path/query/header params (not body)
479
+ const nonBodyKeys = new Set<string>();
480
+
481
+ const props = getStructProps(ast);
482
+
483
+ for (const prop of props) {
484
+ const tsName = String(prop.name);
485
+ const value = input[tsName];
486
+
487
+ if (value === undefined || value === null) {
488
+ continue;
489
+ }
490
+
491
+ // Path parameter
492
+ const pathWireName = getPathParamWireName(prop);
493
+ if (pathWireName !== undefined) {
494
+ nonBodyKeys.add(tsName);
495
+ path = path.replace(
496
+ `{${pathWireName}}`,
497
+ encodeURIComponent(String(value)),
498
+ );
499
+ continue;
500
+ }
501
+
502
+ // Query parameter
503
+ const queryParam = getQueryParam(prop);
504
+ if (queryParam !== undefined) {
505
+ nonBodyKeys.add(tsName);
506
+ const wireName = typeof queryParam === "string" ? queryParam : tsName;
507
+ if (Array.isArray(value)) {
508
+ query[wireName] = value.map(String);
509
+ } else {
510
+ query[wireName] = String(value);
511
+ }
512
+ continue;
513
+ }
514
+
515
+ // Header parameter
516
+ const headerParam = getHeaderParam(prop);
517
+ if (headerParam !== undefined) {
518
+ nonBodyKeys.add(tsName);
519
+ headers[headerParam] = String(value);
520
+ continue;
521
+ }
522
+
523
+ // Body field (HttpBody annotation means this IS the entire body)
524
+ const isBodyField = getAnnotation<boolean>(prop.type, httpBodySymbol);
525
+ if (isBodyField) {
526
+ rawBody = value;
527
+ hasRawBody = true;
528
+ nonBodyKeys.add(tsName);
529
+ continue;
530
+ }
531
+ }
532
+
533
+ // Build body from remaining (non-path/query/header) properties
534
+ let finalBody: Record<string, unknown> | undefined;
535
+
536
+ if (hasRawBody) {
537
+ // For HttpBody fields, encode through the schema to get wire-format keys
538
+ // (e.g., camelCase → snake_case via encodeKeys on nested schemas)
539
+ if (inputSchema) {
540
+ const encoded = Schema.encodeSync(inputSchema)(input);
541
+ const encodedRecord = encoded as Record<string, unknown>;
542
+ // Find the body field name in the encoded output
543
+ for (const prop of props) {
544
+ const tsName = String(prop.name);
545
+ const isBody = getAnnotation<boolean>(prop.type, httpBodySymbol);
546
+ if (isBody && encodedRecord[tsName] !== undefined) {
547
+ finalBody = encodedRecord[tsName] as Record<string, unknown>;
548
+ break;
549
+ }
550
+ }
551
+ // Fallback to raw body if encoding didn't produce it
552
+ if (finalBody === undefined) {
553
+ finalBody = rawBody as Record<string, unknown> | undefined;
554
+ }
555
+ } else {
556
+ finalBody = rawBody as Record<string, unknown> | undefined;
557
+ }
558
+ } else {
559
+ // Encode the input through the schema to get wire-format keys
560
+ // This handles encodeKeys (camelCase → snake_case) and any other encoding transforms
561
+ if (inputSchema) {
562
+ const encoded = Schema.encodeSync(inputSchema)(input);
563
+ const encodedRecord = encoded as Record<string, unknown>;
564
+
565
+ // Build a mapping from tsName → encoded key name
566
+ // by encoding a minimal test object to discover key mappings
567
+ const bodyFromEncoded: Record<string, unknown> = {};
568
+ let hasBodyFields = false;
569
+
570
+ for (const [key, value] of Object.entries(encodedRecord)) {
571
+ // Check if this encoded key corresponds to a non-body TS property
572
+ // by seeing if any non-body prop encodes to this key
573
+ let isNonBody = false;
574
+ for (const nbKey of nonBodyKeys) {
575
+ // Simple heuristic: if the encoded key matches the non-body key or its encoding
576
+ if (key === nbKey) {
577
+ isNonBody = true;
578
+ break;
579
+ }
580
+ }
581
+ if (!isNonBody && value !== undefined) {
582
+ bodyFromEncoded[key] = value;
583
+ hasBodyFields = true;
584
+ }
585
+ }
586
+
587
+ finalBody = hasBodyFields ? bodyFromEncoded : undefined;
588
+ } else {
589
+ // Fallback: no schema encoding, use TS property names as-is (for backwards compat)
590
+ const body: Record<string, unknown> = {};
591
+ let hasBody = false;
592
+ for (const prop of props) {
593
+ const tsName = String(prop.name);
594
+ if (nonBodyKeys.has(tsName)) continue;
595
+ const value = input[tsName];
596
+ if (value === undefined || value === null) continue;
597
+ body[tsName] = value;
598
+ hasBody = true;
599
+ }
600
+ finalBody = hasBody ? body : undefined;
601
+ }
602
+ }
603
+
604
+ return {
605
+ path,
606
+ query,
607
+ headers,
608
+ body: finalBody,
609
+ isMultipart,
610
+ };
611
+ };
612
+
613
+ /**
614
+ * Helper to get a value from an object using a dot-separated path.
615
+ * Used for pagination traits and nested property access.
616
+ */
617
+ export const getPath = (obj: unknown, path: string): unknown => {
618
+ const parts = path.split(".");
619
+ let current: unknown = obj;
620
+ for (const part of parts) {
621
+ if (current == null || typeof current !== "object") {
622
+ return undefined;
623
+ }
624
+ current = (current as Record<string, unknown>)[part];
625
+ }
626
+ return current;
627
+ };