Skip to content

Commit 68378b4

Browse files
authored
refactor(client, server)!: minify link and handler options (#202)
1 parent b0d4662 commit 68378b4

22 files changed

+56
-120
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export * from './link-fetch-client'
12
export * from './rpc-link'
23
export * from './types'

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import type { ClientContext, ClientLink, ClientOptionsOut } from '../../types'
22
import type { StandardLinkOptions, StandardRPCLinkCodecOptions } from '../standard'
33
import type { LinkFetchClientOptions } from './link-fetch-client'
4-
import { StandardLink, StandardRPCLinkCodec } from '../standard'
4+
import { RPCSerializer, StandardLink, StandardRPCLinkCodec } from '../standard'
55
import { LinkFetchClient } from './link-fetch-client'
66

77
export interface RPCLinkOptions<T extends ClientContext>
8-
extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, LinkFetchClientOptions<T> {
9-
linkCodec?: StandardRPCLinkCodec<T>
10-
linkClient?: LinkFetchClient<T>
11-
}
8+
extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, LinkFetchClientOptions<T> {}
129

1310
export class RPCLink<T extends ClientContext> implements ClientLink<T> {
1411
private readonly standardLink: StandardLink<T>
1512

1613
constructor(options: RPCLinkOptions<T>) {
17-
const linkCodec = options.linkCodec ?? new StandardRPCLinkCodec(options)
18-
const linkClient = options.linkClient ?? new LinkFetchClient(options)
14+
const serializer = new RPCSerializer()
15+
const linkCodec = new StandardRPCLinkCodec(serializer, options)
16+
const linkClient = new LinkFetchClient(options)
17+
1918
this.standardLink = new StandardLink(linkCodec, linkClient, options)
2019
}
2120

packages/client/src/adapters/standard/rpc-link-codec.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ describe('standardRPCLinkCodec', () => {
1414

1515
describe('encode', () => {
1616
const method = vi.fn()
17-
const codec = new StandardRPCLinkCodec({
17+
const codec = new StandardRPCLinkCodec(serializer, {
1818
url: 'http://localhost:3000',
19-
rpcSerializer: serializer,
2019
method,
2120
headers: () => ({ 'x-custom-header': 'custom-value' }),
2221
})
@@ -78,9 +77,8 @@ describe('standardRPCLinkCodec', () => {
7877
['blob inside', { blob: new Blob(['blob'], { type: 'text/plain' }) }],
7978
['event-iterator', (async function* () { })()],
8079
])('fallback method when method=GET: %s', async (_, input) => {
81-
const codec = new StandardRPCLinkCodec({
80+
const codec = new StandardRPCLinkCodec(serializer, {
8281
url: 'http://localhost:3000',
83-
rpcSerializer: serializer,
8482
method: 'GET',
8583
maxUrlLength: 100,
8684
fallbackMethod: 'PATCH',
@@ -101,9 +99,8 @@ describe('standardRPCLinkCodec', () => {
10199
})
102100

103101
describe('decode', () => {
104-
const codec = new StandardRPCLinkCodec({
102+
const codec = new StandardRPCLinkCodec(serializer, {
105103
url: 'http://localhost:3000',
106-
rpcSerializer: serializer,
107104
})
108105

109106
it('should decode output', async () => {

packages/client/src/adapters/standard/rpc-link-codec.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { StandardHeaders, StandardLazyResponse, StandardRequest } from '@orpc/standard-server'
22
import type { ClientContext, ClientOptionsOut } from '../../types'
3+
import type { RPCSerializer } from './rpc-serializer'
34
import type { StandardLinkCodec } from './types'
45
import { isAsyncIteratorObject, stringifyJSON, trim, value, type Value } from '@orpc/shared'
56
import { ORPCError } from '../../error'
6-
import { RPCSerializer } from './rpc-serializer'
77

88
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
99

@@ -55,8 +55,6 @@ export interface StandardRPCLinkCodecOptions<T extends ClientContext> {
5555
path: readonly string[],
5656
input: unknown,
5757
]>
58-
59-
rpcSerializer?: RPCSerializer
6058
}
6159

6260
export class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
@@ -65,15 +63,16 @@ export class StandardRPCLinkCodec<T extends ClientContext> implements StandardLi
6563
private readonly fallbackMethod: Exclude<StandardRPCLinkCodecOptions<T>['fallbackMethod'], undefined>
6664
private readonly expectedMethod: Exclude<StandardRPCLinkCodecOptions<T>['method'], undefined>
6765
private readonly headers: Exclude<StandardRPCLinkCodecOptions<T>['headers'], undefined>
68-
private readonly rpcSerializer: Exclude<StandardRPCLinkCodecOptions<T>['rpcSerializer'], undefined>
6966

70-
constructor(options: StandardRPCLinkCodecOptions<T>) {
67+
constructor(
68+
private readonly serializer: RPCSerializer,
69+
options: StandardRPCLinkCodecOptions<T>,
70+
) {
7171
this.baseUrl = options.url
7272
this.maxUrlLength = options.maxUrlLength ?? 2083
7373
this.fallbackMethod = options.fallbackMethod ?? 'POST'
7474
this.expectedMethod = options.method ?? this.fallbackMethod
7575
this.headers = options.headers ?? {}
76-
this.rpcSerializer = options.rpcSerializer ?? new RPCSerializer()
7776
}
7877

7978
async encode(path: readonly string[], input: unknown, options: ClientOptionsOut<any>): Promise<StandardRequest> {
@@ -82,7 +81,7 @@ export class StandardRPCLinkCodec<T extends ClientContext> implements StandardLi
8281
const baseUrl = await value(this.baseUrl, options, path, input)
8382
const url = new URL(`${trim(baseUrl.toString(), '/')}/${path.map(encodeURIComponent).join('/')}`)
8483

85-
const serialized = this.rpcSerializer.serialize(input)
84+
const serialized = this.serializer.serialize(input)
8685

8786
if (
8887
expectedMethod === 'GET'
@@ -126,7 +125,7 @@ export class StandardRPCLinkCodec<T extends ClientContext> implements StandardLi
126125

127126
isBodyOk = true
128127

129-
return this.rpcSerializer.deserialize(body)
128+
return this.serializer.deserialize(body)
130129
}
131130
catch (error) {
132131
if (!isBodyOk) {

packages/openapi/src/adapters/fetch/openapi-handler.test.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { StandardHandler } from '@orpc/server/standard'
22
import { toFetchResponse, toStandardLazyRequest } from '@orpc/standard-server-fetch'
33
import { describe, expect, it, vi } from 'vitest'
44
import { router } from '../../../../server/tests/shared'
5-
import { OpenAPICodec, OpenAPIMatcher } from '../standard'
65
import { OpenAPIHandler } from './openapi-handler'
76

87
vi.mock('@orpc/server/standard', async origin => ({
@@ -93,22 +92,4 @@ describe('openAPIHandler', () => {
9392

9493
expect(vi.mocked(toFetchResponse)).not.toHaveBeenCalled()
9594
})
96-
97-
it('standardHandler constructor', async () => {
98-
const options = {
99-
codec: new OpenAPICodec(),
100-
matcher: new OpenAPIMatcher(),
101-
interceptors: [vi.fn()],
102-
}
103-
104-
const handler = new OpenAPIHandler(router, options)
105-
106-
expect(StandardHandler).toHaveBeenCalledOnce()
107-
expect(StandardHandler).toHaveBeenCalledWith(
108-
router,
109-
options.matcher,
110-
options.codec,
111-
options,
112-
)
113-
})
11495
})

packages/openapi/src/adapters/fetch/openapi-handler.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import type { Context, Router } from '@orpc/server'
22
import type { FetchHandler, FetchHandleResult } from '@orpc/server/fetch'
3-
import type { StandardHandleOptions } from '@orpc/server/standard'
3+
import type { StandardHandleOptions, StandardHandlerOptions } from '@orpc/server/standard'
44
import type { MaybeOptionalOptions } from '@orpc/shared'
55
import type { ToFetchResponseOptions } from '@orpc/standard-server-fetch'
6-
import type { OpenAPIHandlerOptions } from '../standard'
6+
import { OpenAPISerializer } from '@orpc/openapi-client/standard'
77
import { StandardHandler } from '@orpc/server/standard'
88
import { toFetchResponse, toStandardLazyRequest } from '@orpc/standard-server-fetch'
99
import { OpenAPICodec, OpenAPIMatcher } from '../standard'
1010

1111
export class OpenAPIHandler<T extends Context> implements FetchHandler<T> {
1212
private readonly standardHandler: StandardHandler<T>
1313

14-
constructor(router: Router<T, any>, options?: NoInfer<OpenAPIHandlerOptions<T>>) {
15-
const matcher = options?.matcher ?? new OpenAPIMatcher()
16-
const codec = options?.codec ?? new OpenAPICodec()
14+
constructor(router: Router<T, any>, options: NoInfer<StandardHandlerOptions<T>> = {}) {
15+
const serializer = new OpenAPISerializer()
16+
const matcher = new OpenAPIMatcher()
17+
const codec = new OpenAPICodec(serializer)
1718

1819
this.standardHandler = new StandardHandler(router, matcher, codec, options)
1920
}

packages/openapi/src/adapters/node/openapi-handler.test.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { StandardHandler } from '@orpc/server/standard'
22
import { sendStandardResponse, toStandardLazyRequest } from '@orpc/standard-server-node'
33
import inject from 'light-my-request'
44
import { router } from '../../../../server/tests/shared'
5-
import { OpenAPICodec, OpenAPIMatcher } from '../standard'
65
import { OpenAPIHandler } from './openapi-handler'
76

87
vi.mock('@orpc/standard-server-node', () => ({
@@ -110,22 +109,4 @@ describe('openapiHandler', async () => {
110109

111110
expect(sendStandardResponse).not.toHaveBeenCalled()
112111
})
113-
114-
it('standardHandler constructor', async () => {
115-
const options = {
116-
codec: new OpenAPICodec(),
117-
matcher: new OpenAPIMatcher(),
118-
interceptors: [vi.fn()],
119-
}
120-
121-
const handler = new OpenAPIHandler(router, options)
122-
123-
expect(StandardHandler).toHaveBeenCalledOnce()
124-
expect(StandardHandler).toHaveBeenCalledWith(
125-
router,
126-
options.matcher,
127-
options.codec,
128-
options,
129-
)
130-
})
131112
})

packages/openapi/src/adapters/node/openapi-handler.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import type { Context, Router } from '@orpc/server'
22
import type { NodeHttpHandler, NodeHttpHandleResult, NodeHttpRequest, NodeHttpResponse } from '@orpc/server/node'
3-
import type { StandardHandleOptions } from '@orpc/server/standard'
3+
import type { StandardHandleOptions, StandardHandlerOptions } from '@orpc/server/standard'
44
import type { MaybeOptionalOptions } from '@orpc/shared'
55
import type { SendStandardResponseOptions } from '@orpc/standard-server-node'
6-
import type { OpenAPIHandlerOptions } from '../standard'
6+
import { OpenAPISerializer } from '@orpc/openapi-client/standard'
77
import { StandardHandler } from '@orpc/server/standard'
88
import { sendStandardResponse, toStandardLazyRequest } from '@orpc/standard-server-node'
99
import { OpenAPICodec, OpenAPIMatcher } from '../standard'
1010

1111
export class OpenAPIHandler<T extends Context> implements NodeHttpHandler<T> {
1212
private readonly standardHandler: StandardHandler<T>
1313

14-
constructor(router: Router<T, any>, options?: NoInfer<OpenAPIHandlerOptions<T>>) {
15-
const matcher = options?.matcher ?? new OpenAPIMatcher()
16-
const codec = options?.codec ?? new OpenAPICodec()
14+
constructor(router: Router<T, any>, options: NoInfer<StandardHandlerOptions<T>> = {}) {
15+
const serializer = new OpenAPISerializer()
16+
const matcher = new OpenAPIMatcher()
17+
const codec = new OpenAPICodec(serializer)
1718

18-
this.standardHandler = new StandardHandler(router, matcher, codec, { ...options })
19+
this.standardHandler = new StandardHandler(router, matcher, codec, options)
1920
}
2021

2122
async handle(
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from './openapi-codec'
2-
export * from './openapi-handler'
32
export * from './openapi-matcher'

packages/openapi/src/adapters/standard/openapi-codec.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,4 @@ describe('openAPICodec', () => {
209209
expect(serializer.serialize).toHaveBeenCalledOnce()
210210
expect(serializer.serialize).toHaveBeenCalledWith(error.toJSON())
211211
})
212-
213-
it('work without arguments', async () => {
214-
const codec = new OpenAPICodec()
215-
})
216212
})

0 commit comments

Comments
 (0)