Skip to content

Commit d2b3c23

Browse files
authored
chore: remove simple-get from examples (#304)
1 parent 820f1d7 commit d2b3c23

File tree

8 files changed

+86
-109
lines changed

8 files changed

+86
-109
lines changed

examples/discovery.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const fastify = require('fastify')({ logger: { level: 'trace' } })
4-
const sget = require('simple-get')
54

65
const cookieOpts = {
76
// domain: 'localhost',
@@ -62,26 +61,25 @@ fastify.register(oauthPlugin, {
6261
fastify.get('/interaction/callback/google', function (request, reply) {
6362
// Note that in this example a "reply" is also passed, it's so that code verifier cookie can be cleaned before
6463
// token is requested from token endpoint
65-
this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply, (err, result) => {
64+
this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply, async (err, result) => {
6665
if (err) {
6766
reply.send(err)
6867
return
6968
}
7069

71-
sget.concat({
72-
url: 'https://www.googleapis.com/oauth2/v2/userinfo',
73-
method: 'GET',
70+
const fetchResult = await fetch('https://www.googleapis.com/oauth2/v2/userinfo', {
7471
headers: {
7572
Authorization: 'Bearer ' + result.token.access_token
76-
},
77-
json: true
78-
}, function (err, _res, data) {
79-
if (err) {
80-
reply.send(err)
81-
return
8273
}
83-
reply.send(data)
8474
})
75+
76+
if (!result.ok) {
77+
reply.send(new Error('Failed to fetch user info'))
78+
return
79+
}
80+
81+
const data = await fetchResult.json()
82+
reply.send(data)
8583
})
8684
})
8785

examples/facebook.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const fastify = require('fastify')({ logger: { level: 'trace' } })
4-
const sget = require('simple-get')
54

65
// const oauthPlugin = require('fastify-oauth2')
76
const oauthPlugin = require('..')
@@ -20,26 +19,25 @@ fastify.register(oauthPlugin, {
2019
})
2120

2221
fastify.get('/login/facebook/callback', function (request, reply) {
23-
this.facebookOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, (err, result) => {
22+
this.facebookOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, async (err, result) => {
2423
if (err) {
2524
reply.send(err)
2625
return
2726
}
2827

29-
sget.concat({
30-
url: 'https://graph.facebook.com/v6.0/me',
31-
method: 'GET',
28+
const fetchResult = await fetch('https://graph.facebook.com/v6.0/me', {
3229
headers: {
33-
Authorization: 'Bearer ' + result.access_token
34-
},
35-
json: true
36-
}, function (err, _res, data) {
37-
if (err) {
38-
reply.send(err)
39-
return
30+
Authorization: 'Bearer ' + result.token.access_token
4031
}
41-
reply.send(data)
4232
})
33+
34+
if (!fetchResult.ok) {
35+
reply.send(new Error('Failed to fetch user info'))
36+
return
37+
}
38+
39+
const data = await fetchResult.json()
40+
reply.send(data)
4341
})
4442
})
4543

examples/github.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const fastify = require('fastify')({ logger: { level: 'trace' } })
4-
const sget = require('simple-get')
54

65
// const oauthPlugin = require('fastify-oauth2')
76
const oauthPlugin = require('..')
@@ -62,31 +61,28 @@ fastify.get('/login/github/refreshAccessToken', async function (request, reply)
6261
})
6362

6463
// Check access token: https://docs.github.com/en/rest/apps/oauth-applications#check-a-token
65-
fastify.get('/login/github/verifyAccessToken', function (request, reply) {
64+
fastify.get('/login/github/verifyAccessToken', async function (request, reply) {
6665
const { accessToken } = request.query
6766

68-
sget.concat(
69-
{
70-
url: 'https://api.github.com/applications/<CLIENT_ID>/token',
71-
method: 'POST',
72-
headers: {
73-
Authorization:
74-
'Basic ' +
75-
Buffer.from('<CLIENT_ID>' + ':' + '<CLIENT_SECRET').toString(
76-
'base64'
77-
)
78-
},
79-
body: JSON.stringify({ access_token: accessToken }),
80-
json: true
67+
const result = await fetch('https://api.github.com/applications/<CLIENT_ID>/token', {
68+
method: 'POST',
69+
headers: {
70+
Authorization:
71+
'Basic ' +
72+
Buffer.from('<CLIENT_ID>' + ':' + '<CLIENT_SECRET').toString(
73+
'base64'
74+
)
8175
},
82-
function (err, _res, data) {
83-
if (err) {
84-
reply.send(err)
85-
return
86-
}
87-
reply.send(data)
88-
}
89-
)
76+
body: JSON.stringify({ access_token: accessToken }),
77+
})
78+
79+
if (!result.ok) {
80+
reply.send(new Error('Failed to verify access token'))
81+
return
82+
}
83+
84+
const data = await result.json()
85+
reply.send(data)
9086
})
9187

9288
fastify.listen({ port: 3000 })

examples/google-with-pkce.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const fastify = require('fastify')({ logger: { level: 'trace' } })
4-
const sget = require('simple-get')
54

65
const cookieOpts = {
76
// domain: 'localhost',
@@ -57,26 +56,25 @@ fastify.register(oauthPlugin, {
5756
fastify.get('/interaction/callback/google', function (request, reply) {
5857
// Note that in this example a "reply" is also passed, it's so that code verifier cookie can be cleaned before
5958
// token is requested from token endpoint
60-
this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply, (err, result) => {
59+
this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply, async (err, result) => {
6160
if (err) {
6261
reply.send(err)
6362
return
6463
}
6564

66-
sget.concat({
67-
url: 'https://www.googleapis.com/oauth2/v2/userinfo',
68-
method: 'GET',
65+
const fetchResult = await fetch('https://www.googleapis.com/oauth2/v2/userinfo', {
6966
headers: {
7067
Authorization: 'Bearer ' + result.token.access_token
71-
},
72-
json: true
73-
}, function (err, _res, data) {
74-
if (err) {
75-
reply.send(err)
76-
return
7768
}
78-
reply.send(data)
7969
})
70+
71+
if (!fetchResult.ok) {
72+
reply.send(new Error('Failed to fetch user info'))
73+
return
74+
}
75+
76+
const data = await fetchResult.json()
77+
reply.send(data)
8078
})
8179
})
8280

examples/google.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const fastify = require('fastify')({ logger: { level: 'trace' } })
4-
const sget = require('simple-get')
54

65
// const oauthPlugin = require('fastify-oauth2')
76
const oauthPlugin = require('..')
@@ -21,26 +20,25 @@ fastify.register(oauthPlugin, {
2120
})
2221

2322
fastify.get('/login/google/callback', function (request, reply) {
24-
this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, (err, result) => {
23+
this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, async (err, result) => {
2524
if (err) {
2625
reply.send(err)
2726
return
2827
}
2928

30-
sget.concat({
31-
url: 'https://www.googleapis.com/oauth2/v2/userinfo',
32-
method: 'GET',
29+
const fetchResult = await fetch('https://www.googleapis.com/oauth2/v2/userinfo', {
3330
headers: {
3431
Authorization: 'Bearer ' + result.token.access_token
35-
},
36-
json: true
37-
}, function (err, _res, data) {
38-
if (err) {
39-
reply.send(err)
40-
return
4132
}
42-
reply.send(data)
4333
})
34+
35+
if (!fetchResult.ok) {
36+
reply.send(new Error('Failed to fetch user info'))
37+
return
38+
}
39+
40+
const data = await fetchResult.json()
41+
reply.send(data)
4442
})
4543
})
4644

examples/linkedin.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const fastify = require('fastify')({ logger: { level: 'trace' } })
4-
const sget = require('simple-get')
54

65
// const oauthPlugin = require('fastify-oauth2')
76
const oauthPlugin = require('..')
@@ -27,29 +26,25 @@ fastify.register(oauthPlugin, {
2726
fastify.get('/login/linkedin/callback', function (request, reply) {
2827
this.linkedinOAuth2.getAccessTokenFromAuthorizationCodeFlow(
2928
request,
30-
(err, result) => {
29+
async (err, result) => {
3130
if (err) {
3231
reply.send(err)
3332
return
3433
}
3534

36-
sget.concat(
37-
{
38-
url: 'https://api.linkedin.com/v2/userinfo',
39-
method: 'GET',
40-
headers: {
41-
Authorization: 'Bearer ' + result.token.access_token
42-
},
43-
json: true
44-
},
45-
function (err, _res, data) {
46-
if (err) {
47-
reply.send(err)
48-
return
49-
}
50-
reply.send(data)
35+
const fetchResult = await fetch('https://api.linkedin.com/v2/userinfo', {
36+
headers: {
37+
Authorization: 'Bearer ' + result.token.access_token
5138
}
52-
)
39+
})
40+
41+
if (!fetchResult.ok) {
42+
reply.send(new Error('Failed to fetch user info'))
43+
return
44+
}
45+
46+
const data = await fetchResult.json()
47+
reply.send(data)
5348
}
5449
)
5550
})

examples/vatsim-dev.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const fastify = require('fastify')({ logger: { level: 'trace' } })
4-
const sget = require('simple-get')
54

65
// const oauthPlugin = require('fastify-oauth2')
76
const oauthPlugin = require('..')
@@ -23,29 +22,25 @@ fastify.register(oauthPlugin, {
2322
fastify.get('/login/vatsim/callback', function (request, reply) {
2423
this.vatsimoauthdev.getAccessTokenFromAuthorizationCodeFlow(
2524
request,
26-
(err, result) => {
25+
async (err, result) => {
2726
if (err) {
2827
reply.send(err)
2928
return
3029
}
3130

32-
sget.concat(
33-
{
34-
url: 'https://auth-dev.vatsim.net/api/user',
35-
method: 'GET',
36-
headers: {
37-
Authorization: 'Bearer ' + result.access_token
38-
},
39-
json: true
40-
},
41-
function (err, _res, data) {
42-
if (err) {
43-
reply.send(err)
44-
return
45-
}
46-
reply.send(data)
31+
const fetchResult = await fetch('https://auth-dev.vatsim.net/api/user', {
32+
headers: {
33+
Authorization: 'Bearer ' + result.token.access_token
4734
}
48-
)
35+
})
36+
37+
if (!fetchResult.ok) {
38+
reply.send(new Error('Failed to fetch user info'))
39+
return
40+
}
41+
42+
const data = await fetchResult.json()
43+
reply.send(data)
4944
}
5045
)
5146
})

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
"fastify": "^5.0.0",
6767
"neostandard": "^0.12.0",
6868
"nock": "^13.5.4",
69-
"simple-get": "^4.0.1",
7069
"tsd": "^0.32.0"
7170
},
7271
"dependencies": {

0 commit comments

Comments
 (0)