Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 6, 2019
1 parent f39dc2e commit 061fa0d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 94 deletions.
173 changes: 93 additions & 80 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,106 @@
interface CommonOptions {
/**
* The issue body.
*/
readonly body?: string;
declare namespace newGithubIssueUrl {
interface CommonOptions {
/**
The issue body.
*/
readonly body?: string;

/**
* The issue title.
*/
readonly title?: string;
/**
The issue title.
*/
readonly title?: string;

/**
* Use an [issue template](https://help.github.com/articles/manually-creating-a-single-issue-template-for-your-repository/).
*
* @example
*
* 'unicorn.md' // If you want to use a template at `ISSUE_TEMPLATE/unicorn.md`.
*/
readonly template?: string;
/**
Use an [issue template](https://help.github.com/articles/manually-creating-a-single-issue-template-for-your-repository/).
/**
* The labels for the issue.
*
* *Requires the user to have the permission to add labels.*
*/
readonly labels?: string[];
@example
```
'unicorn.md' // If you want to use a template at `ISSUE_TEMPLATE/unicorn.md`.
```
*/
readonly template?: string;

/**
* The milestone for the issue.
*
* *Requires the user to have the permission to add milestone.*
*/
readonly milestone?: string;
/**
The labels for the issue.
/**
* The user to assign to the issue.
*
* *Requires the user to have the permission to add assignee.*
*/
readonly assignee?: string;
_Requires the user to have the permission to add labels._
*/
readonly labels?: string[];

/**
* The projects to add the issue to.
* 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`.
*
* *Requires the user to have the permission to add projects.*
*/
readonly projects?: string[];
}
/**
The milestone for the issue.
_Requires the user to have the permission to add milestone._
*/
readonly milestone?: string;

/**
The user to assign to the issue.
_Requires the user to have the permission to add assignee._
*/
readonly assignee?: string;

/**
The projects to add the issue to.
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`.
_Requires the user to have the permission to add projects._
*/
readonly projects?: string[];
}

interface RepoUrlOptions extends CommonOptions {
/**
The full URL to the repo.
*/
readonly repoUrl: string;
}

interface UserAndRepoOptions extends CommonOptions {
/**
GitHub username or organization.
*/
readonly user: string;

/**
GitHub repo.
*/
readonly repo: string;
}

interface RepoUrlOptions extends CommonOptions {
/**
* The full URL to the repo.
*/
readonly repoUrl: string;
You are required to either specify the `repoUrl` option or both the `user` and `repo` options.
*/
type Options = RepoUrlOptions | UserAndRepoOptions;
}

interface UserAndRepoOptions extends CommonOptions {
declare const newGithubIssueUrl: {
/**
* GitHub username or organization.
*/
readonly user: string;
Generate a URL for opening a new GitHub issue with prefilled title, body, and other fields.
/**
* GitHub repo.
*/
readonly repo: string;
}
@example
```
import newGithubIssueUrl = require('new-github-issue-url');
import opn = require('opn');
const url = newGithubIssueUrl({
user: 'sindresorhus',
repo: 'new-github-issue-url',
body: '\n\n\n---\nI\'m a human. Please be nice.'
});
//=> 'https://github.com/sindresorhus/new-github-issue-url/issues/new?body=%0A%0A%0A---%0AI%27m+a+human.+Please+be+nice.'
// Then open it
opn(url);
```
*/
(options: newGithubIssueUrl.Options): string;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function newGithubIssueUrl(options: newGithubIssueUrl.Options): string;
// export = newGithubIssueUrl;
default: typeof newGithubIssueUrl;
};

/**
* You are required to either specify the `repoUrl` option or both the `user` and `repo` options.
*/
export type Options = RepoUrlOptions | UserAndRepoOptions

/**
* Generate a URL for opening a new GitHub issue with prefilled title, body, and other fields.
*
* @example
*
* import newGithubIssueUrl from 'new-github-issue-url';
* import opn from 'opn';
*
* const url = newGithubIssueUrl({
* user: 'sindresorhus',
* repo: 'new-github-issue-url',
* body: '\n\n\n---\nI\'m a human. Please be nice.'
* });
* //=> 'https://github.com/sindresorhus/new-github-issue-url/issues/new?body=%0A%0A%0A---%0AI%27m+a+human.+Please+be+nice.'
*
* // Then open it
* opn(url);
*/
export default function newGithubIssueUrl(options: Options): string;
export = newGithubIssueUrl;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ module.exports = (options = {}) => {
return url.toString();
};

// TODO: Remove this for the next major release
module.exports.default = module.exports;
24 changes: 14 additions & 10 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {expectType} from 'tsd-check';
import newGithubIssueUrl from '.';
import {expectType} from 'tsd';
import newGithubIssueUrl = require('.');

expectType<string>(newGithubIssueUrl({
repoUrl: 'https://github.com/sindresorhus/new-github-issue-url',
body: 'test'
}));
expectType<string>(
newGithubIssueUrl({
repoUrl: 'https://github.com/sindresorhus/new-github-issue-url',
body: 'test'
})
);

expectType<string>(newGithubIssueUrl({
user: 'sindresorhus',
repo: 'new-github-issue-url'
}));
expectType<string>(
newGithubIssueUrl({
user: 'sindresorhus',
repo: 'new-github-issue-url'
})
);
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": ">=10"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -32,8 +32,8 @@
"params"
],
"devDependencies": {
"ava": "^0.25.0",
"tsd-check": "^0.3.0",
"xo": "^0.23.0"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}

0 comments on commit 061fa0d

Please sign in to comment.