Skip to content

Commit

Permalink
Require Node.js 12 and upgrade dependencies
Browse files Browse the repository at this point in the history
Fixes #61
  • Loading branch information
sindresorhus committed Jun 24, 2021
1 parent 0df3973 commit 75c5002
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 69 deletions.
59 changes: 26 additions & 33 deletions index.d.ts
@@ -1,4 +1,3 @@
/// <reference types="node"/>
import {Agent as HttpAgent} from 'http';
import {Agent as HttpsAgent} from 'https';

Expand Down Expand Up @@ -69,19 +68,20 @@ declare namespace packageJson {
}

interface DistTags {
readonly latest: string;
readonly [tagName: string]: string;
readonly latest: string;
}

interface AbbreviatedMetadata {
readonly [key: string]: unknown;
readonly 'dist-tags': DistTags;
readonly modified: string;
readonly name: string;
readonly versions: {readonly [version: string]: AbbreviatedVersion};
readonly [key: string]: unknown;
readonly versions: Readonly<Record<string, AbbreviatedVersion>>;
}

interface AbbreviatedVersion {
readonly [key: string]: unknown;
readonly name: string;
readonly version: string;
readonly dist: {
Expand All @@ -90,16 +90,15 @@ declare namespace packageJson {
readonly integrity?: string;
};
readonly deprecated?: string;
readonly dependencies?: {readonly [name: string]: string};
readonly optionalDependencies?: {readonly [name: string]: string};
readonly devDependencies?: {readonly [name: string]: string};
readonly bundleDependencies?: {readonly [name: string]: string};
readonly peerDependencies?: {readonly [name: string]: string};
readonly bin?: {readonly [key: string]: string};
readonly dependencies?: Readonly<Record<string, string>>;
readonly optionalDependencies?: Readonly<Record<string, string>>;
readonly devDependencies?: Readonly<Record<string, string>>;
readonly bundleDependencies?: Readonly<Record<string, string>>;
readonly peerDependencies?: Readonly<Record<string, string>>;
readonly bin?: Readonly<Record<string, string>>;
readonly directories?: readonly string[];
readonly engines?: {readonly [type: string]: string};
readonly engines?: Readonly<Record<string, string>>;
readonly _hasShrinkwrap?: boolean;
readonly [key: string]: unknown;
}

interface Person {
Expand All @@ -111,8 +110,8 @@ declare namespace packageJson {
interface HoistedData {
readonly author?: Person;
readonly bugs?:
| {readonly url: string; readonly email?: string}
| {readonly url?: string; readonly email: string};
| {readonly url: string; readonly email?: string}
| {readonly url?: string; readonly email: string};
readonly contributors?: readonly Person[];
readonly description?: string;
readonly homepage?: string;
Expand All @@ -125,38 +124,38 @@ declare namespace packageJson {
}

interface FullMetadata extends AbbreviatedMetadata, HoistedData {
readonly [key: string]: unknown;
readonly _id: string;
readonly _rev: string;
readonly time: {
readonly [version: string]: string;
readonly created: string;
readonly modified: string;
readonly [version: string]: string;
};
readonly users?: {readonly [user: string]: boolean};
readonly versions: {readonly [version: string]: FullVersion};
readonly [key: string]: unknown;
readonly users?: Readonly<Record<string, boolean>>;
readonly versions: Readonly<Record<string, FullVersion>>;
}

interface FullVersion extends AbbreviatedVersion, HoistedData {
readonly [key: string]: unknown;
readonly _id: string;
readonly _nodeVersion: string;
readonly _npmUser: string;
readonly _npmVersion: string;
readonly main?: string;
readonly files?: readonly string[];
readonly man?: readonly string[];
readonly scripts?: {readonly [scriptName: string]: string};
readonly scripts?: Readonly<Record<string, string>>;
readonly gitHead?: string;
readonly types?: string;
readonly typings?: string;
readonly [key: string]: unknown;
}

type VersionNotFoundError = VersionNotFoundErrorClass;
type PackageNotFoundError = PackageNotFoundErrorClass;
}

declare const packageJson: {
declare const packageJson: { // eslint-disable-line no-redeclare
/**
Get metadata of a package from the npm registry.
Expand All @@ -166,21 +165,15 @@ declare const packageJson: {
```
import packageJson = require('package-json');
(async () => {
console.log(await packageJson('ava'));
//=> {name: 'ava', ...}
console.log(await packageJson('ava'));
//=> {name: 'ava', ...}
// Also works with scoped packages
console.log(await packageJson('@sindresorhus/df'));
})();
// Also works with scoped packages
console.log(await packageJson('@sindresorhus/df'));
```
*/
(packageName: string, options: packageJson.FullMetadataOptions): Promise<
packageJson.FullMetadata
>;
(packageName: string, options?: packageJson.Options): Promise<
packageJson.AbbreviatedMetadata
>;
(packageName: string, options: packageJson.FullMetadataOptions): Promise<packageJson.FullMetadata>;
(packageName: string, options?: packageJson.Options): Promise<packageJson.AbbreviatedMetadata>;

/**
The error thrown when the given package version cannot be found.
Expand Down
10 changes: 3 additions & 7 deletions index.js
@@ -1,5 +1,4 @@
'use strict';
const {URL} = require('url');
const {Agent: HttpAgent} = require('http');
const {Agent: HttpsAgent} = require('https');
const got = require('got');
Expand Down Expand Up @@ -54,7 +53,6 @@ const packageJson = async (packageName, options) => {
}

const gotOptions = {
json: true,
headers,
agent: {
http: httpAgent,
Expand All @@ -66,19 +64,17 @@ const packageJson = async (packageName, options) => {
gotOptions.agent = options.agent;
}

let response;
let data;
try {
response = await got(packageUrl, gotOptions);
data = await got(packageUrl, gotOptions).json();
} catch (error) {
if (error.statusCode === 404) {
if (error.response.statusCode === 404) {
throw new PackageNotFoundError(packageName);
}

throw error;
}

let data = response.body;

if (options.allVersions) {
return data;
}
Expand Down
8 changes: 4 additions & 4 deletions index.test-d.ts
@@ -1,13 +1,13 @@
import {expectType} from 'tsd';
import packageJson = require('.');
import packageJson = require('./index.js');
import {
FullMetadata,
FullVersion,
AbbreviatedMetadata,
AbbreviatedVersion,
PackageNotFoundError,
VersionNotFoundError
} from '.';
} from './index.js';

expectType<Promise<AbbreviatedMetadata>>(packageJson('package-json'));
expectType<Promise<AbbreviatedMetadata>>(
Expand All @@ -29,9 +29,9 @@ expectType<typeof PackageNotFoundError>(PackageNotFoundError);
expectType<typeof VersionNotFoundError>(VersionNotFoundError);

const packageNotFoundError = new PackageNotFoundError('foo');
packageNotFoundError instanceof PackageNotFoundError;
packageNotFoundError instanceof PackageNotFoundError; // eslint-disable-line @typescript-eslint/no-unused-expressions
expectType<PackageNotFoundError>(packageNotFoundError);

const versionNotFoundError = new VersionNotFoundError('foo', 'bar');
versionNotFoundError instanceof VersionNotFoundError;
versionNotFoundError instanceof VersionNotFoundError; // eslint-disable-line @typescript-eslint/no-unused-expressions
expectType<VersionNotFoundError>(versionNotFoundError);
2 changes: 1 addition & 1 deletion license
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
17 changes: 9 additions & 8 deletions package.json
Expand Up @@ -4,13 +4,14 @@
"description": "Get metadata of a package from the npm registry",
"license": "MIT",
"repository": "sindresorhus/package-json",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -31,16 +32,16 @@
"scoped"
],
"dependencies": {
"got": "^9.6.0",
"got": "^11.8.2",
"registry-auth-token": "^4.0.0",
"registry-url": "^5.0.0",
"semver": "^6.2.0"
"semver": "^7.3.5"
},
"devDependencies": {
"@types/node": "^12.6.8",
"ava": "^2.2.0",
"@types/node": "^15.12.4",
"ava": "^2.4.0",
"mock-private-registry": "^1.1.2",
"tsd": "^0.7.4",
"xo": "^0.24.0"
"tsd": "^0.17.0",
"xo": "^0.39.0"
}
}
15 changes: 4 additions & 11 deletions readme.md
Expand Up @@ -2,14 +2,12 @@

> Get metadata of a package from the npm registry

## Install

```
$ npm install package-json
```


## Usage

```js
Expand All @@ -24,7 +22,6 @@ const packageJson = require('package-json');
})();
```


## API

### packageJson(packageName, options?)
Expand All @@ -41,7 +38,7 @@ Type: `object`

##### version

Type: `string`<br>
Type: `string`\
Default: `latest`

Package version such as `1.0.0` or a [dist tag](https://docs.npmjs.com/cli/dist-tag) such as `latest`.
Expand All @@ -55,21 +52,21 @@ The version can also be in any format supported by the [semver](https://github.c

##### fullMetadata

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

By default, only an abbreviated metadata object is returned for performance reasons. [Read more.](https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md)

##### allVersions

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Return the [main entry](https://registry.npmjs.org/ava) containing all versions.

##### registryUrl

Type: `string`<br>
Type: `string`\
Default: Auto-detected

The registry URL is by default inferred from the npm defaults and `.npmrc`. This is beneficial as `package-json` and any project using it will work just like npm. This option is **only** intended for internal tools. You should **not** use this option in reusable packages. Prefer just using `.npmrc` whenever possible.
Expand All @@ -80,7 +77,6 @@ Type: `http.Agent | https.Agent | object | false`

Overwrite the `agent` option that is passed down to [`got`](https://github.com/sindresorhus/got#agent). This might be useful to add [proxy support](https://github.com/sindresorhus/got#proxies).


### packageJson.PackageNotFoundError

The error thrown when the given package name cannot be found.
Expand All @@ -89,19 +85,16 @@ The error thrown when the given package name cannot be found.

The error thrown when the given package version cannot be found.


## Authentication

Both public and private registries are supported, for both scoped and unscoped packages, as long as the registry uses either bearer tokens or basic authentication.


## package-json for enterprise

Available as part of the Tidelift Subscription.

The maintainers of package-json and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-package-json?utm_source=npm-package-json&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)


## Related

- [package-json-cli](https://github.com/sindresorhus/package-json-cli) - CLI for this module
Expand Down
10 changes: 5 additions & 5 deletions test.js
@@ -1,8 +1,8 @@
import {promisify} from 'util';
import http from 'http';
import test from 'ava';
import privateRegistry from 'mock-private-registry/promise';
import packageJson from '.';
import privateRegistry from 'mock-private-registry/promise.js';
import packageJson from './index.js';

test('latest version', async t => {
const json = await packageJson('ava');
Expand Down Expand Up @@ -32,11 +32,11 @@ test('specific version', async t => {

test('incomplete version x', async t => {
const json = await packageJson('pageres', {version: '0'});
t.is(json.version.substr(0, 2), '0.');
t.is(json.version.slice(0, 2), '0.');
});

test.failing('custom registry url', async t => {
const json = await packageJson('ava', {registryUrl: 'http://registry.node-modules.io/'});
test('custom registry url', async t => {
const json = await packageJson('ava', {registryUrl: 'https://npm.open-registry.dev/'});
t.is(json.name, 'ava');
t.falsy(json.versions);
});
Expand Down

0 comments on commit 75c5002

Please sign in to comment.