Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 31, 2019
1 parent c2f3d07 commit 2e8a263
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 156 deletions.
334 changes: 184 additions & 150 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,165 +1,199 @@
/// <reference types="node"/>
import {Agent as HttpAgent} from 'http';
import {Agent as HttpsAgent} from 'https';

export interface Agents {
http?: HttpAgent;
https?: HttpsAgent;
}

export interface Options {
/**
* Package version such as `1.0.0` or a [dist tag](https://docs.npmjs.com/cli/dist-tag) such as `latest`.
*
* The version can also be in any format supported by the [semver](https://github.com/npm/node-semver) module. For example:
* - `1` - Get the latest `1.x.x`
* - `1.2` - Get the latest `1.2.x`
* - `^1.2.3` - Get the latest `1.x.x` but at least `1.2.3`
* - `~1.2.3` - Get the latest `1.2.x` but at least `1.2.3`
*
* @default 'latest'
*/
readonly version?: string;

/**
* 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)
*
* @default false
*/
readonly fullMetadata?: boolean;

/**
* Return the [main entry](https://registry.npmjs.org/ava) containing all versions.
*
* @default false
*/
readonly allVersions?: boolean;

/**
* 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.
*/
readonly registryUrl?: string;

/**
* 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).
*/
readonly agent?: HttpAgent | HttpsAgent | Agents | false;
}

