Skip to content

Commit 97eb6aa

Browse files
committed
fix: hide route metadata marker type
1 parent 630813d commit 97eb6aa

3 files changed

Lines changed: 27 additions & 28 deletions

File tree

‎src/http.ts‎

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
getRouteMetadata,
44
stripRouteMetadata,
55
type RouteMetadata,
6-
type RouteMetadataMarker,
76
} from './metadata.js'
87
import type { RawBodySchema, RouteSchema } from './types/schema.js'
98

@@ -59,8 +58,6 @@ export type HttpNode = HttpAction | HttpResource
5958
/** Route tree accepted by HTTP clients and routers. */
6059
export type HttpRouteTree = { [key: string]: HttpNode }
6160

62-
type RouteDeclaration<T extends object> = T & Partial<RouteMetadataMarker>
63-
6461
type StringKeys<T> = Pick<T, Extract<keyof T, string>>
6562

6663
/**
@@ -75,7 +72,7 @@ export function resource<
7572
const TChildren extends HttpRouteTree,
7673
>(
7774
path: P,
78-
children: RouteDeclaration<TChildren>
75+
children: TChildren
7976
): HttpResource<P, StringKeys<TChildren>> {
8077
const metadata = getRouteMetadata(children)
8178
return {
@@ -89,74 +86,74 @@ export function resource<
8986
/** Declare a GET action, optionally with an action-local path segment. */
9087
export function get<const P extends string, const T extends RouteSchema>(
9188
path: P,
92-
schema: RouteDeclaration<T>
89+
schema: T
9390
): HttpAction<P, T, 'GET'>
9491
export function get<const T extends RouteSchema>(
95-
schema: RouteDeclaration<T>
92+
schema: T
9693
): HttpAction<'', T, 'GET'>
9794
export function get(
98-
pathOrSchema: string | RouteDeclaration<RouteSchema>,
99-
schema?: RouteDeclaration<RouteSchema>
95+
pathOrSchema: string | RouteSchema,
96+
schema?: RouteSchema
10097
): any {
10198
return action('GET', pathOrSchema, schema)
10299
}
103100

104101
/** Declare a POST action, optionally with an action-local path segment. */
105102
export function post<const P extends string, const T extends RouteSchema>(
106103
path: P,
107-
schema: RouteDeclaration<T>
104+
schema: T
108105
): HttpAction<P, T, 'POST'>
109106
export function post<const T extends RouteSchema>(
110-
schema: RouteDeclaration<T>
107+
schema: T
111108
): HttpAction<'', T, 'POST'>
112109
export function post(
113-
pathOrSchema: string | RouteDeclaration<RouteSchema>,
114-
schema?: RouteDeclaration<RouteSchema>
110+
pathOrSchema: string | RouteSchema,
111+
schema?: RouteSchema
115112
): any {
116113
return action('POST', pathOrSchema, schema)
117114
}
118115

119116
/** Declare a PUT action, optionally with an action-local path segment. */
120117
export function put<const P extends string, const T extends RouteSchema>(
121118
path: P,
122-
schema: RouteDeclaration<T>
119+
schema: T
123120
): HttpAction<P, T, 'PUT'>
124121
export function put<const T extends RouteSchema>(
125-
schema: RouteDeclaration<T>
122+
schema: T
126123
): HttpAction<'', T, 'PUT'>
127124
export function put(
128-
pathOrSchema: string | RouteDeclaration<RouteSchema>,
129-
schema?: RouteDeclaration<RouteSchema>
125+
pathOrSchema: string | RouteSchema,
126+
schema?: RouteSchema
130127
): any {
131128
return action('PUT', pathOrSchema, schema)
132129
}
133130

134131
/** Declare a PATCH action, optionally with an action-local path segment. */
135132
export function patch<const P extends string, const T extends RouteSchema>(
136133
path: P,
137-
schema: RouteDeclaration<T>
134+
schema: T
138135
): HttpAction<P, T, 'PATCH'>
139136
export function patch<const T extends RouteSchema>(
140-
schema: RouteDeclaration<T>
137+
schema: T
141138
): HttpAction<'', T, 'PATCH'>
142139
export function patch(
143-
pathOrSchema: string | RouteDeclaration<RouteSchema>,
144-
schema?: RouteDeclaration<RouteSchema>
140+
pathOrSchema: string | RouteSchema,
141+
schema?: RouteSchema
145142
): any {
146143
return action('PATCH', pathOrSchema, schema)
147144
}
148145

149146
/** Declare a DELETE action, optionally with an action-local path segment. */
150147
function deleteAction<const P extends string, const T extends RouteSchema>(
151148
path: P,
152-
schema: RouteDeclaration<T>
149+
schema: T
153150
): HttpAction<P, T, 'DELETE'>
154151
function deleteAction<const T extends RouteSchema>(
155-
schema: RouteDeclaration<T>
152+
schema: T
156153
): HttpAction<'', T, 'DELETE'>
157154
function deleteAction(
158-
pathOrSchema: string | RouteDeclaration<RouteSchema>,
159-
schema?: RouteDeclaration<RouteSchema>
155+
pathOrSchema: string | RouteSchema,
156+
schema?: RouteSchema
160157
): any {
161158
return action('DELETE', pathOrSchema, schema)
162159
}
@@ -182,8 +179,8 @@ export function isRawBodySchema(schema: unknown): schema is RawBodySchema {
182179

183180
function action(
184181
method: HttpMethod,
185-
pathOrSchema: string | RouteDeclaration<RouteSchema>,
186-
schema?: RouteDeclaration<RouteSchema>
182+
pathOrSchema: string | RouteSchema,
183+
schema?: RouteSchema
187184
) {
188185
const path =
189186
typeof pathOrSchema === 'string'

‎src/metadata.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ export type RouteMetadata = {
88
description?: string
99
}
1010

11-
export type RouteMetadataMarker = {
11+
type RouteMetadataMarker = {
1212
readonly [routeMetadataKey]: RouteMetadata
1313
}
1414

1515
/** Attach runtime metadata to a route declaration. */
16-
export function metadata(value: RouteMetadata): RouteMetadataMarker {
16+
export function metadata(value: RouteMetadata): object {
1717
return { [routeMetadataKey]: value }
1818
}
1919

‎test/metadata.test-d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const client = createClient({
2929
})
3030

3131
test('metadata does not change client or handler types', () => {
32+
expectTypeOf(metadata({ summary: 'Sessions' })).toEqualTypeOf<object>()
33+
3234
expectTypeOf<
3335
Extract<keyof typeof sessions.children.list.schema, string>
3436
>().toEqualTypeOf<'path' | 'body'>()

0 commit comments

Comments
 (0)