it-tools/src/tools/url-parser/url-parser.vue

79 lines
2.2 KiB
Vue
Raw Normal View History

2022-04-19 00:12:44 +02:00
<template>
<n-card>
2022-04-22 23:31:40 +02:00
<n-form-item label="Your url to parse:" :feedback="validation.message" :validation-status="validation.status">
<n-input v-model:value="urlToParse" placeholder="Your url to parse..." />
2022-04-19 00:12:44 +02:00
</n-form-item>
2022-04-22 23:31:40 +02:00
<n-divider style="margin-top: 0" />
2022-04-19 00:12:44 +02:00
<n-form>
2022-04-22 23:31:40 +02:00
<n-input-group v-for="{ title, key } in properties" :key="key">
<n-input-group-label style="flex: 0 0 120px"> {{ title }}: </n-input-group-label>
<input-copyable :value="(urlParsed?.[key] as string) ?? ''" readonly placeholder=" " />
2022-04-19 00:12:44 +02:00
</n-input-group>
<n-input-group
v-for="[k, v] in Object.entries(Object.fromEntries(urlParsed?.searchParams.entries() ?? []))"
:key="k"
2022-04-22 23:31:40 +02:00
>
<n-input-group-label style="flex: 0 0 120px">
2022-04-19 00:12:44 +02:00
<n-icon :component="SubdirectoryArrowRightRound" />
</n-input-group-label>
2022-04-22 23:31:40 +02:00
<input-copyable :value="k" readonly />
<input-copyable :value="v" readonly />
2022-04-19 00:12:44 +02:00
</n-input-group>
</n-form>
2022-04-22 23:31:40 +02:00
</n-card>
2022-04-19 00:12:44 +02:00
</template>
<script setup lang="ts">
import { computed, ref } from 'vue';
import { SubdirectoryArrowRightRound } from '@vicons/material';
import { useValidation } from '@/composable/validation';
2022-08-04 22:46:50 +02:00
import InputCopyable from '../../components/InputCopyable.vue';
2022-04-19 00:12:44 +02:00
2022-04-22 23:31:40 +02:00
const urlToParse = ref('https://me:pwd@it-tools.tech:3000/url-parser?key1=value&key2=value2#the-hash');
2022-04-19 00:12:44 +02:00
const urlParsed = computed<URL | undefined>(() => {
try {
2022-04-22 23:31:40 +02:00
return new URL(urlToParse.value);
2022-04-19 00:12:44 +02:00
} catch (_) {
2022-04-22 23:31:40 +02:00
return undefined;
2022-04-19 00:12:44 +02:00
}
2022-04-22 23:31:40 +02:00
});
const validation = useValidation({
source: urlToParse,
rules: [
{
validator: (value) => {
try {
new URL(value);
return true;
} catch (_) {
return false;
}
},
message: 'Invalid url',
},
],
});
2022-04-19 00:12:44 +02:00
2022-04-22 23:31:40 +02:00
const properties: { title: string; key: keyof URL }[] = [
{ title: 'Protocol', key: 'protocol' },
{ title: 'Username', key: 'username' },
{ title: 'Password', key: 'password' },
{ title: 'Hostname', key: 'hostname' },
{ title: 'Port', key: 'port' },
{ title: 'Path', key: 'pathname' },
{ title: 'Params', key: 'search' },
];
2022-04-19 00:12:44 +02:00
</script>
<style lang="less" scoped>
.n-input-group-label {
text-align: right;
}
.n-input-group {
margin: 2px 0;
2022-04-19 00:12:44 +02:00
}
2022-04-22 23:31:40 +02:00
</style>