diff options
author | vnugent <public@vaughnnugent.com> | 2024-03-11 21:21:18 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-03-11 21:21:18 -0400 |
commit | 748cdbf4880d830fd794e92856e8c35a46e4f884 (patch) | |
tree | 27d539f1f8d65d07f25c2e63947c5358ab48d9c3 /front-end/src/store/websiteLookup.ts | |
parent | 3883de080e263d2f076f65b4600a5021d3d64a21 (diff) |
feat(app): #1 update libs & add curl support
Diffstat (limited to 'front-end/src/store/websiteLookup.ts')
-rw-r--r-- | front-end/src/store/websiteLookup.ts | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/front-end/src/store/websiteLookup.ts b/front-end/src/store/websiteLookup.ts new file mode 100644 index 0000000..7d4f3ca --- /dev/null +++ b/front-end/src/store/websiteLookup.ts @@ -0,0 +1,74 @@ + +import 'pinia' +import { MaybeRef, Ref, shallowRef, watch } from 'vue'; +import { WebMessage, apiCall, useAxios } from '@vnuge/vnlib.browser' +import { get, set } from '@vueuse/core'; +import { PiniaPluginContext, PiniaPlugin, storeToRefs } from 'pinia' +import { defer, noop } from 'lodash-es'; + +export interface WebsiteLookupResult { + title: string | undefined, + description: string | undefined, + keywords: string[] | undefined, +} + +export interface LookupApi{ + isSupported: Ref<boolean>, + timeout: Ref<number>, + execLookup(url:string): Promise<WebsiteLookupResult> +} + +declare module 'pinia' { + export interface PiniaCustomProperties { + websiteLookup:{ + isSupported: boolean, + execLookup(url: string): Promise<WebsiteLookupResult> + } + } +} + +const urlToBase64UrlEncoded = (url: string) => { + return btoa(url) + .replace(/-/g, '+') + .replace(/_/g, '/') + .replace(/\./g, '=') //Fix padding +} + +export const siteLookupPlugin = (lookupEndpoint: MaybeRef<string>, to: number): PiniaPlugin => { + + return ({ store }: PiniaPluginContext) => { + + const { loggedIn } = storeToRefs(store) + const axios = useAxios(null) + + const isSupported = shallowRef(false) + const timeout = shallowRef(to) + + const checkIsSupported = () => { + return apiCall(async () => { + //Execute test with the 'support' query parameter + const { data } = await axios.get<WebMessage>(`${get(lookupEndpoint)}?support`) + set(isSupported, data.success) + }); + } + + const execLookup = async (url:string) => { + const base64Url = urlToBase64UrlEncoded(url) + + //Execute test with the 'support' query parameter + const { data } = await axios.get<WebMessage<WebsiteLookupResult>>(`${get(lookupEndpoint)}?timeout=${get(timeout)}&url=${base64Url}`) + return data.getResultOrThrow(); + } + + //If login status changes, recheck support + watch([loggedIn], ([li]) => li ? defer(checkIsSupported) : noop(), { immediate: true }) + + return { + websiteLookup: { + isSupported, + execLookup, + timeout + } as LookupApi + } as any + } +}
\ No newline at end of file |