Skip to content

Commit

Permalink
refactor: replace ofetch with $fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
kingyue737 committed Jun 18, 2024
1 parent 6ea5685 commit a7df1b5
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 34 deletions.
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/auth0.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'
Expand Down Expand Up @@ -102,7 +101,7 @@ export function auth0EventHandler({ config, onSuccess, onError }: OAuthConfig<OA

// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tokens: any = await ofetch(
const tokens: any = await $fetch(
tokenURL as string,
{
method: 'POST',
Expand Down Expand Up @@ -135,7 +134,7 @@ export function auth0EventHandler({ config, onSuccess, onError }: OAuthConfig<OA

// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch(`https://${config.domain}/userinfo`, {
const user: any = await $fetch(`https://${config.domain}/userinfo`, {
headers: {
Authorization: `${tokenType} ${accessToken}`,
},
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/server/lib/oauth/battledotnet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { randomUUID } from 'node:crypto'
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { ofetch } from 'ofetch'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
Expand Down Expand Up @@ -146,7 +145,7 @@ export function battledotnetEventHandler({ config, onSuccess, onError }: OAuthCo

// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch('https://oauth.battle.net/userinfo', {
const user: any = await $fetch('https://oauth.battle.net/userinfo', {
headers: {
'User-Agent': `Battledotnet-OAuth-${config.clientId}`,
'Authorization': `Bearer ${accessToken}`,
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/cognito.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'
Expand Down Expand Up @@ -76,7 +75,7 @@ export function cognitoEventHandler({ config, onSuccess, onError }: OAuthConfig<

// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tokens: any = await ofetch(
const tokens: any = await $fetch(
tokenURL as string,
{
method: 'POST',
Expand All @@ -103,7 +102,7 @@ export function cognitoEventHandler({ config, onSuccess, onError }: OAuthConfig<
const accessToken = tokens.access_token
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch(`https://${config.userPoolId}.auth.${config.region}.amazoncognito.com/oauth2/userInfo`, {
const user: any = await $fetch(`https://${config.userPoolId}.auth.${config.region}.amazoncognito.com/oauth2/userInfo`, {
headers: {
Authorization: `${tokenType} ${accessToken}`,
},
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/discord.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'
Expand Down Expand Up @@ -100,7 +99,7 @@ export function discordEventHandler({ config, onSuccess, onError }: OAuthConfig<
parsedRedirectUrl.search = ''
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tokens: any = await ofetch(
const tokens: any = await $fetch(
config.tokenURL as string,
{
method: 'POST',
Expand Down Expand Up @@ -132,7 +131,7 @@ export function discordEventHandler({ config, onSuccess, onError }: OAuthConfig<
const accessToken = tokens.access_token
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch('https://discord.com/api/users/@me', {
const user: any = await $fetch('https://discord.com/api/users/@me', {
headers: {
'user-agent': 'Nuxt Auth Utils',
'Authorization': `Bearer ${accessToken}`,
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/server/lib/oauth/facebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getRequestURL,
sendRedirect,
} from 'h3'
import { ofetch } from 'ofetch'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
Expand Down Expand Up @@ -133,7 +132,7 @@ export function facebookEventHandler({
config.fields = config.fields || ['id', 'name']
const fields = config.fields.join(',')

const user = await ofetch(
const user = await $fetch(
`https://graph.facebook.com/v19.0/me?fields=${fields}&access_token=${accessToken}`,
)

Expand Down
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/github.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { ofetch } from 'ofetch'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
Expand Down Expand Up @@ -122,7 +121,7 @@ export function githubEventHandler({ config, onSuccess, onError }: OAuthConfig<O
const accessToken = tokens.access_token
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch('https://api.github.com/user', {
const user: any = await $fetch('https://api.github.com/user', {
headers: {
'User-Agent': `Github-OAuth-${config.clientId}`,
'Authorization': `token ${accessToken}`,
Expand All @@ -133,7 +132,7 @@ export function githubEventHandler({ config, onSuccess, onError }: OAuthConfig<O
if (!user.email && config.emailRequired) {
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const emails: any[] = await ofetch('https://api.github.com/user/emails', {
const emails: any[] = await $fetch('https://api.github.com/user/emails', {
headers: {
'User-Agent': `Github-OAuth-${config.clientId}`,
'Authorization': `token ${accessToken}`,
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
sendRedirect,
} from 'h3'
import { withQuery, parsePath } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'
Expand Down Expand Up @@ -109,7 +108,7 @@ export function googleEventHandler({
}
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tokens: any = await ofetch(config.tokenURL as string, {
const tokens: any = await $fetch(config.tokenURL as string, {
method: 'POST',
body,
}).catch((error) => {
Expand All @@ -130,7 +129,7 @@ export function googleEventHandler({
const accessToken = tokens.access_token
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch(
const user: any = await $fetch(
config.userURL as string,
{
headers: {
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/keycloak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getRequestURL,
sendRedirect,
} from 'h3'
import { ofetch } from 'ofetch'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
Expand Down Expand Up @@ -114,7 +113,7 @@ export function keycloakEventHandler({

// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tokens: any = await ofetch(tokenURL, {
const tokens: any = await $fetch(tokenURL, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Expand Down Expand Up @@ -146,7 +145,7 @@ export function keycloakEventHandler({

// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch(
const user: any = await $fetch(
`${realmURL}/protocol/openid-connect/userinfo`,
{
headers: {
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/linkedin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { H3Event, H3Error } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'

Expand Down Expand Up @@ -96,7 +95,7 @@ export function linkedinEventHandler({ config, onSuccess, onError }: OAuthConfig
parsedRedirectUrl.search = ''
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tokens: any = await ofetch(
const tokens: any = await $fetch(
config.tokenURL as string,
{
method: 'POST',
Expand Down Expand Up @@ -127,7 +126,7 @@ export function linkedinEventHandler({ config, onSuccess, onError }: OAuthConfig
const accessToken = tokens.access_token
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch('https://api.linkedin.com/v2/userinfo', {
const user: any = await $fetch('https://api.linkedin.com/v2/userinfo', {
headers: {
'user-agent': 'Nuxt Auth Utils',
'Authorization': `Bearer ${accessToken}`,
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/microsoft.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { H3Event, H3Error } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'

Expand Down Expand Up @@ -109,7 +108,7 @@ export function microsoftEventHandler({ config, onSuccess, onError }: OAuthConfi

// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tokens: any = await ofetch(
const tokens: any = await $fetch(
tokenURL as string,
{
method: 'POST',
Expand All @@ -136,7 +135,7 @@ export function microsoftEventHandler({ config, onSuccess, onError }: OAuthConfi
const userURL = config.userURL || 'https://graph.microsoft.com/v1.0/me'
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch(userURL, {
const user: any = await $fetch(userURL, {
headers: {
Authorization: `${tokenType} ${accessToken}`,
},
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/spotify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'
Expand Down Expand Up @@ -90,7 +89,7 @@ export function spotifyEventHandler({ config, onSuccess, onError }: OAuthConfig<
const authCode = Buffer.from(`${config.clientId}:${config.clientSecret}`).toString('base64')
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tokens: any = await ofetch(
const tokens: any = await $fetch(
config.tokenURL as string,
{
method: 'POST',
Expand Down Expand Up @@ -120,7 +119,7 @@ export function spotifyEventHandler({ config, onSuccess, onError }: OAuthConfig<
const accessToken = tokens.access_token
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch('https://api.spotify.com/v1/me', {
const user: any = await $fetch('https://api.spotify.com/v1/me', {
headers: {
Authorization: `Bearer ${accessToken}`,
},
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/twitch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'
Expand Down Expand Up @@ -92,7 +91,7 @@ export function twitchEventHandler({ config, onSuccess, onError }: OAuthConfig<O

// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tokens: any = await ofetch(
const tokens: any = await $fetch(
config.tokenURL as string,
{
method: 'POST',
Expand Down Expand Up @@ -123,7 +122,7 @@ export function twitchEventHandler({ config, onSuccess, onError }: OAuthConfig<O
const accessToken = tokens.access_token
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const users: any = await ofetch('https://api.twitch.tv/helix/users', {
const users: any = await $fetch('https://api.twitch.tv/helix/users', {
headers: {
'Client-ID': config.clientId,
'Authorization': `Bearer ${accessToken}`,
Expand Down

0 comments on commit a7df1b5

Please sign in to comment.