Skip to content

Commit

Permalink
Merge pull request #1521 from input-output-hk/fix/http-provider-proxy…
Browse files Browse the repository at this point in the history
…-has

fix(cardano-services-client): comply HttpProvider with 'normal' Object behavior
  • Loading branch information
mkazlauskas authored Nov 4, 2024
2 parents bb4bba5 + 9900635 commit 4f0330b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/cardano-services-client/src/HttpProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ export const createHttpProvider = <T extends Provider>({
new Proxy<T>({} 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 = {
Expand Down Expand Up @@ -123,5 +121,9 @@ export const createHttpProvider = <T extends Provider>({
throw new ProviderError(ProviderFailure.Unknown, error);
}
};
},

has(_, prop) {
return prop in paths;
}
});
10 changes: 8 additions & 2 deletions packages/cardano-services-client/test/HttpProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 4f0330b

Please sign in to comment.