it-tools/src/tools/api-tester/api-tester.vue

156 lines
4.8 KiB
Vue
Raw Normal View History

2024-06-16 12:45:14 +02:00
<script setup lang="ts">
import { useQueryParamOrStorage } from '@/composable/queryParams';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
interface KeyValuePair {
key: string
value?: string
}
const baseUrl = useQueryParamOrStorage({ name: 'url', storageName: 'api-tester:url', defaultValue: '' });
const method = useQueryParamOrStorage({ name: 'method', storageName: 'api-tester:m', defaultValue: 'POST' });
const queryParams = useQueryParamOrStorage<KeyValuePair[]>({ name: 'params', storageName: 'api-tester:params', defaultValue: [] });
const headers = useQueryParamOrStorage<KeyValuePair[]>({ name: 'headers', storageName: 'api-tester:headers', defaultValue: [] });
2024-06-16 21:39:55 +02:00
const contentType = useQueryParamOrStorage({ name: 'ct', storageName: 'api-tester:ct', defaultValue: 'application/json' });
2024-06-16 12:45:14 +02:00
const body = useQueryParamOrStorage({ name: 'body', storageName: 'api-tester:body', defaultValue: '' });
2024-06-16 21:39:55 +02:00
const noCORS = ref(false);
2024-06-16 12:45:14 +02:00
const apiCallResult = ref();
const inprogress = ref(false);
async function callAPI() {
const url = new URL(baseUrl.value);
for (const kv of queryParams.value) {
2024-06-16 21:39:55 +02:00
if (!kv.key) {
continue;
}
2024-06-16 12:45:14 +02:00
url.searchParams.append(kv.key, kv.value || '');
}
const queryHeaders = [] as [string, string][];
for (const kv of headers.value) {
2024-06-16 21:39:55 +02:00
if (!kv.key) {
continue;
}
2024-06-16 12:45:14 +02:00
queryHeaders.push([kv.key, kv.value || '']);
}
2024-06-16 21:39:55 +02:00
queryHeaders.push(['Content-Type', contentType.value || '']);
2024-06-16 12:45:14 +02:00
try {
const response = await fetch(url, {
method: method.value,
headers: queryHeaders,
2024-06-16 21:39:55 +02:00
body: (method.value === 'GET' || method.value === 'HEAD') ? null : body.value,
mode: noCORS.value ? 'no-cors' : 'cors',
2024-06-16 12:45:14 +02:00
});
2024-06-16 21:39:55 +02:00
let responseText = await response.text();
try {
responseText = JSON.stringify(JSON.parse(responseText), null, 2);
}
catch (_) {
}
2024-06-16 12:45:14 +02:00
apiCallResult.value = {
code: response.status,
error: '',
2024-06-16 21:39:55 +02:00
result: responseText,
2024-06-16 12:45:14 +02:00
};
}
catch (err: any) {
apiCallResult.value = {
code: -1,
error: err.toString(),
result: null,
};
}
}
2024-06-16 21:39:55 +02:00
function emptyKeyPair() {
return {
key: '',
value: '',
};
}
2024-06-16 12:45:14 +02:00
</script>
<template>
<div>
2024-06-16 21:39:55 +02:00
<c-card title="API Calling">
2024-06-16 12:45:14 +02:00
<c-input-text
v-model:value="baseUrl"
label="Base API Url"
placeholder="Base API Url"
mb-2
/>
<c-select
v-model:value="method"
label="HTTP Method:"
:options="['GET', 'POST', 'PUT', 'DELETE', 'PATCH']"
2024-06-16 21:39:55 +02:00
mb-2
2024-06-16 12:45:14 +02:00
/>
<c-card title="Headers" mb-2>
2024-06-16 21:39:55 +02:00
<n-dynamic-input v-model:value="headers" :on-create="emptyKeyPair">
2024-06-16 12:45:14 +02:00
<template #create-button-default>
Add a new HTTP Header
</template>
<template #default="{ value }">
2024-06-16 21:39:55 +02:00
<div v-if="value" w-100 flex justify-center gap-2>
<c-input-text v-model:value="value.key" placeholder="Header Name" type="text" />
<c-input-text v-model:value="value.value" placeholder="Value" type="text" />
2024-06-16 12:45:14 +02:00
</div>
</template>
</n-dynamic-input>
2024-06-16 21:39:55 +02:00
<c-select
v-model:value="contentType"
label="Content-Type:"
:options="['application/json', 'text/plain']"
mt-2
/>
2024-06-16 12:45:14 +02:00
</c-card>
<c-card title="Query Parameters" mb-2>
2024-06-16 21:39:55 +02:00
<n-dynamic-input v-model:value="queryParams" :on-create="emptyKeyPair">
2024-06-16 12:45:14 +02:00
<template #create-button-default>
2024-06-16 21:39:55 +02:00
Add a new Query Parameter
2024-06-16 12:45:14 +02:00
</template>
<template #default="{ value }">
2024-06-16 21:39:55 +02:00
<div v-if="value" w-100 flex justify-center gap-2>
<c-input-text v-model:value="value.key" placeholder="Param Name" type="text" />
<c-input-text v-model:value="value.value" placeholder="Value" type="text" />
2024-06-16 12:45:14 +02:00
</div>
</template>
</n-dynamic-input>
</c-card>
<c-input-text
2024-06-16 21:39:55 +02:00
v-if="method !== 'GET' && method !== 'HEAD'"
2024-06-16 12:45:14 +02:00
v-model:value="body"
label="Body"
placeholder="HTTP Query body"
multiline
monospace
mb-2
/>
2024-06-16 21:39:55 +02:00
<n-checkbox v-model:checked="noCORS">
No CORS
</n-checkbox>
<div mt-5 flex justify-center>
<c-button secondary @click="callAPI">
Call API
</c-button>
</div>
2024-06-16 12:45:14 +02:00
</c-card>
<n-spin
v-if="inprogress"
size="small"
/>
2024-06-16 21:39:55 +02:00
<c-alert v-if="!inprogress && apiCallResult && apiCallResult.code !== 200" type="error" mt-12 title="Error while calling API">
2024-06-16 12:45:14 +02:00
<p><strong>Status code = {{ apiCallResult.code }}</strong></p>
2024-06-16 21:39:55 +02:00
<TextareaCopyable :value="apiCallResult.error" copy-placement="none" />
2024-06-16 12:45:14 +02:00
</c-alert>
2024-06-16 21:39:55 +02:00
<c-card v-if="!inprogress && apiCallResult && apiCallResult.code === 200" mt-12 title="API Call result">
<TextareaCopyable :value="apiCallResult.result" word-wrap />
2024-06-16 12:45:14 +02:00
</c-card>
</div>
</template>