feat(setup): prefix endpoint url with current url protocol if missing

This commit is contained in:
kunish 2023-09-10 00:58:21 +08:00
parent 730ab8641b
commit e935aedfcf
No known key found for this signature in database
GPG Key ID: 647A12B4F782C430
7 changed files with 76 additions and 31 deletions

View File

@ -55,6 +55,7 @@
"solid-apexcharts": "^0.3.2", "solid-apexcharts": "^0.3.2",
"solid-infinite-scroll": "^1.0.1", "solid-infinite-scroll": "^1.0.1",
"solid-js": "^1.7.11", "solid-js": "^1.7.11",
"solid-toast": "^0.5.0",
"sort-package-json": "^2.5.1", "sort-package-json": "^2.5.1",
"tailwind-merge": "^1.14.0", "tailwind-merge": "^1.14.0",
"tailwindcss": "^3.3.3", "tailwindcss": "^3.3.3",

25
pnpm-lock.yaml generated
View File

@ -137,6 +137,9 @@ dependencies:
solid-js: solid-js:
specifier: ^1.7.11 specifier: ^1.7.11
version: 1.7.11 version: 1.7.11
solid-toast:
specifier: ^0.5.0
version: 0.5.0(solid-js@1.7.11)
sort-package-json: sort-package-json:
specifier: ^2.5.1 specifier: ^2.5.1
version: 2.5.1 version: 2.5.1
@ -2913,13 +2916,6 @@ packages:
} }
dev: false dev: false
/@types/node@20.5.9:
resolution:
{
integrity: sha512-PcGNd//40kHAS3sTlzKB9C9XL4K0sTup8nbG5lC14kzEteTNuAFh9u5nA0o5TWnSG2r/JNPRXFVcHJIIeRlmqQ==,
}
dev: false
/@types/node@20.6.0: /@types/node@20.6.0:
resolution: resolution:
{ {
@ -2940,7 +2936,7 @@ packages:
integrity: sha512-yy7HuzQhj0dhGpD8RLXSZWEkLsV9ibvxvi6EiJ3bkqLAO1RGo0WbkWQiwpRlSFymTJRz0d3k5LM3kkx8ArDbLw==, integrity: sha512-yy7HuzQhj0dhGpD8RLXSZWEkLsV9ibvxvi6EiJ3bkqLAO1RGo0WbkWQiwpRlSFymTJRz0d3k5LM3kkx8ArDbLw==,
} }
dependencies: dependencies:
'@types/node': 20.5.9 '@types/node': 20.6.0
dev: false dev: false
/@types/semver@7.5.1: /@types/semver@7.5.1:
@ -5580,7 +5576,7 @@ packages:
} }
engines: { node: '>= 10.13.0' } engines: { node: '>= 10.13.0' }
dependencies: dependencies:
'@types/node': 20.5.9 '@types/node': 20.6.0
merge-stream: 2.0.0 merge-stream: 2.0.0
supports-color: 7.2.0 supports-color: 7.2.0
dev: false dev: false
@ -7177,6 +7173,17 @@ packages:
solid-js: 1.7.11 solid-js: 1.7.11
dev: false dev: false
/solid-toast@0.5.0(solid-js@1.7.11):
resolution:
{
integrity: sha512-t770JakjyS2P9b8Qa1zMLOD51KYKWXbTAyJePVUoYex5c5FH5S/HtUBUbZAWFcqRCKmAE8KhyIiCvDZA8bOnxQ==,
}
peerDependencies:
solid-js: ^1.5.4
dependencies:
solid-js: 1.7.11
dev: false
/sort-object-keys@1.1.3: /sort-object-keys@1.1.3:
resolution: resolution:
{ {

View File

@ -1,5 +1,6 @@
import { Navigate, Route, Routes, useNavigate } from '@solidjs/router' import { Navigate, Route, Routes, useNavigate } from '@solidjs/router'
import { Show, createEffect, lazy, onMount } from 'solid-js' import { Show, createEffect, lazy, onMount } from 'solid-js'
import { Toaster } from 'solid-toast'
import { twMerge } from 'tailwind-merge' import { twMerge } from 'tailwind-merge'
import { Header } from '~/components' import { Header } from '~/components'
import { ROUTES } from '~/constants' import { ROUTES } from '~/constants'
@ -71,6 +72,8 @@ export const App = () => {
/> />
</Routes> </Routes>
</div> </div>
<Toaster position="bottom-center" />
</div> </div>
) )
} }

View File

