-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add built-in
jsr
upgrade provider (rework) (#692)
Co-authored-by: Benjamin Fischer <[email protected]>
- Loading branch information
Showing
3 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,72 @@ | ||
import { Provider, Versions } from "../provider.ts"; | ||
|
||
type Semver = | ||
| `${number}.${number}.${number}` | ||
| `${number}.${number}.${number}-${string}`; | ||
type Package = `@${string}/${string}`; | ||
|
||
type JsrApiPackageMetadata = { | ||
scope: string; | ||
name: string; | ||
latest: Semver; | ||
versions: { | ||
[version: Semver]: { yanked?: boolean }; | ||
}; | ||
}; | ||
|
||
export type JsrProviderOptions = { | ||
package: Package; | ||
} | { | ||
scope: string; | ||
name?: string; | ||
}; | ||
|
||
export class JsrProvider extends Provider { | ||
name = "jsr"; | ||
private readonly repositoryUrl = "https://jsr.io/"; | ||
private readonly packageName?: string; | ||
private readonly packageScope: string; | ||
|
||
constructor(options: JsrProviderOptions) { | ||
super(); | ||
this.packageScope = "package" in options | ||
? options.package.split("/")[0].slice(1) | ||
: options.scope; | ||
this.packageName = "package" in options | ||
? options.package.split("/")[1] | ||
: options.name; | ||
} | ||
|
||
async getVersions( | ||
name: string, | ||
): Promise<Versions> { | ||
const response = await fetch( | ||
`${this.repositoryUrl}/@${this.packageScope}/${ | ||
this.packageName ?? name | ||
}/meta.json`, | ||
); | ||
if (!response.ok) { | ||
throw new Error( | ||
"couldn't fetch the latest version - try again after sometime", | ||
); | ||
} | ||
|
||
const { latest, versions } = await response.json() as JsrApiPackageMetadata; | ||
|
||
return { | ||
latest, | ||
versions: Object.keys(versions), | ||
}; | ||
} | ||
|
||
getRepositoryUrl(name: string): string { | ||
return new URL( | ||
`@${this.packageScope}/${this.packageName ?? name}`, | ||
this.repositoryUrl, | ||
).href; | ||
} | ||
|
||
getRegistryUrl(name: string, version: Semver): string { | ||
return `jsr:@${this.packageScope}/${this.packageName ?? name}@${version}`; | ||
} | ||
} |