refactor path handling

This commit is contained in:
Malte Teichert
2024-05-22 00:22:55 +02:00
parent b60931881d
commit 4819f609ae
6 changed files with 241 additions and 132 deletions

View File

@@ -1,40 +1,44 @@
<script lang="ts">
import { addPath } from '$lib';
import { openApiStore, pathRegex, sortPathsAlphabetically } from '$lib';
import { pathTemplate } from '$lib/pathTemplate';
import PathListItem from '../atoms/PathListItem.svelte';
import { getModalStore } from '@skeletonlabs/skeleton';
const modalStore = getModalStore();
// match path with parameters
// add path
const addPath = () => {
// prompt user to enter path
const path = prompt(
'Enter path. Wrap path parameters in curly braces. Example: /users/{userId}'
);
if (!path) return;
// check if path is valid
if (!pathRegex.test(path)) {
alert('Invalid path');
return;
}
// const addPath = () => {
// // prompt user to enter path
// const path = prompt(
// 'Enter path. Wrap path parameters in curly braces. Example: /users/{userId}'
// );
// if (!path) return;
// // check if path is valid
// if (!pathRegex.test(path)) {
// alert('Invalid path');
// return;
// }
// check if path already exists
// @ts-expect-error - we are working with an initially empty object
if ($openApiStore.paths[path]) {
alert('Path already exists');
return;
}
// // check if path already exists
// // @ts-expect-error - we are working with an initially empty object
// if ($openApiStore.paths[path]) {
// alert('Path already exists');
// return;
// }
// create a temporary object to store paths
// add path to paths object
$openApiStore.paths = {
...$openApiStore.paths,
[path]: pathTemplate
};
// // create a temporary object to store paths
// // add path to paths object
// $openApiStore.paths = {
// ...$openApiStore.paths,
// [path]: pathTemplate
// };
// sort paths alphabetically
sortPathsAlphabetically();
};
// // sort paths alphabetically
// sortPathsAlphabetically();
// };
</script>
<div
@@ -44,7 +48,13 @@
<PathListItem {pathName} id={index} />
{/each}
<span class="w-full flex justify-center">
<button type="button" class="btn variant-filled-primary" on:click={addPath}>Add Path</button>
<button
type="button"
class="btn variant-filled-primary"
on:click={() => {
addPath(modalStore);
}}>Add Path</button
>
</span>
<span class="w-full flex justify-center">
<button