Skip to content

Commit cc4cb21

Browse files
authored
feat!: update standard server (#179)
* feat!: only parse as text when content-type is text/plain * refactor!: avoid JsonValue * refactor!: move share utils into @orpc/shared * refactor!: replace isEventMetaContainer with isTypescriptObject * improve tsconfig
1 parent 370978b commit cc4cb21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+170
-150
lines changed

packages/client/src/adapters/fetch/rpc-link.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { ORPCError } from '@orpc/contract'
22
import { os } from '@orpc/server'
33
import { RPCHandler } from '@orpc/server/fetch'
4-
import { getEventMeta, isAsyncIteratorObject, withEventMeta } from '@orpc/standard-server'
4+
import { isAsyncIteratorObject } from '@orpc/shared'
5+
import { getEventMeta, withEventMeta } from '@orpc/standard-server'
56
import { beforeEach, describe, expect, it, vi } from 'vitest'
67
import { supportedDataTypes } from '../../../tests/shared'
78
import { RPCLink } from './rpc-link'

packages/client/src/adapters/fetch/rpc-link.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Value } from '@orpc/shared'
2+
import type { StandardBody } from '@orpc/standard-server'
23
import type { ClientContext, ClientLink, ClientOptionsOut } from '../../types'
34
import type { FetchWithContext } from './types'
4-
import { trim, value } from '@orpc/shared'
5-
import { isAsyncIteratorObject, type StandardBody } from '@orpc/standard-server'
5+
import { isAsyncIteratorObject, trim, value } from '@orpc/shared'
66
import { toFetchBody, toStandardBody } from '@orpc/standard-server-fetch'
77
import { ORPCError } from '../../error'
88
import { createAutoRetryEventIterator, type EventIteratorReconnectOptions } from '../../event-iterator'

packages/client/src/event-iterator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EventIteratorState } from './event-iterator-state'
2-
import { retry } from '@orpc/shared'
3-
import { getEventMeta, isEventMetaContainer, withEventMeta } from '@orpc/standard-server'
2+
import { isTypescriptObject, retry } from '@orpc/shared'
3+
import { getEventMeta, withEventMeta } from '@orpc/standard-server'
44
import { registerEventIteratorState, updateEventIteratorStatus } from './event-iterator-state'
55

