@orpc/contract 0.0.0-next.e9dc36e → 0.0.0-next.eb37cbe

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/src/router.ts DELETED
@@ -1,55 +0,0 @@
1
- import type { SchemaInput, SchemaOutput } from './types'
2
- import {
3
- type ContractProcedure,
4
- type DecoratedContractProcedure,
5
- isContractProcedure,
6
- type WELL_DEFINED_CONTRACT_PROCEDURE,
7
- } from './procedure'
8
-
9
- export interface ContractRouter {
10
- [k: string]: ContractProcedure<any, any> | ContractRouter
11
- }
12
-
13
- export type HandledContractRouter<TContract extends ContractRouter> = {
14
- [K in keyof TContract]: TContract[K] extends ContractProcedure<
15
- infer UInputSchema,
16
- infer UOutputSchema
17
- >
18
- ? DecoratedContractProcedure<UInputSchema, UOutputSchema>
19
- : TContract[K] extends ContractRouter
20
- ? HandledContractRouter<TContract[K]>
21
- : never
22
- }
23
-
24
- export function eachContractRouterLeaf(
25
- router: ContractRouter,
26
- callback: (item: WELL_DEFINED_CONTRACT_PROCEDURE, path: string[]) => void,
27
- prefix: string[] = [],
28
- ) {
29
- for (const key in router) {
30
- const item = router[key]
31
-
32
- if (isContractProcedure(item)) {
33
- callback(item, [...prefix, key])
34
- }
35
- else {
36
- eachContractRouterLeaf(item as ContractRouter, callback, [...prefix, key])
37
- }
38
- }
39
- }
40
-
41
- export type InferContractRouterInputs<T extends ContractRouter> = {
42
- [K in keyof T]: T[K] extends ContractProcedure<infer UInputSchema, any>
43
- ? SchemaInput<UInputSchema>
44
- : T[K] extends ContractRouter
45
- ? InferContractRouterInputs<T[K]>
46
- : never
47
- }
48
-
49
- export type InferContractRouterOutputs<T extends ContractRouter> = {
50
- [K in keyof T]: T[K] extends ContractProcedure<any, infer UOutputSchema>
51
- ? SchemaOutput<UOutputSchema>
52
- : T[K] extends ContractRouter
53
- ? InferContractRouterOutputs<T[K]>
54
- : never
55
- }
@@ -1,16 +0,0 @@
1
- import type { SchemaInput, SchemaOutput } from './types'
2
- import { z } from 'zod'
3
-
4
- test('SchemaInput', () => {
5
- const schema = z.string()
6
-
7
- expectTypeOf<SchemaInput<undefined>>().toEqualTypeOf<unknown>()
8
- expectTypeOf<SchemaInput<typeof schema>>().toEqualTypeOf<string>()
9
- })
10
-
11
- test('SchemaOutput', () => {
12
- const schema = z.string().transform(v => Number.parseFloat(v))
13
-
14
- expectTypeOf<SchemaOutput<undefined>>().toEqualTypeOf<unknown>()
15
- expectTypeOf<SchemaOutput<typeof schema>>().toEqualTypeOf<number>()
16
- })
package/src/types.ts DELETED
@@ -1,25 +0,0 @@
1
- import type { input, output, ZodType } from 'zod'
2
-
3
- export type HTTPPath = `/${string}`
4
- export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
5
- export type HTTPStatus = number
6
-
7
- export type Schema = ZodType<any, any, any> | undefined
8
-
9
- export type SchemaInput<
10
- TSchema extends Schema,
11
- TFallback = unknown,
12
- > = TSchema extends undefined
13
- ? TFallback
14
- : TSchema extends ZodType<any, any, any>
15
- ? input<TSchema>
16
- : TFallback
17
-
18
- export type SchemaOutput<
19
- TSchema extends Schema,
20
- TFallback = unknown,
21
- > = TSchema extends undefined
22
- ? TFallback
23
- : TSchema extends ZodType<any, any, any>
24
- ? output<TSchema>
25
- : TFallback
package/src/utils.test.ts DELETED
@@ -1,18 +0,0 @@
1
- import { prefixHTTPPath, standardizeHTTPPath } from './utils'
2
-
3
- it('standardizeHTTPPath', () => {
4
- expect(standardizeHTTPPath('/abc')).toBe('/abc')
5
- expect(standardizeHTTPPath('/abc/')).toBe('/abc')
6
- expect(standardizeHTTPPath('/abc//')).toBe('/abc')
7
- expect(standardizeHTTPPath('//abc//')).toBe('/abc')
8
- })
9
-
10
- it('prefixHTTPPath', () => {
11
- expect(prefixHTTPPath('/', '/abc')).toBe('/abc')
12
- expect(prefixHTTPPath('/', '/abc/')).toBe('/abc')
13
- expect(prefixHTTPPath('/', '/abc//')).toBe('/abc')
14
- expect(prefixHTTPPath('/', '//abc//')).toBe('/abc')
15
- expect(prefixHTTPPath('/abc', '/abc')).toBe('/abc/abc')
16
- expect(prefixHTTPPath('/abc', '/abc/')).toBe('/abc/abc')
17
- expect(prefixHTTPPath('/abc', '/abc//')).toBe('/abc/abc')
18
- })
package/src/utils.ts DELETED
@@ -1,17 +0,0 @@
1
- import type { HTTPPath } from './types'
2
-
3
- export function standardizeHTTPPath(path: HTTPPath): HTTPPath {
4
- return `/${path.replace(/\/{2,}/g, '/').replace(/^\/|\/$/g, '')}`
5
- }
6
-
7
- export function prefixHTTPPath(prefix: HTTPPath, path: HTTPPath): HTTPPath {
8
- const prefix_ = standardizeHTTPPath(prefix)
9
- const path_ = standardizeHTTPPath(path)
10
-
11
- if (prefix_ === '/')
12
- return path_
13
- if (path_ === '/')
14
- return prefix_
15
-
16
- return `${prefix_}${path_}`
17
- }