Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cardano-services-client): comply HttpProvider with 'normal' Object behavior #1521

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
mirceahasegan marked this conversation as resolved.
Show resolved Hide resolved
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;
iccicci marked this conversation as resolved.
Show resolved Hide resolved
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();
iccicci marked this conversation as resolved.
Show resolved Hide resolved
});

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
Loading