Skip to content

Commit 061fa0d

Browse files
BendingBendersindresorhus
authored andcommitted
Refactor TypeScript definition to CommonJS compatible export (#3)
1 parent f39dc2e commit 061fa0d

File tree

4 files changed

+112
-94
lines changed

4 files changed

+112
-94
lines changed

index.d.ts

Lines changed: 93 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,106 @@
1-
interface CommonOptions {
2-
/**
3-
* The issue body.
4-
*/
5-
readonly body?: string;
1+
declare namespace newGithubIssueUrl {
2+
interface CommonOptions {
3+
/**
4+
The issue body.
5+
*/
6+
readonly body?: string;
67

7-
/**
8-
* The issue title.
9-
*/
10-
readonly title?: string;
8+
/**
9+
The issue title.
10+
*/
11+
readonly title?: string;
1112

12-
/**
13-
* Use an [issue template](https://help.github.com/articles/manually-creating-a-single-issue-template-for-your-repository/).
14-
*
15-
* @example
16-
*
17-
* 'unicorn.md' // If you want to use a template at `ISSUE_TEMPLATE/unicorn.md`.
18-
*/
19-
readonly template?: string;
13+
/**
14+
Use an [issue template](https://help.github.com/articles/manually-creating-a-single-issue-template-for-your-repository/).
2015
21-
/**
22-
* The labels for the issue.
23-
*
24-
* *Requires the user to have the permission to add labels.*
25-
*/
26-
readonly labels?: string[];
16+
@example
17+
```
18+
'unicorn.md' // If you want to use a template at `ISSUE_TEMPLATE/unicorn.md`.
19+
```
20+
*/
21+
readonly template?: string;
2722

28-
/**
29-
* The milestone for the issue.
30-
*
31-
* *Requires the user to have the permission to add milestone.*
32-
*/
33-
readonly milestone?: string;
23+
/**
24+
The labels for the issue.
3425
35-
/**
36-
* The user to assign to the issue.
37-
*
38-
* *Requires the user to have the permission to add assignee.*
39-
*/
40-
readonly assignee?: string;
26+
_Requires the user to have the permission to add labels._
27+
*/
28+
readonly labels?: string[];
4129

42-
/**
43-
* The projects to add the issue to.
44-
* The project reference format is `user/<project-number>`, for example, if the URL to the project is `https://github.com/sindresorhus/some-repo/projects/3`, the project reference would be `some-repo/3`.
45-
*
46-
* *Requires the user to have the permission to add projects.*
47-
*/
48-
readonly projects?: string[];
49-
}
30+
/**
31+
The milestone for the issue.
32+
33+
_Requires the user to have the permission to add milestone._
34+
*/
35+
readonly milestone?: string;
36+
37+
/**
38+
The user to assign to the issue.
39+
40+
_Requires the user to have the permission to add assignee._
41+
*/
42+
readonly assignee?: string;
43+
44+
/**
45+
The projects to add the issue to.
46+
The project reference format is `user/<project-number>`, for example, if the URL to the project is `https://github.com/sindresorhus/some-repo/projects/3`, the project reference would be `some-repo/3`.
47+
48+
_Requires the user to have the permission to add projects._
49+
*/
50+
readonly projects?: string[];
51+
}
52+
53+
interface RepoUrlOptions extends CommonOptions {
54+
/**
55+
The full URL to the repo.
56+
*/
57+
readonly repoUrl: string;
58+
}
59+
60+
interface UserAndRepoOptions extends CommonOptions {
61+
/**
62+
GitHub username or organization.
63+
*/
64+
readonly user: string;
65+
66+
/**
67+
GitHub repo.
68+
*/
69+
readonly repo: string;
70+
}
5071

51-
interface RepoUrlOptions extends CommonOptions {
5272
/**
53-
* The full URL to the repo.
54-
*/
55-
readonly repoUrl: string;
73+
You are required to either specify the `repoUrl` option or both the `user` and `repo` options.
74+
*/
75+
type Options = RepoUrlOptions | UserAndRepoOptions;
5676
}
5777

58-
interface UserAndRepoOptions extends CommonOptions {
78+
declare const newGithubIssueUrl: {
5979
/**
60-
* GitHub username or organization.
61-
*/
62-
readonly user: string;
80+
Generate a URL for opening a new GitHub issue with prefilled title, body, and other fields.
6381
64-
/**
65-
* GitHub repo.
66-
*/
67-
readonly repo: string;
68-
}
82+
@example
83+
```
84+
import newGithubIssueUrl = require('new-github-issue-url');
85+
import opn = require('opn');
86+
87+
const url = newGithubIssueUrl({
88+
user: 'sindresorhus',
89+
repo: 'new-github-issue-url',
90+
body: '\n\n\n---\nI\'m a human. Please be nice.'
91+
});
92+
//=> 'https://github.com/sindresorhus/new-github-issue-url/issues/new?body=%0A%0A%0A---%0AI%27m+a+human.+Please+be+nice.'
93+
94+
// Then open it
95+
opn(url);
96+
```
97+
*/
98+
(options: newGithubIssueUrl.Options): string;
99+
100+
// TODO: Remove this for the next major release, refactor the whole definition to:
101+
// declare function newGithubIssueUrl(options: newGithubIssueUrl.Options): string;
102+
// export = newGithubIssueUrl;
103+
default: typeof newGithubIssueUrl;
104+
};
69105

70-
/**
71-
* You are required to either specify the `repoUrl` option or both the `user` and `repo` options.
72-
*/
73-
export type Options = RepoUrlOptions | UserAndRepoOptions
74-
75-
/**
76-
* Generate a URL for opening a new GitHub issue with prefilled title, body, and other fields.
77-
*
78-
* @example
79-
*
80-
* import newGithubIssueUrl from 'new-github-issue-url';
81-
* import opn from 'opn';
82-
*
83-
* const url = newGithubIssueUrl({
84-
* user: 'sindresorhus',
85-
* repo: 'new-github-issue-url',
86-
* body: '\n\n\n---\nI\'m a human. Please be nice.'
87-
* });
88-
* //=> 'https://github.com/sindresorhus/new-github-issue-url/issues/new?body=%0A%0A%0A---%0AI%27m+a+human.+Please+be+nice.'
89-
*
90-
* // Then open it
91-
* opn(url);
92-
*/
93-
export default function newGithubIssueUrl(options: Options): string;
106+
export = newGithubIssueUrl;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ module.exports = (options = {}) => {
4242
return url.toString();
4343
};
4444

45+
// TODO: Remove this for the next major release
4546
module.exports.default = module.exports;

index.test-d.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import {expectType} from 'tsd-check';
2-
import newGithubIssueUrl from '.';
1+
import {expectType} from 'tsd';
2+
import newGithubIssueUrl = require('.');
33

4-
expectType<string>(newGithubIssueUrl({
5-
repoUrl: 'https://github.com/sindresorhus/new-github-issue-url',
6-
body: 'test'
7-
}));
4+
expectType<string>(
5+
newGithubIssueUrl({
6+
repoUrl: 'https://github.com/sindresorhus/new-github-issue-url',
7+
body: 'test'
8+
})
9+
);
810

9-
expectType<string>(newGithubIssueUrl({
10-
user: 'sindresorhus',
11-
repo: 'new-github-issue-url'
12-
}));
11+
expectType<string>(
12+
newGithubIssueUrl({
13+
user: 'sindresorhus',
14+
repo: 'new-github-issue-url'
15+
})
16+
);

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node": ">=10"
1414
},
1515
"scripts": {
16-
"test": "xo && ava && tsd-check"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
1919
"index.js",
@@ -32,8 +32,8 @@
3232
"params"
3333
],
3434
"devDependencies": {
35-
"ava": "^0.25.0",
36-
"tsd-check": "^0.3.0",
37-
"xo": "^0.23.0"
35+
"ava": "^1.4.1",
36+
"tsd": "^0.7.2",
37+
"xo": "^0.24.0"
3838
}
3939
}

0 commit comments

Comments
 (0)