From 9900635e8c32c2c0330426d795c170e541b52bcf Mon Sep 17 00:00:00 2001 From: Martynas Kazlauskas Date: Mon, 4 Nov 2024 17:09:01 +0200 Subject: [PATCH] fix(cardano-services-client): comply HttpProvider with 'normal' Object behavior HttpProvider is implemented as a Proxy object, which is currently not compatible with SDK's web-extension messaging utilities that are checking exposed/underlying object properties with 'in' operator This commit has 2 fixes: - do not throw an error when trying to access an undefined property of an object - return 'true' with with 'in' operator for defined properties --- packages/cardano-services-client/src/HttpProvider.ts | 8 +++++--- .../cardano-services-client/test/HttpProvider.test.ts | 10 ++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/cardano-services-client/src/HttpProvider.ts b/packages/cardano-services-client/src/HttpProvider.ts index 0d239525b77..76ee5b18e5f 100644 --- a/packages/cardano-services-client/src/HttpProvider.ts +++ b/packages/cardano-services-client/src/HttpProvider.ts @@ -78,11 +78,9 @@ export const createHttpProvider = ({ new Proxy({} as T, { // eslint-disable-next-line sonarjs/cognitive-complexity get(_, prop) { - if (prop === 'then') return; const method = prop as keyof T; const urlPath = paths[method]; - if (!urlPath) - throw new ProviderError(ProviderFailure.NotImplemented, `HttpProvider missing path for '${prop.toString()}'`); + if (!urlPath) return; return async (...args: any[]) => { try { const req: AxiosRequestConfig = { @@ -123,5 +121,9 @@ export const createHttpProvider = ({ throw new ProviderError(ProviderFailure.Unknown, error); } }; + }, + + has(_, prop) { + return prop in paths; } }); diff --git a/packages/cardano-services-client/test/HttpProvider.test.ts b/packages/cardano-services-client/test/HttpProvider.test.ts index 656a7f89b3b..12f992895b5 100644 --- a/packages/cardano-services-client/test/HttpProvider.test.ts +++ b/packages/cardano-services-client/test/HttpProvider.test.ts @@ -63,10 +63,16 @@ describe('createHttpProvider', () => { if (closeServer) await closeServer(); }); - it('attempting to access unimplemented method throws ProviderError', async () => { + it('attempting to access unimplemented method returns undefined', async () => { const provider = createTxSubmitProviderClient(); + expect('doesNotExist' in provider).toBe(false); // eslint-disable-next-line @typescript-eslint/no-explicit-any - expect(() => (provider as any).doesNotExist).toThrowError(ProviderError); + expect((provider as any).doesNotExist).toBeUndefined(); + }); + + it('"in" operator for implemented property returns true', async () => { + const provider = createTxSubmitProviderClient(); + expect('healthCheck' in provider).toBe(true); }); it('passes through axios options, merging custom header with the included provider version headers', async () => {