Skip to content

Commit 15a8d03

Browse files
wolfy1339gr2m
authored andcommitted
fix: include all parameters in url (#10)
Make a URL builder to include all parameters in the final returned URL when the function gets called
1 parent bed2972 commit 15a8d03

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/index.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,38 @@ export function oauthLoginUrl (options: Options): Result {
2828
? options.scopes.split(/[,\s]+/).filter(Boolean)
2929
: Array.isArray(options.scopes) ? options.scopes : []
3030

31-
return {
31+
const result: Result = {
3232
allowSignup: options.allowSignup === false ? false : true,
3333
clientId: options.clientId,
3434
login: options.login || null,
3535
redirectUrl: options.redirectUrl || null,
3636
scopes: scopesNormalized,
3737
state: options.state || Math.random().toString(36).substr(2),
38-
url: `${BASE_URL}?client_id=${options.clientId}`
38+
url: ''
3939
}
40+
result.url = urlBuilderAuthorize(BASE_URL, result)
41+
42+
return result
43+
}
44+
type ResultKeys = Exclude<keyof Result, 'url'>
45+
function urlBuilderAuthorize (base: string, options: Result) {
46+
const map = {
47+
allowSignup: 'allow_signup',
48+
clientId: 'client_id',
49+
login: 'login',
50+
redirectUrl: 'redirect_url',
51+
scopes: 'scopes',
52+
state: 'state',
53+
}
54+
55+
let url = base
56+
57+
Object.entries(options).filter(([k, v]) => v !== null && k !== 'url') // Filter out keys that are null and remove the url key
58+
.filter(([, v]) => Array.isArray(v) ? v.length !== 0 : true) // Filter out empty Array
59+
.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
60+
.forEach(([key, value], index) => { // Finally, build the URL
61+
url += index === 0 ? `?` : '&'
62+
url += `${key}=${value}`
63+
})
64+
return url
4065
}

test/index.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test('oauthLoginUrl({clientId: "1234567890abcdef1234"})', () => {
1818
redirectUrl: null,
1919
scopes: [],
2020
state: '4feornbt361',
21-
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
21+
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361'
2222
})
2323
})
2424

@@ -32,7 +32,7 @@ test('oauthLoginUrl({clientId: "4321fedcba0987654321"})', () => {
3232
redirectUrl: null,
3333
scopes: [],
3434
state: '4feornbt361',
35-
url: 'https://github.com/login/oauth/authorize?client_id=4321fedcba0987654321'
35+
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=4321fedcba0987654321&state=4feornbt361'
3636
})
3737
})
3838

@@ -47,7 +47,7 @@ test('redirectUrl option', () => {
4747
redirectUrl: 'https://example.com',
4848
scopes: [],
4949
state: '4feornbt361',
50-
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
50+
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&redirect_url=https://example.com&state=4feornbt361'
5151
})
5252
})
5353

@@ -62,7 +62,7 @@ test('login option', () => {
6262
redirectUrl: null,
6363
scopes: [],
6464
state: '4feornbt361',
65-
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
65+
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361'
6666
})
6767
})
6868

@@ -78,7 +78,7 @@ test('scopes = []', () => {
7878
redirectUrl: null,
7979
scopes: [],
8080
state: '4feornbt361',
81-
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
81+
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361'
8282
})
8383
})
8484

@@ -94,7 +94,7 @@ test('scopes = ""', () => {
9494
redirectUrl: null,
9595
scopes: [],
9696
state: '4feornbt361',
97-
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
97+
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361'
9898
})
9999
})
100100

@@ -110,7 +110,7 @@ test('scopes = "user,public_repo, gist notifications"', () => {
110110
redirectUrl: null,
111111
scopes: ['user', 'public_repo', 'gist', 'notifications'],
112112
state: '4feornbt361',
113-
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
113+
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&scopes=user,public_repo,gist,notifications&state=4feornbt361'
114114
})
115115
})
116116

@@ -127,7 +127,7 @@ test('allowSignup = false', () => {
127127
redirectUrl: null,
128128
scopes: ['user', 'public_repo', 'gist', 'notifications'],
129129
state: '4feornbt361',
130-
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
130+
url: 'https://github.com/login/oauth/authorize?allow_signup=false&client_id=1234567890abcdef1234&login=octocat&scopes=user,public_repo,gist,notifications&state=4feornbt361'
131131
})
132132
})
133133

@@ -143,6 +143,6 @@ test('state = Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI', () => {
143143
redirectUrl: null,
144144
scopes: [],
145145
state: 'Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI',
146-
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
146+
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI'
147147
})
148148
})

0 commit comments

Comments
 (0)