Skip to content

Commit

Permalink
feat: added the getHealthyProvidersWhiteList helper for requestors
Browse files Browse the repository at this point in the history
So that they are able to use it with the whitelisting filters and work with a healthy provider

JST-468.
  • Loading branch information
grisha87 committed Oct 4, 2023
1 parent 5d9bb73 commit f7cb4e1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"devDependencies": {
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@johanblumenberg/ts-mockito": "^1.0.39",
"@rollup/plugin-alias": "^5.0.0",
"@rollup/plugin-commonjs": "^25.0.3",
"@rollup/plugin-json": "^6.0.0",
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default [
commonjs(),
nodePolyfills(),
json(), // Required because one our dependencies (bottleneck) loads its own 'version.json'
typescript({ tsconfig: "./tsconfig.json" }),
typescript({ tsconfig: "./tsconfig.json", exclude: ["**/__tests__", "**/*.test.ts"] }),
terser({ keep_classnames: true }),
],
},
Expand All @@ -52,6 +52,6 @@ export default [
{ file: pkg.main, format: "cjs", sourcemap: true },
{ file: pkg.module, format: "es", sourcemap: true },
],
plugins: [typescript({ tsconfig: "./tsconfig.json" })],
plugins: [typescript({ tsconfig: "./tsconfig.json", exclude: ["**/__tests__", "**/*.test.ts"] })],
},
];
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export {
} from "./storage";
export { ActivityStateEnum, Result } from "./activity";
export { AgreementCandidate, AgreementSelectors } from "./agreement";
export { ProposalFilters, ProposalFilter } from "./market";
export { ProposalFilters, ProposalFilter, Helpers } from "./market";
export { Package, PackageOptions } from "./package";
export { PaymentFilters } from "./payment";
export { Events, BaseEvent, EventType } from "./events";
Expand Down
56 changes: 56 additions & 0 deletions src/market/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { MockPropertyPolicy, imock, instance, when } from "@johanblumenberg/ts-mockito";

import { getHealthyProvidersWhiteList } from "./helpers";

const mockFetch = jest.spyOn(global, "fetch");
const response = imock<Response>();

beforeEach(() => {
jest.resetAllMocks();
});

describe("Market Helpers", () => {
describe("Getting public healthy providers whitelist", () => {
describe("Positive cases", () => {
test("Will return the list returned by the endpoint", async () => {
// Given
when(response.json()).thenResolve(["0xAAA", "0xBBB"]);
mockFetch.mockResolvedValue(instance(response));

// When
const data = await getHealthyProvidersWhiteList();

// Then
expect(data).toEqual(["0xAAA", "0xBBB"]);
});
});

describe("Negative cases", () => {
test("If the request will be made, but will not be a successful one, it will return an empty array", async () => {
// Given
const mockResponse = imock<Response>(MockPropertyPolicy.StubAsProperty);
when(mockResponse.ok).thenReturn(false);
mockFetch.mockResolvedValue(instance(mockResponse));

// When
const data = await getHealthyProvidersWhiteList();

// Then
expect(data).toEqual([]);
});

test("If the implementation will throw any kind of error, then it will return an empty array", async () => {
// Given
mockFetch.mockImplementation(() => {
throw new Error("Something went wrong really bad!");
});

// When
const data = await getHealthyProvidersWhiteList();

// Then
expect(data).toEqual([]);
});
});
});
});
18 changes: 18 additions & 0 deletions src/market/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export async function getHealthyProvidersWhiteList(): Promise<string[]> {
try {
const response = await fetch("https://provider-health.golem.network/v1/provider-whitelist");

if (response.ok) {
return response.json();
} else {
const body = await response.text();
console.error("Request to download healthy provider whitelist failed.", body);

return [];
}
} catch (err) {
console.error("Failed to download healthy provider whitelist due to an error", err);

return [];
}
}
1 change: 1 addition & 0 deletions src/market/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { Proposal, ProposalDetails } from "./proposal";
export { MarketDecoration } from "./builder";
export { DemandConfig } from "./config";
export * as ProposalFilters from "./strategy";
export * as Helpers from "./helpers";

0 comments on commit f7cb4e1

Please sign in to comment.