export interface FullMetadataOptions extends Options {
/**
* 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)
*
* @default false
*/
readonly fullMetadata: true;
}
declare class VersionNotFoundErrorClass extends Error {
readonly name: 'VersionNotFoundError';

export interface DistTags {
readonly latest: string;
readonly [tagName: string]: string;
constructor(packageName: string, version: string);
}

export interface AbbreviatedMetadata {
readonly 'dist-tags': DistTags;
readonly modified: string;
readonly name: string;
readonly versions: {readonly [version: string]: AbbreviatedVersion};
readonly [key: string]: unknown;
}
declare class PackageNotFoundErrorClass extends Error {
readonly name: 'PackageNotFoundError';

export interface AbbreviatedVersion {
readonly name: string;
readonly version: string;
readonly dist: {
readonly shasum: string;
readonly tarball: string;
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 directories?: ReadonlyArray<string>;
readonly engines?: {readonly [type: string]: string};
readonly _hasShrinkwrap?: boolean;
readonly [key: string]: unknown;
constructor(packageName: string);
}

export interface Person {
readonly name?: string;
readonly email?: string;
readonly url?: string;
declare namespace packageJson {
interface Agents {
http?: HttpAgent;
https?: HttpsAgent;
}

interface Options {
/**
Package version such as `1.0.0` or a [dist tag](https://docs.npmjs.com/cli/dist-tag) such as `latest`.
The version can also be in any format supported by the [semver](https://github.com/npm/node-semver) module. For example:
- `1` - Get the latest `1.x.x`
- `1.2` - Get the latest `1.2.x`
- `^1.2.3` - Get the latest `1.x.x` but at least `1.2.3`
- `~1.2.3` - Get the latest `1.2.x` but at least `1.2.3`
@default 'latest'
*/
readonly version?: string;

/**
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)
@default false
*/
readonly fullMetadata?: boolean;

/**
Return the [main entry](https://registry.npmjs.org/ava) containing all versions.
@default false
*/
readonly allVersions?: boolean;

/**
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.
*/
readonly registryUrl?: string;

/**
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).
*/
readonly agent?: HttpAgent | HttpsAgent | Agents | false;
}

interface FullMetadataOptions extends Options {
/**
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)
@default false
*/
readonly fullMetadata: true;
}

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

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

interface AbbreviatedVersion {
readonly name: string;
readonly version: string;
readonly dist: {
readonly shasum: string;
readonly tarball: string;
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 directories?: ReadonlyArray<string>;
readonly engines?: {readonly [type: string]: string};
readonly _hasShrinkwrap?: boolean;
readonly [key: string]: unknown;
}

interface Person {
readonly name?: string;
readonly email?: string;
readonly url?: string;
}

interface HoistedData {
readonly author?: Person;
readonly bugs?:
| {readonly url: string; readonly email?: string}
| {readonly url?: string; readonly email: string};
readonly contributors?: ReadonlyArray<Person>;
readonly description?: string;
readonly homepage?: string;
readonly keywords?: ReadonlyArray<string>;
readonly license?: string;
readonly maintainers?: ReadonlyArray<Person>;
readonly readme?: string;
readonly readmeFilename?: string;
readonly repository?: {readonly type: string; readonly url: string};
}

interface FullMetadata extends AbbreviatedMetadata, HoistedData {
readonly _id: string;
readonly _rev: string;
readonly time: {
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;
}

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

type VersionNotFoundError = VersionNotFoundErrorClass;
type PackageNotFoundError = PackageNotFoundErrorClass;
}

export interface HoistedData {
readonly author?: Person;
readonly bugs?:
| {readonly url: string; readonly email?: string}
| {readonly url?: string; readonly email: string};
readonly contributors?: ReadonlyArray<Person>;
readonly description?: string;
readonly homepage?: string;
readonly keywords?: ReadonlyArray<string>;
readonly license?: string;
readonly maintainers?: ReadonlyArray<Person>;
readonly readme?: string;
readonly readmeFilename?: string;
readonly repository?: {readonly type: string; readonly url: string};
}
declare const packageJson: {
/**
Get metadata of a package from the npm registry.
@param packageName - Name of the package.
@example
```
import packageJson = require('package-json');
(async () => {
console.log(await packageJson('ava'));
//=> {name: 'ava', ...}
// 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
>;

export interface FullMetadata extends AbbreviatedMetadata, HoistedData {
readonly _id: string;
readonly _rev: string;
readonly time: {
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;
}
/**
The error thrown when the given package version cannot be found.
*/
VersionNotFoundError: typeof VersionNotFoundErrorClass;

export interface FullVersion extends AbbreviatedVersion, HoistedData {
readonly _id: string;
readonly _nodeVersion: string;
readonly _npmUser: string;
readonly _npmVersion: string;
readonly main?: string;
readonly files?: ReadonlyArray<string>;
readonly man?: ReadonlyArray<string>;
readonly scripts?: {readonly [scriptName: string]: string};
readonly gitHead?: string;
readonly types?: string;
readonly typings?: string;
readonly [key: string]: unknown;
}
/**
The error thrown when the given package name cannot be found.
*/
PackageNotFoundError: typeof PackageNotFoundErrorClass;

/**
* Get metadata of a package from the npm registry.
*/
export default function packageJson(
packageName: string,
options: FullMetadataOptions
): Promise<FullMetadata>;
export default function packageJson(
packageName: string,
options?: Options
): Promise<AbbreviatedMetadata>;

/**
* The error thrown when the given package name cannot be found.
*/
export class PackageNotFoundError extends Error {
readonly name: 'PackageNotFoundError'
}
// TODO: remove this in the next major version
default: typeof packageJson;
};

/**
* The error thrown when the given package version cannot be found.
*/
export class VersionNotFoundError extends Error {
readonly name: 'VersionNotFoundError'
}
export = packageJson;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const packageJson = async (packageName, options) => {
};

module.exports = packageJson;
// TODO: remove this in the next major version
module.exports.default = packageJson;
module.exports.PackageNotFoundError = PackageNotFoundError;
module.exports.VersionNotFoundError = VersionNotFoundError;
13 changes: 11 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expectType} from 'tsd-check';
import packageJson, {
import {expectType} from 'tsd';
import packageJson = require('.');
import {
FullMetadata,
FullVersion,
AbbreviatedMetadata,
Expand All @@ -26,3 +27,11 @@ expectType<FullVersion>(fullMetadata.versions['1.2.3']);

expectType<typeof PackageNotFoundError>(PackageNotFoundError);
expectType<typeof VersionNotFoundError>(VersionNotFoundError);

const packageNotFoundError = new PackageNotFoundError('foo');
packageNotFoundError instanceof PackageNotFoundError;
expectType<PackageNotFoundError>(packageNotFoundError);

const versionNotFoundError = new VersionNotFoundError('foo', 'bar');
versionNotFoundError instanceof VersionNotFoundError;
expectType<VersionNotFoundError>(versionNotFoundError);
Loading

0 comments on commit 2e8a263

Please sign in to comment.