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
4 changes: 2 additions & 2 deletions typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.5",
"version": "0.1.6",
"name": "@mastercard/developers-agent-toolkit",
"homepage": "https://github.com/mastercard/developers-agent-toolkit",
"description": "Agent Toolkit for Mastercard Developers Platform",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { fetchGithubReadme } from '@/shared/tools/documentation/githubReadme';
import { createMockApi } from '@/tests/mockDevelopersApi';
import fetch from 'node-fetch';

jest.mock('node-fetch');

const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
const mockApi = createMockApi();

describe('fetchGithubReadme', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('uses fetchGithubGuide when provided and returns content on success', async () => {
const fetchGithubGuide = jest.fn().mockResolvedValue('github content');

const result = await fetchGithubReadme('oauth1-signer-java', {
client: { ...mockApi, fetchGithubGuide },
});

expect(fetchGithubGuide).toHaveBeenCalledWith(
'https://raw.githubusercontent.com/Mastercard/oauth1-signer-java/refs/heads/main/README.md'
);
expect(result).toBe('github content');
});

it('returns undefined when fetchGithubGuide is provided but fails', async () => {
const fetchGithubGuide = jest
.fn()
.mockRejectedValue(new Error('network error'));

const result = await fetchGithubReadme('oauth1-signer-java', {
client: { ...mockApi, fetchGithubGuide },
});

expect(result).toBeUndefined();
});

it('uses plain fetch when fetchGithubGuide is not provided and returns content on success', async () => {
mockFetch.mockResolvedValue({
ok: true,
text: jest.fn().mockResolvedValue('github content'),
} as any);

const result = await fetchGithubReadme('oauth1-signer-java', {
client: mockApi,
});

expect(mockFetch).toHaveBeenCalledWith(
'https://raw.githubusercontent.com/Mastercard/oauth1-signer-java/refs/heads/main/README.md'
);
expect(result).toBe('github content');
});

it('returns undefined when plain fetch response is not ok', async () => {
mockFetch.mockResolvedValue({ ok: false } as any);

const result = await fetchGithubReadme('oauth1-signer-java', {
client: mockApi,
});

expect(result).toBeUndefined();
});
});
10 changes: 4 additions & 6 deletions typescript/src/shared/tools/documentation/getOAuth10aGuide.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod';
import fetch from 'node-fetch';
import { Tool, ToolContext } from '@/shared/types';
import { fetchGithubReadme } from './githubReadme';

const getDescription = (): string => {
return `Retrieves the comprehensive OAuth 1.0a integration guide including step-by-step instructions,
Expand Down Expand Up @@ -58,11 +58,9 @@ export const execute = async (
}

if (repositoryName !== undefined) {
const githubUrl = `https://raw.githubusercontent.com/Mastercard/${repositoryName}/refs/heads/main/README.md`;
const response = await fetch(githubUrl);

if (response.ok) {
return await response.text();
const content = await fetchGithubReadme(repositoryName, context);
if (content !== undefined) {
return content;
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions typescript/src/shared/tools/documentation/getOAuth20Guide.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod';
import fetch from 'node-fetch';
import { Tool, ToolContext } from '@/shared/types';
import { fetchGithubReadme } from './githubReadme';

const getDescription = (): string => {
return `Retrieves the comprehensive OAuth 2.0 integration guide including step-by-step instructions,
Expand Down Expand Up @@ -40,11 +40,9 @@ export const execute = async (
}

if (repositoryName !== undefined) {
const githubUrl = `https://raw.githubusercontent.com/Mastercard/${repositoryName}/refs/heads/main/README.md`;
const response = await fetch(githubUrl);

if (response.ok) {
return await response.text();
const content = await fetchGithubReadme(repositoryName, context);
if (content !== undefined) {
return content;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions typescript/src/shared/tools/documentation/githubReadme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fetch from 'node-fetch';
import { ToolContext } from '@/shared/types';

export async function fetchGithubReadme(
repositoryName: string,
context: ToolContext
): Promise<string | undefined> {
const url = `https://raw.githubusercontent.com/Mastercard/${repositoryName}/refs/heads/main/README.md`;
if (context.client.fetchGithubGuide !== undefined) {
return context.client.fetchGithubGuide(url).catch(() => undefined);
}
const response = await fetch(url);
return response.ok ? response.text() : undefined;
}
1 change: 1 addition & 0 deletions typescript/src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface DevelopersApi {
method: string,
path: string
): Promise<string>;
fetchGithubGuide?(url: string): Promise<string>;
}

export interface ToolContext {
Expand Down
Loading