@orpc/contract
Advanced tools
Comparing version 0.0.0-next.c6c659d to 0.0.0-next.ca29a36
@@ -0,4 +1,20 @@ | ||
// src/error.ts | ||
var ValidationError = class extends Error { | ||
issues; | ||
constructor(options) { | ||
super(options.message, options); | ||
this.issues = options.issues; | ||
} | ||
}; | ||
function mergeErrorMap(errorMap1, errorMap2) { | ||
return { ...errorMap1, ...errorMap2 }; | ||
} | ||
// src/meta.ts | ||
function mergeMeta(meta1, meta2) { | ||
return { ...meta1, ...meta2 }; | ||
} | ||
// src/procedure.ts | ||
var ContractProcedure = class { | ||
"~type" = "ContractProcedure"; | ||
"~orpc"; | ||
@@ -9,3 +25,3 @@ constructor(def) { | ||
} | ||
if (Object.values(def.errorMap ?? {}).some((val) => val && val.status && (val.status < 400 || val.status > 599))) { | ||
if (Object.values(def.errorMap).some((val) => val && val.status && (val.status < 400 || val.status > 599))) { | ||
throw new Error("[ContractProcedure] The error status code must be in the 400-599 range."); | ||
@@ -20,308 +36,137 @@ } | ||
} | ||
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"] && "errorMap" in item["~orpc"]; | ||
return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "inputSchema" in item["~orpc"] && "outputSchema" in item["~orpc"] && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"]; | ||
} | ||
// src/procedure-decorated.ts | ||
var DecoratedContractProcedure = class _DecoratedContractProcedure extends ContractProcedure { | ||
static decorate(procedure) { | ||
if (procedure instanceof _DecoratedContractProcedure) { | ||
return procedure; | ||
} | ||
return new _DecoratedContractProcedure(procedure["~orpc"]); | ||
// src/route.ts | ||
function mergeRoute(a, b) { | ||
return { ...a, ...b }; | ||
} | ||
function prefixRoute(route, prefix) { | ||
if (!route.path) { | ||
return route; | ||
} | ||
route(route) { | ||
return new _DecoratedContractProcedure({ | ||
...this["~orpc"], | ||
route: { | ||
...this["~orpc"].route, | ||
...route | ||
} | ||
return { | ||
...route, | ||
path: `${prefix}${route.path}` | ||
}; | ||
} | ||
function unshiftTagRoute(route, tags) { | ||
return { | ||
...route, | ||
tags: [...tags, ...route.tags ?? []] | ||
}; | ||
} | ||
function mergePrefix(a, b) { | ||
return a ? `${a}${b}` : b; | ||
} | ||
function mergeTags(a, b) { | ||
return a ? [...a, ...b] : b; | ||
} | ||
function adaptRoute(route, options) { | ||
let router = route; | ||
if (options.prefix) { | ||
router = prefixRoute(router, options.prefix); | ||
} | ||
if (options.tags) { | ||
router = unshiftTagRoute(router, options.tags); | ||
} | ||
return router; | ||
} | ||
// src/router.ts | ||
function adaptContractRouter(contract, options) { | ||
if (isContractProcedure(contract)) { | ||
const adapted2 = new ContractProcedure({ | ||
...contract["~orpc"], | ||
errorMap: mergeErrorMap(options.errorMap, contract["~orpc"].errorMap), | ||
route: adaptRoute(contract["~orpc"].route, options) | ||
}); | ||
return adapted2; | ||
} | ||
prefix(prefix) { | ||
return new _DecoratedContractProcedure({ | ||
const adapted = {}; | ||
for (const key in contract) { | ||
adapted[key] = adaptContractRouter(contract[key], options); | ||
} | ||
return adapted; | ||
} | ||
// src/builder.ts | ||
var ContractBuilder = class _ContractBuilder extends ContractProcedure { | ||
constructor(def) { | ||
super(def); | ||
this["~orpc"].prefix = def.prefix; | ||
this["~orpc"].tags = def.tags; | ||
} | ||
/** | ||
* Reset initial meta | ||
*/ | ||
$meta(initialMeta) { | ||
return new _ContractBuilder({ | ||
...this["~orpc"], | ||
...this["~orpc"].route?.path ? { | ||
route: { | ||
...this["~orpc"].route, | ||
path: `${prefix}${this["~orpc"].route.path}` | ||
} | ||
} : void 0 | ||
meta: initialMeta | ||
}); | ||
} | ||
unshiftTag(...tags) { | ||
return new _DecoratedContractProcedure({ | ||
/** | ||
* Reset initial route | ||
*/ | ||
$route(initialRoute) { | ||
return new _ContractBuilder({ | ||
...this["~orpc"], | ||
route: { | ||
...this["~orpc"].route, | ||
tags: [ | ||
...tags, | ||
...this["~orpc"].route?.tags?.filter((tag) => !tags.includes(tag)) ?? [] | ||
] | ||
} | ||
route: initialRoute | ||
}); | ||
} | ||
input(schema, example) { | ||
return new _DecoratedContractProcedure({ | ||
errors(errors) { | ||
return new _ContractBuilder({ | ||
...this["~orpc"], | ||
InputSchema: schema, | ||
inputExample: example | ||
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors) | ||
}); | ||
} | ||
output(schema, example) { | ||
return new _DecoratedContractProcedure({ | ||
meta(meta) { | ||
return new _ContractBuilder({ | ||
...this["~orpc"], | ||
OutputSchema: schema, | ||
outputExample: example | ||
meta: mergeMeta(this["~orpc"].meta, meta) | ||
}); | ||
} | ||
errors(errorMap) { | ||
return new _DecoratedContractProcedure({ | ||
route(route) { | ||
return new _ContractBuilder({ | ||
...this["~orpc"], | ||
errorMap | ||
route: mergeRoute(this["~orpc"].route, route) | ||
}); | ||
} | ||
}; | ||
// src/router-builder.ts | ||
var ContractRouterBuilder = class _ContractRouterBuilder { | ||
"~type" = "ContractProcedure"; | ||
"~orpc"; | ||
constructor(def) { | ||
this["~orpc"] = def; | ||
} | ||
prefix(prefix) { | ||
return new _ContractRouterBuilder({ | ||
input(schema) { | ||
return new _ContractBuilder({ | ||
...this["~orpc"], | ||
prefix: `${this["~orpc"].prefix ?? ""}${prefix}` | ||
inputSchema: schema | ||
}); | ||
} | ||
tag(...tags) { | ||
return new _ContractRouterBuilder({ | ||
output(schema) { | ||
return new _ContractBuilder({ | ||
...this["~orpc"], | ||
tags: [...this["~orpc"].tags ?? [], ...tags] | ||
outputSchema: schema | ||
}); | ||
} | ||
router(router) { | ||
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; | ||
} | ||
const adapted = {}; | ||
for (const key in router) { | ||
adapted[key] = this.router(router[key]); | ||
} | ||
return adapted; | ||
} | ||
}; | ||
// src/builder.ts | ||
var ContractBuilder = class { | ||
prefix(prefix) { | ||
return new ContractRouterBuilder({ | ||
prefix | ||
return new _ContractBuilder({ | ||
...this["~orpc"], | ||
prefix: mergePrefix(this["~orpc"].prefix, prefix) | ||
}); | ||
} | ||
tag(...tags) { | ||
return new ContractRouterBuilder({ | ||
tags | ||
return new _ContractBuilder({ | ||
...this["~orpc"], | ||
tags: mergeTags(this["~orpc"].tags, tags) | ||
}); | ||
} | ||
route(route) { | ||
return new DecoratedContractProcedure({ | ||
route, | ||
InputSchema: void 0, | ||
OutputSchema: void 0, | ||
errorMap: void 0 | ||
}); | ||
} | ||
input(schema, example) { | ||
return new DecoratedContractProcedure({ | ||
InputSchema: schema, | ||
inputExample: example, | ||
OutputSchema: void 0, | ||
errorMap: void 0 | ||
}); | ||
} | ||
output(schema, example) { | ||
return new DecoratedContractProcedure({ | ||
OutputSchema: schema, | ||
outputExample: example, | ||
InputSchema: void 0, | ||
errorMap: void 0 | ||
}); | ||
} | ||
errors(errorMap) { | ||
return new DecoratedContractProcedure({ | ||
InputSchema: void 0, | ||
OutputSchema: void 0, | ||
errorMap | ||
}); | ||
} | ||
router(router) { | ||
return router; | ||
return adaptContractRouter(router, this["~orpc"]); | ||
} | ||
}; | ||
var oc = new ContractBuilder({ | ||
errorMap: {}, | ||
inputSchema: void 0, | ||
outputSchema: void 0, | ||
route: {}, | ||
meta: {} | ||
}); | ||
// src/error-orpc.ts | ||
import { isPlainObject } from "@orpc/shared"; | ||
var COMMON_ORPC_ERROR_DEFS = { | ||
BAD_REQUEST: { | ||
status: 400, | ||
message: "Bad Request" | ||
}, | ||
UNAUTHORIZED: { | ||
status: 401, | ||
message: "Unauthorized" | ||
}, | ||
FORBIDDEN: { | ||
status: 403, | ||
message: "Forbidden" | ||
}, | ||
NOT_FOUND: { | ||
status: 404, | ||
message: "Not Found" | ||
}, | ||
METHOD_NOT_SUPPORTED: { | ||
status: 405, | ||
message: "Method Not Supported" | ||
}, | ||
NOT_ACCEPTABLE: { | ||
status: 406, | ||
message: "Not Acceptable" | ||
}, | ||
TIMEOUT: { | ||
status: 408, | ||
message: "Request Timeout" | ||
}, | ||
CONFLICT: { | ||
status: 409, | ||
message: "Conflict" | ||
}, | ||
PRECONDITION_FAILED: { | ||
status: 412, | ||
message: "Precondition Failed" | ||
}, | ||
PAYLOAD_TOO_LARGE: { | ||
status: 413, | ||
message: "Payload Too Large" | ||
}, | ||
UNSUPPORTED_MEDIA_TYPE: { | ||
status: 415, | ||
message: "Unsupported Media Type" | ||
}, | ||
UNPROCESSABLE_CONTENT: { | ||
status: 422, | ||
message: "Unprocessable Content" | ||
}, | ||
TOO_MANY_REQUESTS: { | ||
status: 429, | ||
message: "Too Many Requests" | ||
}, | ||
CLIENT_CLOSED_REQUEST: { | ||
status: 499, | ||
message: "Client Closed Request" | ||
}, | ||
INTERNAL_SERVER_ERROR: { | ||
status: 500, | ||
message: "Internal Server Error" | ||
}, | ||
NOT_IMPLEMENTED: { | ||
status: 501, | ||
message: "Not Implemented" | ||
}, | ||
BAD_GATEWAY: { | ||
status: 502, | ||
message: "Bad Gateway" | ||
}, | ||
SERVICE_UNAVAILABLE: { | ||
status: 503, | ||
message: "Service Unavailable" | ||
}, | ||
GATEWAY_TIMEOUT: { | ||
status: 504, | ||
message: "Gateway Timeout" | ||
} | ||
}; | ||
function fallbackORPCErrorStatus(code, status) { | ||
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500; | ||
} | ||
function fallbackORPCErrorMessage(code, message) { | ||
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code; | ||
} | ||
var ORPCError = class extends Error { | ||
defined; | ||
code; | ||
status; | ||
data; | ||
constructor(options) { | ||
if (options.status && (options.status < 400 || options.status >= 600)) { | ||
throw new Error("[ORPCError] The error status code must be in the 400-599 range."); | ||
} | ||
const message = fallbackORPCErrorMessage(options.code, options.message); | ||
super(message, options); | ||
this.code = options.code; | ||
this.status = fallbackORPCErrorStatus(options.code, options.status); | ||
this.defined = options.defined ?? false; | ||
this.data = options.data; | ||
} | ||
toJSON() { | ||
return { | ||
defined: this.defined, | ||
code: this.code, | ||
status: this.status, | ||
message: this.message, | ||
data: this.data | ||
}; | ||
} | ||
static isValidJSON(json) { | ||
return isPlainObject(json) && "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string"; | ||
} | ||
}; | ||
function isDefinedError(error) { | ||
return error instanceof ORPCError && error.defined; | ||
} | ||
async function validateORPCError(map, error) { | ||
const { code, status, message, data, cause, defined } = error; | ||
const config = map?.[error.code]; | ||
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) { | ||
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error; | ||
} | ||
if (!config.data) { | ||
return defined ? error : new ORPCError({ defined: true, code, status, message, data, cause }); | ||
} | ||
const validated = await config.data["~standard"].validate(error.data); | ||
if (validated.issues) { | ||
return defined ? new ORPCError({ defined: false, code, status, message, data, cause }) : error; | ||
} | ||
return new ORPCError({ | ||
defined: true, | ||
code, | ||
status, | ||
message, | ||
data: validated.value, | ||
cause | ||
}); | ||
} | ||
// src/client-utils.ts | ||
async function safe(promise) { | ||
try { | ||
const output = await promise; | ||
return [output, void 0, false]; | ||
} catch (e) { | ||
const error = e; | ||
if (isDefinedError(error)) { | ||
return [void 0, error, true]; | ||
} | ||
return [void 0, error, false]; | ||
} | ||
} | ||
// src/config.ts | ||
@@ -335,16 +180,5 @@ var DEFAULT_CONFIG = { | ||
}; | ||
var GLOBAL_CONFIG_REF = { value: DEFAULT_CONFIG }; | ||
function configGlobal(config) { | ||
if (config.defaultSuccessStatus !== void 0 && (config.defaultSuccessStatus < 200 || config.defaultSuccessStatus > 299)) { | ||
throw new Error("[configGlobal] The defaultSuccessStatus must be between 200 and 299"); | ||
} | ||
GLOBAL_CONFIG_REF.value = config; | ||
} | ||
function fallbackToGlobalConfig(key, value) { | ||
function fallbackContractConfig(key, value) { | ||
if (value === void 0) { | ||
const fallback = GLOBAL_CONFIG_REF.value[key]; | ||
if (fallback === void 0) { | ||
return DEFAULT_CONFIG[key]; | ||
} | ||
return fallback; | ||
return DEFAULT_CONFIG[key]; | ||
} | ||
@@ -354,31 +188,87 @@ return value; | ||
// src/error.ts | ||
var ValidationError = class extends Error { | ||
issues; | ||
constructor(options) { | ||
super(options.message, options); | ||
this.issues = options.issues; | ||
// src/event-iterator.ts | ||
import { mapEventIterator, ORPCError } from "@orpc/client"; | ||
import { isAsyncIteratorObject } from "@orpc/shared"; | ||
var EVENT_ITERATOR_SCHEMA_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_SCHEMA"); | ||
function eventIterator(yields, returns) { | ||
return { | ||
"~standard": { | ||
[EVENT_ITERATOR_SCHEMA_SYMBOL]: { yields, returns }, | ||
vendor: "orpc", | ||
version: 1, | ||
validate(iterator) { | ||
if (!isAsyncIteratorObject(iterator)) { | ||
return { issues: [{ message: "Expect event source iterator", path: [] }] }; | ||
} | ||
const mapped = mapEventIterator(iterator, { | ||
async value(value, done) { | ||
const schema = done ? returns : yields; | ||
if (!schema) { | ||
return value; | ||
} | ||
const result = await schema["~standard"].validate(value); | ||
if (result.issues) { | ||
throw new ORPCError("EVENT_ITERATOR_VALIDATION_FAILED", { | ||
message: "Event source iterator validation failed", | ||
cause: new ValidationError({ | ||
issues: result.issues, | ||
message: "Event source iterator validation failed" | ||
}) | ||
}); | ||
} | ||
return result.value; | ||
}, | ||
error: async (error) => error | ||
}); | ||
return { value: mapped }; | ||
} | ||
} | ||
}; | ||
} | ||
function getEventIteratorSchemaDetails(schema) { | ||
if (schema === void 0) { | ||
return void 0; | ||
} | ||
}; | ||
return schema["~standard"][EVENT_ITERATOR_SCHEMA_SYMBOL]; | ||
} | ||
// src/schema.ts | ||
function type(...[map]) { | ||
return { | ||
"~standard": { | ||
vendor: "custom", | ||
version: 1, | ||
async validate(value) { | ||
if (map) { | ||
return { value: await map(value) }; | ||
} | ||
return { value }; | ||
} | ||
} | ||
}; | ||
} | ||
// src/index.ts | ||
var oc = new ContractBuilder(); | ||
import { ORPCError as ORPCError2 } from "@orpc/client"; | ||
export { | ||
COMMON_ORPC_ERROR_DEFS, | ||
ContractBuilder, | ||
ContractProcedure, | ||
ContractRouterBuilder, | ||
DecoratedContractProcedure, | ||
ORPCError, | ||
ORPCError2 as ORPCError, | ||
ValidationError, | ||
configGlobal, | ||
fallbackORPCErrorMessage, | ||
fallbackORPCErrorStatus, | ||
fallbackToGlobalConfig, | ||
adaptContractRouter, | ||
adaptRoute, | ||
eventIterator, | ||
fallbackContractConfig, | ||
getEventIteratorSchemaDetails, | ||
isContractProcedure, | ||
isDefinedError, | ||
mergeErrorMap, | ||
mergeMeta, | ||
mergePrefix, | ||
mergeRoute, | ||
mergeTags, | ||
oc, | ||
safe, | ||
validateORPCError | ||
prefixRoute, | ||
type, | ||
unshiftTagRoute | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,16 +0,32 @@ | ||
import type { ErrorMap } from './error-map'; | ||
import type { RouteOptions } from './procedure'; | ||
import type { ContractRouter } from './router'; | ||
import type { HTTPPath, Schema, SchemaInput, SchemaOutput } from './types'; | ||
import { DecoratedContractProcedure } from './procedure-decorated'; | ||
import { ContractRouterBuilder } from './router-builder'; | ||
export declare class ContractBuilder { | ||
prefix(prefix: HTTPPath): ContractRouterBuilder; | ||
tag(...tags: string[]): ContractRouterBuilder; | ||
route(route: RouteOptions): DecoratedContractProcedure<undefined, undefined, undefined>; | ||
input<U extends Schema>(schema: U, example?: SchemaInput<U>): DecoratedContractProcedure<U, undefined, undefined>; | ||
output<U extends Schema>(schema: U, example?: SchemaOutput<U>): DecoratedContractProcedure<undefined, U, undefined>; | ||
errors<const U extends ErrorMap>(errorMap: U): DecoratedContractProcedure<undefined, undefined, U>; | ||
router<T extends ContractRouter>(router: T): T; | ||
import type { ContractProcedureBuilder, ContractProcedureBuilderWithInput, ContractProcedureBuilderWithOutput, ContractRouterBuilder } from './builder-variants'; | ||
import type { ContractProcedureDef } from './procedure'; | ||
import type { AdaptContractRouterOptions, AdaptedContractRouter, ContractRouter } from './router'; | ||
import type { Schema } from './schema'; | ||
import { type ErrorMap, type MergedErrorMap } from './error'; | ||
import { type Meta } from './meta'; | ||
import { ContractProcedure } from './procedure'; | ||
import { type HTTPPath, type Route } from './route'; | ||
export interface ContractBuilderDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>, AdaptContractRouterOptions<TErrorMap> { | ||
} | ||
export declare class ContractBuilder<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedure<TInputSchema, TOutputSchema, TErrorMap, TMeta> { | ||
'~orpc': ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>; | ||
constructor(def: ContractBuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>); | ||
/** | ||
* Reset initial meta | ||
*/ | ||
$meta<U extends Meta>(initialMeta: U): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, U>; | ||
/** | ||
* Reset initial route | ||
*/ | ||
$route(initialRoute: Route): ContractBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>; | ||
errors<U extends ErrorMap>(errors: U): ContractBuilder<TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>; | ||
meta(meta: TMeta): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>; | ||
route(route: Route): ContractProcedureBuilder<TInputSchema, TOutputSchema, TErrorMap, TMeta>; | ||
input<U extends Schema>(schema: U): ContractProcedureBuilderWithInput<U, TOutputSchema, TErrorMap, TMeta>; | ||
output<U extends Schema>(schema: U): ContractProcedureBuilderWithOutput<TInputSchema, U, TErrorMap, TMeta>; | ||
prefix(prefix: HTTPPath): ContractRouterBuilder<TErrorMap, TMeta>; | ||
tag(...tags: string[]): ContractRouterBuilder<TErrorMap, TMeta>; | ||
router<T extends ContractRouter<TMeta>>(router: T): AdaptedContractRouter<T, TErrorMap>; | ||
} | ||
export declare const oc: ContractBuilder<undefined, undefined, {}, {}>; | ||
//# sourceMappingURL=builder.d.ts.map |
@@ -1,36 +0,10 @@ | ||
import type { HTTPMethod, InputStructure } from './types'; | ||
export interface ORPCConfig { | ||
/** | ||
* @default 'POST' | ||
*/ | ||
defaultMethod?: HTTPMethod; | ||
/** | ||
* | ||
* @default 200 | ||
*/ | ||
defaultSuccessStatus?: number; | ||
/** | ||
* | ||
* @default 'OK' | ||
*/ | ||
defaultSuccessDescription?: string; | ||
/** | ||
* | ||
* @default 'compact' | ||
*/ | ||
defaultInputStructure?: InputStructure; | ||
/** | ||
* | ||
* @default 'compact' | ||
*/ | ||
defaultOutputStructure?: InputStructure; | ||
import type { HTTPMethod, InputStructure } from './route'; | ||
export interface ContractConfig { | ||
defaultMethod: HTTPMethod; | ||
defaultSuccessStatus: number; | ||
defaultSuccessDescription: string; | ||
defaultInputStructure: InputStructure; | ||
defaultOutputStructure: InputStructure; | ||
} | ||
/** | ||
* Set the global configuration, this configuration can effect entire project | ||
*/ | ||
export declare function configGlobal(config: ORPCConfig): void; | ||
/** | ||
* Fallback the value to the global config if it is undefined | ||
*/ | ||
export declare function fallbackToGlobalConfig<T extends keyof ORPCConfig>(key: T, value: ORPCConfig[T]): Exclude<ORPCConfig[T], undefined>; | ||
export declare function fallbackContractConfig<T extends keyof ContractConfig>(key: T, value: ContractConfig[T] | undefined): ContractConfig[T]; | ||
//# sourceMappingURL=config.d.ts.map |
@@ -0,5 +1,4 @@ | ||
import type { ORPCError, ORPCErrorCode } from '@orpc/client'; | ||
import type { StandardSchemaV1 } from '@standard-schema/spec'; | ||
import type { ErrorMap } from './error-map'; | ||
import type { ORPCErrorFromErrorMap } from './error-orpc'; | ||
export type ErrorFromErrorMap<TErrorMap extends ErrorMap> = Error | ORPCErrorFromErrorMap<TErrorMap>; | ||
import type { Schema, SchemaOutput } from './schema'; | ||
export interface ValidationErrorOptions extends ErrorOptions { | ||
@@ -13,2 +12,17 @@ message: string; | ||
} | ||
export type ErrorMapItem<TDataSchema extends Schema> = { | ||
status?: number; | ||
message?: string; | ||
description?: string; | ||
data?: TDataSchema; | ||
}; | ||
export type ErrorMap = { | ||
[key in ORPCErrorCode]?: ErrorMapItem<Schema>; | ||
}; | ||
export type MergedErrorMap<T1 extends ErrorMap, T2 extends ErrorMap> = Omit<T1, keyof T2> & T2; | ||
export declare function mergeErrorMap<T1 extends ErrorMap, T2 extends ErrorMap>(errorMap1: T1, errorMap2: T2): MergedErrorMap<T1, T2>; | ||
export type ORPCErrorFromErrorMap<TErrorMap extends ErrorMap> = { | ||
[K in keyof TErrorMap]: K extends string ? TErrorMap[K] extends ErrorMapItem<infer TDataSchema> ? ORPCError<K, SchemaOutput<TDataSchema>> : never : never; | ||
}[keyof TErrorMap]; | ||
export type ErrorFromErrorMap<TErrorMap extends ErrorMap> = Error | ORPCErrorFromErrorMap<TErrorMap>; | ||
//# sourceMappingURL=error.d.ts.map |
/** unnoq */ | ||
import { ContractBuilder } from './builder'; | ||
export * from './builder'; | ||
export * from './client'; | ||
export * from './client-utils'; | ||
export * from './builder-variants'; | ||
export * from './config'; | ||
export * from './error'; | ||
export * from './error-map'; | ||
export * from './error-orpc'; | ||
export * from './event-iterator'; | ||
export * from './meta'; | ||
export * from './procedure'; | ||
export * from './procedure-decorated'; | ||
export * from './procedure-client'; | ||
export * from './route'; | ||
export * from './router'; | ||
export * from './router-builder'; | ||
export * from './types'; | ||
export declare const oc: ContractBuilder; | ||
export * from './router-client'; | ||
export * from './schema'; | ||
export { ORPCError } from '@orpc/client'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,83 +0,18 @@ | ||
import type { ErrorMap } from './error-map'; | ||
import type { HTTPMethod, HTTPPath, InputStructure, OutputStructure, Schema, SchemaOutput } from './types'; | ||
export interface RouteOptions { | ||
method?: HTTPMethod; | ||
path?: HTTPPath; | ||
summary?: string; | ||
description?: string; | ||
deprecated?: boolean; | ||
tags?: readonly string[]; | ||
/** | ||
* The status code of the response when the procedure is successful. | ||
* | ||
* @default 200 | ||
*/ | ||
successStatus?: number; | ||
/** | ||
* The description of the response when the procedure is successful. | ||
* | ||
* @default 'OK' | ||
*/ | ||
successDescription?: string; | ||
/** | ||
* Determines how the input should be structured based on `params`, `query`, `headers`, and `body`. | ||
* | ||
* @option 'compact' | ||
* Combines `params` and either `query` or `body` (depending on the HTTP method) into a single object. | ||
* | ||
* @option 'detailed' | ||
* Keeps each part of the request (`params`, `query`, `headers`, and `body`) as separate fields in the input object. | ||
* | ||
* Example: | ||
* ```ts | ||
* const input = { | ||
* params: { id: 1 }, | ||
* query: { search: 'hello' }, | ||
* headers: { 'Content-Type': 'application/json' }, | ||
* body: { name: 'John' }, | ||
* } | ||
* ``` | ||
* | ||
* @default 'compact' | ||
*/ | ||
inputStructure?: InputStructure; | ||
/** | ||
* Determines how the response should be structured based on the output. | ||
* | ||
* @option 'compact' | ||
* Includes only the body data, encoded directly in the response. | ||
* | ||
* @option 'detailed' | ||
* Separates the output into `headers` and `body` fields. | ||
* - `headers`: Custom headers to merge with the response headers. | ||
* - `body`: The response data. | ||
* | ||
* Example: | ||
* ```ts | ||
* const output = { | ||
* headers: { 'x-custom-header': 'value' }, | ||
* body: { message: 'Hello, world!' }, | ||
* }; | ||
* ``` | ||
* | ||
* @default 'compact' | ||
*/ | ||
outputStructure?: OutputStructure; | ||
} | ||
export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> { | ||
route?: RouteOptions; | ||
InputSchema: TInputSchema; | ||
inputExample?: SchemaOutput<TInputSchema>; | ||
OutputSchema: TOutputSchema; | ||
outputExample?: SchemaOutput<TOutputSchema>; | ||
import type { ErrorMap } from './error'; | ||
import type { Meta } from './meta'; | ||
import type { Route } from './route'; | ||
import type { Schema } from './schema'; | ||
export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> { | ||
meta: TMeta; | ||
route: Route; | ||
inputSchema: TInputSchema; | ||
outputSchema: TOutputSchema; | ||
errorMap: TErrorMap; | ||
} | ||
export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap> { | ||
'~type': "ContractProcedure"; | ||
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap>; | ||
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap>); | ||
export declare class ContractProcedure<TInputSchema extends Schema, TOutputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> { | ||
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>; | ||
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>); | ||
} | ||
export type ANY_CONTRACT_PROCEDURE = ContractProcedure<any, any, any>; | ||
export type WELL_CONTRACT_PROCEDURE = ContractProcedure<Schema, Schema, ErrorMap>; | ||
export declare function isContractProcedure(item: unknown): item is ANY_CONTRACT_PROCEDURE; | ||
export type AnyContractProcedure = ContractProcedure<any, any, any, any>; | ||
export declare function isContractProcedure(item: unknown): item is AnyContractProcedure; | ||
//# sourceMappingURL=procedure.d.ts.map |
@@ -1,12 +0,29 @@ | ||
import type { ANY_CONTRACT_PROCEDURE, ContractProcedure } from './procedure'; | ||
import type { SchemaInput, SchemaOutput } from './types'; | ||
export type ContractRouter = ANY_CONTRACT_PROCEDURE | { | ||
[k: string]: ContractRouter; | ||
import type { Meta } from './meta'; | ||
import type { SchemaInput, SchemaOutput } from './schema'; | ||
import { type ErrorMap, type MergedErrorMap } from './error'; | ||
import { ContractProcedure } from './procedure'; | ||
import { type HTTPPath } from './route'; | ||
export type ContractRouter<TMeta extends Meta> = ContractProcedure<any, any, any, TMeta> | { | ||
[k: string]: ContractRouter<TMeta>; | ||
}; | ||
export type InferContractRouterInputs<T extends ContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any> ? SchemaInput<UInputSchema> : { | ||
[K in keyof T]: T[K] extends ContractRouter ? InferContractRouterInputs<T[K]> : never; | ||
export type AnyContractRouter = ContractRouter<any>; | ||
export type AdaptedContractRouter<TContract extends AnyContractRouter, TErrorMap extends ErrorMap> = { | ||
[K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrors>, UMeta> : TContract[K] extends AnyContractRouter ? AdaptedContractRouter<TContract[K], TErrorMap> : never; | ||
}; | ||
export type InferContractRouterOutputs<T extends ContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any> ? SchemaOutput<UOutputSchema> : { | ||
[K in keyof T]: T[K] extends ContractRouter ? InferContractRouterOutputs<T[K]> : never; | ||
export interface AdaptContractRouterOptions<TErrorMap extends ErrorMap> { | ||
errorMap: TErrorMap; | ||
prefix?: HTTPPath; | ||
tags?: readonly string[]; | ||
} | ||
export declare function adaptContractRouter<TRouter extends ContractRouter<any>, TErrorMap extends ErrorMap>(contract: TRouter, options: AdaptContractRouterOptions<TErrorMap>): AdaptedContractRouter<TRouter, TErrorMap>; | ||
export type InferContractRouterInputs<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, any, any, any> ? SchemaInput<UInputSchema> : { | ||
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterInputs<T[K]> : never; | ||
}; | ||
export type InferContractRouterOutputs<T extends AnyContractRouter> = T extends ContractProcedure<any, infer UOutputSchema, any, any> ? SchemaOutput<UOutputSchema> : { | ||
[K in keyof T]: T[K] extends AnyContractRouter ? InferContractRouterOutputs<T[K]> : never; | ||
}; | ||
export type ContractRouterToErrorMap<T extends AnyContractRouter> = T extends ContractProcedure<any, any, infer UErrorMap, any> ? UErrorMap : { | ||
[K in keyof T]: T[K] extends AnyContractRouter ? ContractRouterToErrorMap<T[K]> : never; | ||
}[keyof T]; | ||
export type ContractRouterToMeta<T extends AnyContractRouter> = T extends ContractRouter<infer UMeta> ? UMeta : never; | ||
//# sourceMappingURL=router.d.ts.map |
{ | ||
"name": "@orpc/contract", | ||
"type": "module", | ||
"version": "0.0.0-next.c6c659d", | ||
"version": "0.0.0-next.ca29a36", | ||
"license": "MIT", | ||
@@ -32,4 +32,6 @@ "homepage": "https://orpc.unnoq.com", | ||
"dependencies": { | ||
"@standard-schema/spec": "1.0.0-beta.4", | ||
"@orpc/shared": "0.0.0-next.c6c659d" | ||
"@standard-schema/spec": "^1.0.0", | ||
"@orpc/client": "0.0.0-next.ca29a36", | ||
"@orpc/standard-server": "0.0.0-next.ca29a36", | ||
"@orpc/shared": "0.0.0-next.ca29a36" | ||
}, | ||
@@ -39,3 +41,3 @@ "devDependencies": { | ||
"valibot": "1.0.0-beta.9", | ||
"zod": "3.24.1" | ||
"zod": "^3.24.1" | ||
}, | ||
@@ -42,0 +44,0 @@ "scripts": { |
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
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
30785
17
0
102
4
531
+ Added@orpc/client@0.0.0-next.ca29a36(transitive)
+ Added@orpc/shared@0.0.0-next.ca29a36(transitive)
+ Added@orpc/standard-server@0.0.0-next.ca29a36(transitive)
+ Added@orpc/standard-server-fetch@0.0.0-next.ca29a36(transitive)
+ Added@standard-schema/spec@1.0.0(transitive)
+ Added@tinyhttp/content-disposition@2.2.2(transitive)
- Removed@orpc/shared@0.0.0-next.c6c659d(transitive)
- Removed@standard-schema/spec@1.0.0-beta.4(transitive)
- Removedis-what@5.2.0(transitive)
Updated@standard-schema/spec@^1.0.0