Skip to content

Commit

Permalink
feat(client): add fallback inference for operation resolution
Browse files Browse the repository at this point in the history
Enhanced the resolveOperation function to handle cases where the operationId is not found in the document. This update includes a fallback mechanism that infers a simple `Operation` type when the specified operationId does not exist. This improvement ensures that the function can gracefully handle unknown operation IDs by returning a basic operation structure, which is crucial for maintaining robustness in dynamic API exploration scenarios.

The changes include updates to both the implementation and test files to support and verify the new behavior, ensuring that the function continues to operate correctly under wider usage conditions.
shorwood committed Nov 28, 2024
1 parent 70e2ece commit 06935d2
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/client/openapi/resolveOperation.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Operation } from './resolveOperation'
import { resolveOperation } from './resolveOperation'

describe('getOperationById', () => {
@@ -27,13 +28,18 @@ describe('getOperationById', () => {

describe('inference', () => {
it('should infer the correct operation type', () => {
const result = resolveOperation(document, 'getUser')
expectTypeOf(result).toEqualTypeOf<{
const result = () => resolveOperation(document, 'getUser')
expectTypeOf(result).toEqualTypeOf<() => {
readonly operationId: 'getUser'
method: 'get'
path: '/users/{username}'
}>()
})

it('should infer a simple `Operation` type', () => {
const result = () => resolveOperation(document, 'UNKNOWN')
expectTypeOf(result).toEqualTypeOf<() => Operation>()
})
})

describe('edge cases', () => {
6 changes: 4 additions & 2 deletions packages/client/openapi/resolveOperation.ts
Original file line number Diff line number Diff line change
@@ -40,7 +40,9 @@ export type OperationById<T, U extends OperationId<T>> =
* @returns The resolved operation.
* @example resolveOperation(document, 'getUser') // { method: 'get', path: '/users/{username}', ... }
*/
export function resolveOperation<T, U extends OperationId<T>>(document: T, operationId: U): OperationById<T, U> {
export function resolveOperation<T, U extends OperationId<T>>(document: T, operationId: U): OperationById<T, U>
export function resolveOperation(document: object, operationId: string): Operation
export function resolveOperation(document: object, operationId: string): Operation {

// --- Validate the specification.
if (!document
@@ -67,7 +69,7 @@ export function resolveOperation<T, U extends OperationId<T>>(document: T, opera
|| operation.operationId !== operationId) continue

// --- Route was found, return the operation.
return { ...route[method], method, path } as OperationById<T, U>
return { ...route[method], method, path }
}
}

0 comments on commit 06935d2

Please sign in to comment.