-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rubygems): Use cascade of endpoints for unknown servers (#23523)
- Loading branch information
Showing
5 changed files
with
248 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,44 @@ | ||
import { assignKeys } from '../../../util/assign-keys'; | ||
import { type Http, HttpError, type SafeJsonError } from '../../../util/http'; | ||
import { type AsyncResult, Result } from '../../../util/result'; | ||
import type { Http, SafeJsonError } from '../../../util/http'; | ||
import type { AsyncResult } from '../../../util/result'; | ||
import { joinUrlParts as join } from '../../../util/url'; | ||
import type { Release, ReleaseResult } from '../types'; | ||
import type { ReleaseResult } from '../types'; | ||
import { GemMetadata, GemVersions } from './schema'; | ||
|
||
export function getV1Releases( | ||
export function assignMetadata( | ||
releases: ReleaseResult, | ||
metadata: GemMetadata | ||
): ReleaseResult { | ||
return assignKeys(releases, metadata, [ | ||
'changelogUrl', | ||
'sourceUrl', | ||
'homepage', | ||
]); | ||
} | ||
|
||
export function getV1Metadata( | ||
http: Http, | ||
registryUrl: string, | ||
packageName: string | ||
): AsyncResult< | ||
ReleaseResult, | ||
SafeJsonError | 'empty-releases' | 'unsupported-api' | ||
> { | ||
const fileName = `${packageName}.json`; | ||
const versionsUrl = join(registryUrl, '/api/v1/versions', fileName); | ||
const metadataUrl = join(registryUrl, '/api/v1/gems', fileName); | ||
|
||
const addMetadata = (releases: Release[]): Promise<ReleaseResult> => | ||
http | ||
.getJsonSafe(metadataUrl, GemMetadata) | ||
.transform((metadata) => | ||
assignKeys({ releases } as ReleaseResult, metadata, [ | ||
'changelogUrl', | ||
'sourceUrl', | ||
'homepage', | ||
]) | ||
) | ||
.unwrap({ releases }); | ||
): AsyncResult<GemMetadata, SafeJsonError> { | ||
const metadataUrl = join(registryUrl, '/api/v1/gems', `${packageName}.json`); | ||
return http.getJsonSafe(metadataUrl, GemMetadata); | ||
} | ||
|
||
return http | ||
.getJsonSafe(versionsUrl, GemVersions) | ||
.catch((err) => { | ||
if (err instanceof HttpError) { | ||
const status = err.response?.statusCode; | ||
if (status === 404 || status === 400) { | ||
return Result.err('unsupported-api'); | ||
} | ||
} | ||
export function getV1Releases( | ||
http: Http, | ||
registryUrl: string, | ||
packageName: string | ||
): AsyncResult<ReleaseResult, SafeJsonError | 'unsupported-api'> { | ||
const versionsUrl = join( | ||
registryUrl, | ||
'/api/v1/versions', | ||
`${packageName}.json` | ||
); | ||
|
||
return Result.err(err); | ||
}) | ||
.transform((releases) => { | ||
return releases.length > 0 | ||
? Result.ok(releases) | ||
: Result.err('empty-releases'); | ||
}) | ||
.transform(addMetadata); | ||
return http.getJsonSafe(versionsUrl, GemVersions).transform((releaseResult) => | ||
getV1Metadata(http, registryUrl, packageName) | ||
.transform((metadata) => assignMetadata(releaseResult, metadata)) | ||
.unwrap(releaseResult) | ||
); | ||
} |
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
Oops, something went wrong.