Skip to content

Commit 6be28b8

Browse files
committed
breaking: new export name
BREAKING CHANGE: `import { oauthLoginUrl } from "@octokit/oauth-authorization-url"` is now `import { oauthAuthorizationUrl } from "@octokit/oauth-authorization-url" BREAKING CHANGE: `.defaults()` method has been removed BREAKING CHANGE: `redirectUri` option is now `redirectUrl` BREAKING CHANGE: `defaultRedirectUri` option has been removed BREAKING CHANGE: `log` option has been removed
1 parent b8d4b3a commit 6be28b8

File tree

4 files changed

+198
-191
lines changed

4 files changed

+198
-191
lines changed

README.md

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Load `@octokit/oauth-authorization-url` directly from [cdn.pika.dev](https://cdn
2222

2323
```html
2424
<script type="module">
25-
import { oauthLoginUrl } from "https://cdn.pika.dev/@octokit/oauth-authorization-url";
25+
import { oauthAuthorizationUrl } from "https://cdn.pika.dev/@octokit/oauth-authorization-url";
2626
</script>
2727
```
2828

@@ -36,41 +36,31 @@ Load `@octokit/oauth-authorization-url` directly from [cdn.pika.dev](https://cdn
3636
Install with <code>npm install @octokit/oauth-authorization-url</code>
3737

3838
```js
39-
const { oauthLoginUrl } = require("@octokit/oauth-authorization-url");
40-
// or: import { oauthLoginUrl } from "@octokit/oauth-authorization-url";
39+
const { oauthAuthorizationUrl } = require("@octokit/oauth-authorization-url");
40+
// or: import { oauthAuthorizationUrl } from "@octokit/oauth-authorization-url";
4141
```
4242

4343
</td></tr>
4444
</tbody>
4545
</table>
4646

4747
```js
48-
const { url, clientId, redirectUri, login, scopes, state } = oauthLoginUrl({
48+
const {
49+
url,
50+
clientId,
51+
redirectUrl,
52+
login,
53+
scopes,
54+
state
55+
} = oauthAuthorizationUrl({
4956
clientId: "1234567890abcdef1234",
50-
redirectUri: "https://example.com",
57+
redirectUrl: "https://example.com",
5158
login: "octocat",
5259
scopes: ["repo", "admin:org"],
53-
state: "secret123",
54-
log: {
55-
warn(message) {
56-
myLogger.log(message, { level: "warn" });
57-
}
58-
}
60+
state: "secret123"
5961
});
6062
```
6163

62-
Override or set default options
63-
64-
```js
65-
const myLogin = login.defaults({
66-
baseUrl: "https://github.my-enterprise.com",
67-
defaultRedirectUri: "https://app.my-enterprise.com",
68-
client: "1234567890abcdef1234"
69-
});
70-
71-
location.href = oauthLoginUrl().url;
72-
```
73-
7464
## Options
7565

7666
<table>
@@ -95,7 +85,7 @@ location.href = oauthLoginUrl().url;
9585
</tr>
9686
<tr>
9787
<th>
98-
<code>redirectUri</code>
88+
<code>redirectUrl</code>
9989
</th>
10090
<td>
10191
The URL in your application where users will be sent after authorization. See <a href="https://developer.github.com/enterprise/2.16/apps/building-oauth-apps/authorizing-oauth-apps/#redirect-urls">Redirect URLs</a> in GitHub’s Developer Guide.
@@ -134,14 +124,6 @@ location.href = oauthLoginUrl().url;
134124
Whether or not unauthenticated users will be offered an option to sign up for GitHub during the OAuth flow. The default is <code>true</code>. Use <code>false</code> in the case that a policy prohibits signups.
135125
</td>
136126
</tr>
137-
<tr>
138-
<th>
139-
<code>log</code>
140-
</th>
141-
<td>
142-
When invalid options are passed, warnings are logged using <code>log.warn(message)</code>. Defaults to <a href="https://developer.mozilla.org/en-US/docs/Web/API/console"><code>console</code></a>.
143-
</td>
144-
</tr>
145127
<tr>
146128
<th>
147129
<code>baseUrl</code>
@@ -150,20 +132,12 @@ location.href = oauthLoginUrl().url;
150132
When using GitHub Enterprise Server, set the baseUrl to the origin, e.g. <code>https://github.my-enterprise.com/</code>.
151133
</td>
152134
</tr>
153-
<tr>
154-
<th>
155-
<code>defaultRedirectUri</code>
156-
</th>
157-
<td>
158-
Set to the redirect URL as defined in your OAuth app. When a <code>redirectUri</code> is passed which does not include <code>defaultRedirectUri</code>, an error is thrown.
159-
</td>
160-
</tr>
161135
</tbody>
162136
</table>
163137

164138
## Result
165139

166-
`oauthLoginUrl()` returns an object with the following properties
140+
`oauthAuthorizationUrl()` returns an object with the following properties
167141

168142
<table>
169143
<thead align=left>
@@ -203,10 +177,10 @@ location.href = oauthLoginUrl().url;
203177
</tr>
204178
<tr>
205179
<th>
206-
<code>redirectUri</code>
180+
<code>redirectUrl</code>
207181
</th>
208182
<td>
209-
Returns <code>options.redirectUri</code> if it was set. Defaults to <code>options.defaultRedirectUri</code> if it was set, otherwise <code>null</code>.
183+
Returns <code>options.redirectUrl</code> if it was set. Defaults to <code>null</code>.
210184
</td>
211185
</tr>
212186
<tr>

src/index.ts

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
1-
import { Options, Result, ResultKeys } from './types'
1+
import { Options, Result, ResultKeys } from "./types";
22

3-
export const BASE_URL = 'https://github.com/login/oauth/authorize'
3+
export const BASE_URL = "https://github.com/login/oauth/authorize";
44

5-
export function oauthLoginUrl (options: Options): Result {
6-
const scopesNormalized = typeof options.scopes === 'string'
7-
? options.scopes.split(/[,\s]+/).filter(Boolean)
8-
: Array.isArray(options.scopes) ? options.scopes : []
5+
export function oauthAuthorizationUrl(options: Options): Result {
6+
const scopesNormalized =
7+
typeof options.scopes === "string"
8+
? options.scopes.split(/[,\s]+/).filter(Boolean)
9+
: Array.isArray(options.scopes)
10+
? options.scopes
11+
: [];
912

1013
const result: Result = {
1114
allowSignup: options.allowSignup === false ? false : true,
1215
clientId: options.clientId,
1316
login: options.login || null,
1417
redirectUrl: options.redirectUrl || null,
1518
scopes: scopesNormalized,
16-
state: options.state || Math.random().toString(36).substr(2),
17-
url: ''
18-
}
19-
result.url = urlBuilderAuthorize(BASE_URL, result)
19+
state:
20+
options.state ||
21+
Math.random()
22+
.toString(36)
23+
.substr(2),
24+
url: ""
25+
};
26+
result.url = urlBuilderAuthorize(BASE_URL, result);
2027

21-
return result
28+
return result;
2229
}
2330

24-
function urlBuilderAuthorize (base: string, options: Result) {
31+
function urlBuilderAuthorize(base: string, options: Result) {
2532
const map = {
26-
allowSignup: 'allow_signup',
27-
clientId: 'client_id',
28-
login: 'login',
29-
redirectUrl: 'redirect_url',
30-
scopes: 'scope',
31-
state: 'state',
32-
}
33+
allowSignup: "allow_signup",
34+
clientId: "client_id",
35+
login: "login",
36+
redirectUrl: "redirect_url",
37+
scopes: "scope",
38+
state: "state"
39+
};
3340

34-
let url = base
41+
let url = base;
3542

36-
Object.entries(options).filter(([k, v]) => v !== null && k !== 'url') // Filter out keys that are null and remove the url key
37-
.filter(([, v]) => Array.isArray(v) ? v.length !== 0 : true) // Filter out empty Array
38-
.map(([key, ]) => [ map[key as ResultKeys], `${options[key as ResultKeys]!}` ]) // Map Array with the proper URL parameter names and change the value to a string using template strings
39-
.forEach(([key, value], index) => { // Finally, build the URL
40-
url += index === 0 ? `?` : '&'
41-
url += `${key}=${value}`
42-
})
43-
return url
43+
Object.entries(options)
44+
.filter(([k, v]) => v !== null && k !== "url") // Filter out keys that are null and remove the url key
45+
.filter(([, v]) => (Array.isArray(v) ? v.length !== 0 : true)) // Filter out empty Array
46+
.map(([key]) => [map[key as ResultKeys], `${options[key as ResultKeys]!}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings
47+
.forEach(([key, value], index) => {
48+
// Finally, build the URL
49+
url += index === 0 ? `?` : "&";
50+
url += `${key}=${value}`;
51+
});
52+
return url;
4453
}

src/types.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
export interface Options {
2-
clientId: string
2+
clientId: string;
33

4-
allowSignup?: boolean
5-
login?: string
6-
scopes?: string | string[]
7-
redirectUrl?: string
8-
state?: string
9-
log?: {
10-
[key: string]: (message: string) => void
11-
}
4+
allowSignup?: boolean;
5+
login?: string;
6+
scopes?: string | string[];
7+
redirectUrl?: string;
8+
state?: string;
129
}
1310

1411
export interface Result {
15-
allowSignup: boolean,
16-
clientId: string,
17-
login: string | null,
18-
redirectUrl: string | null,
19-
scopes: string[],
20-
state: string,
21-
url: string
12+
allowSignup: boolean;
13+
clientId: string;
14+
login: string | null;
15+
redirectUrl: string | null;
16+
scopes: string[];
17+
state: string;
18+
url: string;
2219
}
2320

24-
export type ResultKeys = Exclude<keyof Result, 'url'>
21+
export type ResultKeys = Exclude<keyof Result, "url">;

0 commit comments

Comments
 (0)