@octokit/oauth-authorization-url
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -5,5 +5,5 @@ 'use strict'; | ||
const BASE_URL = 'https://github.com/login/oauth/authorize'; | ||
function oauthLoginUrl(options) { | ||
const scopesNormalized = typeof options.scopes === 'string' ? options.scopes.split(/[,\s]+/).filter(Boolean) : Array.isArray(options.scopes) ? options.scopes : []; | ||
const BASE_URL = "https://github.com/login/oauth/authorize"; | ||
function oauthAuthorizationUrl(options) { | ||
const scopesNormalized = typeof options.scopes === "string" ? options.scopes.split(/[,\s]+/).filter(Boolean) : Array.isArray(options.scopes) ? options.scopes : []; | ||
const result = { | ||
@@ -16,3 +16,3 @@ allowSignup: options.allowSignup === false ? false : true, | ||
state: options.state || Math.random().toString(36).substr(2), | ||
url: '' | ||
url: "" | ||
}; | ||
@@ -25,15 +25,16 @@ result.url = urlBuilderAuthorize(BASE_URL, result); | ||
const map = { | ||
allowSignup: 'allow_signup', | ||
clientId: 'client_id', | ||
login: 'login', | ||
redirectUrl: 'redirect_url', | ||
scopes: 'scope', | ||
state: 'state' | ||
allowSignup: "allow_signup", | ||
clientId: "client_id", | ||
login: "login", | ||
redirectUrl: "redirect_url", | ||
scopes: "scope", | ||
state: "state" | ||
}; | ||
let url = base; | ||
Object.entries(options).filter(([k, v]) => v !== null && k !== 'url') // Filter out keys that are null and remove the url key | ||
Object.entries(options).filter(([k, v]) => v !== null && k !== "url") // Filter out keys that are null and remove the url key | ||
.filter(([, v]) => Array.isArray(v) ? v.length !== 0 : true) // Filter out empty Array | ||
.map(([key]) => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
.forEach(([key, value], index) => { | ||
url += index === 0 ? `?` : '&'; | ||
// Finally, build the URL | ||
url += index === 0 ? `?` : "&"; | ||
url += `${key}=${value}`; | ||
@@ -45,3 +46,3 @@ }); | ||
exports.BASE_URL = BASE_URL; | ||
exports.oauthLoginUrl = oauthLoginUrl; | ||
exports.oauthAuthorizationUrl = oauthAuthorizationUrl; | ||
//# sourceMappingURL=index.js.map |
@@ -1,6 +0,8 @@ | ||
export const BASE_URL = 'https://github.com/login/oauth/authorize'; | ||
export function oauthLoginUrl(options) { | ||
const scopesNormalized = typeof options.scopes === 'string' | ||
export const BASE_URL = "https://github.com/login/oauth/authorize"; | ||
export function oauthAuthorizationUrl(options) { | ||
const scopesNormalized = typeof options.scopes === "string" | ||
? options.scopes.split(/[,\s]+/).filter(Boolean) | ||
: Array.isArray(options.scopes) ? options.scopes : []; | ||
: Array.isArray(options.scopes) | ||
? options.scopes | ||
: []; | ||
const result = { | ||
@@ -12,4 +14,7 @@ allowSignup: options.allowSignup === false ? false : true, | ||
scopes: scopesNormalized, | ||
state: options.state || Math.random().toString(36).substr(2), | ||
url: '' | ||
state: options.state || | ||
Math.random() | ||
.toString(36) | ||
.substr(2), | ||
url: "" | ||
}; | ||
@@ -21,15 +26,17 @@ result.url = urlBuilderAuthorize(BASE_URL, result); | ||
const map = { | ||
allowSignup: 'allow_signup', | ||
clientId: 'client_id', | ||
login: 'login', | ||
redirectUrl: 'redirect_url', | ||
scopes: 'scope', | ||
state: 'state', | ||
allowSignup: "allow_signup", | ||
clientId: "client_id", | ||
login: "login", | ||
redirectUrl: "redirect_url", | ||
scopes: "scope", | ||
state: "state" | ||
}; | ||
let url = base; | ||
Object.entries(options).filter(([k, v]) => v !== null && k !== 'url') // Filter out keys that are null and remove the url key | ||
.filter(([, v]) => Array.isArray(v) ? v.length !== 0 : true) // Filter out empty Array | ||
.map(([key,]) => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
Object.entries(options) | ||
.filter(([k, v]) => v !== null && k !== "url") // Filter out keys that are null and remove the url key | ||
.filter(([, v]) => (Array.isArray(v) ? v.length !== 0 : true)) // Filter out empty Array | ||
.map(([key]) => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
.forEach(([key, value], index) => { | ||
url += index === 0 ? `?` : '&'; | ||
// Finally, build the URL | ||
url += index === 0 ? `?` : "&"; | ||
url += `${key}=${value}`; | ||
@@ -36,0 +43,0 @@ }); |
@@ -1,3 +0,3 @@ | ||
import { Options, Result } from './types'; | ||
import { Options, Result } from "./types"; | ||
export declare const BASE_URL = "https://github.com/login/oauth/authorize"; | ||
export declare function oauthLoginUrl(options: Options): Result; | ||
export declare function oauthAuthorizationUrl(options: Options): Result; |
@@ -8,5 +8,2 @@ export interface Options { | ||
state?: string; | ||
log?: { | ||
[key: string]: (message: string) => void; | ||
}; | ||
} | ||
@@ -22,2 +19,2 @@ export interface Result { | ||
} | ||
export declare type ResultKeys = Exclude<keyof Result, 'url'>; | ||
export declare type ResultKeys = Exclude<keyof Result, "url">; |
@@ -1,6 +0,8 @@ | ||
const BASE_URL = 'https://github.com/login/oauth/authorize'; | ||
function oauthLoginUrl(options) { | ||
const scopesNormalized = typeof options.scopes === 'string' | ||
const BASE_URL = "https://github.com/login/oauth/authorize"; | ||
function oauthAuthorizationUrl(options) { | ||
const scopesNormalized = typeof options.scopes === "string" | ||
? options.scopes.split(/[,\s]+/).filter(Boolean) | ||
: Array.isArray(options.scopes) ? options.scopes : []; | ||
: Array.isArray(options.scopes) | ||
? options.scopes | ||
: []; | ||
const result = { | ||
@@ -12,4 +14,7 @@ allowSignup: options.allowSignup === false ? false : true, | ||
scopes: scopesNormalized, | ||
state: options.state || Math.random().toString(36).substr(2), | ||
url: '' | ||
state: options.state || | ||
Math.random() | ||
.toString(36) | ||
.substr(2), | ||
url: "" | ||
}; | ||
@@ -21,15 +26,17 @@ result.url = urlBuilderAuthorize(BASE_URL, result); | ||
const map = { | ||
allowSignup: 'allow_signup', | ||
clientId: 'client_id', | ||
login: 'login', | ||
redirectUrl: 'redirect_url', | ||
scopes: 'scope', | ||
state: 'state', | ||
allowSignup: "allow_signup", | ||
clientId: "client_id", | ||
login: "login", | ||
redirectUrl: "redirect_url", | ||
scopes: "scope", | ||
state: "state" | ||
}; | ||
let url = base; | ||
Object.entries(options).filter(([k, v]) => v !== null && k !== 'url') // Filter out keys that are null and remove the url key | ||
.filter(([, v]) => Array.isArray(v) ? v.length !== 0 : true) // Filter out empty Array | ||
.map(([key,]) => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
Object.entries(options) | ||
.filter(([k, v]) => v !== null && k !== "url") // Filter out keys that are null and remove the url key | ||
.filter(([, v]) => (Array.isArray(v) ? v.length !== 0 : true)) // Filter out empty Array | ||
.map(([key]) => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
.forEach(([key, value], index) => { | ||
url += index === 0 ? `?` : '&'; | ||
// Finally, build the URL | ||
url += index === 0 ? `?` : "&"; | ||
url += `${key}=${value}`; | ||
@@ -40,3 +47,3 @@ }); | ||
export { BASE_URL, oauthLoginUrl }; | ||
export { BASE_URL, oauthAuthorizationUrl }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@octokit/oauth-authorization-url", | ||
"description": "Universal library to retrieve GitHub’s identity URL for the OAuth web flow", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "files": [ |
@@ -25,3 +25,3 @@ # oauth-authorization-url.js | ||
<script type="module"> | ||
import { oauthLoginUrl } from "https://cdn.pika.dev/@octokit/oauth-authorization-url"; | ||
import { oauthAuthorizationUrl } from "https://cdn.pika.dev/@octokit/oauth-authorization-url"; | ||
</script> | ||
@@ -40,4 +40,4 @@ ``` | ||
```js | ||
const { oauthLoginUrl } = require("@octokit/oauth-authorization-url"); | ||
// or: import { oauthLoginUrl } from "@octokit/oauth-authorization-url"; | ||
const { oauthAuthorizationUrl } = require("@octokit/oauth-authorization-url"); | ||
// or: import { oauthAuthorizationUrl } from "@octokit/oauth-authorization-url"; | ||
``` | ||
@@ -50,28 +50,18 @@ | ||
```js | ||
const { url, clientId, redirectUri, login, scopes, state } = oauthLoginUrl({ | ||
const { | ||
url, | ||
clientId, | ||
redirectUrl, | ||
login, | ||
scopes, | ||
state | ||
} = oauthAuthorizationUrl({ | ||
clientId: "1234567890abcdef1234", | ||
redirectUri: "https://example.com", | ||
redirectUrl: "https://example.com", | ||
login: "octocat", | ||
scopes: ["repo", "admin:org"], | ||
state: "secret123", | ||
log: { | ||
warn(message) { | ||
myLogger.log(message, { level: "warn" }); | ||
} | ||
} | ||
state: "secret123" | ||
}); | ||
``` | ||
Override or set default options | ||
```js | ||
const myLogin = login.defaults({ | ||
baseUrl: "https://github.my-enterprise.com", | ||
defaultRedirectUri: "https://app.my-enterprise.com", | ||
client: "1234567890abcdef1234" | ||
}); | ||
location.href = oauthLoginUrl().url; | ||
``` | ||
## Options | ||
@@ -101,3 +91,3 @@ | ||
<th> | ||
<code>redirectUri</code> | ||
<code>redirectUrl</code> | ||
</th> | ||
@@ -143,10 +133,2 @@ <td> | ||
<th> | ||
<code>log</code> | ||
</th> | ||
<td> | ||
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>. | ||
</td> | ||
</tr> | ||
<tr> | ||
<th> | ||
<code>baseUrl</code> | ||
@@ -158,10 +140,2 @@ </th> | ||
</tr> | ||
<tr> | ||
<th> | ||
<code>defaultRedirectUri</code> | ||
</th> | ||
<td> | ||
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. | ||
</td> | ||
</tr> | ||
</tbody> | ||
@@ -172,3 +146,3 @@ </table> | ||
`oauthLoginUrl()` returns an object with the following properties | ||
`oauthAuthorizationUrl()` returns an object with the following properties | ||
@@ -213,6 +187,6 @@ <table> | ||
<th> | ||
<code>redirectUri</code> | ||
<code>redirectUrl</code> | ||
</th> | ||
<td> | ||
Returns <code>options.redirectUri</code> if it was set. Defaults to <code>options.defaultRedirectUri</code> if it was set, otherwise <code>null</code>. | ||
Returns <code>options.redirectUrl</code> if it was set. Defaults to <code>null</code>. | ||
</td> | ||
@@ -219,0 +193,0 @@ </tr> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
149
20984
216