Skip to content

Commit 8cc204b

Browse files
authored
fix(typescript): simplify types by using [Conditional Types](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) (#132)
1 parent 8c33742 commit 8cc204b

File tree

3 files changed

+46
-55
lines changed

3 files changed

+46
-55
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,15 @@ Returns an array of strings. Returns <code>options.scopes</code> if it was set a
267267
## Types
268268

269269
```ts
270-
import { ClientType, Options, Result } from "@octokit/oauth-authorization-url";
270+
import {
271+
ClientType,
272+
OAuthAppOptions,
273+
OAuthAppResult,
274+
GitHubAppOptions,
275+
GitHubAppResult,
276+
} from "@octokit/oauth-authorization-url";
271277
```
272278

273-
- `ClientType` is a union of `"oauth-app"` and `"github-app"`
274-
- `Options<"oauth-app">` returns types for OAuth Apps Options. `Options<"github-app">` returns types for GitHub Apps Options.
275-
- `Result<"oauth-app">` returns types for OAuth Apps Result object. `Result<"github-app">` returns types for GitHub Apps Result object.
276-
277279
## License
278280

279281
[MIT](LICENSE)

src/index.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1-
import { ClientType, Options, Result } from "./types";
2-
export { ClientType, Options, Result } from "./types";
1+
import {
2+
ClientType,
3+
OAuthAppOptions,
4+
GitHubAppOptions,
5+
OAuthAppResult,
6+
GitHubAppResult,
7+
} from "./types";
8+
export {
9+
ClientType,
10+
OAuthAppOptions,
11+
GitHubAppOptions,
12+
OAuthAppResult,
13+
GitHubAppResult,
14+
} from "./types";
315

4-
export function oauthAuthorizationUrl<
5-
TClientType extends ClientType = "oauth-app"
6-
>(options: Options<TClientType>): Result<TClientType> {
7-
const scopesNormalized =
8-
typeof options.scopes === "string"
9-
? options.scopes.split(/[,\s]+/).filter(Boolean)
10-
: Array.isArray(options.scopes)
11-
? options.scopes
12-
: [];
16+
export function oauthAuthorizationUrl(options: OAuthAppOptions): OAuthAppResult;
17+
export function oauthAuthorizationUrl(
18+
options: GitHubAppOptions
19+
): GitHubAppResult;
1320

21+
export function oauthAuthorizationUrl(
22+
options: Record<string, unknown>
23+
): Record<string, unknown> {
1424
const clientType = options.clientType || "oauth-app";
1525
const baseUrl = options.baseUrl || "https://github.com";
16-
const common = {
26+
const result: Record<string, unknown> = {
27+
clientType,
1728
allowSignup: options.allowSignup === false ? false : true,
1829
clientId: options.clientId,
1930
login: options.login || null,
@@ -22,21 +33,17 @@ export function oauthAuthorizationUrl<
2233
url: "",
2334
};
2435

25-
const result =
26-
clientType === "oauth-app"
27-
? {
28-
...common,
29-
clientType: "oauth-app",
30-
scopes: scopesNormalized,
31-
}
32-
: {
33-
...common,
34-
clientType: "github-app",
35-
};
36+
if (clientType === "oauth-app") {
37+
const scopes = "scopes" in options ? options.scopes : [];
38+
result.scopes =
39+
typeof scopes === "string"
40+
? scopes.split(/[,\s]+/).filter(Boolean)
41+
: scopes;
42+
}
3643

3744
result.url = urlBuilderAuthorize(`${baseUrl}/login/oauth/authorize`, result);
3845

39-
return result as Result<TClientType>;
46+
return result;
4047
}
4148

4249
function urlBuilderAuthorize(

src/types.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export type ClientType = "oauth-app" | "github-app";
22

3-
export type OAuthAppOptions<TClientType extends "oauth-app"> = {
3+
export type OAuthAppOptions = {
44
clientId: string;
55

6-
clientType?: TClientType;
6+
clientType?: "oauth-app";
77
allowSignup?: boolean;
88
login?: string;
99
scopes?: string | string[];
@@ -12,51 +12,33 @@ export type OAuthAppOptions<TClientType extends "oauth-app"> = {
1212
baseUrl?: string;
1313
};
1414

15-
export type GitHubAppOptions<TClientType extends "github-app"> = {
15+
export type GitHubAppOptions = {
1616
clientId: string;
1717

18-
clientType: TClientType;
19-
/** `scopes` are not permitted for GitHub Apps */
20-
scopes?: never;
18+
clientType: "github-app";
2119
allowSignup?: boolean;
2220
login?: string;
2321
redirectUrl?: string;
2422
state?: string;
2523
baseUrl?: string;
2624
};
2725

28-
export type Options<
29-
TClientType extends ClientType
30-
> = TClientType extends "oauth-app"
31-
? OAuthAppOptions<TClientType>
32-
: TClientType extends "github-app"
33-
? GitHubAppOptions<TClientType>
34-
: never;
35-
36-
type OAuthAppResult<TClientType extends "oauth-app"> = {
26+
export type OAuthAppResult = {
3727
allowSignup: boolean;
3828
clientId: string;
39-
clientType: TClientType;
29+
clientType: "oauth-app";
4030
login: string | null;
4131
redirectUrl: string | null;
4232
scopes: string[];
4333
state: string;
4434
url: string;
4535
};
46-
type GitHubAppResult<TClientType extends "github-app"> = {
36+
export type GitHubAppResult = {
4737
allowSignup: boolean;
4838
clientId: string;
49-
clientType: TClientType;
39+
clientType: "github-app";
5040
login: string | null;
5141
redirectUrl: string | null;
5242
state: string;
5343
url: string;
5444
};
55-
56-
export type Result<
57-
TClientType extends ClientType
58-
> = TClientType extends "oauth-app"
59-
? OAuthAppResult<TClientType>
60-
: TClientType extends "github-app"
61-
? GitHubAppResult<TClientType>
62-
: never;

0 commit comments

Comments
 (0)