66
export function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(
@@ -19,7 +19,7 @@ export function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn
1919

2020
if (mappedValue !== value) {
2121
const meta = getEventMeta(value)
22-
if (meta && isEventMetaContainer(mappedValue)) {
22+
if (meta && isTypescriptObject(mappedValue)) {
2323
mappedValue = withEventMeta(mappedValue, meta)
2424
}
2525
}
@@ -36,7 +36,7 @@ export function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn
3636

3737
if (mappedError !== error) {
3838
const meta = getEventMeta(error)
39-
if (meta && isEventMetaContainer(mappedError)) {
39+
if (meta && isTypescriptObject(mappedError)) {
4040
mappedError = withEventMeta(mappedError, meta)
4141
}
4242
}

packages/client/src/openapi/serializer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { JsonValue } from '@orpc/standard-server'
2-
import { ErrorEvent, isAsyncIteratorObject } from '@orpc/standard-server'
1+
import { isAsyncIteratorObject } from '@orpc/shared'
2+
import { ErrorEvent } from '@orpc/standard-server'
33
import { ORPCError, toORPCError } from '../error'
44
import { mapEventIterator } from '../event-iterator'
55
import { BracketNotationSerializer } from './bracket-notation'
@@ -19,13 +19,13 @@ export class OpenAPISerializer {
1919
error: async (e) => {
2020
if (e instanceof ErrorEvent) {
2121
return new ErrorEvent({
22-
data: this.#serialize(e.data, false) as JsonValue,
22+
data: this.#serialize(e.data, false),
2323
cause: e,
2424
})
2525
}
2626

2727
return new ErrorEvent({
28-
data: this.#serialize(toORPCError(e).toJSON(), false) as JsonValue,
28+
data: this.#serialize(toORPCError(e).toJSON(), false),
2929
cause: e,
3030
})
3131
},

packages/client/src/rpc/serializer.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ORPCError } from '@orpc/contract'
2-
import { ErrorEvent, getEventMeta, isAsyncIteratorObject, parseEmptyableJSON, withEventMeta } from '@orpc/standard-server'
2+
import { isAsyncIteratorObject, parseEmptyableJSON } from '@orpc/shared'
3+
import { ErrorEvent, getEventMeta, withEventMeta } from '@orpc/standard-server'
34
import { supportedDataTypes } from '../../tests/shared'
45
import { RPCSerializer } from './serializer'
56

packages/client/src/rpc/serializer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { JsonValue } from '@orpc/standard-server'
2-
import { ErrorEvent, isAsyncIteratorObject } from '@orpc/standard-server'
1+
import { isAsyncIteratorObject } from '@orpc/shared'
2+
import { ErrorEvent } from '@orpc/standard-server'
33
import { ORPCError, toORPCError } from '../error'
44
import { mapEventIterator } from '../event-iterator'
55
import { RPCJsonSerializer } from './json-serializer'
@@ -16,13 +16,13 @@ export class RPCSerializer {
1616
error: async (e) => {
1717
if (e instanceof ErrorEvent) {
1818
return new ErrorEvent({
19-
data: this.#serialize(e.data, false) as JsonValue,
19+
data: this.#serialize(e.data, false),
2020
cause: e,
2121
})
2222
}
2323

2424
return new ErrorEvent({
25-
data: this.#serialize(toORPCError(e).toJSON(), false) as JsonValue,
25+
data: this.#serialize(toORPCError(e).toJSON(), false),
2626
cause: e,
2727
})
2828
},
@@ -78,7 +78,7 @@ export class RPCSerializer {
7878
}
7979

8080
return new ErrorEvent({
81-
data: deserialized as JsonValue,
81+
data: deserialized,
8282
cause: e,
8383
})
8484
},

packages/contract/src/event-iterator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { StandardSchemaV1 } from '@standard-schema/spec'
22
import type { Schema } from './schema'
33
import { mapEventIterator, ORPCError } from '@orpc/client'
4-
import { isAsyncIteratorObject } from '@orpc/standard-server'
4+
import { isAsyncIteratorObject } from '@orpc/shared'
55
import { ValidationError } from './error'
66

77
const EVENT_ITERATOR_SCHEMA_SYMBOL = Symbol('ORPC_EVENT_ITERATOR_SCHEMA')

packages/server/src/adapters/standard/rpc-codec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { ORPCError } from '@orpc/client'
2+
import type { StandardBody, StandardRequest, StandardResponse } from '@orpc/standard-server'
23
import type { AnyProcedure } from '../../procedure'
34
import type { StandardCodec, StandardParams } from './types'
45
import { RPCSerializer } from '@orpc/client/rpc'
5-
import { parseEmptyableJSON, type StandardBody, type StandardRequest, type StandardResponse } from '@orpc/standard-server'
6+
import { parseEmptyableJSON } from '@orpc/shared'
67

78
export interface StandardCodecOptions {
89
serializer?: RPCSerializer
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { once } from './function'
2+
3+
it('once', () => {
4+
const fn = vi.fn(() => ({}))
5+
const onceFn = once(fn)
6+
7+
expect(onceFn()).toBe(fn.mock.results[0]!.value)
8+
expect(onceFn()).toBe(fn.mock.results[0]!.value)
9+
expect(onceFn()).toBe(fn.mock.results[0]!.value)
10+
expect(onceFn()).toBe(fn.mock.results[0]!.value)
11+
expect(onceFn()).toBe(fn.mock.results[0]!.value)
12+
13+
expect(fn).toHaveBeenCalledTimes(1)
14+
})

packages/shared/src/function.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
export type AnyFunction = (...args: any[]) => any
2+
3+
export function once<T extends () => any>(fn: T): () => ReturnType<T> {
4+
let cached: { result: ReturnType<T> } | undefined
5+
6+
return (): ReturnType<T> => {
7+
if (cached) {
8+
return cached.result
9+
}
10+
11+
const result = fn()
12+
cached = { result }
13+
14+
return result
15+
}
16+
}

0 commit comments

Comments
 (0)