Skip to content

Commit

Permalink
fix(util/result): Types for wrapNullable (#23713)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Aug 4, 2023
1 parent 77952db commit 8c0013f
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 131 deletions.
2 changes: 1 addition & 1 deletion lib/modules/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export function getRawPkgReleases(
return AsyncResult.err('no-package-name');
}

return Result.wrapNullable(getRawReleases(config), 'no-result')
return Result.wrapNullable(getRawReleases(config), 'no-result' as const)
.catch((e) => {
if (e instanceof ExternalHostError) {
e.hostType = config.datasource;
Expand Down
8 changes: 4 additions & 4 deletions lib/modules/datasource/rubygems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { MetadataCache } from './metadata-cache';
import { GemInfo, MarshalledVersionInfo } from './schema';
import { VersionsEndpointCache } from './versions-endpoint-cache';

function unlessServerSide<T, E>(
err: E,
cb: () => AsyncResult<T, E>
): AsyncResult<T, E> {
function unlessServerSide<
T extends NonNullable<unknown>,
E extends NonNullable<unknown>
>(err: E, cb: () => AsyncResult<T, E>): AsyncResult<T, E> {
if (err instanceof HttpError && err.response?.statusCode) {
const code = err.response.statusCode;
if (code >= 500 && code <= 599) {
Expand Down
6 changes: 3 additions & 3 deletions lib/modules/datasource/rubygems/metadata-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export class MetadataCache {
const cacheKey = `metadata-cache:${registryUrl}:${packageName}`;
const hash = toSha256(versions.join(''));

const loadCache = (): AsyncResult<ReleaseResult, unknown> =>
const loadCache = (): AsyncResult<ReleaseResult, NonNullable<unknown>> =>
Result.wrapNullable(
packageCache.get<CacheRecord>(cacheNs, cacheKey),
'cache-not-found'
'cache-not-found' as const
).transform((cache) => {
return hash === cache.hash
? Result.ok(cache.data)
: Result.err('cache-outdated');
: Result.err('cache-outdated' as const);
});

const saveCache = async (data: ReleaseResult): Promise<ReleaseResult> => {
Expand Down
10 changes: 8 additions & 2 deletions lib/util/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ describe('util/result', () => {
});

it('wraps nullable callback', () => {
const res = Result.wrapNullable(() => 42, 'oops');
const res: Result<number, 'oops'> = Result.wrapNullable(
(): number | null => 42,
'oops'
);
expect(res).toEqual(Result.ok(42));
});

Expand Down Expand Up @@ -225,7 +228,10 @@ describe('util/result', () => {
});

it('wraps nullable promise', async () => {
const res = Result.wrapNullable(Promise.resolve(42), 'oops');
const res: AsyncResult<number, 'oops'> = Result.wrapNullable(
Promise.resolve<number | null>(42),
'oops'
);
await expect(res).resolves.toEqual(Result.ok(42));
});

Expand Down
Loading

0 comments on commit 8c0013f

Please sign in to comment.