mirror of
https://github.com/pesde-pkg/pesde.git
synced 2025-05-04 10:33:47 +01:00
feat(website): use new /latest/any endpoint
This commit is contained in:
parent
a31ef0e666
commit
08b3132abe
2 changed files with 9 additions and 32 deletions
Binary file not shown.
|
@ -1,49 +1,26 @@
|
||||||
import {
|
import {
|
||||||
fetchRegistryJson,
|
fetchRegistryJson,
|
||||||
RegistryHttpError,
|
RegistryHttpError,
|
||||||
type PackageVersionsResponse,
|
|
||||||
type PackageVersionResponse,
|
type PackageVersionResponse,
|
||||||
} from "$lib/registry-api"
|
} from "$lib/registry-api"
|
||||||
import { error } from "@sveltejs/kit"
|
import { error } from "@sveltejs/kit"
|
||||||
import type { LayoutServerLoad } from "./$types"
|
import type { LayoutServerLoad } from "./$types"
|
||||||
|
|
||||||
type FetchPackageOptions =
|
type FetchPackageOptions = {
|
||||||
| {
|
|
||||||
scope: string
|
scope: string
|
||||||
name: string
|
name: string
|
||||||
}
|
version?: string
|
||||||
| {
|
target?: string
|
||||||
scope: string
|
}
|
||||||
name: string
|
|
||||||
version: string
|
|
||||||
target: string
|
|
||||||
}
|
|
||||||
|
|
||||||
const fetchPackage = async (fetcher: typeof fetch, options: FetchPackageOptions) => {
|
const fetchPackage = async (fetcher: typeof fetch, options: FetchPackageOptions) => {
|
||||||
const { scope, name } = options
|
const { scope, name, version = "latest", target = "any" } = options
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if ("version" in options) {
|
return await fetchRegistryJson<PackageVersionResponse>(
|
||||||
if (options.target === undefined) {
|
|
||||||
error(404, "Not Found")
|
|
||||||
}
|
|
||||||
|
|
||||||
const { version, target } = options
|
|
||||||
return fetchRegistryJson<PackageVersionResponse>(
|
|
||||||
`packages/${encodeURIComponent(`${scope}/${name}`)}/${version}/${target}`,
|
`packages/${encodeURIComponent(`${scope}/${name}`)}/${version}/${target}`,
|
||||||
fetcher,
|
fetcher,
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
const versions = await fetchRegistryJson<PackageVersionsResponse>(
|
|
||||||
`packages/${encodeURIComponent(`${scope}/${name}`)}`,
|
|
||||||
fetcher,
|
|
||||||
)
|
|
||||||
|
|
||||||
const latestVersion = versions.at(-1)
|
|
||||||
if (latestVersion === undefined) throw new Error("package has no versions *blows up*")
|
|
||||||
|
|
||||||
return latestVersion
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof RegistryHttpError && e.response.status === 404) {
|
if (e instanceof RegistryHttpError && e.response.status === 404) {
|
||||||
error(404, "This package does not exist.")
|
error(404, "This package does not exist.")
|
||||||
|
|
Loading…
Add table
Reference in a new issue