@orpc/contract
Advanced tools
Comparing version 0.16.0 to 0.17.0
// src/procedure.ts | ||
var ContractProcedure = class { | ||
constructor(zz$cp) { | ||
this.zz$cp = zz$cp; | ||
"~type" = "ContractProcedure"; | ||
"~orpc"; | ||
constructor(def) { | ||
this["~orpc"] = def; | ||
} | ||
}; | ||
function isContractProcedure(item) { | ||
if (item instanceof ContractProcedure) { | ||
return true; | ||
} | ||
return (typeof item === "object" || typeof item === "function") && item !== null && "~type" in item && item["~type"] === "ContractProcedure" && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "InputSchema" in item["~orpc"] && "OutputSchema" in item["~orpc"]; | ||
} | ||
// src/procedure-decorated.ts | ||
var DecoratedContractProcedure = class _DecoratedContractProcedure extends ContractProcedure { | ||
static decorate(cp) { | ||
if (cp instanceof _DecoratedContractProcedure) | ||
return cp; | ||
return new _DecoratedContractProcedure(cp.zz$cp); | ||
static decorate(procedure) { | ||
if (procedure instanceof _DecoratedContractProcedure) { | ||
return procedure; | ||
} | ||
return new _DecoratedContractProcedure(procedure["~orpc"]); | ||
} | ||
route(opts) { | ||
route(route) { | ||
return new _DecoratedContractProcedure({ | ||
...this.zz$cp, | ||
...opts, | ||
method: opts.method, | ||
path: opts.path | ||
...this["~orpc"], | ||
route | ||
}); | ||
} | ||
prefix(prefix) { | ||
if (!this.zz$cp.path) | ||
return this; | ||
return new _DecoratedContractProcedure({ | ||
...this.zz$cp, | ||
path: `${prefix}${this.zz$cp.path}` | ||
...this["~orpc"], | ||
...this["~orpc"].route?.path ? { | ||
route: { | ||
...this["~orpc"].route, | ||
path: `${prefix}${this["~orpc"].route.path}` | ||
} | ||
} : void 0 | ||
}); | ||
} | ||
addTags(...tags) { | ||
if (!tags.length) | ||
return this; | ||
unshiftTag(...tags) { | ||
return new _DecoratedContractProcedure({ | ||
...this.zz$cp, | ||
tags: [...this.zz$cp.tags ?? [], ...tags] | ||
...this["~orpc"], | ||
route: { | ||
...this["~orpc"].route, | ||
tags: [ | ||
...tags, | ||
...this["~orpc"].route?.tags?.filter((tag) => !tags.includes(tag)) ?? [] | ||
] | ||
} | ||
}); | ||
@@ -39,3 +55,3 @@ } | ||
return new _DecoratedContractProcedure({ | ||
...this.zz$cp, | ||
...this["~orpc"], | ||
InputSchema: schema, | ||
@@ -47,3 +63,3 @@ inputExample: example | ||
return new _DecoratedContractProcedure({ | ||
...this.zz$cp, | ||
...this["~orpc"], | ||
OutputSchema: schema, | ||
@@ -54,44 +70,38 @@ outputExample: example | ||
}; | ||
function isContractProcedure(item) { | ||
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; | ||
} | ||
// src/router-builder.ts | ||
var ContractRouterBuilder = class _ContractRouterBuilder { | ||
constructor(zz$crb) { | ||
this.zz$crb = zz$crb; | ||
if (zz$crb.prefix && zz$crb.prefix.includes("{")) { | ||
throw new Error('Prefix cannot contain "{" for dynamic routing'); | ||
} | ||
"~type" = "ContractProcedure"; | ||
"~orpc"; | ||
constructor(def) { | ||
this["~orpc"] = def; | ||
} | ||
prefix(prefix) { | ||
return new _ContractRouterBuilder({ | ||
...this.zz$crb, | ||
prefix: `${this.zz$crb.prefix ?? ""}${prefix}` | ||
...this["~orpc"], | ||
prefix: `${this["~orpc"].prefix ?? ""}${prefix}` | ||
}); | ||
} | ||
tags(...tags) { | ||
if (!tags.length) | ||
return this; | ||
tag(...tags) { | ||
return new _ContractRouterBuilder({ | ||
...this.zz$crb, | ||
tags: [...this.zz$crb.tags ?? [], ...tags] | ||
...this["~orpc"], | ||
tags: [...this["~orpc"].tags ?? [], ...tags] | ||
}); | ||
} | ||
router(router) { | ||
const handled = {}; | ||
for (const key in router) { | ||
const item = router[key]; | ||
if (isContractProcedure(item)) { | ||
const decorated = DecoratedContractProcedure.decorate(item).addTags( | ||
...this.zz$crb.tags ?? [] | ||
); | ||
handled[key] = this.zz$crb.prefix ? decorated.prefix(this.zz$crb.prefix) : decorated; | ||
} else { | ||
handled[key] = this.router(item); | ||
if (isContractProcedure(router)) { | ||
let decorated = DecoratedContractProcedure.decorate(router); | ||
if (this["~orpc"].tags) { | ||
decorated = decorated.unshiftTag(...this["~orpc"].tags); | ||
} | ||
if (this["~orpc"].prefix) { | ||
decorated = decorated.prefix(this["~orpc"].prefix); | ||
} | ||
return decorated; | ||
} | ||
return handled; | ||
const adapted = {}; | ||
for (const key in router) { | ||
adapted[key] = this.router(router[key]); | ||
} | ||
return adapted; | ||
} | ||
@@ -107,3 +117,3 @@ }; | ||
} | ||
tags(...tags) { | ||
tag(...tags) { | ||
return new ContractRouterBuilder({ | ||
@@ -113,7 +123,7 @@ tags | ||
} | ||
route(opts) { | ||
route(route) { | ||
return new DecoratedContractProcedure({ | ||
route, | ||
InputSchema: void 0, | ||
OutputSchema: void 0, | ||
...opts | ||
OutputSchema: void 0 | ||
}); | ||
@@ -130,5 +140,5 @@ } | ||
return new DecoratedContractProcedure({ | ||
InputSchema: void 0, | ||
OutputSchema: schema, | ||
outputExample: example | ||
outputExample: example, | ||
InputSchema: void 0 | ||
}); | ||
@@ -141,32 +151,2 @@ } | ||
// src/constants.ts | ||
var ORPC_HEADER = "x-orpc-transformer"; | ||
var ORPC_HEADER_VALUE = "t"; | ||
// src/router.ts | ||
function eachContractRouterLeaf(router, callback, prefix = []) { | ||
for (const key in router) { | ||
const item = router[key]; | ||
if (isContractProcedure(item)) { | ||
callback(item, [...prefix, key]); | ||
} else { | ||
eachContractRouterLeaf(item, callback, [...prefix, key]); | ||
} | ||
} | ||
} | ||
// src/utils.ts | ||
function standardizeHTTPPath(path) { | ||
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`; | ||
} | ||
function prefixHTTPPath(prefix, path) { | ||
const prefix_ = standardizeHTTPPath(prefix); | ||
const path_ = standardizeHTTPPath(path); | ||
if (prefix_ === "/") | ||
return path_; | ||
if (path_ === "/") | ||
return prefix_; | ||
return `${prefix_}${path_}`; | ||
} | ||
// src/index.ts | ||
@@ -177,11 +157,7 @@ var oc = new ContractBuilder(); | ||
ContractProcedure, | ||
ContractRouterBuilder, | ||
DecoratedContractProcedure, | ||
ORPC_HEADER, | ||
ORPC_HEADER_VALUE, | ||
eachContractRouterLeaf, | ||
isContractProcedure, | ||
oc, | ||
prefixHTTPPath, | ||
standardizeHTTPPath | ||
oc | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -0,13 +1,14 @@ | ||
import type { RouteOptions } from './procedure'; | ||
import type { ContractRouter } from './router'; | ||
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types'; | ||
import { DecoratedContractProcedure, type RouteOptions } from './procedure'; | ||
import { DecoratedContractProcedure } from './procedure-decorated'; | ||
import { ContractRouterBuilder } from './router-builder'; | ||
export declare class ContractBuilder { | ||
prefix(prefix: HTTPPath): ContractRouterBuilder; | ||
tags(...tags: string[]): ContractRouterBuilder; | ||
route(opts: RouteOptions): DecoratedContractProcedure<undefined, undefined>; | ||
input<USchema extends Schema>(schema: USchema, example?: SchemaInput<USchema>): DecoratedContractProcedure<USchema, undefined>; | ||
output<USchema extends Schema>(schema: USchema, example?: SchemaOutput<USchema>): DecoratedContractProcedure<undefined, USchema>; | ||
tag(...tags: string[]): ContractRouterBuilder; | ||
route(route: RouteOptions): DecoratedContractProcedure<undefined, undefined>; | ||
input<U extends Schema = undefined>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, undefined>; | ||
output<U extends Schema = undefined>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<undefined, U>; | ||
router<T extends ContractRouter>(router: T): T; | ||
} | ||
//# sourceMappingURL=builder.d.ts.map |
/** unnoq */ | ||
import { ContractBuilder } from './builder'; | ||
export * from './builder'; | ||
export * from './constants'; | ||
export * from './procedure'; | ||
export * from './procedure-decorated'; | ||
export * from './router'; | ||
export * from './router-builder'; | ||
export * from './types'; | ||
export * from './utils'; | ||
export declare const oc: ContractBuilder; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import type { HTTPMethod, HTTPPath, Schema, SchemaInput, SchemaOutput } from './types'; | ||
import type { HTTPMethod, HTTPPath, Schema, SchemaOutput } from './types'; | ||
export interface RouteOptions { | ||
@@ -8,41 +8,19 @@ method?: HTTPMethod; | ||
deprecated?: boolean; | ||
tags?: string[]; | ||
tags?: readonly string[]; | ||
} | ||
export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema> { | ||
route?: RouteOptions; | ||
InputSchema: TInputSchema; | ||
inputExample?: SchemaOutput<TInputSchema>; | ||
OutputSchema: TOutputSchema; | ||
outputExample?: SchemaOutput<TOutputSchema>; | ||
} | ||
export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema> { | ||
zz$cp: { | ||
path?: HTTPPath; | ||
method?: HTTPMethod; | ||
summary?: string; | ||
description?: string; | ||
deprecated?: boolean; | ||
tags?: string[]; | ||
InputSchema: TInputSchema; | ||
inputExample?: SchemaOutput<TInputSchema>; | ||
OutputSchema: TOutputSchema; | ||
outputExample?: SchemaOutput<TOutputSchema>; | ||
}; | ||
constructor(zz$cp: { | ||
path?: HTTPPath; | ||
method?: HTTPMethod; | ||
summary?: string; | ||
description?: string; | ||
deprecated?: boolean; | ||
tags?: string[]; | ||
InputSchema: TInputSchema; | ||
inputExample?: SchemaOutput<TInputSchema>; | ||
OutputSchema: TOutputSchema; | ||
outputExample?: SchemaOutput<TOutputSchema>; | ||
}); | ||
'~type': "ContractProcedure"; | ||
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema>; | ||
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema>); | ||
} | ||
export type ANY_CONTRACT_PROCEDURE = ContractProcedure<any, any>; | ||
export type WELL_CONTRACT_PROCEDURE = ContractProcedure<Schema, Schema>; | ||
export declare class DecoratedContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema> extends ContractProcedure<TInputSchema, TOutputSchema> { | ||
static decorate<TInputSchema extends Schema, TOutputSchema extends Schema>(cp: ContractProcedure<TInputSchema, TOutputSchema>): DecoratedContractProcedure<TInputSchema, TOutputSchema>; | ||
route(opts: RouteOptions): DecoratedContractProcedure<TInputSchema, TOutputSchema>; | ||
prefix(prefix: HTTPPath): DecoratedContractProcedure<TInputSchema, TOutputSchema>; | ||
addTags(...tags: string[]): DecoratedContractProcedure<TInputSchema, TOutputSchema>; | ||
input<USchema extends Schema>(schema: USchema, example?: SchemaInput<USchema>): DecoratedContractProcedure<USchema, TOutputSchema>; | ||
output<USchema extends Schema>(schema: USchema, example?: SchemaOutput<USchema>): DecoratedContractProcedure<TInputSchema, USchema>; | ||
} | ||
export type WELL_DEFINED_CONTRACT_PROCEDURE = ContractProcedure<Schema, Schema>; | ||
export declare function isContractProcedure(item: unknown): item is WELL_DEFINED_CONTRACT_PROCEDURE; | ||
export declare function isContractProcedure(item: unknown): item is ANY_CONTRACT_PROCEDURE; | ||
//# sourceMappingURL=procedure.d.ts.map |
@@ -1,16 +0,20 @@ | ||
import type { ContractRouter, HandledContractRouter } from './router'; | ||
import type { ContractProcedure } from './procedure'; | ||
import type { ContractRouter } from './router'; | ||
import type { HTTPPath } from './types'; | ||
import { DecoratedContractProcedure } from './procedure-decorated'; | ||
export type AdaptedContractRouter<TContract extends ContractRouter> = { | ||
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> ? DecoratedContractProcedure<UInputSchema, UOutputSchema> : TContract[K] extends ContractRouter ? AdaptedContractRouter<TContract[K]> : never; | ||
}; | ||
export interface ContractRouterBuilderDef { | ||
prefix?: HTTPPath; | ||
tags?: string[]; | ||
} | ||
export declare class ContractRouterBuilder { | ||
zz$crb: { | ||
prefix?: HTTPPath; | ||
tags?: string[]; | ||
}; | ||
constructor(zz$crb: { | ||
prefix?: HTTPPath; | ||
tags?: string[]; | ||
}); | ||
'~type': "ContractProcedure"; | ||
'~orpc': ContractRouterBuilderDef; | ||
constructor(def: ContractRouterBuilderDef); | ||
prefix(prefix: HTTPPath): ContractRouterBuilder; | ||
tags(...tags: string[]): ContractRouterBuilder; | ||
router<T extends ContractRouter>(router: T): HandledContractRouter<T>; | ||
tag(...tags: string[]): ContractRouterBuilder; | ||
router<T extends ContractRouter>(router: T): AdaptedContractRouter<T>; | ||
} | ||
//# sourceMappingURL=router-builder.d.ts.map |
@@ -0,16 +1,12 @@ | ||
import type { ANY_CONTRACT_PROCEDURE, ContractProcedure } from './procedure'; | ||
import type { SchemaInput, SchemaOutput } from './types'; | ||
import { type ContractProcedure, type DecoratedContractProcedure, type WELL_DEFINED_CONTRACT_PROCEDURE } from './procedure'; | ||
export interface ContractRouter { | ||
[k: string]: ContractProcedure<any, any> | ContractRouter; | ||
} | ||
export type HandledContractRouter<TContract extends ContractRouter> = { | ||
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> ? DecoratedContractProcedure<UInputSchema, UOutputSchema> : TContract[K] extends ContractRouter ? HandledContractRouter<TContract[K]> : never; | ||
export type ContractRouter = ANY_CONTRACT_PROCEDURE | { | ||
[k: string]: 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 InferContractRouterInputs<T extends ContractRouter> = T extends ContractProcedure<infer UInputSchema, any> ? SchemaInput<UInputSchema> : { | ||
[K in keyof T]: 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; | ||
export type InferContractRouterOutputs<T extends ContractRouter> = T extends ContractProcedure<any, infer UOutputSchema> ? SchemaOutput<UOutputSchema> : { | ||
[K in keyof T]: T[K] extends ContractRouter ? InferContractRouterOutputs<T[K]> : never; | ||
}; | ||
//# sourceMappingURL=router.d.ts.map |
import type { StandardSchemaV1 } from '@standard-schema/spec'; | ||
export type HTTPPath = `/${string}`; | ||
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; | ||
export type HTTPStatus = number; | ||
export type Schema = StandardSchemaV1 | undefined; | ||
@@ -6,0 +5,0 @@ export type SchemaInput<TSchema extends Schema, TFallback = unknown> = TSchema extends undefined ? TFallback : TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : TFallback; |
{ | ||
"name": "@orpc/contract", | ||
"type": "module", | ||
"version": "0.16.0", | ||
"version": "0.17.0", | ||
"license": "MIT", | ||
@@ -33,4 +33,9 @@ "homepage": "https://orpc.unnoq.com", | ||
"@standard-schema/spec": "1.0.0-beta.4", | ||
"@orpc/shared": "0.16.0" | ||
"@orpc/shared": "0.17.0" | ||
}, | ||
"devDependencies": { | ||
"arktype": "2.0.0-rc.26", | ||
"valibot": "1.0.0-beta.9", | ||
"zod": "3.24.1" | ||
}, | ||
"scripts": { | ||
@@ -37,0 +42,0 @@ "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11655
3
10
243
1
+ Added@orpc/shared@0.17.0(transitive)
- Removed@orpc/shared@0.16.0(transitive)
Updated@orpc/shared@0.17.0