@contractkit/plugin-typescript 0.23.0 → 0.24.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contractkit/plugin-typescript",
3
- "version": "0.23.0",
3
+ "version": "0.24.0",
4
4
  "description": "ContractKit built-in plugin: TypeScript codegen (SDK clients, Koa routers, Zod schemas, plain types)",
5
5
  "author": {
6
6
  "name": "Marooned Software",
@@ -29,8 +29,8 @@
29
29
  "@contractkit/core": "0.17.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@repo/config-typescript": "0.1.0",
33
- "@repo/config-eslint": "0.3.1"
32
+ "@repo/config-eslint": "0.3.1",
33
+ "@repo/config-typescript": "0.1.0"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "tsup src/index.ts --format esm --sourcemap --dts && tsc --emitDeclarationOnly --declaration",
@@ -300,7 +300,17 @@ function generateSimpleModel(model: ModelNode, outPath?: string): string[] {
300
300
  for (const field of model.fields) {
301
301
  const inputKey = applyCase(field.name, inputCase);
302
302
  const outputKey = applyCase(field.name, outputCase);
303
- lines.push(` ${quoteKey(outputKey)}: data.${inputKey},`);
303
+ if (field.optional) {
304
+ // Conditional spread keeps the field optional (`k?: T`) in the inferred
305
+ // z.output / z.input type, instead of widening to required-nullable (`k: T | undefined`).
306
+ // Consumer code built with `...(x ? { k: x } : {})` is only assignable to the optional form.
307
+ // When inputCase is set, the input schema uses `.nullish()` so the guard must reject both
308
+ // null and undefined; otherwise `.optional()` only allows undefined.
309
+ const guard = hasInputTransform ? `data.${inputKey} != null` : `data.${inputKey} !== undefined`;
310
+ lines.push(` ...(${guard} ? { ${quoteKey(outputKey)}: data.${inputKey} } : {}),`);
311
+ } else {
312
+ lines.push(` ${quoteKey(outputKey)}: data.${inputKey},`);
313
+ }
304
314
  }
305
315
  lines.push(`}));`);
306
316
  // When only outputCase is set, the developer-facing type is the schema's
@@ -738,9 +748,13 @@ function renderInlineObject(o: InlineObjectTypeNode, parseCaseTransform?: 'snake
738
748
  const transformEntries = o.fields
739
749
  .map(f => {
740
750
  const snakeKey = camelToSnake(f.name);
741
- // Optional fields use .nullish() on input; coerce null undefined in output
742
- const val = f.optional ? `data.${snakeKey} ?? undefined` : `data.${snakeKey}`;
743
- return ` ${quoteKey(f.name)}: ${val},`;
751
+ // Optional fields use .nullish() on input. Conditional spread (instead of `?? undefined`)
752
+ // keeps the key optional in the inferred output type (`k?: T`) rather than widening to
753
+ // required-nullable (`k: T | undefined`).
754
+ if (f.optional) {
755
+ return ` ...(data.${snakeKey} != null ? { ${quoteKey(f.name)}: data.${snakeKey} } : {}),`;
756
+ }
757
+ return ` ${quoteKey(f.name)}: data.${snakeKey},`;
744
758
  })
745
759
  .join('\n');
746
760
  return `${wrapper}({\n${joined}\n}).transform(data => ({\n${transformEntries}\n}))`;
@@ -751,8 +765,10 @@ function renderInlineObject(o: InlineObjectTypeNode, parseCaseTransform?: 'snake
751
765
  const transformEntries = o.fields
752
766
  .map(f => {
753
767
  const pascalKey = camelToPascal(f.name);
754
- const val = f.optional ? `data.${pascalKey} ?? undefined` : `data.${pascalKey}`;
755
- return ` ${quoteKey(f.name)}: ${val},`;
768
+ if (f.optional) {
769
+ return ` ...(data.${pascalKey} != null ? { ${quoteKey(f.name)}: data.${pascalKey} } : {}),`;
770
+ }
771
+ return ` ${quoteKey(f.name)}: data.${pascalKey},`;
756
772
  })
757
773
  .join('\n');
758
774
  return `${wrapper}({\n${joined}\n}).transform(data => ({\n${transformEntries}\n}))`;
@@ -1060,9 +1060,59 @@ describe('generateContract', () => {
1060
1060
  /export const Child = z\.strictObject\(\{[\s\S]*grant_type:[\s\S]*client_id:[\s\S]*client_secret:[\s\S]*\}\)\.transform/,
1061
1061
  );
1062
1062
  expect(output).toContain('grantType: data.grant_type');
1063
- expect(output).toContain('clientId: data.client_id');
1063
+ expect(output).toContain('clientId: data.client_id,');
1064
1064
  expect(output).toContain('clientSecret: data.client_secret');
1065
1065
  expect(output).toContain('export type Child = z.output<typeof Child>');
1066
1066
  });
1067
+
1068
+ it('input=snake: optional fields use conditional spread guarded by `!= null`', () => {
1069
+ // .nullish() on input accepts null and undefined; conditional spread keeps the field
1070
+ // optional in the inferred output type (`k?: T`) instead of widening to required-nullable
1071
+ // (`k: T | undefined`). The `!= null` guard omits the key for both null and undefined.
1072
+ const root = contractRoot([
1073
+ model(
1074
+ 'User',
1075
+ [field('firstName', scalarType('string')), field('clientId', scalarType('uuid'), { optional: true })],
1076
+ { inputCase: 'snake' },
1077
+ ),
1078
+ ]);
1079
+ const output = generateContract(root);
1080
+ expect(output).toContain('client_id: z.uuid().nullish()');
1081
+ expect(output).toContain('firstName: data.first_name,');
1082
+ expect(output).toContain('...(data.client_id != null ? { clientId: data.client_id } : {}),');
1083
+ expect(output).not.toContain('?? undefined');
1084
+ });
1085
+
1086
+ it('output=snake: optional fields use conditional spread guarded by `!== undefined`', () => {
1087
+ // With only outputCase set, input is `.optional()` (undefined-only). Conditional spread keeps
1088
+ // the field optional in the inferred z.input type so consumers building values with
1089
+ // `...(x ? { k: x } : {})` are assignable.
1090
+ const root = contractRoot([
1091
+ model('Token', [field('accessToken', scalarType('string')), field('refreshToken', scalarType('string'), { optional: true })], {
1092
+ outputCase: 'snake',
1093
+ }),
1094
+ ]);
1095
+ const output = generateContract(root);
1096
+ expect(output).toContain('refreshToken: z.string().optional()');
1097
+ expect(output).toContain('access_token: data.accessToken,');
1098
+ expect(output).toContain('...(data.refreshToken !== undefined ? { refresh_token: data.refreshToken } : {}),');
1099
+ expect(output).not.toContain('?? undefined');
1100
+ });
1101
+
1102
+ it('input=pascal: inline-object optional fields use conditional spread guarded by `!= null`', () => {
1103
+ const dataType = inlineObjectType([
1104
+ field('id', scalarType('uuid')),
1105
+ field('amount', scalarType('number'), { optional: true }),
1106
+ ]);
1107
+ const root = contractRoot([
1108
+ model('Webhook', [field('event', scalarType('string')), field('data', dataType)], { inputCase: 'pascal' }),
1109
+ ]);
1110
+ const output = generateContract(root);
1111
+ // Inline object emits its own transform inside the Webhook input shape.
1112
+ expect(output).toContain('Amount: z.coerce.number().nullish()');
1113
+ expect(output).toContain('id: data.Id,');
1114
+ expect(output).toContain('...(data.Amount != null ? { amount: data.Amount } : {}),');
1115
+ expect(output).not.toContain('?? undefined');
1116
+ });
1067
1117
  });
1068
1118
  });