@orpc/contract
Advanced tools
Comparing version 0.39.0 to 0.40.0
@@ -274,7 +274,17 @@ // src/error-map.ts | ||
} | ||
static fromJSON(json) { | ||
return new _ORPCError(json.code, json); | ||
static fromJSON(json, options) { | ||
return new _ORPCError(json.code, { | ||
...options, | ||
...json | ||
}); | ||
} | ||
static isValidJSON(json) { | ||
return isObject(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"; | ||
if (!isObject(json)) { | ||
return false; | ||
} | ||
const validKeys = ["defined", "code", "status", "message", "data"]; | ||
if (Object.keys(json).some((k) => !validKeys.includes(k))) { | ||
return false; | ||
} | ||
return "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"; | ||
} | ||
@@ -368,2 +378,78 @@ }; | ||
// src/event-iterator.ts | ||
import { getEventMeta, isAsyncIteratorObject, isEventMetaContainer, withEventMeta } from "@orpc/server-standard"; | ||
function mapEventIterator(iterator, maps) { | ||
return async function* () { | ||
try { | ||
while (true) { | ||
const { done, value } = await iterator.next(); | ||
let mappedValue = await maps.value(value, done); | ||
if (mappedValue !== value) { | ||
const meta = getEventMeta(value); | ||
if (meta && isEventMetaContainer(mappedValue)) { | ||
mappedValue = withEventMeta(mappedValue, meta); | ||
} | ||
} | ||
if (done) { | ||
return mappedValue; | ||
} | ||
yield mappedValue; | ||
} | ||
} catch (error) { | ||
let mappedError = await maps.error(error); | ||
if (mappedError !== error) { | ||
const meta = getEventMeta(error); | ||
if (meta && isEventMetaContainer(mappedError)) { | ||
mappedError = withEventMeta(mappedError, meta); | ||
} | ||
} | ||
throw mappedError; | ||
} finally { | ||
await iterator.return?.(); | ||
} | ||
}(); | ||
} | ||
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 | ||
@@ -393,7 +479,10 @@ function type(...[map]) { | ||
createORPCErrorConstructorMap, | ||
eventIterator, | ||
fallbackContractConfig, | ||
fallbackORPCErrorMessage, | ||
fallbackORPCErrorStatus, | ||
getEventIteratorSchemaDetails, | ||
isContractProcedure, | ||
isDefinedError, | ||
mapEventIterator, | ||
mergeErrorMap, | ||
@@ -400,0 +489,0 @@ mergeMeta, |
export type ClientContext = Record<string, any>; | ||
export type ClientOptions<TClientContext extends ClientContext> = { | ||
signal?: AbortSignal; | ||
lastEventId?: string | undefined; | ||
} & (Record<never, never> extends TClientContext ? { | ||
@@ -5,0 +6,0 @@ context?: TClientContext; |
@@ -105,3 +105,3 @@ import type { MaybeOptionalOptions } from '@orpc/shared'; | ||
toJSON(): ORPCErrorJSON<TCode, TData>; | ||
static fromJSON<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>): ORPCError<TCode, TData>; | ||
static fromJSON<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>; | ||
static isValidJSON(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>; | ||
@@ -108,0 +108,0 @@ } |
@@ -11,2 +11,3 @@ /** unnoq */ | ||
export * from './error-utils'; | ||
export * from './event-iterator'; | ||
export * from './meta'; | ||
@@ -13,0 +14,0 @@ export * from './procedure'; |
{ | ||
"name": "@orpc/contract", | ||
"type": "module", | ||
"version": "0.39.0", | ||
"version": "0.40.0", | ||
"license": "MIT", | ||
@@ -32,4 +32,5 @@ "homepage": "https://orpc.unnoq.com", | ||
"dependencies": { | ||
"@standard-schema/spec": "1.0.0", | ||
"@orpc/shared": "0.39.0" | ||
"@orpc/server-standard": "^0.4.0", | ||
"@standard-schema/spec": "^1.0.0", | ||
"@orpc/shared": "0.40.0" | ||
}, | ||
@@ -36,0 +37,0 @@ "devDependencies": { |
38890
21
911
3
+ Added@orpc/server-standard@^0.4.0
+ Added@orpc/server-standard@0.4.0(transitive)
+ Added@orpc/shared@0.40.0(transitive)
+ Added@tinyhttp/content-disposition@2.2.2(transitive)
- Removed@orpc/shared@0.39.0(transitive)
Updated@orpc/shared@0.40.0
Updated@standard-schema/spec@^1.0.0