index.d.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /// <reference types="node"/>
  2. import {Agent as HttpAgent} from 'http';
  3. import {Agent as HttpsAgent} from 'https';
  4. declare class VersionNotFoundErrorClass extends Error {
  5. readonly name: 'VersionNotFoundError';
  6. constructor(packageName: string, version: string);
  7. }
  8. declare class PackageNotFoundErrorClass extends Error {
  9. readonly name: 'PackageNotFoundError';
  10. constructor(packageName: string);
  11. }
  12. declare namespace packageJson {
  13. interface Agents {
  14. http?: HttpAgent;
  15. https?: HttpsAgent;
  16. }
  17. interface Options {
  18. /**
  19. Package version such as `1.0.0` or a [dist tag](https://docs.npmjs.com/cli/dist-tag) such as `latest`.
  20. The version can also be in any format supported by the [semver](https://github.com/npm/node-semver) module. For example:
  21. - `1` - Get the latest `1.x.x`
  22. - `1.2` - Get the latest `1.2.x`
  23. - `^1.2.3` - Get the latest `1.x.x` but at least `1.2.3`
  24. - `~1.2.3` - Get the latest `1.2.x` but at least `1.2.3`
  25. @default 'latest'
  26. */
  27. readonly version?: string;
  28. /**
  29. 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)
  30. @default false
  31. */
  32. readonly fullMetadata?: boolean;
  33. /**
  34. Return the [main entry](https://registry.npmjs.org/ava) containing all versions.
  35. @default false
  36. */
  37. readonly allVersions?: boolean;
  38. /**
  39. 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.
  40. */
  41. readonly registryUrl?: string;
  42. /**
  43. 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).
  44. */
  45. readonly agent?: HttpAgent | HttpsAgent | Agents | false;
  46. }
  47. interface FullMetadataOptions extends Options {
  48. /**
  49. 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)
  50. @default false
  51. */
  52. readonly fullMetadata: true;
  53. }
  54. interface DistTags {
  55. readonly latest: string;
  56. readonly [tagName: string]: string;
  57. }
  58. interface AbbreviatedMetadata {
  59. readonly 'dist-tags': DistTags;
  60. readonly modified: string;
  61. readonly name: string;
  62. readonly versions: {readonly [version: string]: AbbreviatedVersion};
  63. readonly [key: string]: unknown;
  64. }
  65. interface AbbreviatedVersion {
  66. readonly name: string;
  67. readonly version: string;
  68. readonly dist: {
  69. readonly shasum: string;
  70. readonly tarball: string;
  71. readonly integrity?: string;
  72. };
  73. readonly deprecated?: string;
  74. readonly dependencies?: {readonly [name: string]: string};
  75. readonly optionalDependencies?: {readonly [name: string]: string};
  76. readonly devDependencies?: {readonly [name: string]: string};
  77. readonly bundleDependencies?: {readonly [name: string]: string};
  78. readonly peerDependencies?: {readonly [name: string]: string};
  79. readonly bin?: {readonly [key: string]: string};
  80. readonly directories?: readonly string[];
  81. readonly engines?: {readonly [type: string]: string};
  82. readonly _hasShrinkwrap?: boolean;
  83. readonly [key: string]: unknown;
  84. }
  85. interface Person {
  86. readonly name?: string;
  87. readonly email?: string;
  88. readonly url?: string;
  89. }
  90. interface HoistedData {
  91. readonly author?: Person;
  92. readonly bugs?:
  93. | {readonly url: string; readonly email?: string}
  94. | {readonly url?: string; readonly email: string};
  95. readonly contributors?: readonly Person[];
  96. readonly description?: string;
  97. readonly homepage?: string;
  98. readonly keywords?: readonly string[];
  99. readonly license?: string;
  100. readonly maintainers?: readonly Person[];
  101. readonly readme?: string;
  102. readonly readmeFilename?: string;
  103. readonly repository?: {readonly type: string; readonly url: string};
  104. }
  105. interface FullMetadata extends AbbreviatedMetadata, HoistedData {
  106. readonly _id: string;
  107. readonly _rev: string;
  108. readonly time: {
  109. readonly created: string;
  110. readonly modified: string;
  111. readonly [version: string]: string;
  112. };
  113. readonly users?: {readonly [user: string]: boolean};
  114. readonly versions: {readonly [version: string]: FullVersion};
  115. readonly [key: string]: unknown;
  116. }
  117. interface FullVersion extends AbbreviatedVersion, HoistedData {
  118. readonly _id: string;
  119. readonly _nodeVersion: string;
  120. readonly _npmUser: string;
  121. readonly _npmVersion: string;
  122. readonly main?: string;
  123. readonly files?: readonly string[];
  124. readonly man?: readonly string[];
  125. readonly scripts?: {readonly [scriptName: string]: string};
  126. readonly gitHead?: string;
  127. readonly types?: string;
  128. readonly typings?: string;
  129. readonly [key: string]: unknown;
  130. }
  131. type VersionNotFoundError = VersionNotFoundErrorClass;
  132. type PackageNotFoundError = PackageNotFoundErrorClass;
  133. }
  134. declare const packageJson: {
  135. /**
  136. Get metadata of a package from the npm registry.
  137. @param packageName - Name of the package.
  138. @example
  139. ```
  140. import packageJson = require('package-json');
  141. (async () => {
  142. console.log(await packageJson('ava'));
  143. //=> {name: 'ava', ...}
  144. // Also works with scoped packages
  145. console.log(await packageJson('@sindresorhus/df'));
  146. })();
  147. ```
  148. */
  149. (packageName: string, options: packageJson.FullMetadataOptions): Promise<
  150. packageJson.FullMetadata
  151. >;
  152. (packageName: string, options?: packageJson.Options): Promise<
  153. packageJson.AbbreviatedMetadata
  154. >;
  155. /**
  156. The error thrown when the given package version cannot be found.
  157. */
  158. VersionNotFoundError: typeof VersionNotFoundErrorClass;
  159. /**
  160. The error thrown when the given package name cannot be found.
  161. */
  162. PackageNotFoundError: typeof PackageNotFoundErrorClass;
  163. // TODO: remove this in the next major version
  164. default: typeof packageJson;
  165. };
  166. export = packageJson;