import type { ORPCError } from '@orpc/client' import type { ContractClientFactory } from '@orpc/contract' import { meta, oc, type } from '@orpc/contract' import type { ContractJsonifiedClientFactory } from '@orpc/openapi' import type { PromiseWithError } from '@orpc/shared' import { createContractJsonifiedUtilsFactory, createContractUtilsFactory } from './contract-utils' import type { ProcedureUtils } from './procedure-utils' import type { SharedUtils } from './shared-utils' import type { TanstackQueryOperationContext } from './types' const contract = { ping: oc.meta(meta.path(['ping'])), nested: { pong: oc .meta(meta.path(['nested', 'pong'])) .errors({ BAD_GATEWAY: { data: type(vi.fn()) } }) .input(type(vi.fn())) .output(type(vi.fn())), }, } const clientFactory = {} as ContractClientFactory<{ cache?: boolean }> const jsonifiedClientFactory = {} as ContractJsonifiedClientFactory<{ cache?: boolean }> describe('createContractUtilsFactory', () => { it('infers interceptor input, output, errors types', () => { createContractUtilsFactory(clientFactory, { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & TanstackQueryOperationContext>() const result = next() expectTypeOf(result).toEqualTypeOf>() return result }, ], scoped: { ping: { queryInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() expectTypeOf(context).toEqualTypeOf< { cache?: boolean } & TanstackQueryOperationContext >() const result = next() expectTypeOf(result).toEqualTypeOf< PromiseWithError> >() return result }, ], }, nested: { pong: { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() expectTypeOf(context).toEqualTypeOf< { cache?: boolean } & TanstackQueryOperationContext >() const result = next() expectTypeOf(result).toEqualTypeOf< PromiseWithError> >() return result }, ], }, }, }, }) }) it('return general and procedure utils', () => { const createUtils = createContractUtilsFactory(clientFactory, {}) const utils = createUtils(contract.nested.pong) expectTypeOf(utils).toEqualTypeOf< Omit< ProcedureUtils<{ cache?: boolean }, string, Date, Error | ORPCError<'BAD_GATEWAY', URL>>, 'path' | 'options' > >() }) it('returns router utils when a router contract is passed', () => { const createUtils = createContractUtilsFactory(clientFactory, {}) const utils = createUtils(contract) expectTypeOf(utils.nested.pong).toEqualTypeOf< Omit< ProcedureUtils<{ cache?: boolean }, string, Date, Error | ORPCError<'BAD_GATEWAY', URL>>, 'path' | 'options' > >() expectTypeOf(utils.key).toEqualTypeOf['key']>() }) }) describe('createContractJsonifiedUtilsFactory', () => { it('infers interceptor input, output, errors types', () => { createContractJsonifiedUtilsFactory(jsonifiedClientFactory, { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & TanstackQueryOperationContext>() const result = next() expectTypeOf(result).toEqualTypeOf>() return result }, ], scoped: { ping: { queryInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() expectTypeOf(context).toEqualTypeOf< { cache?: boolean } & TanstackQueryOperationContext >() const result = next() expectTypeOf(result).toEqualTypeOf< PromiseWithError> >() return result }, ], }, nested: { pong: { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() expectTypeOf(context).toEqualTypeOf< { cache?: boolean } & TanstackQueryOperationContext >() const result = next() expectTypeOf(result).toEqualTypeOf< PromiseWithError> >() return result }, ], }, }, }, }) }) it('return jsonified general and procedure utils', () => { const createUtils = createContractJsonifiedUtilsFactory(jsonifiedClientFactory, {}) const utils = createUtils(contract.nested.pong) expectTypeOf(utils).toEqualTypeOf< Omit< ProcedureUtils< { cache?: boolean }, string, string, Error | ORPCError<'BAD_GATEWAY', string> >, 'path' | 'options' > >() }) it('returns jsonified router utils when a router contract is passed', () => { const createUtils = createContractJsonifiedUtilsFactory(jsonifiedClientFactory, {}) const utils = createUtils(contract) expectTypeOf(utils.nested.pong).toEqualTypeOf< Omit< ProcedureUtils< { cache?: boolean }, string, string, Error | ORPCError<'BAD_GATEWAY', string> >, 'path' | 'options' > >() }) })