Skip to content

Commit

Permalink
feat: add validation to github metadata provider
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Aug 27, 2024
1 parent cb70ecc commit 6497dea
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/metadata/src/providers/githubMetadata.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,30 @@ export const GITHUB_METADATA_PREFIX = "github-metadata";
*/
export class GithubMetadataProvider implements IMetadataProvider {
private readonly axios: AxiosInstance;

/**
* Creates a new instance of the GithubMetadataProvider.
*
* @param tokenJsonUrl The URL to fetch the token metadata from.
* @param chainJsonUrl The URL to fetch the chain metadata from.
* @param logger The logger to use.
* @param cache The cache to use.
* @throws InvalidSchema if the tokenJsonUrl or chainJsonUrl is invalid.
*/
constructor(
private readonly tokenJsonUrl: string,
private readonly chainJsonUrl: string,
private readonly logger: ILogger,
private readonly cache: Cache,
) {
if (!z.string().url().safeParse(tokenJsonUrl).success) {
throw new InvalidSchema(`Invalid tokenJsonUrl: ${tokenJsonUrl}`);
}

if (!z.string().url().safeParse(chainJsonUrl).success) {
throw new InvalidSchema(`Invalid chainJsonUrl: ${chainJsonUrl}`);
}

this.axios = axios.create({
headers: {
Accept: "application/json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,57 @@ describe("GithubMetadataProvider", () => {
vi.resetAllMocks();
});

describe("constructor", () => {
it("create a new instance of GithubMetadataProvider", () => {
const tokenJsonUrl = "https://github.com/token.json";
const chainJsonUrl = "https://github.com/chain.json";

const provider = new GithubMetadataProvider(
tokenJsonUrl,
chainJsonUrl,
mockLogger,
mockCache,
);

expect(provider).toBeInstanceOf(GithubMetadataProvider);
expect(provider["tokenJsonUrl"]).toBe(tokenJsonUrl);
expect(provider["chainJsonUrl"]).toBe(chainJsonUrl);
});

it("throw an InvalidSchema error if the tokenJsonUrl is invalid", () => {
const tokenJsonUrl = "invalid-url";
const chainJsonUrl = "https://example.com/chain.json";
const logger = {} as ILogger;
const cache = {} as Cache;

expect(
() => new GithubMetadataProvider(tokenJsonUrl, chainJsonUrl, logger, cache),
).toThrow(InvalidSchema);
});

it("throw an InvalidSchema error if the chainJsonUrl is invalid", () => {
const tokenJsonUrl = "https://example.com/token.json";
const chainJsonUrl = "";
const logger = {} as ILogger;
const cache = {} as Cache;

expect(
() => new GithubMetadataProvider(tokenJsonUrl, chainJsonUrl, logger, cache),
).toThrow(InvalidSchema);
});

it("throw an InvalidSchema error if the chainJsonUrl is undefined", () => {
const tokenJsonUrl = "https://example.com/token.json";
const chainJsonUrl = undefined;
const logger = {} as ILogger;
const cache = {} as Cache;

expect(
() => new GithubMetadataProvider(tokenJsonUrl, chainJsonUrl as any, logger, cache),
).toThrow(InvalidSchema);
});
});

describe("getChainsMetadata", () => {
it("return the cached chains metadata", async () => {
const cachedData = new Map<bigint, ZKChainMetadataItem>([
Expand Down

0 comments on commit 6497dea

Please sign in to comment.