Skip to content

Commit 5e37bc4

Browse files
committed
feat(api): add NPM package fetch API and related type definitions
1 parent 311f052 commit 5e37bc4

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

app/api/npm/npm.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const NPM_API = `https://api.npmjs.org/downloads/point/last-month`
2+
3+
export async function getNpmPackage(packageName: string) {
4+
return fetch(`${NPM_API}/${packageName}`, {
5+
cache: 'no-store',
6+
})
7+
}

app/api/npm/route.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getNpmPackage } from './npm'
2+
import type { NextRequest } from 'next/server'
3+
4+
export async function GET(request: NextRequest) {
5+
const params = new URL(request.url).searchParams
6+
const packageName = params.get('package')
7+
if (!packageName) {
8+
return Response.json({ message: 'Missing package parameter' }, { status: 400 })
9+
}
10+
const response = await getNpmPackage(packageName)
11+
if (response.status === 204 || response.status > 400) {
12+
return Response.json({ message: 'Package not found' }, { status: 404 })
13+
}
14+
15+
const data = await response.json()
16+
17+
return Response.json(data)
18+
}

types/data.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ export type GithubRepository = {
122122
repositoryTopics: string[]
123123
}
124124

125+
export type NpmPackage = {
126+
downloads: number
127+
start: string
128+
end: string
129+
package: string
130+
}
131+
125132
export type ImdbFriends = {
126133
type: string
127134
name: string

0 commit comments

Comments
 (0)