Skip to content

Commit 92193b5

Browse files
committed
moved has changelog to it's own api endpoint
1 parent 4873b1c commit 92193b5

File tree

10 files changed

+75
-13
lines changed

10 files changed

+75
-13
lines changed

app/composables/usePackageAnalysis.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ export interface PackageAnalysisResponse {
1010
npm?: string
1111
}
1212
createPackage?: CreatePackageInfo
13-
// TODO move this to it's own composable
14-
hasChangelog?: boolean
1513
}
1614

1715
/**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function usePackageHasChangelog(
2+
packageName: MaybeRefOrGetter<string>,
3+
version?: MaybeRefOrGetter<string | null | undefined>,
4+
) {
5+
return useLazyFetch<boolean>(
6+
() => {
7+
const name = toValue(packageName)
8+
const ver = toValue(version)
9+
const base = `/api/changelog/has/${name}`
10+
return ver ? `${base}/v/${ver}` : base
11+
},
12+
{
13+
onResponse(r) {
14+
console.log({ r })
15+
},
16+
},
17+
)
18+
}

app/pages/package/[...package].vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ const { data: skillsData } = useLazyFetch<SkillsListResponse>(
117117
118118
const { data: packageAnalysis } = usePackageAnalysis(packageName, requestedVersion)
119119
const { data: moduleReplacement } = useModuleReplacement(packageName)
120+
const { data: hasChangelog } = usePackageHasChangelog(packageName, requestedVersion)
121+
122+
watchEffect(() => console.log('hasChangelog', hasChangelog.value))
120123
121124
const {
122125
data: resolvedVersion,
@@ -756,9 +759,8 @@ onKeyStroke(
756759
{{ $t('package.links.issues') }}
757760
</a>
758761
</li>
759-
760762
<NuxtLink
761-
v-if="packageAnalysis?.hasChangelog"
763+
v-if="hasChangelog"
762764
class="link-subtle font-mono text-sm inline-flex items-center gap-1.5"
763765
>
764766
<span class="i-carbon:catalog w-4 h-4" aria-hidden="true" />

i18n/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@
183183
"code": "code",
184184
"docs": "docs",
185185
"fund": "fund",
186-
"compare": "compare"
186+
"compare": "compare",
187+
"changelog": "changelog"
187188
},
188189
"docs": {
189190
"not_available": "Docs not available",

lunaria/files/en-GB.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@
183183
"code": "code",
184184
"docs": "docs",
185185
"fund": "fund",
186-
"compare": "compare"
186+
"compare": "compare",
187+
"changelog": "changelog"
187188
},
188189
"docs": {
189190
"not_available": "Docs not available",

lunaria/files/en-US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@
183183
"code": "code",
184184
"docs": "docs",
185185
"fund": "fund",
186-
"compare": "compare"
186+
"compare": "compare",
187+
"changelog": "changelog"
187188
},
188189
"docs": {
189190
"not_available": "Docs not available",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { ExtendedPackageJson } from '#shared/utils/package-analysis'
2+
import { PackageRouteParamsSchema } from '#shared/schemas/package'
3+
import {
4+
ERROR_PACKAGE_HAS_CHANGELOG,
5+
NPM_REGISTRY,
6+
CACHE_MAX_AGE_ONE_DAY,
7+
} from '#shared/utils/constants'
8+
import * as v from 'valibot'
9+
import { detectHasChangelog } from '~~/server/utils/has-changelog'
10+
11+
export default defineCachedEventHandler(
12+
async event => {
13+
const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? []
14+
15+
const { rawPackageName, rawVersion } = parsePackageParams(pkgParamSegments)
16+
17+
try {
18+
const { packageName, version } = v.parse(PackageRouteParamsSchema, {
19+
packageName: rawPackageName,
20+
version: rawVersion,
21+
})
22+
23+
const encodedName = encodePackageName(packageName)
24+
const versionSuffix = version ? `/${version}` : '/latest'
25+
const pkg = await $fetch<ExtendedPackageJson>(
26+
`${NPM_REGISTRY}/${encodedName}${versionSuffix}`,
27+
)
28+
29+
return await detectHasChangelog(pkg)
30+
} catch (error) {
31+
handleApiError(error, {
32+
statusCode: 502,
33+
message: ERROR_PACKAGE_HAS_CHANGELOG,
34+
})
35+
}
36+
},
37+
{
38+
maxAge: CACHE_MAX_AGE_ONE_DAY, // 24 hours - analysis rarely changes
39+
swr: true,
40+
getKey: event => {
41+
const pkg = getRouterParam(event, 'pkg') ?? ''
42+
return `changelog:v1:${pkg.replace(/\/+$/, '').trim()}`
43+
},
44+
},
45+
)

server/api/registry/analysis/[...pkg].get.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
} from '#shared/utils/constants'
2020
import { parseRepoUrl } from '#shared/utils/git-providers'
2121
import { getLatestVersion, getLatestVersionBatch } from 'fast-npm-meta'
22-
import { detectHasChangelog } from '~~/server/utils/has-changelog'
2322

2423
export default defineCachedEventHandler(
2524
async event => {
@@ -54,13 +53,10 @@ export default defineCachedEventHandler(
5453
const createPackage = await findAssociatedCreatePackage(packageName, pkg)
5554

5655
const analysis = analyzePackage(pkg, { typesPackage, createPackage })
57-
// TODO move this to it's own endpoint
58-
const hasChangelog = await detectHasChangelog(pkg)
5956
return {
6057
package: packageName,
6158
version: pkg.version ?? version ?? 'latest',
6259
...analysis,
63-
hasChangelog,
6460
} satisfies PackageAnalysisResponse
6561
} catch (error: unknown) {
6662
handleApiError(error, {
@@ -218,5 +214,4 @@ function hasSameRepositoryOwner(
218214
export interface PackageAnalysisResponse extends PackageAnalysis {
219215
package: string
220216
version: string
221-
hasChangelog: boolean | null
222217
}

server/utils/has-changelog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function detectHasChangelog(
2424
return true
2525
}
2626

27-
return null
27+
return false
2828
}
2929

3030
/**

shared/utils/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const ERROR_PACKAGE_ANALYSIS_FAILED = 'Failed to analyze package.'
1616
export const ERROR_PACKAGE_VERSION_AND_FILE_FAILED = 'Version and file path are required.'
1717
export const ERROR_PACKAGE_REQUIREMENTS_FAILED =
1818
'Package name, version, and file path are required.'
19+
export const ERROR_PACKAGE_HAS_CHANGELOG = 'failed to detect package has changelog'
1920
export const ERROR_FILE_LIST_FETCH_FAILED = 'Failed to fetch file list.'
2021
export const ERROR_CALC_INSTALL_SIZE_FAILED = 'Failed to calculate install size.'
2122
export const NPM_MISSING_README_SENTINEL = 'ERROR: No README data found!'

0 commit comments

Comments
 (0)