Skip to content

Commit

Permalink
refactor!: drop support for callbacks
Browse files Browse the repository at this point in the history
BREAKING CHANGE: dropped support for callbacks
  • Loading branch information
jamieweavis committed Feb 25, 2025
1 parent b2bbffb commit dea98ef
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 78 deletions.
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,17 @@ npm install contribution
```javascript
import { fetchStats } from 'contribution';

// Callbacks
fetchStats('jamieweavis', {
onSuccess: (gitHubStats) => console.log(gitHubStats),
onFailure: (error) => console.log(error),
});

// Promises
// Promise chaining
fetchStats('jamieweavis')
.then((gitHubStats) => console.log(gitHubStats))
.catch((error) => console.log(error));

// Async/await
// Try catch with async/await
try {
const gitHubStats = await fetchStats('jamieweavis');
console.log(gitHubStats);
console.info(gitHubStats);
} catch (error) {
console.log(error);
console.error(error);
}
```

Expand Down
11 changes: 2 additions & 9 deletions src/contribution.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import type { GitHubStats } from './transformers';
import { parseContributions, parseGitHubStats } from './transformers';

interface Options {
onSuccess?: (stats: GitHubStats) => unknown;
onFailure?: (error: Response) => unknown;
}

const fetchStats = async (username: string, options?: Options) => {
const fetchStats = async (username: string): Promise<GitHubStats> => {
try {
const response = await fetch(
`https://github.com/users/${username}/contributions`,
Expand All @@ -17,11 +12,9 @@ const fetchStats = async (username: string, options?: Options) => {
const contributions = parseContributions(body);
const gitHubStats = parseGitHubStats(contributions);

if (options?.onSuccess) return options.onSuccess(gitHubStats);
return gitHubStats;
} catch (error) {
if (options?.onFailure) return options.onFailure(error);
throw error;
throw new Error(error);
}
};

Expand Down
61 changes: 2 additions & 59 deletions src/tests/contribution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,9 @@ describe('fetchStats', () => {
);
expect(parseContributions).toHaveBeenCalledWith(mockHtmlResponse);
expect(parseGitHubStats).toHaveBeenCalledWith(mockContributions);
expect(result).toEqual(mockGitHubStats);
});

it('should call onSuccess callback when provided', async () => {
(global.fetch as jest.Mock).mockResolvedValue({
ok: true,
text: jest.fn().mockResolvedValue(mockHtmlResponse),
});
const onSuccess = jest.fn();

await fetchStats(mockUsername, { onSuccess });

expect(onSuccess).toHaveBeenCalledWith(mockGitHubStats);
});

it('should handle async/await correctly with a successful response', async () => {
it('should resolve with a successful response', async () => {
(global.fetch as jest.Mock).mockResolvedValue({
ok: true,
text: jest.fn().mockResolvedValue(mockHtmlResponse),
Expand All @@ -61,7 +48,7 @@ describe('fetchStats', () => {
await expect(fetchStats(mockUsername)).resolves.toEqual(mockGitHubStats);
});

it('should handle async/await correctly with a failed response', async () => {
it('should reject with a failed response', async () => {
(global.fetch as jest.Mock).mockResolvedValue({ ok: false });

await expect(fetchStats(mockUsername)).rejects.toThrow(
Expand All @@ -75,16 +62,6 @@ describe('fetchStats', () => {
await expect(fetchStats(mockUsername)).rejects.toThrow('Network error');
});

it('should call onFailure callback when fetch fails', async () => {
const error = new Error('Network error');
(global.fetch as jest.Mock).mockRejectedValue(error);
const onFailure = jest.fn();

await fetchStats(mockUsername, { onFailure });

expect(onFailure).toHaveBeenCalledWith(error);
});

it('should throw an error when response is not ok', async () => {
(global.fetch as jest.Mock).mockResolvedValue({
ok: false,
Expand All @@ -96,38 +73,4 @@ describe('fetchStats', () => {
'Failed to fetch GitHub contributions',
);
});

it('should call onFailure when response is not ok', async () => {
const error = new Error('Failed to fetch GitHub contributions');
(global.fetch as jest.Mock).mockResolvedValue({ ok: false });
const onFailure = jest.fn();

await fetchStats(mockUsername, { onFailure });

expect(onFailure).toHaveBeenCalled();
});

it('should properly execute onSuccess with async/await', async () => {
(global.fetch as jest.Mock).mockResolvedValue({
ok: true,
text: jest.fn().mockResolvedValue(mockHtmlResponse),
});
const onSuccess = jest.fn();

await expect(
fetchStats(mockUsername, { onSuccess }),
).resolves.toBeUndefined();
expect(onSuccess).toHaveBeenCalledWith(mockGitHubStats);
});

it('should properly execute onFailure with async/await', async () => {
const error = new Error('Network error');
(global.fetch as jest.Mock).mockRejectedValue(error);
const onFailure = jest.fn();

await expect(
fetchStats(mockUsername, { onFailure }),
).resolves.toBeUndefined();
expect(onFailure).toHaveBeenCalledWith(error);
});
});

0 comments on commit dea98ef

Please sign in to comment.