Skip to content

Commit

Permalink
refactor(datasource/npm): Remove unused fields from types (#21192)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Apr 11, 2023
1 parent e1d5012 commit 3b53efe
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 66 deletions.
27 changes: 0 additions & 27 deletions lib/modules/datasource/npm/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
exports[`modules/datasource/npm/index should fetch package info from custom registry 1`] = `
{
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://npm.mycustomregistry.com",
"releases": [
{
Expand All @@ -20,14 +19,12 @@ exports[`modules/datasource/npm/index should fetch package info from custom regi
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;

exports[`modules/datasource/npm/index should fetch package info from npm 1`] = `
{
"isPrivate": false,
"name": "foobar",
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
Expand All @@ -44,14 +41,12 @@ exports[`modules/datasource/npm/index should fetch package info from npm 1`] = `
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;

exports[`modules/datasource/npm/index should handle foobar 1`] = `
{
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
Expand All @@ -68,14 +63,12 @@ exports[`modules/datasource/npm/index should handle foobar 1`] = `
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;

exports[`modules/datasource/npm/index should handle no time 1`] = `
{
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
Expand All @@ -91,14 +84,12 @@ exports[`modules/datasource/npm/index should handle no time 1`] = `
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;

exports[`modules/datasource/npm/index should not send an authorization header if public package 1`] = `
{
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
Expand All @@ -115,14 +106,12 @@ exports[`modules/datasource/npm/index should not send an authorization header if
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;

exports[`modules/datasource/npm/index should parse repo url (string) 1`] = `
{
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
Expand All @@ -134,14 +123,12 @@ exports[`modules/datasource/npm/index should parse repo url (string) 1`] = `
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;

exports[`modules/datasource/npm/index should parse repo url 1`] = `
{
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
Expand All @@ -153,14 +140,12 @@ exports[`modules/datasource/npm/index should parse repo url 1`] = `
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;

exports[`modules/datasource/npm/index should replace any environment variable in npmrc 1`] = `
{
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://registry.from-env.com",
"releases": [
{
Expand All @@ -177,7 +162,6 @@ exports[`modules/datasource/npm/index should replace any environment variable in
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;

Expand All @@ -188,9 +172,7 @@ exports[`modules/datasource/npm/index should return deprecated 1`] = `
\`This is deprecated\`
Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.",
"deprecationSource": "npm",
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
Expand All @@ -207,7 +189,6 @@ Marking the latest version of an npm package as deprecated results in the entire
"tags": {
"latest": "0.0.2",
},
"versions": {},
}
`;
Expand All @@ -222,7 +203,6 @@ Marking the latest version of an npm package as deprecated results in the entire
exports[`modules/datasource/npm/index should send an authorization header if provided 1`] = `
{
"isPrivate": true,
"name": "@foobar/core",
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
Expand All @@ -239,14 +219,12 @@ exports[`modules/datasource/npm/index should send an authorization header if pro
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;
exports[`modules/datasource/npm/index should use default registry if missing from npmrc 1`] = `
{
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://registry.npmjs.org",
"releases": [
{
Expand All @@ -263,14 +241,12 @@ exports[`modules/datasource/npm/index should use default registry if missing fro
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;
exports[`modules/datasource/npm/index should use host rules by baseUrl if provided 1`] = `
{
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://npm.mycustomregistry.com/_packaging/mycustomregistry/npm/registry",
"releases": [
{
Expand All @@ -287,14 +263,12 @@ exports[`modules/datasource/npm/index should use host rules by baseUrl if provid
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;
exports[`modules/datasource/npm/index should use host rules by hostName if provided 1`] = `
{
"isPrivate": true,
"name": "foobar",
"registryUrl": "https://npm.mycustomregistry.com",
"releases": [
{
Expand All @@ -311,6 +285,5 @@ exports[`modules/datasource/npm/index should use host rules by hostName if provi
"tags": {
"latest": "0.0.1",
},
"versions": {},
}
`;
1 change: 0 additions & 1 deletion lib/modules/datasource/npm/common.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const defaultRegistryUrls = ['https://registry.npmjs.org'];
export const id = 'npm';
24 changes: 8 additions & 16 deletions lib/modules/datasource/npm/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ import type { Http } from '../../../util/http';
import type { HttpOptions } from '../../../util/http/types';
import { regEx } from '../../../util/regex';
import { joinUrlParts } from '../../../util/url';
import { id } from './common';
import type {
CachedNpmDependency,
NpmDependency,
NpmRelease,
NpmResponse,
} from './types';
import type { Release, ReleaseResult } from '../types';
import type { CachedReleaseResult, NpmResponse } from './types';

interface PackageSource {
sourceUrl?: string;
Expand Down Expand Up @@ -58,14 +53,14 @@ export async function getDependency(
http: Http,
registryUrl: string,
packageName: string
): Promise<NpmDependency | null> {
): Promise<ReleaseResult | null> {
logger.trace(`npm.getDependency(${packageName})`);

const packageUrl = joinUrlParts(registryUrl, packageName.replace('/', '%2F'));

// Now check the persistent cache
const cacheNamespace = 'datasource-npm';
const cachedResult = await packageCache.get<CachedNpmDependency>(
const cacheNamespace = 'datasource-npm:data';
const cachedResult = await packageCache.get<CachedReleaseResult>(
cacheNamespace,
packageUrl
);
Expand Down Expand Up @@ -130,23 +125,20 @@ export async function getDependency(
const { sourceUrl, sourceDirectory } = getPackageSource(res.repository);

// Simplify response before caching and returning
const dep: NpmDependency = {
name: res.name,
const dep: ReleaseResult = {
homepage: res.homepage,
sourceUrl,
sourceDirectory,
versions: {},
releases: [],
'dist-tags': res['dist-tags'],
tags: res['dist-tags'],
registryUrl,
};

if (latestVersion?.deprecated) {
dep.deprecationMessage = `On registry \`${registryUrl}\`, the "latest" version of dependency \`${packageName}\` has the following deprecation notice:\n\n\`${latestVersion.deprecated}\`\n\nMarking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.`;
dep.deprecationSource = id;
}
dep.releases = Object.keys(res.versions).map((version) => {
const release: NpmRelease = {
const release: Release = {
version,
gitRef: res.versions?.[version].gitHead,
dependencies: res.versions?.[version].dependencies,
Expand Down
7 changes: 1 addition & 6 deletions lib/modules/datasource/npm/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as npmVersioning from '../../versioning/npm';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import { id } from './common';
import { getDependency } from './get';

export { setNpmrc } from './npmrc';

export class NpmDatasource extends Datasource {
static readonly id = id;
static readonly id = 'npm';

override readonly customRegistrySupport = true;

Expand All @@ -29,10 +28,6 @@ export class NpmDatasource extends Datasource {
}

const res = await getDependency(this.http, registryUrl, packageName);
if (res) {
res.tags ||= res['dist-tags'];
delete res['dist-tags'];
}
return res;
}
}
18 changes: 2 additions & 16 deletions lib/modules/datasource/npm/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PackageRule } from '../../../config/types';
import type { HostRule } from '../../../types';
import type { Release, ReleaseResult } from '../types';
import type { ReleaseResult } from '../types';

export interface NpmrcRules {
hostRules: HostRule[];
Expand Down Expand Up @@ -32,21 +32,7 @@ export interface NpmResponse {
'dist-tags'?: Record<string, string>;
}

export interface NpmRelease extends Release {
gitRef?: string;
}
export interface NpmDependency extends ReleaseResult {
releases: NpmRelease[];
deprecationSource?: string;
name: string;
homepage?: string;
sourceUrl?: string;
versions: Record<string, any>;
'dist-tags'?: Record<string, string>;
sourceDirectory?: string;
}

export interface CachedNpmDependency extends NpmDependency {
export interface CachedReleaseResult extends ReleaseResult {
cacheData?: {
etag: string | undefined;
softExpireAt: string;
Expand Down

0 comments on commit 3b53efe

Please sign in to comment.