@ -1,5 +1,8 @@
import { createSignal } from 'solid-js' import { createSignal } from 'solid-js'
export const transformEndpointURL = (url: string) =>
/^https?/.test(url) ? url : `${window.location.protocol}//${url}`
export const useStringBooleanMap = () => { export const useStringBooleanMap = () => {
const [map, setMap] = createSignal<Record<string, boolean>>({}) const [map, setMap] = createSignal<Record<string, boolean>>({})
const set = (name: string, value: boolean) => { const set = (name: string, value: boolean) => {

View File

@ -73,4 +73,6 @@ export default {
sequence: 'Sequence', sequence: 'Sequence',
payload: 'Payload', payload: 'Payload',
details: 'Details', details: 'Details',
endpointURL: 'Endpoint URL',
secret: 'Secret',
} }

View File

@ -73,4 +73,6 @@ export default {
sequence: '序列号', sequence: '序列号',
payload: '内容', payload: '内容',
details: '详情', details: '详情',
endpointURL: '后端地址',
secret: '密钥',
} }

View File

@ -5,9 +5,11 @@ import { useNavigate } from '@solidjs/router'
import { IconX } from '@tabler/icons-solidjs' import { IconX } from '@tabler/icons-solidjs'
import ky from 'ky' import ky from 'ky'
import { For, onMount } from 'solid-js' import { For, onMount } from 'solid-js'
import { toast } from 'solid-toast'
import { v4 as uuid } from 'uuid' import { v4 as uuid } from 'uuid'
import { z } from 'zod' import { z } from 'zod'
import { Button } from '~/components' import { Button } from '~/components'
import { transformEndpointURL } from '~/helpers'
import { import {
endpointList, endpointList,
selectedEndpoint, selectedEndpoint,
@ -16,7 +18,7 @@ import {
} from '~/signals' } from '~/signals'
const schema = z.object({ const schema = z.object({
url: z.string().url().nonempty(), url: z.string().nonempty(),
secret: z.string(), secret: z.string(),
}) })
@ -39,7 +41,11 @@ export default () => {
: {}, : {},
}) })
.then(({ ok }) => ok) .then(({ ok }) => ok)
.catch(() => false) .catch((err) => {
const { message } = err as Error
toast.error(message)
})
const onEndpointSelect = async (id: string) => { const onEndpointSelect = async (id: string) => {
const endpoint = endpointList().find((e) => e.id === id) const endpoint = endpointList().find((e) => e.id === id)
@ -56,17 +62,19 @@ export default () => {
} }
const onSubmit = async ({ url, secret }: { url: string; secret: string }) => { const onSubmit = async ({ url, secret }: { url: string; secret: string }) => {
if (!(await checkEndpoint(url, secret))) { const transformedURL = transformEndpointURL(url)
if (!(await checkEndpoint(transformedURL, secret))) {
return return
} }
const id = uuid() const id = uuid()
const list = endpointList().slice() const list = endpointList().slice()
const point = list.find((history) => history.url === url) const point = list.find((history) => history.url === transformedURL)
if (!point) { if (!point) {
// new host and secret // new host and secret
setEndpointList([{ id, url, secret }, ...list]) setEndpointList([{ id, url: transformedURL, secret }, ...list])
onSetupSuccess(id) onSetupSuccess(id)
return return
@ -80,9 +88,16 @@ export default () => {
onSetupSuccess(id) onSetupSuccess(id)
} }
const onError = (err: unknown) => {
const { message } = err as Error
toast.error(message)
}
const { form } = createForm<z.infer<typeof schema>>({ const { form } = createForm<z.infer<typeof schema>>({
extend: validator({ schema }), extend: validator({ schema }),
onSubmit, onSubmit,
onError,
}) })
const onRemove = (id: string) => { const onRemove = (id: string) => {
@ -116,27 +131,39 @@ export default () => {
}) })
return ( return (
<div class="mx-auto flex flex-col items-center gap-4 py-10 sm:w-2/3"> <div class="mx-auto flex max-w-screen-md flex-col items-center gap-4 py-10">
<form class="contents" use:form={form}> <form class="contents" use:form={form}>
<div class="flex w-full flex-col gap-4"> <div class="flex w-full flex-col gap-4">
<input <div class="flex-1">
name="url" <label class="label">
type="url" <span class="label-text">{t('endpointURL')}</span>
class="input input-bordered" </label>
placeholder="host url"
list="defaultEndpoints"
/>
<datalist id="defaultEndpoints"> <input
<option value="http://127.0.0.1:9090" /> name="url"
</datalist> type="url"
class="input input-bordered w-full"
placeholder="http://127.0.0.1:9090"
list="defaultEndpoints"
/>
<input <datalist id="defaultEndpoints">
name="secret" <option value="http://127.0.0.1:9090" />
type="password" </datalist>
class="input input-bordered" </div>
placeholder="secret"
/> <div class="flex-1">
<label class="label">
<span class="label-text">{t('secret')}</span>
</label>
<input
name="secret"
type="password"
class="input input-bordered w-full"
placeholder="secret"
/>
</div>
<Button type="submit" class="btn-primary join-item uppercase"> <Button type="submit" class="btn-primary join-item uppercase">
{t('add')} {t('add')}