@orpc/contract
Advanced tools
Comparing version 0.0.0-next-20241120100614 to 0.0.0-next-20241123135451
@@ -9,3 +9,4 @@ // src/procedure.ts | ||
static decorate(cp) { | ||
if (cp instanceof _DecoratedContractProcedure) return cp; | ||
if (cp instanceof _DecoratedContractProcedure) | ||
return cp; | ||
return new _DecoratedContractProcedure(cp.zz$cp); | ||
@@ -22,3 +23,4 @@ } | ||
prefix(prefix) { | ||
if (!this.zz$cp.path) return this; | ||
if (!this.zz$cp.path) | ||
return this; | ||
return new _DecoratedContractProcedure({ | ||
@@ -30,3 +32,4 @@ ...this.zz$cp, | ||
addTags(...tags) { | ||
if (!tags.length) return this; | ||
if (!tags.length) | ||
return this; | ||
return new _DecoratedContractProcedure({ | ||
@@ -53,3 +56,4 @@ ...this.zz$cp, | ||
function isContractProcedure(item) { | ||
if (item instanceof ContractProcedure) return true; | ||
if (item instanceof ContractProcedure) | ||
return true; | ||
return (typeof item === "object" || typeof item === "function") && item !== null && "zz$cp" in item && typeof item.zz$cp === "object" && item.zz$cp !== null && "InputSchema" in item.zz$cp && "OutputSchema" in item.zz$cp; | ||
@@ -70,3 +74,4 @@ } | ||
tags(...tags) { | ||
if (!tags.length) return this; | ||
if (!tags.length) | ||
return this; | ||
return new _ContractRouterBuilder({ | ||
@@ -155,4 +160,6 @@ ...this.zz$crb, | ||
const path_ = standardizeHTTPPath(path); | ||
if (prefix_ === "/") return path_; | ||
if (path_ === "/") return prefix_; | ||
if (prefix_ === "/") | ||
return path_; | ||
if (path_ === "/") | ||
return prefix_; | ||
return `${prefix_}${path_}`; | ||
@@ -159,0 +166,0 @@ } |
@@ -0,5 +1,5 @@ | ||
import type { ContractRouter } from './router'; | ||
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types'; | ||
import { DecoratedContractProcedure, type RouteOptions } from './procedure'; | ||
import type { ContractRouter } from './router'; | ||
import { ContractRouterBuilder } from './router-builder'; | ||
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types'; | ||
export declare class ContractBuilder { | ||
@@ -6,0 +6,0 @@ prefix(prefix: HTTPPath): ContractRouterBuilder; |
@@ -0,1 +1,2 @@ | ||
import type { SchemaInput, SchemaOutput } from './types'; | ||
import { type ContractProcedure, type DecoratedContractProcedure, type WELL_DEFINED_CONTRACT_PROCEDURE } from './procedure'; | ||
@@ -9,2 +10,8 @@ export interface ContractRouter { | ||
export declare function eachContractRouterLeaf(router: ContractRouter, callback: (item: WELL_DEFINED_CONTRACT_PROCEDURE, path: string[]) => void, prefix?: string[]): void; | ||
export type InferContractRouterInputs<T extends ContractRouter> = { | ||
[K in keyof T]: T[K] extends ContractProcedure<infer UInputSchema, any> ? SchemaInput<UInputSchema> : T[K] extends ContractRouter ? InferContractRouterInputs<T[K]> : never; | ||
}; | ||
export type InferContractRouterOutputs<T extends ContractRouter> = { | ||
[K in keyof T]: T[K] extends ContractProcedure<any, infer UOutputSchema> ? SchemaOutput<UOutputSchema> : T[K] extends ContractRouter ? InferContractRouterOutputs<T[K]> : never; | ||
}; | ||
//# sourceMappingURL=router.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import type { ZodType, input, output } from 'zod'; | ||
import type { input, output, ZodType } from 'zod'; | ||
export type HTTPPath = `/${string}`; | ||
@@ -3,0 +3,0 @@ export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; |
{ | ||
"name": "@orpc/contract", | ||
"type": "module", | ||
"version": "0.0.0-next-20241120100614", | ||
"version": "0.0.0-next-20241123135451", | ||
"author": { | ||
@@ -38,8 +38,8 @@ "name": "unnoq", | ||
], | ||
"dependencies": { | ||
"@orpc/shared": "0.0.0-next-20241120100614" | ||
}, | ||
"peerDependencies": { | ||
"zod": "^3" | ||
}, | ||
"dependencies": { | ||
"@orpc/shared": "0.0.5" | ||
}, | ||
"scripts": { | ||
@@ -46,0 +46,0 @@ "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'", |
@@ -5,3 +5,3 @@ import { z } from 'zod' | ||
describe('define a procedure', () => { | ||
test('use route method', () => { | ||
it('use route method', () => { | ||
const procedure = oc.route({ | ||
@@ -30,3 +30,3 @@ method: 'GET', | ||
test('use input method', () => { | ||
it('use input method', () => { | ||
const schema = z.object({ | ||
@@ -48,3 +48,3 @@ id: z.string(), | ||
test('use output method', () => { | ||
it('use output method', () => { | ||
const schema = z.object({ id: z.string() }) | ||
@@ -51,0 +51,0 @@ |
@@ -0,5 +1,5 @@ | ||
import type { ContractRouter } from './router' | ||
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types' | ||
import { DecoratedContractProcedure, type RouteOptions } from './procedure' | ||
import type { ContractRouter } from './router' | ||
import { ContractRouterBuilder } from './router-builder' | ||
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types' | ||
@@ -9,3 +9,3 @@ export class ContractBuilder { | ||
return new ContractRouterBuilder({ | ||
prefix: prefix, | ||
prefix, | ||
}) | ||
@@ -16,3 +16,3 @@ } | ||
return new ContractRouterBuilder({ | ||
tags: tags, | ||
tags, | ||
}) | ||
@@ -19,0 +19,0 @@ } |
@@ -0,6 +1,6 @@ | ||
import type { DecoratedContractProcedure } from './procedure' | ||
import { z } from 'zod' | ||
import { isContractProcedure, oc } from '.' | ||
import type { DecoratedContractProcedure } from './procedure' | ||
test('prefix method', () => { | ||
it('prefix method', () => { | ||
const os = oc | ||
@@ -20,3 +20,3 @@ const p1 = os.route({ | ||
test('route method', () => { | ||
it('route method', () => { | ||
const p = oc | ||
@@ -49,3 +49,3 @@ .route({ | ||
test('input method', () => { | ||
it('input method', () => { | ||
const schema = z.string() | ||
@@ -64,3 +64,3 @@ const p = oc.route({}).input(schema) | ||
test('output method', () => { | ||
it('output method', () => { | ||
const schema = z.string() | ||
@@ -94,3 +94,3 @@ const p = oc.route({}).output(schema) | ||
test('isContractProcedure function', () => { | ||
it('isContractProcedure function', () => { | ||
expect(isContractProcedure(oc)).toBe(false) | ||
@@ -97,0 +97,0 @@ expect(isContractProcedure(oc.router({}))).toBe(false) |
@@ -45,3 +45,4 @@ import type { | ||
): DecoratedContractProcedure<TInputSchema, TOutputSchema> { | ||
if (cp instanceof DecoratedContractProcedure) return cp | ||
if (cp instanceof DecoratedContractProcedure) | ||
return cp | ||
return new DecoratedContractProcedure(cp.zz$cp) | ||
@@ -64,3 +65,4 @@ } | ||
): DecoratedContractProcedure<TInputSchema, TOutputSchema> { | ||
if (!this.zz$cp.path) return this | ||
if (!this.zz$cp.path) | ||
return this | ||
@@ -76,3 +78,4 @@ return new DecoratedContractProcedure({ | ||
): DecoratedContractProcedure<TInputSchema, TOutputSchema> { | ||
if (!tags.length) return this | ||
if (!tags.length) | ||
return this | ||
@@ -113,13 +116,14 @@ return new DecoratedContractProcedure({ | ||
): item is WELL_DEFINED_CONTRACT_PROCEDURE { | ||
if (item instanceof ContractProcedure) return true | ||
if (item instanceof ContractProcedure) | ||
return true | ||
return ( | ||
(typeof item === 'object' || typeof item === 'function') && | ||
item !== null && | ||
'zz$cp' in item && | ||
typeof item.zz$cp === 'object' && | ||
item.zz$cp !== null && | ||
'InputSchema' in item.zz$cp && | ||
'OutputSchema' in item.zz$cp | ||
(typeof item === 'object' || typeof item === 'function') | ||
&& item !== null | ||
&& 'zz$cp' in item | ||
&& typeof item.zz$cp === 'object' | ||
&& item.zz$cp !== null | ||
&& 'InputSchema' in item.zz$cp | ||
&& 'OutputSchema' in item.zz$cp | ||
) | ||
} |
@@ -5,11 +5,11 @@ import { z } from 'zod' | ||
test('prefix method', () => { | ||
it('prefix method', () => { | ||
expect(oc.prefix('/1').prefix('/2').zz$crb.prefix).toEqual('/1/2') | ||
}) | ||
test('tags method', () => { | ||
it('tags method', () => { | ||
expect(oc.tags('1').tags('2').zz$crb.tags).toEqual(['1', '2']) | ||
}) | ||
test('define a router', () => { | ||
it('define a router', () => { | ||
const ping = oc.route({ method: 'GET', path: '/ping' }) | ||
@@ -26,7 +26,7 @@ const pong = oc.input(z.object({ id: z.string() })) | ||
.router({ | ||
ping: ping, | ||
pong: pong, | ||
ping, | ||
pong, | ||
nested: { | ||
ping: ping, | ||
ping, | ||
}, | ||
@@ -33,0 +33,0 @@ }), |
@@ -1,7 +0,7 @@ | ||
import { DecoratedContractProcedure, isContractProcedure } from './procedure' | ||
import type { ContractRouter, HandledContractRouter } from './router' | ||
import type { HTTPPath } from './types' | ||
import { DecoratedContractProcedure, isContractProcedure } from './procedure' | ||
export class ContractRouterBuilder { | ||
constructor(public zz$crb: { prefix?: HTTPPath; tags?: string[] }) {} | ||
constructor(public zz$crb: { prefix?: HTTPPath, tags?: string[] }) {} | ||
@@ -16,3 +16,4 @@ prefix(prefix: HTTPPath): ContractRouterBuilder { | ||
tags(...tags: string[]): ContractRouterBuilder { | ||
if (!tags.length) return this | ||
if (!tags.length) | ||
return this | ||
@@ -38,3 +39,4 @@ return new ContractRouterBuilder({ | ||
: decorated | ||
} else { | ||
} | ||
else { | ||
handled[key] = this.router(item as ContractRouter) | ||
@@ -41,0 +43,0 @@ } |
import { eachContractRouterLeaf, oc } from '.' | ||
test('each router leaf', () => { | ||
it('each router leaf', () => { | ||
const router = { | ||
@@ -5,0 +5,0 @@ ping: oc.route({ |
@@ -0,6 +1,7 @@ | ||
import type { SchemaInput, SchemaOutput } from './types' | ||
import { | ||
type ContractProcedure, | ||
type DecoratedContractProcedure, | ||
isContractProcedure, | ||
type WELL_DEFINED_CONTRACT_PROCEDURE, | ||
isContractProcedure, | ||
} from './procedure' | ||
@@ -33,3 +34,4 @@ | ||
callback(item, [...prefix, key]) | ||
} else { | ||
} | ||
else { | ||
eachContractRouterLeaf(item as ContractRouter, callback, [...prefix, key]) | ||
@@ -39,1 +41,17 @@ } | ||
} | ||
export type InferContractRouterInputs<T extends ContractRouter> = { | ||
[K in keyof T]: T[K] extends ContractProcedure<infer UInputSchema, any> | ||
? SchemaInput<UInputSchema> | ||
: T[K] extends ContractRouter | ||
? InferContractRouterInputs<T[K]> | ||
: never | ||
} | ||
export type InferContractRouterOutputs<T extends ContractRouter> = { | ||
[K in keyof T]: T[K] extends ContractProcedure<any, infer UOutputSchema> | ||
? SchemaOutput<UOutputSchema> | ||
: T[K] extends ContractRouter | ||
? InferContractRouterOutputs<T[K]> | ||
: never | ||
} |
@@ -0,3 +1,3 @@ | ||
import type { SchemaInput, SchemaOutput } from './types' | ||
import { z } from 'zod' | ||
import type { SchemaInput, SchemaOutput } from './types' | ||
@@ -12,3 +12,3 @@ test('SchemaInput', () => { | ||
test('SchemaOutput', () => { | ||
const schema = z.string().transform((v) => Number.parseFloat(v)) | ||
const schema = z.string().transform(v => Number.parseFloat(v)) | ||
@@ -15,0 +15,0 @@ expectTypeOf<SchemaOutput<undefined>>().toEqualTypeOf<unknown>() |
@@ -1,2 +0,2 @@ | ||
import type { ZodType, input, output } from 'zod' | ||
import type { input, output, ZodType } from 'zod' | ||
@@ -3,0 +3,0 @@ export type HTTPPath = `/${string}` |
import { prefixHTTPPath, standardizeHTTPPath } from './utils' | ||
test('standardizeHTTPPath', () => { | ||
it('standardizeHTTPPath', () => { | ||
expect(standardizeHTTPPath('/abc')).toBe('/abc') | ||
@@ -10,3 +10,3 @@ expect(standardizeHTTPPath('/abc/')).toBe('/abc') | ||
test('prefixHTTPPath', () => { | ||
it('prefixHTTPPath', () => { | ||
expect(prefixHTTPPath('/', '/abc')).toBe('/abc') | ||
@@ -13,0 +13,0 @@ expect(prefixHTTPPath('/', '/abc/')).toBe('/abc') |
@@ -11,6 +11,8 @@ import type { HTTPPath } from './types' | ||
if (prefix_ === '/') return path_ | ||
if (path_ === '/') return prefix_ | ||
if (prefix_ === '/') | ||
return path_ | ||
if (path_ === '/') | ||
return prefix_ | ||
return `${prefix_}${path_}` | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
68276
36
963
+ Added@orpc/shared@0.0.5(transitive)
- Removed@orpc/shared@0.0.0-next-20241120100614(transitive)
Updated@orpc/shared@0.0.5