Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"docula": "./bin/docula.mjs"
},
"dependencies": {
"axios": "^1.12.2",
"@cacheable/net": "^2.0.1",
"cheerio": "^1.1.2",
"ecto": "^4.6.0",
"feed": "^5.1.0",
Expand Down
17 changes: 10 additions & 7 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from "node:process";
import axios from "axios";
import { CacheableNet } from "@cacheable/net";

export type GithubOptions = {
api?: string | undefined;
Expand All @@ -19,8 +19,11 @@ export class Github {
repo: "",
};

private net: CacheableNet;

constructor(options: GithubOptions) {
this.parseOptions(options);
this.net = new CacheableNet();
}

async getData(): Promise<GithubData> {
Expand All @@ -39,9 +42,9 @@ export class Github {
// biome-ignore lint/suspicious/noExplicitAny: need to fix
async getReleases(): Promise<any> {
const url = `${this.options.api}/repos/${this.options.author}/${this.options.repo}/releases`;
let config = {};
let options = {};
if (process.env.GITHUB_TOKEN) {
config = {
options = {
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
Accept: "application/vnd.github.v3+json",
Expand All @@ -50,7 +53,7 @@ export class Github {
}

try {
const result = await axios.get(url, config);
const result = await this.net.get<unknown[]>(url, options);

if (result && result.data.length > 0) {
// biome-ignore lint/suspicious/noExplicitAny: need to fix
Expand All @@ -73,9 +76,9 @@ export class Github {
// biome-ignore lint/suspicious/noExplicitAny: need to fix
async getContributors(): Promise<any> {
const url = `${this.options.api}/repos/${this.options.author}/${this.options.repo}/contributors`;
let config = {};
let options = {};
if (process.env.GITHUB_TOKEN) {
config = {
options = {
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
Accept: "application/vnd.github.v3+json",
Expand All @@ -84,7 +87,7 @@ export class Github {
}

try {
const result = await axios.get(url, config);
const result = await this.net.get<unknown[]>(url, options);
if (result && result.data.length > 0) {
return result.data;
}
Expand Down
8 changes: 4 additions & 4 deletions test/builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "node:fs";
import axios from "axios";
import { CacheableNet } from "@cacheable/net";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
DoculaBuilder,
Expand All @@ -11,7 +11,7 @@ import { DoculaOptions } from "../src/options.js";
import githubMockContributors from "./fixtures/data-mocks/github-contributors.json";
import githubMockReleases from "./fixtures/data-mocks/github-releases.json";

vi.mock("axios");
vi.mock("@cacheable/net");

describe("DoculaBuilder", () => {
const doculaData: DoculaData = {
Expand All @@ -29,7 +29,7 @@ describe("DoculaBuilder", () => {
});
beforeEach(() => {
// biome-ignore lint/suspicious/noExplicitAny: test file
(axios.get as any).mockImplementation(async (url: string) => {
(CacheableNet.prototype.get as any) = vi.fn(async (url: string) => {
if (url.endsWith("releases")) {
return { data: githubMockReleases };
}
Expand Down Expand Up @@ -147,7 +147,7 @@ describe("DoculaBuilder", () => {
describe("Docula Builder - Get Data", () => {
it("should get github data", async () => {
const builder = new DoculaBuilder();
vi.spyOn(axios, "get").mockResolvedValue({ data: {} });
CacheableNet.prototype.get = vi.fn().mockResolvedValue({ data: {} });
const githubData = await builder.getGithubData("jaredwray/docula");
expect(githubData).toBeTruthy();
vi.resetAllMocks();
Expand Down
6 changes: 3 additions & 3 deletions test/docula.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import axios from "axios";
import { CacheableNet } from "@cacheable/net";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import Docula, { DoculaHelpers } from "../src/docula.js";
import { DoculaOptions } from "../src/options.js";
Expand All @@ -26,7 +26,7 @@ const defaultOptions: DoculaOptions = new DoculaOptions({
siteUrl: "https://custom-url.com",
});

vi.mock("axios");
vi.mock("@cacheable/net");

describe("docula", () => {
afterEach(() => {
Expand All @@ -35,7 +35,7 @@ describe("docula", () => {
});
beforeEach(() => {
// biome-ignore lint/suspicious/noExplicitAny: test file
(axios.get as any).mockImplementation(async (url: string) => {
(CacheableNet.prototype.get as any) = vi.fn(async (url: string) => {
if (url.endsWith("releases")) {
return { data: githubMockReleases };
}
Expand Down
55 changes: 24 additions & 31 deletions test/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from "node:process";
import axios from "axios";
import { CacheableNet } from "@cacheable/net";
import dotenv from "dotenv";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { Github, type GithubOptions } from "../src/github.js";
Expand All @@ -12,7 +12,7 @@ const defaultOptions: GithubOptions = {
repo: "docula",
};

vi.mock("axios");
vi.mock("@cacheable/net");

describe("Github", () => {
afterEach(() => {
Expand All @@ -21,7 +21,7 @@ describe("Github", () => {
});
beforeEach(() => {
// biome-ignore lint/suspicious/noExplicitAny: test file
(axios.get as any).mockImplementation(async (url: string) => {
(CacheableNet.prototype.get as any) = vi.fn(async (url: string) => {
if (url.endsWith("releases")) {
return { data: githubMockReleases };
}
Expand Down Expand Up @@ -52,22 +52,23 @@ describe("Github", () => {
});
it("should be able to get the contributors", async () => {
const github = new Github(defaultOptions);
// @ts-expect-error - mock

axios.get.mockResolvedValue({ data: githubMockContributors });
CacheableNet.prototype.get = vi
.fn()
.mockResolvedValue({ data: githubMockContributors });

const result = await github.getContributors();
expect(result).toBeDefined();
});
it("should use GITHUB_TOKEN for contributors if present", async () => {
process.env.GITHUB_TOKEN = "test-token";
const mockGet = vi.fn().mockResolvedValue({ data: githubMockContributors });
CacheableNet.prototype.get = mockGet;
const github = new Github(defaultOptions);
// @ts-expect-error - mock
axios.get.mockResolvedValue({ data: githubMockContributors });

await github.getContributors();

expect(axios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
`${defaultOptions.api}/repos/${defaultOptions.author}/${defaultOptions.repo}/contributors`,
{
headers: {
Expand All @@ -80,54 +81,51 @@ describe("Github", () => {
delete process.env.GITHUB_TOKEN;
});
it("should be throw an error on 404", async () => {
const github = new Github(defaultOptions);
const errorResponse = {
response: {
status: 404,
data: "Not Found",
},
};
// @ts-expect-error - mock

axios.get.mockRejectedValue(errorResponse);
CacheableNet.prototype.get = vi.fn().mockRejectedValue(errorResponse);
const github = new Github(defaultOptions);

await expect(github.getContributors()).rejects.toThrow(
`Repository ${defaultOptions.author}/${defaultOptions.repo} not found.`,
);
});
it("should be throw an error", async () => {
const github = new Github(defaultOptions);
const errorResponse = {
response: {
status: 500,
data: "Server Error",
},
};
// @ts-expect-error - mock

axios.get.mockRejectedValue(errorResponse);
CacheableNet.prototype.get = vi.fn().mockRejectedValue(errorResponse);
const github = new Github(defaultOptions);

await expect(github.getContributors()).rejects.toThrow();
});
it("should be able to get the releases", async () => {
const github = new Github(defaultOptions);
// @ts-expect-error - mock

axios.get.mockResolvedValue({ data: githubMockReleases });
CacheableNet.prototype.get = vi
.fn()
.mockResolvedValue({ data: githubMockReleases });

const result = await github.getReleases();

expect(result).toBeDefined();
});
it("should use GITHUB_TOKEN for releases if present", async () => {
process.env.GITHUB_TOKEN = "test-token";
const mockGet = vi.fn().mockResolvedValue({ data: githubMockReleases });
CacheableNet.prototype.get = mockGet;
const github = new Github(defaultOptions);
// @ts-expect-error - mock
axios.get.mockResolvedValue({ data: githubMockReleases });

await github.getReleases();

expect(axios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
`${defaultOptions.api}/repos/${defaultOptions.author}/${defaultOptions.repo}/releases`,
{
headers: {
Expand All @@ -140,40 +138,35 @@ describe("Github", () => {
delete process.env.GITHUB_TOKEN;
});
it("should return empty array when no releases found", async () => {
CacheableNet.prototype.get = vi.fn().mockResolvedValue({ data: [] });
const github = new Github(defaultOptions);
// @ts-expect-error - mock
axios.get.mockResolvedValue({ data: [] });

const result = await github.getReleases();
expect(result).toEqual([]);
});
it("should be throw an error on 404", async () => {
const github = new Github(defaultOptions);
const errorResponse = {
response: {
status: 404,
data: "Not Found",
},
};
// @ts-expect-error - mock

axios.get.mockRejectedValue(errorResponse);
CacheableNet.prototype.get = vi.fn().mockRejectedValue(errorResponse);
const github = new Github(defaultOptions);

await expect(github.getReleases()).rejects.toThrow(
`Repository ${defaultOptions.author}/${defaultOptions.repo} not found.`,
);
});
it("should be throw an error", async () => {
const github = new Github(defaultOptions);
const errorResponse = {
response: {
status: 500,
data: "Server Error",
},
};
// @ts-expect-error - mock

axios.get.mockRejectedValue(errorResponse);
CacheableNet.prototype.get = vi.fn().mockRejectedValue(errorResponse);
const github = new Github(defaultOptions);

await expect(github.getReleases()).rejects.toThrow();
});
Expand Down
Loading