Initial release v1.0.0
Open-source marketplace for AI Agent skills. Features: - Next.js 15 web app with i18n (en/fa) - CLI tool for skill installation (npx skillhub) - GitHub crawler/indexer with multi-strategy discovery - Security scanning for all indexed skills - Self-hostable with Docker Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
94
apps/web/app/api/skills/recent/route.test.ts
Normal file
94
apps/web/app/api/skills/recent/route.test.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
// Helper to create mock skill
|
||||
function createMockSkill(overrides: Partial<{
|
||||
id: string;
|
||||
name: string;
|
||||
updatedAt: Date;
|
||||
}> = {}) {
|
||||
return {
|
||||
id: 'test-owner/test-repo/test-skill',
|
||||
name: 'test-skill',
|
||||
description: 'A test skill',
|
||||
githubOwner: 'test-owner',
|
||||
githubRepo: 'test-repo',
|
||||
githubStars: 100,
|
||||
downloadCount: 50,
|
||||
securityScore: 85,
|
||||
isVerified: false,
|
||||
compatibility: { platforms: ['claude'] },
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date(),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// Mock db
|
||||
vi.mock('@skillhub/db', () => ({
|
||||
createDb: vi.fn(() => ({})),
|
||||
skillQueries: {
|
||||
getRecent: vi.fn(),
|
||||
},
|
||||
skills: {},
|
||||
}));
|
||||
|
||||
import { GET } from './route';
|
||||
import { skillQueries } from '@skillhub/db';
|
||||
|
||||
describe('GET /api/skills/recent', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should return recent skills', async () => {
|
||||
const mockSkills = [
|
||||
createMockSkill({ id: 'skill-1', updatedAt: new Date('2024-01-02') }),
|
||||
createMockSkill({ id: 'skill-2', updatedAt: new Date('2024-01-01') }),
|
||||
];
|
||||
vi.mocked(skillQueries.getRecent).mockResolvedValue(mockSkills as any);
|
||||
|
||||
const request = new NextRequest('http://localhost:3000/api/skills/recent');
|
||||
const response = await GET(request);
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(data.skills).toBeDefined();
|
||||
expect(Array.isArray(data.skills)).toBe(true);
|
||||
expect(data.skills.length).toBe(2);
|
||||
});
|
||||
|
||||
it('should order by updatedAt descending', async () => {
|
||||
const mockSkills = [
|
||||
createMockSkill({ id: 'skill-1', updatedAt: new Date('2024-01-02') }),
|
||||
createMockSkill({ id: 'skill-2', updatedAt: new Date('2024-01-01') }),
|
||||
];
|
||||
vi.mocked(skillQueries.getRecent).mockResolvedValue(mockSkills as any);
|
||||
|
||||
const request = new NextRequest('http://localhost:3000/api/skills/recent');
|
||||
const response = await GET(request);
|
||||
const data = await response.json();
|
||||
|
||||
expect(data.skills[0].updatedAt).toBeDefined();
|
||||
});
|
||||
|
||||
it('should respect limit parameter', async () => {
|
||||
vi.mocked(skillQueries.getRecent).mockResolvedValue([]);
|
||||
|
||||
const request = new NextRequest('http://localhost:3000/api/skills/recent?limit=5');
|
||||
await GET(request);
|
||||
|
||||
expect(skillQueries.getRecent).toHaveBeenCalledWith(expect.anything(), 5);
|
||||
});
|
||||
|
||||
it('should handle database errors gracefully', async () => {
|
||||
vi.mocked(skillQueries.getRecent).mockRejectedValue(new Error('Database error'));
|
||||
|
||||
const request = new NextRequest('http://localhost:3000/api/skills/recent');
|
||||
const response = await GET(request);
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(500);
|
||||
expect(data.error).toBeDefined();
|
||||
});
|
||||
});
|
||||
85
apps/web/app/api/skills/recent/route.ts
Normal file
85
apps/web/app/api/skills/recent/route.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { NextResponse, type NextRequest } from 'next/server';
|
||||
import { createDb, skillQueries, type skills } from '@skillhub/db';
|
||||
import { getCached, setCache, cacheKeys, cacheTTL } from '@/lib/cache';
|
||||
import { withRateLimit, createRateLimitResponse, createRateLimitHeaders } from '@/lib/rate-limit';
|
||||
|
||||
const db = createDb();
|
||||
|
||||
type Skill = typeof skills.$inferSelect;
|
||||
|
||||
interface SkillData {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string | null;
|
||||
githubOwner: string;
|
||||
githubRepo: string;
|
||||
githubStars: number | null;
|
||||
downloadCount: number | null;
|
||||
securityStatus: string | null;
|
||||
isVerified: boolean | null;
|
||||
compatibility: unknown;
|
||||
updatedAt: Date | null;
|
||||
createdAt: Date | null;
|
||||
}
|
||||
|
||||
interface RecentResponse {
|
||||
skills: SkillData[];
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
// Rate limiting
|
||||
const rateLimitResult = await withRateLimit(request, 'anonymous');
|
||||
if (!rateLimitResult.allowed) {
|
||||
return createRateLimitResponse(rateLimitResult);
|
||||
}
|
||||
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const limit = parseInt(searchParams.get('limit') || '10');
|
||||
|
||||
// Try to get from cache first (only for default limit)
|
||||
const cacheKey = cacheKeys.recentSkills();
|
||||
if (limit === 10) {
|
||||
const cached = await getCached<RecentResponse>(cacheKey);
|
||||
if (cached) {
|
||||
return NextResponse.json(cached, {
|
||||
headers: { 'X-Cache': 'HIT', ...createRateLimitHeaders(rateLimitResult) },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const recentSkills = await skillQueries.getRecent(db, limit);
|
||||
|
||||
const data: RecentResponse = {
|
||||
skills: recentSkills.map((skill: Skill) => ({
|
||||
id: skill.id,
|
||||
name: skill.name,
|
||||
description: skill.description,
|
||||
githubOwner: skill.githubOwner,
|
||||
githubRepo: skill.githubRepo,
|
||||
githubStars: skill.githubStars,
|
||||
downloadCount: skill.downloadCount,
|
||||
securityStatus: skill.securityStatus,
|
||||
isVerified: skill.isVerified,
|
||||
compatibility: skill.compatibility,
|
||||
updatedAt: skill.updatedAt,
|
||||
createdAt: skill.createdAt,
|
||||
})),
|
||||
};
|
||||
|
||||
// Cache the result (1 hour)
|
||||
if (limit === 10) {
|
||||
await setCache(cacheKey, data, cacheTTL.recent);
|
||||
}
|
||||
|
||||
return NextResponse.json(data, {
|
||||
headers: { 'X-Cache': 'MISS', ...createRateLimitHeaders(rateLimitResult) },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching recent skills:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch recent skills' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user