Skip to content

Commit b81d47f

Browse files
authored
fix(contract): missing .$input builder method (#1304)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a new $input method on ContractBuilder to set/configure input schemas with type-safe behavior. * **Tests** * Added runtime and type-level tests for $input, updated type expectations across builder variants, and extended existing tests to assert chaining/instance behavior. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 65fbdd2 commit b81d47f

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

packages/contract/src/builder-variants.test-d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('ContractProcedureBuilder', () => {
1414
const builder = {} as ContractProcedureBuilder<typeof inputSchema, typeof outputSchema, typeof baseErrorMap, BaseMeta>
1515

1616
it('backward compatibility', () => {
17-
const expected = {} as OmitChainMethodDeep<typeof generalBuilder, '$meta' | '$route' | 'prefix' | 'tag' | 'router'>
17+
const expected = {} as OmitChainMethodDeep<typeof generalBuilder, '$meta' | '$route' | '$input' | 'prefix' | 'tag' | 'router'>
1818

1919
expectTypeOf(builder).toMatchTypeOf(expected)
2020
expectTypeOf<keyof typeof builder>().toEqualTypeOf<keyof typeof expected>()
@@ -95,7 +95,7 @@ describe('ContractProcedureBuilderWithInput', () => {
9595
const builder = {} as ContractProcedureBuilderWithInput<typeof inputSchema, typeof outputSchema, typeof baseErrorMap, BaseMeta>
9696

9797
it('backward compatibility', () => {
98-
const expected = {} as OmitChainMethodDeep<typeof generalBuilder, '$meta' | '$route' | 'prefix' | 'tag' | 'router' | 'input'>
98+
const expected = {} as OmitChainMethodDeep<typeof generalBuilder, '$meta' | '$route' | '$input' | 'prefix' | 'tag' | 'router' | 'input'>
9999

100100
expectTypeOf(builder).toMatchTypeOf(expected)
101101
expectTypeOf<keyof typeof builder>().toEqualTypeOf<keyof typeof expected>()
@@ -162,7 +162,7 @@ describe('ContractProcedureBuilderWithOutput', () => {
162162
const builder = {} as ContractProcedureBuilderWithOutput<typeof inputSchema, typeof outputSchema, typeof baseErrorMap, BaseMeta>
163163

164164
it('backward compatibility', () => {
165-
const expected = {} as OmitChainMethodDeep<typeof generalBuilder, '$meta' | '$route' | 'prefix' | 'tag' | 'router' | 'output'>
165+
const expected = {} as OmitChainMethodDeep<typeof generalBuilder, '$meta' | '$route' | '$input' | 'prefix' | 'tag' | 'router' | 'output'>
166166

167167
expectTypeOf(builder).toMatchTypeOf(expected)
168168
expectTypeOf<keyof typeof builder>().toEqualTypeOf<keyof typeof expected>()
@@ -229,7 +229,7 @@ it('ContractProcedureBuilderWithInputOutput', () => {
229229
const builder = {} as ContractProcedureBuilderWithInputOutput<typeof inputSchema, typeof outputSchema, typeof baseErrorMap, BaseMeta>
230230

231231
it('backward compatibility', () => {
232-
const expected = {} as OmitChainMethodDeep<typeof generalBuilder, '$meta' | '$route' | 'prefix' | 'tag' | 'router' | 'input' | 'output'>
232+
const expected = {} as OmitChainMethodDeep<typeof generalBuilder, '$meta' | '$route' | '$input' | 'prefix' | 'tag' | 'router' | 'input' | 'output'>
233233

234234
expectTypeOf(builder).toMatchTypeOf(expected)
235235
expectTypeOf<keyof typeof builder>().toEqualTypeOf<keyof typeof expected>()
@@ -282,7 +282,7 @@ describe('ContractRouterBuilder', () => {
282282
const builder = {} as ContractRouterBuilder<typeof baseErrorMap, BaseMeta>
283283

284284
it('backward compatibility', () => {
285-
const expected = {} as OmitChainMethodDeep<typeof generalBuilder, '$meta' | '$route' | 'route' | 'meta' | 'input' | 'output'>
285+
const expected = {} as OmitChainMethodDeep<typeof generalBuilder, '$meta' | '$route' | '$input' | 'route' | 'meta' | 'input' | 'output'>
286286

287287
// expectTypeOf(builder).toMatchTypeOf(expected)
288288
expectTypeOf<keyof typeof builder>().toEqualTypeOf<keyof typeof expected>()

packages/contract/src/builder.test-d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ describe('ContractBuilder', () => {
4141
builder.$route({ method: 'INVALID' })
4242
})
4343

44+
it('.$input', () => {
45+
expectTypeOf(builder.$input(generalSchema)).toEqualTypeOf<
46+
ContractBuilder<typeof generalSchema, typeof outputSchema, typeof baseErrorMap, BaseMeta>
47+
>()
48+
49+
// @ts-expect-error - schema is invalid
50+
builder.$input({})
51+
})
52+
4453
it('.errors', () => {
4554
expectTypeOf(builder.errors({ INVALID: { message: 'invalid' }, OVERRIDE: { message: 'override' } })).toEqualTypeOf<
4655
ContractBuilder<

packages/contract/src/builder.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,23 @@ describe('contractBuilder', () => {
4343

4444
const applied = builder.$route(route)
4545
expect(applied).toBeInstanceOf(ContractBuilder)
46+
expect(applied).not.toBe(builder)
4647
expect(applied['~orpc']).toEqual({
4748
...def,
4849
route,
4950
})
5051
})
5152

53+
it('.$input', () => {
54+
const applied = builder.$input(generalSchema)
55+
expect(applied).toBeInstanceOf(ContractBuilder)
56+
expect(applied).not.toBe(builder)
57+
expect(applied['~orpc']).toEqual({
58+
...def,
59+
inputSchema: generalSchema,
60+
})
61+
})
62+
5263
it('.errors', () => {
5364
const errors = { BAD_GATEWAY: { data: outputSchema }, OVERRIDE: { message: 'override' } } as const
5465

packages/contract/src/builder.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ export class ContractBuilder<
7373
})
7474
}
7575

76+
/**
77+
* Sets or overrides the initial input schema.
78+
*
79+
* @see {@link https://orpc.dev/docs/procedure#initial-configuration Initial Procedure Configuration Docs}
80+
*/
81+
$input<U extends AnySchema>(
82+
initialInputSchema?: U,
83+
): ContractBuilder<U, TOutputSchema, TErrorMap, TMeta> {
84+
return new ContractBuilder({
85+
...this['~orpc'],
86+
inputSchema: initialInputSchema,
87+
})
88+
}
89+
7690
/**
7791
* Adds type-safe custom errors to the contract.
7892
* The provided errors are spared-merged with any existing errors in the contract.

0 commit comments

Comments
 (0)