fix: eslint errors
This commit is contained in:
+69
-56
@@ -1,9 +1,13 @@
|
||||
/* eslint no-shadow: ["error", { "allow": ["options"] }] */
|
||||
import { ApolloClient } from 'apollo-client'
|
||||
import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'
|
||||
import {
|
||||
InMemoryCache,
|
||||
IntrospectionFragmentMatcher
|
||||
} from 'apollo-cache-inmemory'
|
||||
import { createHttpLink } from 'apollo-link-http'
|
||||
import { setContext } from 'apollo-link-context'
|
||||
import { useAuth } from './auth'
|
||||
import { computed, reactive, toRefs } from '@vue/composition-api'
|
||||
import useAuth from './auth'
|
||||
import introspectionQueryResultData from '../fragmentTypes.json'
|
||||
|
||||
const fragmentMatcher = new IntrospectionFragmentMatcher({
|
||||
@@ -20,7 +24,7 @@ const httpLink = createHttpLink({
|
||||
uri: apiUrl
|
||||
})
|
||||
|
||||
const getToken = async (options) => {
|
||||
const getToken = async options => {
|
||||
const { getTokenSilently } = useAuth()
|
||||
return getTokenSilently.value(options)
|
||||
}
|
||||
@@ -47,60 +51,72 @@ instance = new ApolloClient({
|
||||
|
||||
const merge = (target, ...source) => {
|
||||
const sources = source instanceof Array ? source : [source]
|
||||
return sources.reduce((acc, source) => {
|
||||
for (const key of Object.keys(source)) {
|
||||
if (source[key] instanceof Object) Object.assign(source[key], merge(acc[key], source[key]))
|
||||
}
|
||||
return Object.assign(acc, source)
|
||||
return sources.reduce((acc, src) => {
|
||||
Object.keys(src).forEach(key => {
|
||||
if (src[key] instanceof Object)
|
||||
Object.assign(src[key], merge(acc[key], src[key]))
|
||||
})
|
||||
return Object.assign(acc, src)
|
||||
}, target || {})
|
||||
}
|
||||
|
||||
export const useMutation = (mutation, options) => {
|
||||
const opts = options
|
||||
const doMutate = options => new Promise((resolve, reject) => {
|
||||
out.loading = true
|
||||
instance.mutate({
|
||||
mutation,
|
||||
...opts,
|
||||
...options
|
||||
})
|
||||
.then(result => {
|
||||
out.loading = false
|
||||
out.data = result.data
|
||||
resolve(result)
|
||||
})
|
||||
.catch(e => {
|
||||
out.loading = false
|
||||
out.error = e
|
||||
reject(e)
|
||||
})
|
||||
})
|
||||
const out = reactive({
|
||||
data: {},
|
||||
error: null,
|
||||
loading: false
|
||||
})
|
||||
const doMutate = options =>
|
||||
new Promise((resolve, reject) => {
|
||||
out.loading = true
|
||||
instance
|
||||
.mutate({
|
||||
mutation,
|
||||
...opts,
|
||||
...options
|
||||
})
|
||||
.then(result => {
|
||||
out.loading = false
|
||||
out.data = result.data
|
||||
resolve(result)
|
||||
})
|
||||
.catch(e => {
|
||||
out.loading = false
|
||||
out.error = e
|
||||
reject(e)
|
||||
})
|
||||
})
|
||||
return [doMutate, toRefs(out)]
|
||||
}
|
||||
|
||||
export const useLazyQuery = (query, options) => {
|
||||
const opts = options
|
||||
let watchedQuery = null
|
||||
const doQuery = options => new Promise((resolve, reject) => {
|
||||
out.loading = true
|
||||
const effectiveOptions = merge({ query }, opts || {}, options || {})
|
||||
watchedQuery = instance.watchQuery(effectiveOptions)
|
||||
watchedQuery.subscribe(({ loading, data }) => {
|
||||
out.loading = loading
|
||||
out.data = data || {}
|
||||
out.error = null
|
||||
resolve(data)
|
||||
}, error => {
|
||||
out.loading = false
|
||||
out.error = error
|
||||
reject(error)
|
||||
})
|
||||
const out = reactive({
|
||||
data: {},
|
||||
error: null,
|
||||
loading: false
|
||||
})
|
||||
const doQuery = options =>
|
||||
new Promise((resolve, reject) => {
|
||||
out.loading = true
|
||||
const effectiveOptions = merge({ query }, opts || {}, options || {})
|
||||
watchedQuery = instance.watchQuery(effectiveOptions)
|
||||
watchedQuery.subscribe(
|
||||
({ loading, data }) => {
|
||||
out.loading = loading
|
||||
out.data = data || {}
|
||||
out.error = null
|
||||
resolve(data)
|
||||
},
|
||||
error => {
|
||||
out.loading = false
|
||||
out.error = error
|
||||
reject(error)
|
||||
}
|
||||
)
|
||||
})
|
||||
const refetch = variables => {
|
||||
doQuery({
|
||||
variables: {
|
||||
@@ -114,17 +130,15 @@ export const useLazyQuery = (query, options) => {
|
||||
watchedQuery.stopPolling()
|
||||
}
|
||||
}
|
||||
const out = reactive({
|
||||
data: {},
|
||||
error: null,
|
||||
loading: false
|
||||
})
|
||||
return [doQuery, {
|
||||
...toRefs(out),
|
||||
refetch,
|
||||
startPolling,
|
||||
stopPolling
|
||||
}]
|
||||
return [
|
||||
doQuery,
|
||||
{
|
||||
...toRefs(out),
|
||||
refetch,
|
||||
startPolling,
|
||||
stopPolling
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export const useQuery = (query, options) => {
|
||||
@@ -135,24 +149,23 @@ export const useQuery = (query, options) => {
|
||||
|
||||
export const useResult = (result, defaultValue, pick) => {
|
||||
return computed(() => {
|
||||
const value = result.value
|
||||
const { value } = result
|
||||
if (value) {
|
||||
if (pick) {
|
||||
try {
|
||||
return pick(value)
|
||||
} catch (e) {
|
||||
// Silent error
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
} else {
|
||||
const keys = Object.keys(value)
|
||||
if (keys.length === 1) {
|
||||
// Automatically take the only key in result data
|
||||
return value[keys[0]]
|
||||
} else {
|
||||
// Return entire result data
|
||||
return value
|
||||
}
|
||||
// Return entire result data
|
||||
return value
|
||||
}
|
||||
} else {
|
||||
return defaultValue
|
||||
|
||||
+47
-41
@@ -7,10 +7,10 @@ const DEFAULT_REDIRECT_CALLBACK = () =>
|
||||
|
||||
let instance
|
||||
|
||||
const params = (new URL(window.location)).searchParams
|
||||
const params = new URL(window.location).searchParams
|
||||
const domain = params.get('domain') || 'unbound.eu.auth0.com'
|
||||
|
||||
export const useAuth = (onRedirectCallback = DEFAULT_REDIRECT_CALLBACK) => {
|
||||
export default (onRedirectCallback = DEFAULT_REDIRECT_CALLBACK) => {
|
||||
if (instance) {
|
||||
return toRefs(instance)
|
||||
}
|
||||
@@ -42,15 +42,21 @@ export const useAuth = (onRedirectCallback = DEFAULT_REDIRECT_CALLBACK) => {
|
||||
instance.popupOpen = false
|
||||
}
|
||||
|
||||
instance.user = await instance.auth0Client.then(client => client.getUser())
|
||||
instance.user = await instance.auth0Client.then(client =>
|
||||
client.getUser()
|
||||
)
|
||||
instance.isAuthenticated = true
|
||||
},
|
||||
/** Handles the callback when logging in using a redirect */
|
||||
handleRedirectCallback: async () => {
|
||||
instance.loading = true
|
||||
try {
|
||||
await instance.auth0Client.then(client => client.handleRedirectCallback())
|
||||
instance.user = await instance.auth0Client.then(client => client.getUser())
|
||||
await instance.auth0Client.then(client =>
|
||||
client.handleRedirectCallback()
|
||||
)
|
||||
instance.user = await instance.auth0Client.then(client =>
|
||||
client.getUser()
|
||||
)
|
||||
instance.isAuthenticated = true
|
||||
} catch (e) {
|
||||
instance.error = e
|
||||
@@ -87,46 +93,46 @@ export const useAuth = (onRedirectCallback = DEFAULT_REDIRECT_CALLBACK) => {
|
||||
.then(client => client.isAuthenticated())
|
||||
.then(a => {
|
||||
instance.isAuthenticated = a
|
||||
instance.auth0Client
|
||||
.then(client => client.getUser()
|
||||
.then(u => {
|
||||
instance.user = u
|
||||
instance.loading = false
|
||||
}))
|
||||
instance.auth0Client.then(client =>
|
||||
client.getUser().then(u => {
|
||||
instance.user = u
|
||||
instance.loading = false
|
||||
})
|
||||
)
|
||||
})
|
||||
}
|
||||
// Create a new instance of the SDK client using members of the given options object
|
||||
// Create a new instance of the SDK client using members of the given options object
|
||||
instance.auth0Client = createAuth0Client(options)
|
||||
instance.auth0Client
|
||||
.then(client => {
|
||||
instance.loading = true
|
||||
// instance.auth0Client = client
|
||||
try {
|
||||
// If the user is returning to the app after authentication..
|
||||
if (
|
||||
window.location.search.includes('code=') &&
|
||||
window.location.search.includes('state=')
|
||||
) {
|
||||
console.log('location search', window.location.search)
|
||||
// handle the redirect and retrieve tokens
|
||||
client.handleRedirectCallback()
|
||||
.then(appState => {
|
||||
// Notify subscribers that the redirect callback has happened, passing the appState
|
||||
// (useful for retrieving any pre-authentication state)
|
||||
onRedirectCallback(appState)
|
||||
// Initialize our internal authentication state
|
||||
fetchUser()
|
||||
})
|
||||
.catch(e => console.error('error handling redirect callback', e))
|
||||
} else {
|
||||
fetchUser()
|
||||
}
|
||||
} catch (e) {
|
||||
instance.error = e
|
||||
} finally {
|
||||
instance.loading = false
|
||||
instance.auth0Client.then(client => {
|
||||
instance.loading = true
|
||||
// instance.auth0Client = client
|
||||
try {
|
||||
// If the user is returning to the app after authentication..
|
||||
if (
|
||||
window.location.search.includes('code=') &&
|
||||
window.location.search.includes('state=')
|
||||
) {
|
||||
console.log('location search', window.location.search)
|
||||
// handle the redirect and retrieve tokens
|
||||
client
|
||||
.handleRedirectCallback()
|
||||
.then(appState => {
|
||||
// Notify subscribers that the redirect callback has happened, passing the appState
|
||||
// (useful for retrieving any pre-authentication state)
|
||||
onRedirectCallback(appState)
|
||||
// Initialize our internal authentication state
|
||||
fetchUser()
|
||||
})
|
||||
.catch(e => console.error('error handling redirect callback', e))
|
||||
} else {
|
||||
fetchUser()
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
instance.error = e
|
||||
} finally {
|
||||
instance.loading = false
|
||||
}
|
||||
})
|
||||
|
||||
return toRefs(instance)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user