From 06935d2427fa7cb7f003b0ae8628c17b7aa928c4 Mon Sep 17 00:00:00 2001 From: Stanley Horwood Date: Thu, 28 Nov 2024 23:09:49 +0100 Subject: [PATCH] feat(client): add fallback inference for operation resolution 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. --- packages/client/openapi/resolveOperation.test.ts | 10 ++++++++-- packages/client/openapi/resolveOperation.ts | 6 ++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/client/openapi/resolveOperation.test.ts b/packages/client/openapi/resolveOperation.test.ts index 95e9eda3..fbda4704 100644 --- a/packages/client/openapi/resolveOperation.test.ts +++ b/packages/client/openapi/resolveOperation.test.ts @@ -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', () => { diff --git a/packages/client/openapi/resolveOperation.ts b/packages/client/openapi/resolveOperation.ts index a9bd112b..db0e7f38 100644 --- a/packages/client/openapi/resolveOperation.ts +++ b/packages/client/openapi/resolveOperation.ts @@ -40,7 +40,9 @@ export type OperationById> = * @returns The resolved operation. * @example resolveOperation(document, 'getUser') // { method: 'get', path: '/users/{username}', ... } */ -export function resolveOperation>(document: T, operationId: U): OperationById { +export function resolveOperation>(document: T, operationId: U): OperationById +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>(document: T, opera || operation.operationId !== operationId) continue // --- Route was found, return the operation. - return { ...route[method], method, path } as OperationById + return { ...route[method], method, path } } }