Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions apps/content/docs/integrations/effect.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ import { call, os } from '@orpc/server'
import { handlerGen, WithEffectContext } from '@orpc/experimental-effect'
import { Context, Effect } from 'effect'

class Random extends Context.Tag('MyRandomService')<
class Random extends Context.Service<
Random,
{
readonly next: Effect.Effect<number>
}
>() {}
>()('MyRandomService') {}

interface ServerContext extends WithEffectContext<Random> {}

Expand Down Expand Up @@ -149,7 +149,7 @@ export async function fetch(request: Request) {
context: {
'~effect/context': Context.empty(),
'~effect/wrap': (effect, opts) => effect.pipe(
Effect.catchAllCause((cause) => {
Effect.catchCause((cause) => {

})
),
Expand Down Expand Up @@ -192,13 +192,13 @@ if (isInferableError(error)) {

## Effect Schema

oRPC natively supports [Standard Schema](https://standardschema.dev/schema#what-schema-libraries-implement-the-spec), and [Effect Schema](https://effect.website/docs/schema/introduction/) implements that spec through [Schema.standardSchemaV1](https://effect.website/docs/schema/standard-schema/):
oRPC natively supports [Standard Schema](https://standardschema.dev/schema#what-schema-libraries-implement-the-spec), and [Effect Schema](https://effect.website/docs/schema/introduction/) implements that spec through [Schema.toStandardSchemaV1](https://effect.website/docs/schema/standard-schema/):

```ts
import { Schema } from 'effect'

const procedure = os
.input(Schema.standardSchemaV1(Schema.Struct({ name: Schema.String })))
.input(Schema.toStandardSchemaV1(Schema.Struct({ name: Schema.String })))
.handler(handlerGen(function* ({ input, context }) {
return `Hello ${input.name}!`
}))
Expand Down
2 changes: 1 addition & 1 deletion apps/content/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@tanstack/svelte-query": "^6.1.34",
"@tanstack/vue-query": "^5.101.0",
"@types/node": "^26.0.0",
"effect": "^3.21.3",
"effect": "4.0.0-beta.90",
"markdown-it-task-lists": "^2.1.1",
"mermaid": "^11.15.0",
"openai": "^6.44.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/effect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"type:check": "tsc -b"
},
"peerDependencies": {
"effect": ">=3.21.2"
"effect": ">=4.0.0-beta.90"
},
"dependencies": {
"@orpc/contract": "workspace:*",
Expand All @@ -60,6 +60,6 @@
"@orpc/shared": "workspace:*"
},
"devDependencies": {
"effect": "^3.21.3"
"effect": "4.0.0-beta.90"
}
}
21 changes: 19 additions & 2 deletions packages/effect/src/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ describe('effectSchemaToJsonSchemaConverter', () => {
expect(optional).toBe(false)
})

it('uses standard json schema input and output generators', () => {
const schema = toStandardSchema(Effect.Schema.NumberFromString)
expect(converter.convert(schema, 'input')).toEqual([
expect.objectContaining({ type: 'string' }),
false,
])
expect(converter.convert(schema, 'output')).toEqual([
expect.objectContaining({
anyOf: expect.arrayContaining([
expect.objectContaining({ type: 'number' }),
]),
}),
false,
])
})

it('marks as optional if direction is input and schema accept undefined', () => {
const [, optional1] = converter.convert(toStandardSchema(Effect.Schema.Unknown), 'input')
expect(optional1).toBe(true)
Expand All @@ -43,12 +59,13 @@ describe('effectSchemaToJsonSchemaConverter', () => {
expect(optional1).toBe(true)
})

it('marks as required if validation throw', () => {
it('keeps converting and marks as required if standard validation throws', () => {
const schema = toStandardSchema(Effect.Schema.Unknown)
;(schema as any)['~standard'].validate = () => {
throw new Error('test')
}
const [, optional] = converter.convert(schema, 'input')
const [jsonSchema, optional] = converter.convert(schema, 'input')
expect(jsonSchema).toEqual(expect.any(Object))
expect(optional).toBe(false)
})
})
Expand Down
22 changes: 7 additions & 15 deletions packages/effect/src/converter.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import type { AnySchema } from '@orpc/contract'
import type { JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection } from '@orpc/json-schema'
import type { Schema as EffectSchema } from 'effect'
import { JSONSchema } from 'effect'
import { StandardJsonSchemaConverter } from '@orpc/json-schema'
import { Schema as EffectSchema } from 'effect'

export class EffectSchemaToJsonSchemaConverter implements JsonSchemaConverter {
private readonly converter = new StandardJsonSchemaConverter()

condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean {
return schema?.['~standard'].vendor === 'effect'
}

convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean] {
const effectSchema = schema as unknown as EffectSchema.Schema<any, any> & AnySchema
const jsonSchema = JSONSchema.make(effectSchema, { target: 'jsonSchema2020-12' })

let optional = false
try {
const result = effectSchema['~standard'].validate(undefined)
if (!(result instanceof Promise) && !result.issues) {
optional = direction === 'input' ? true : result.value === undefined
}
}
catch {}

return [jsonSchema as JsonSchema, optional]
const effectSchema = schema as EffectSchema.Constraint & AnySchema
const standardJsonSchema = EffectSchema.toStandardJSONSchemaV1(effectSchema)
return this.converter.convert(standardJsonSchema, direction)
}
}
25 changes: 12 additions & 13 deletions packages/effect/src/extensions/effect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, InitialInputSchema, Schema } from '@orpc/contract'
import type { AnyORPCError, Context, DecoratedProcedure, ImplementedProcedure, MergedContext, ORPCErrorConstructorMap } from '@orpc/server'
import type { Effect } from 'effect'
import type { YieldWrap } from 'effect/Utils'
import type { WithEffectContext } from '../context'
import type { HandlerGen, InferYieldError } from '../handler'
import { Builder, ProcedureImplementer } from '@orpc/server'
Expand All @@ -13,11 +12,11 @@ declare module '@orpc/server' {
TErrorMap extends ErrorMap,
> {
effect<
TYield extends YieldWrap<Effect.Effect<
TYield extends Effect.Effect<
any,
any,
TInitialContext extends WithEffectContext<infer S> ? S : never
>>,
>,
TReturn,
>(
handler: HandlerGen<
Expand All @@ -43,11 +42,11 @@ declare module '@orpc/server' {
TErrorMap extends ErrorMap,
> {
effect<
TYield extends YieldWrap<Effect.Effect<
TYield extends Effect.Effect<
any,
any,
MergedContext<TInitialContext, TInjectedContext> extends WithEffectContext<infer S> ? S : never
>>,
>,
TReturn,
>(
handler: HandlerGen<
Expand All @@ -74,11 +73,11 @@ declare module '@orpc/server' {
TErrorMap extends ErrorMap,
> {
effect<
TYield extends YieldWrap<Effect.Effect<
TYield extends Effect.Effect<
any,
any,
MergedContext<TInitialContext, TInjectedContext> extends WithEffectContext<infer S> ? S : never
>>,
>,
TReturn,
>(
handler: HandlerGen<
Expand All @@ -105,11 +104,11 @@ declare module '@orpc/server' {
TErrorMap extends ErrorMap,
> {
effect<
TYield extends YieldWrap<Effect.Effect<
TYield extends Effect.Effect<
any,
any,
MergedContext<TInitialContext, TInjectedContext> extends WithEffectContext<infer S> ? S : never
>>,
>,
TReturn extends InferSchemaInput<TOutputSchema> | AnyORPCError,
>(
handler: HandlerGen<
Expand Down Expand Up @@ -137,11 +136,11 @@ declare module '@orpc/server' {
TErrorMap extends ErrorMap,
> {
effect<
TYield extends YieldWrap<Effect.Effect<
TYield extends Effect.Effect<
any,
any,
MergedContext<TInitialContext, TInjectedContext> extends WithEffectContext<infer S> ? S : never
>>,
>,
TReturn extends InferSchemaInput<TOutputSchema> | AnyORPCError,
>(
handler: HandlerGen<
Expand Down Expand Up @@ -172,11 +171,11 @@ declare module '@orpc/server' {
handler: HandlerGen<
MergedContext<TInitialContext, TInjectedContext>,
InferSchemaOutput<TInputSchema>,
YieldWrap<Effect.Effect<
Effect.Effect<
any,
any,
MergedContext<TInitialContext, TInjectedContext> extends WithEffectContext<infer S> ? S : never
>>,
>,
AnyORPCError | InferSchemaInput<TOutputSchema>,
ORPCErrorConstructorMap<TErrorMap>
>,
Expand Down
10 changes: 1 addition & 9 deletions packages/effect/src/extensions/input-output.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ const errorMap = {
const schema1 = z.object({ schema1: z.number().transform(n => `${n}`) })
const schema2 = z.object({ schema2: z.number().transform(n => `${n}`) })

const NumberFromString = EffectSchema.transform(
EffectSchema.String,
EffectSchema.JsonNumber,
{
strict: true,
decode: literal => Number(literal),
encode: number => number.toString(),
},
)
const NumberFromString = EffectSchema.NumberFromString

it('adds .input<EffectSchema> .output<EffectSchema> into ContractBuilder', async () => {
const builder = {} as ContractBuilder<typeof errorMap>
Expand Down
Loading
Loading