Skip to content

fix(typescript): simplify types by using [Conditional Types](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,15 @@ Returns an array of strings. Returns <code>options.scopes</code> if it was set a
## Types

```ts
import { ClientType, Options, Result } from "@octokit/oauth-authorization-url";
import {
ClientType,
OAuthAppOptions,
OAuthAppResult,
GitHubAppOptions,
GitHubAppResult,
} from "@octokit/oauth-authorization-url";
```

- `ClientType` is a union of `"oauth-app"` and `"github-app"`
- `Options<"oauth-app">` returns types for OAuth Apps Options. `Options<"github-app">` returns types for GitHub Apps Options.
- `Result<"oauth-app">` returns types for OAuth Apps Result object. `Result<"github-app">` returns types for GitHub Apps Result object.

## License

[MIT](LICENSE)
55 changes: 31 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { ClientType, Options, Result } from "./types";
export { ClientType, Options, Result } from "./types";
import {
ClientType,
OAuthAppOptions,
GitHubAppOptions,
OAuthAppResult,
GitHubAppResult,
} from "./types";
export {
ClientType,
OAuthAppOptions,
GitHubAppOptions,
OAuthAppResult,
GitHubAppResult,
} from "./types";

export function oauthAuthorizationUrl<
TClientType extends ClientType = "oauth-app"
>(options: Options<TClientType>): Result<TClientType> {
const scopesNormalized =
typeof options.scopes === "string"
? options.scopes.split(/[,\s]+/).filter(Boolean)
: Array.isArray(options.scopes)
? options.scopes
: [];
export function oauthAuthorizationUrl(options: OAuthAppOptions): OAuthAppResult;
export function oauthAuthorizationUrl(
options: GitHubAppOptions
): GitHubAppResult;

export function oauthAuthorizationUrl(
options: Record<string, unknown>
): Record<string, unknown> {
const clientType = options.clientType || "oauth-app";
const baseUrl = options.baseUrl || "https://github.com";
const common = {
const result: Record<string, unknown> = {
clientType,
allowSignup: options.allowSignup === false ? false : true,
clientId: options.clientId,
login: options.login || null,
Expand All @@ -22,21 +33,17 @@ export function oauthAuthorizationUrl<
url: "",
};

const result =
clientType === "oauth-app"
? {
...common,
clientType: "oauth-app",
scopes: scopesNormalized,
}
: {
...common,
clientType: "github-app",
};
if (clientType === "oauth-app") {
const scopes = "scopes" in options ? options.scopes : [];
result.scopes =
typeof scopes === "string"
? scopes.split(/[,\s]+/).filter(Boolean)
: scopes;
}

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

return result as Result<TClientType>;
return result;
}

function urlBuilderAuthorize(
Expand Down
34 changes: 8 additions & 26 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export type ClientType = "oauth-app" | "github-app";

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

clientType?: TClientType;
clientType?: "oauth-app";
allowSignup?: boolean;
login?: string;
scopes?: string | string[];
Expand All @@ -12,51 +12,33 @@ export type OAuthAppOptions<TClientType extends "oauth-app"> = {
baseUrl?: string;
};

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

clientType: TClientType;
/** `scopes` are not permitted for GitHub Apps */
scopes?: never;
clientType: "github-app";
allowSignup?: boolean;
login?: string;
redirectUrl?: string;
state?: string;
baseUrl?: string;
};

export type Options<
TClientType extends ClientType
> = TClientType extends "oauth-app"
? OAuthAppOptions<TClientType>
: TClientType extends "github-app"
? GitHubAppOptions<TClientType>
: never;

type OAuthAppResult<TClientType extends "oauth-app"> = {
export type OAuthAppResult = {
allowSignup: boolean;
clientId: string;
clientType: TClientType;
clientType: "oauth-app";
login: string | null;
redirectUrl: string | null;
scopes: string[];
state: string;
url: string;
};
type GitHubAppResult<TClientType extends "github-app"> = {
export type GitHubAppResult = {
allowSignup: boolean;
clientId: string;
clientType: TClientType;
clientType: "github-app";
login: string | null;
redirectUrl: string | null;
state: string;
url: string;
};

export type Result<
TClientType extends ClientType
> = TClientType extends "oauth-app"
? OAuthAppResult<TClientType>
: TClientType extends "github-app"
? GitHubAppResult<TClientType>
: never;