-
-
Notifications
You must be signed in to change notification settings - Fork 432
feat: add a warning when the package license changes #2188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aisiklar
wants to merge
26
commits into
npmx-dev:main
Choose a base branch
from
aisiklar:feature/1826_license_changes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3a0543f
1826-add a warning and info for license changes. Added support for TR…
aisiklar bfd9550
[autofix.ci] apply automated fixes
autofix-ci[bot] fa52bff
fix:warning style changed and unintended chars erased
aisiklar bd4ebe5
[autofix.ci] apply automated fixes
autofix-ci[bot] 9afe21d
fix:added TR language support for warning text and changed logic for …
aisiklar ecbb660
[autofix.ci] apply automated fixes
autofix-ci[bot] b91aab5
fix: removed the vue-118n import and used autoimported
aisiklar 34b84c5
Merge branch 'main' into feature/1826_license_changes
ghostdevv 302952a
fix:feature/1826-warning message moved to proper place, license chang…
aisiklar 45e2528
test added
aisiklar e4fd978
removed the unneeded watcher
aisiklar a5b4e09
Merge remote-tracking branch 'origin/main' into feature/1826_license_…
ghostdevv 61432b3
moved license change logic to server side, to an api, changed nested …
aisiklar ae2ac2a
changes to translations and schema from prev merge
aisiklar 18a58a1
chore: update i18n
ghostdevv ef3d106
Merge remote-tracking branch 'origin/main' into feature/1826_license_…
ghostdevv 0fac1ce
fix(i18n): gen schema
ghostdevv c3b5986
chore: fmt
ghostdevv e1a28da
Update server/api/registry/license-change/[...pkg].get.ts
aisiklar 083e17f
fix: compute license changes in page
43081j b397996
Update i18n/locales/en.json
aisiklar 1cd6227
Update i18n/locales/en.json
aisiklar 4b2d0b9
TR translation added, with suggested changes on the warning message
aisiklar db9c4ed
custom key added to useFetch function in useLicenseChange, comments u…
aisiklar 386460d
Update i18n/locales/en.json
aisiklar 24ebc75
chore: reuse fetchNpmPackage
43081j File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <script setup lang="ts"> | ||
| import type { LicenseChangeResponse } from '~/composables/useLicenseChanges' | ||
| const props = defineProps<{ | ||
| change: LicenseChangeResponse['change'] | ||
| }>() | ||
| </script> | ||
|
|
||
| <template> | ||
| <div | ||
| v-if="props.change" | ||
| class="border border-amber-600/40 bg-amber-500/10 rounded-lg mt-1 gap-x-1 py-2 px-3" | ||
| :aria-label="$t('package.versions.license_change_help')" | ||
| > | ||
| <p class="text-md text-amber-800 dark:text-amber-400 flex items-center gap-2"> | ||
| <span | ||
| class="i-lucide:alert-triangle w-4 h-4 flex-shrink-0" | ||
| role="img" | ||
| :aria-label="$t('package.versions.license_change_help')" | ||
| /> | ||
| {{ $t('package.versions.license_change_warning') }} | ||
| </p> | ||
| <p class="text-md text-amber-800 dark:text-amber-400 mt-1"> | ||
| {{ | ||
| $t('package.versions.license_change_record', { | ||
| from: props.change?.from, | ||
| to: props.change?.to, | ||
| }) | ||
| }} | ||
| </p> | ||
| </div> | ||
| </template> | ||
|
|
||
| <style scoped></style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type { MaybeRefOrGetter } from 'vue' | ||
| import { toValue } from 'vue' | ||
|
|
||
| export interface LicenseChangeResponse { | ||
| change: { from: string; to: string } | null | ||
| } | ||
|
|
||
| /** | ||
| * Composable to detect license changes across all versions of a package | ||
| */ | ||
| export function useLicenseChanges( | ||
| packageName: MaybeRefOrGetter<string | null | undefined>, | ||
| resolvedVersion: MaybeRefOrGetter<string | null | undefined> = () => undefined, | ||
| ) { | ||
| const name = computed(() => toValue(packageName)) | ||
| if (!name) return { data: null } // Don't fetch if no name | ||
|
|
||
| const version = computed(() => toValue(resolvedVersion) ?? 'latest') | ||
|
|
||
| const url = computed(() => { | ||
| return name.value ? `/api/registry/license-change/${encodeURIComponent(name.value)}` : '' | ||
| }) | ||
|
|
||
| const result = useFetch<LicenseChangeResponse>(url, { | ||
| query: computed(() => ({ version: version.value })), | ||
| key: `license-change:${name.value}:${version.value}`, | ||
| watch: [name, version], | ||
| }) | ||
|
|
||
| return result | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| interface LicenseChangeRecord { | ||
| from: string | ||
| to: string | ||
| } | ||
|
|
||
| export default defineCachedEventHandler( | ||
| async event => { | ||
| // 1. Extract the package name from the catch-all parameter | ||
| const packageName = getRouterParam(event, 'pkg') | ||
| if (!packageName) { | ||
| throw createError({ | ||
| statusCode: 400, | ||
| statusMessage: 'Package name is required', | ||
| }) | ||
| } | ||
| const query = getQuery(event) | ||
| const version = query.version || 'latest' | ||
|
|
||
| try { | ||
| // 2. Fetch the "Packument" on the server | ||
| // This stays on the server, so the client never downloads this massive JSON | ||
| const data = await fetchNpmPackage(packageName) | ||
|
|
||
| if (!data.versions || !data.time) { | ||
| throw createError({ | ||
| statusCode: 404, | ||
| statusMessage: 'Package metadata not found', | ||
| }) | ||
| } | ||
| // 3. Process the logic | ||
| const versions = Object.values(data.versions) | ||
|
|
||
| // Sort versions chronologically using the 'time' object | ||
| versions.sort((a, b) => { | ||
| const timeA = new Date(data.time[a.version] as string).getTime() | ||
| const timeB = new Date(data.time[b.version] as string).getTime() | ||
| return timeA - timeB | ||
| }) | ||
| let change: LicenseChangeRecord | null = null | ||
|
|
||
| const currentVersionIndex = | ||
| version === 'latest' ? versions.length - 1 : versions.findIndex(v => v.version === version) | ||
|
|
||
| const previousVersionIndex = currentVersionIndex - 1 | ||
| const currentLicense = String(versions[currentVersionIndex]?.license || 'UNKNOWN') | ||
| const previousLicense = String(versions[previousVersionIndex]?.license || 'UNKNOWN') | ||
|
|
||
| if (currentLicense !== previousLicense) { | ||
| change = { | ||
| from: previousLicense, | ||
| to: currentLicense, | ||
| } | ||
| } | ||
| return { change } | ||
| } catch (error: any) { | ||
| throw createError({ | ||
| statusCode: error.statusCode || 500, | ||
| statusMessage: `Failed to fetch license data: ${error.message}`, | ||
| }) | ||
| } | ||
| }, | ||
| { | ||
| // 5. Cache Configuration | ||
| maxAge: 60 * 60, // time in seconds | ||
| swr: true, | ||
| getKey: event => { | ||
| const pkg = getRouterParam(event, 'pkg') ?? '' | ||
| const query = getQuery(event) | ||
|
|
||
| // 1. remove the /'s from the package name | ||
| const cleanPkg = pkg.replace(/\/+$/, '').trim() | ||
|
|
||
| // 2. Get the version (default to 'latest' if not provided) | ||
| const version = query.version || 'latest' | ||
|
|
||
| // 3. Create a unique string such that it takes into account the pckage name and version | ||
| // sample result: "license-change:v1:faker:2.1.15" | ||
| return `license-change:v2:${cleanPkg}:${version}` | ||
| }, | ||
|
43081j marked this conversation as resolved.
|
||
| }, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.