@contractkit/plugin-typescript 0.23.1 → 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.1",
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,8 +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
- const val = field.optional ? `data.${inputKey} ?? undefined` : `data.${inputKey}`;
304
- lines.push(` ${quoteKey(outputKey)}: ${val},`);
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
+ }
305
314
  }
306
315
  lines.push(`}));`);
307
316
  // When only outputCase is set, the developer-facing type is the schema's
@@ -739,9 +748,13 @@ function renderInlineObject(o: InlineObjectTypeNode, parseCaseTransform?: 'snake
739
748
  const transformEntries = o.fields
740
749
  .map(f => {
741
750
  const snakeKey = camelToSnake(f.name);
742
- // Optional fields use .nullish() on input; coerce null undefined in output
743
- const val = f.optional ? `data.${snakeKey} ?? undefined` : `data.${snakeKey}`;
744
- 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},`;
745
758
  })
746
759
  .join('\n');
747
760
  return `${wrapper}({\n${joined}\n}).transform(data => ({\n${transformEntries}\n}))`;
@@ -752,8 +765,10 @@ function renderInlineObject(o: InlineObjectTypeNode, parseCaseTransform?: 'snake
752
765
  const transformEntries = o.fields
753
766
  .map(f => {
754
767
  const pascalKey = camelToPascal(f.name);
755
- const val = f.optional ? `data.${pascalKey} ?? undefined` : `data.${pascalKey}`;
756
- 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},`;
757
772
  })
758
773
  .join('\n');
759
774
  return `${wrapper}({\n${joined}\n}).transform(data => ({\n${transformEntries}\n}))`;
@@ -1060,12 +1060,15 @@ 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 ?? undefined');
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
1067
 
1068
- it('input=snake: optional fields coerce null undefined in the transform', () => {
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.
1069
1072
  const root = contractRoot([
1070
1073
  model(
1071
1074
  'User',
@@ -1076,7 +1079,40 @@ describe('generateContract', () => {
1076
1079
  const output = generateContract(root);
1077
1080
  expect(output).toContain('client_id: z.uuid().nullish()');
1078
1081
  expect(output).toContain('firstName: data.first_name,');
1079
- expect(output).toContain('clientId: data.client_id ?? undefined,');
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');
1080
1116
  });
1081
1117
  });
1082
1118
  });