Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/contract/src/builder-variants.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('ContractProcedureBuilder', () => {
const builder = {} as ContractProcedureBuilder<typeof inputSchema, typeof outputSchema, typeof baseErrorMap, BaseMeta>

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

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

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

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

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

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

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

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

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

// expectTypeOf(builder).toMatchTypeOf(expected)
expectTypeOf<keyof typeof builder>().toEqualTypeOf<keyof typeof expected>()
Expand Down
9 changes: 9 additions & 0 deletions packages/contract/src/builder.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ describe('ContractBuilder', () => {
builder.$route({ method: 'INVALID' })
})

it('.$input', () => {
expectTypeOf(builder.$input(generalSchema)).toEqualTypeOf<
ContractBuilder<typeof generalSchema, typeof outputSchema, typeof baseErrorMap, BaseMeta>
>()

// @ts-expect-error - schema is invalid
builder.$input({})
})

it('.errors', () => {
expectTypeOf(builder.errors({ INVALID: { message: 'invalid' }, OVERRIDE: { message: 'override' } })).toEqualTypeOf<
ContractBuilder<
Expand Down
11 changes: 11 additions & 0 deletions packages/contract/src/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,23 @@ describe('contractBuilder', () => {

const applied = builder.$route(route)
expect(applied).toBeInstanceOf(ContractBuilder)
expect(applied).not.toBe(builder)
expect(applied['~orpc']).toEqual({
...def,
route,
})
})

it('.$input', () => {
const applied = builder.$input(generalSchema)
expect(applied).toBeInstanceOf(ContractBuilder)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with other tests in this file (e.g., for .meta, .errors, .input), it would be good to also assert that the .$input method returns a new ContractBuilder instance and does not mutate the original one. This ensures the immutability of the builder is properly tested.

    expect(applied).toBeInstanceOf(ContractBuilder)
    expect(applied).not.toBe(builder)

expect(applied).not.toBe(builder)
expect(applied['~orpc']).toEqual({
...def,
inputSchema: generalSchema,
})
})

it('.errors', () => {
const errors = { BAD_GATEWAY: { data: outputSchema }, OVERRIDE: { message: 'override' } } as const

Expand Down
14 changes: 14 additions & 0 deletions packages/contract/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ export class ContractBuilder<
})
}

/**
* Sets or overrides the initial input schema.
*
* @see {@link https://orpc.dev/docs/procedure#initial-configuration Initial Procedure Configuration Docs}
*/
$input<U extends AnySchema>(
initialInputSchema?: U,
Comment thread
dinwwwh marked this conversation as resolved.
): ContractBuilder<U, TOutputSchema, TErrorMap, TMeta> {
return new ContractBuilder({
...this['~orpc'],
inputSchema: initialInputSchema,
})
}

/**
* Adds type-safe custom errors to the contract.
* The provided errors are spared-merged with any existing errors in the contract.
Expand Down
Loading