fix: enhance type safety for HTTP methods in path parameters #157
Annotations
2 errors
|
ci
Process completed with exit code 1.
|
|
test/openapi.test.ts > OpenAPI to TypeScript > generates OpenAPI types:
test/openapi.test.ts#L19
Error: Snapshot `OpenAPI to TypeScript > generates OpenAPI types 1` mismatched
- Expected
+ Received
@@ -11,11 +11,14 @@
: [undefined] extends [T[K]]
? [never] extends [Exclude<T[K], undefined>] ? never : K
: K;
}[keyof T];
type NonNeverKeysWithoutParams<T> = Exclude<NonNeverKeys<T>, 'parameters'>
- type ParseInt<S extends string> = S extends `${infer N extends number}` ? N : never
+ type ParseInt<S extends string> = S extends `${infer N extends number}` ? N : never
+
+ // Generic HttpMethodsForPath type that works with any path object
+ type HttpMethodsForPath<T, P extends keyof T> = NonNeverKeysWithoutParams<T[P]>
interface OpenAPISchemaRepository {
testEcho: TestEchoPaths
}
@@ -40,11 +43,11 @@
never
// Direct type that allows accessing path parameters by specifying the HTTP method
export type PathParamsFromTestEcho<
P extends keyof TestEchoPaths,
- M extends NonNeverKeysWithoutParams<TestEchoPaths[P]>
+ M extends HttpMethodsForPath<TestEchoPaths, P> = HttpMethodsForPath<TestEchoPaths, P> extends string ? HttpMethodsForPath<TestEchoPaths, P> : never
> = GetOperation<TestEchoPaths[P], M> extends infer Op
? Op extends { parameters?: any }
? NonNullable<Op['parameters']>['path'] extends infer Params
? Params extends object
? Params
@@ -54,11 +57,11 @@
: Record<string, never>
// Direct type that allows accessing request body by specifying the HTTP method
export type RequestBodyFromTestEcho<
P extends keyof TestEchoPaths,
- M extends NonNeverKeysWithoutParams<TestEchoPaths[P]>
+ M extends HttpMethodsForPath<TestEchoPaths, P> = HttpMethodsForPath<TestEchoPaths, P> extends string ? HttpMethodsForPath<TestEchoPaths, P> : never
> = GetOperation<TestEchoPaths[P], M> extends infer Op
? Op extends { requestBody?: any }
? NonNullable<Op['requestBody']>['content']['application/json'] extends infer Body
? Body extends object
? Body
@@ -68,11 +71,11 @@
: Record<string, never>
// Direct type that allows accessing query parameters by specifying the HTTP method
export type QueryParamsFromTestEcho<
P extends keyof TestEchoPaths,
- M extends NonNeverKeysWithoutParams<TestEchoPaths[P]>
+ M extends HttpMethodsForPath<TestEchoPaths, P> = HttpMethodsForPath<TestEchoPaths, P> extends string ? HttpMethodsForPath<TestEchoPaths, P> : never
> = GetOperation<TestEchoPaths[P], M> extends infer Op
? Op extends { parameters?: any }
? NonNullable<Op['parameters']>['query'] extends infer Params
? Params extends object
? Params
@@ -82,11 +85,11 @@
: Record<string, never>
// Direct type that allows accessing response body by specifying the HTTP method
export type ResponseFromTestEcho<
P extends keyof TestEchoPaths,
- M extends NonNeverKeysWithoutParams<TestEchoPaths[P]>,
+ M extends HttpMethodsForPath<TestEchoPaths, P> = HttpMethodsForPath<TestEchoPaths, P> extends string ? HttpMethodsForPath<TestEchoPaths, P> : never,
C extends `${keyof NonNullable<GetOperation<TestEchoPaths[P], M>>['responses']}` = '200'
> = GetOperation<TestEchoPaths[P], M> extends infer Op
? Op extends { responses?: any }
? ParseInt<C> extends keyof Op['responses']
? Op['responses'][ParseInt<C>] extends { content: { 'application/json': infer Body } }
❯ test/openapi.test.ts:19:19
|