Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 31, 2019
1 parent c551091 commit 7b1a6b8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
69 changes: 55 additions & 14 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,62 @@
declare class InvalidNameErrorClass extends Error {}

declare const npmName: {
/**
* Check whether a package name is available (not registered) on npm.
*
* @param name - Name to check.
* @returns Whether the given name is available.
*/
Check whether a package name is available (not registered) on npm.
@param name - Name to check.
@returns Whether the given name is available.
@example
```
import npmName = require('npm-name');
(async () => {
console.log(await npmName('chalk'));
//=> false
try {
await npmName('_ABC');
} catch (error) {
console.log(error.message);
// Invalid package name: _ABC
// - name cannot start with an underscore
// - name can no longer contain capital letters
}
})();
```
*/
(name: string): Promise<boolean>;

/**
* Check whether multiple package names are available (not registered) on npm.
*
* @param names - Multiple names to check.
* @returns A `Map` of name and status.
*/
many<NameType extends string>(names: NameType[]): Promise<Map<NameType, boolean>>;
};
Check whether multiple package names are available (not registered) on npm.
export default npmName;
@param names - Multiple names to check.
@returns A `Map` of name and status.
@example
```
import npmName = require('npm-name');
(async () => {
const result = await npmName.many(['chalk', '@sindresorhus/is', 'abc123']);
console.log(result.get('chalk'));
//=> false
console.log(result.get('@sindresorhus/is'));
//=> false
console.log(result.get('abc123'));
//=> true
})();
```
*/
many<NameType extends string>(
names: NameType[]
): Promise<Map<NameType, boolean>>;

InvalidNameError: typeof InvalidNameErrorClass;

// TODO: remove this in the next major version
default: typeof npmName;
};

export class InvalidNameError extends Error {}
export = npmName;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const npmName = name => {
};

module.exports = npmName;
// TODO: remove this in the next major version
module.exports.default = npmName;

module.exports.many = async names => {
Expand Down
7 changes: 4 additions & 3 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 npmName, {InvalidNameError} from '.';
import {expectType} from 'tsd';
import npmName = require('.');
import {InvalidNameError} from '.';

expectType<Promise<boolean>>(npmName('chalk'));

Expand All @@ -9,4 +10,4 @@ expectType<Promise<Map<'chalk' | '@sindresorhus/is' | 'abc123', boolean>>>(
);
expectType<boolean | undefined>((await manyResult).get('chalk'));

expectType<typeof InvalidNameError>(InvalidNameError);
new InvalidNameError('foo') instanceof InvalidNameError;
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -33,13 +33,13 @@
"got": "^9.6.0",
"is-scoped": "^1.0.0",
"lodash.zip": "^4.2.0",
"registry-auth-token": "^3.3.2",
"registry-auth-token": "^3.4.0",
"registry-url": "^4.0.0",
"validate-npm-package-name": "^3.0.0"
},
"devDependencies": {
"ava": "^1.3.1",
"tsd-check": "^0.3.0",
"ava": "^1.4.1",
"tsd": "^0.7.1",
"unique-string": "^1.0.0",
"xo": "^0.24.0"
}
Expand Down

0 comments on commit 7b1a6b8

Please sign in to comment.