Skip to content

refactor: replace ofetch with $fetch #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
@@ -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',
@@ -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}`,
},
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'
@@ -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}`,
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'
@@ -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',
@@ -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}`,
},
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'
@@ -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',
@@ -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}`,
3 changes: 1 addition & 2 deletions src/runtime/server/lib/oauth/facebook.ts
Original file line number Diff line number Diff line change
@@ -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'
@@ -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}`,
)

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'
@@ -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}`,
@@ -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}`,
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/google.ts
Original file line number Diff line number Diff line change
@@ -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'
@@ -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) => {
@@ -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: {
5 changes: 2 additions & 3 deletions src/runtime/server/lib/oauth/keycloak.ts
Original file line number Diff line number Diff line change
@@ -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'
@@ -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',
@@ -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: {
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'

@@ -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',
@@ -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}`,
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'

@@ -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',
@@ -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}`,
},
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'
@@ -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',
@@ -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}`,
},
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'
@@ -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',
@@ -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}`,