Skip to content

Commit

Permalink
Merge pull request #114 from sunnydanu/feat(new-tool)--api-tester
Browse files Browse the repository at this point in the history
feat(new-tool):-api-tester
  • Loading branch information
sunnydanu authored Nov 3, 2024
2 parents 16c2d6d + b1027d8 commit c716e0f
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 3 deletions.
1 change: 1 addition & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare module '@vue/runtime-core' {
'404.page': typeof import('./src/pages/404.page.vue')['default']
About: typeof import('./src/pages/About.vue')['default']
AiPromptSplitter: typeof import('./src/tools/ai-prompt-splitter/ai-prompt-splitter.vue')['default']
ApiTester: typeof import('./src/tools/api-tester/api-tester.vue')['default']
App: typeof import('./src/App.vue')['default']
Argon2HashGenerator: typeof import('./src/tools/argon2-hash-generator/argon2-hash-generator.vue')['default']
AsciiTextDrawer: typeof import('./src/tools/ascii-text-drawer/ascii-text-drawer.vue')['default']
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@
"highlight.js": "^11.7.0",
"iarna-toml-esm": "^3.0.5",
"ibantools": "^4.3.3",
"jpeg-quality-estimator": "^1.0.1",
"js-base64": "^3.7.7",
"json-editor-vue": "^0.17.2",
"ip-address": "^9.0.5",
"ip-bigint": "^8.0.2",
"ip-cidr": "^4.0.0",
"is-cidr": "^5.0.3",
"is-ip": "^5.0.1",
"jpeg-quality-estimator": "^1.0.1",
"js-base64": "^3.7.7",
"json-editor-vue": "^0.17.2",
"json5": "^2.2.3",
"jszip": "^3.10.1",
"jwt-decode": "^3.1.2",
Expand Down
155 changes: 155 additions & 0 deletions src/tools/api-tester/api-tester.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<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: [] });
const contentType = useQueryParamOrStorage({ name: 'ct', storageName: 'api-tester:ct', defaultValue: 'application/json' });
const body = useQueryParamOrStorage({ name: 'body', storageName: 'api-tester:body', defaultValue: '' });
const noCORS = ref(false);
const apiCallResult = ref();
const inprogress = ref(false);
async function callAPI() {
const url = new URL(baseUrl.value);
for (const kv of queryParams.value) {
if (!kv.key) {
continue;
}
url.searchParams.append(kv.key, kv.value || '');
}
const queryHeaders = [] as [string, string][];
for (const kv of headers.value) {
if (!kv.key) {
continue;
}
queryHeaders.push([kv.key, kv.value || '']);
}
queryHeaders.push(['Content-Type', contentType.value || '']);
try {
const response = await fetch(url, {
method: method.value,
headers: queryHeaders,
body: (method.value === 'GET' || method.value === 'HEAD') ? null : body.value,
mode: noCORS.value ? 'no-cors' : 'cors',
});
let responseText = await response.text();
try {
responseText = JSON.stringify(JSON.parse(responseText), null, 2);
}
catch (_) {
}
apiCallResult.value = {
code: response.status,
error: '',
result: responseText,
};
}
catch (err: any) {
apiCallResult.value = {
code: -1,
error: err.toString(),
result: null,
};
}
}
function emptyKeyPair() {
return {
key: '',
value: '',
};
}
</script>

<template>
<div>
<c-card title="API Calling">
<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']"
mb-2
/>

<c-card title="Headers" mb-2>
<n-dynamic-input v-model:value="headers" :on-create="emptyKeyPair">
<template #create-button-default>
Add a new HTTP Header
</template>
<template #default="{ value }">
<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" />
</div>
</template>
</n-dynamic-input>
<c-select
v-model:value="contentType"
label="Content-Type:"
:options="['application/json', 'text/plain']"
mt-2
/>
</c-card>

<c-card title="Query Parameters" mb-2>
<n-dynamic-input v-model:value="queryParams" :on-create="emptyKeyPair">
<template #create-button-default>
Add a new Query Parameter
</template>
<template #default="{ value }">
<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" />
</div>
</template>
</n-dynamic-input>
</c-card>
<c-input-text
v-if="method !== 'GET' && method !== 'HEAD'"
v-model:value="body"
label="Body"
placeholder="HTTP Query body"
multiline
monospace
mb-2
/>

<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>
</c-card>
<n-spin
v-if="inprogress"
size="small"
/>
<c-alert v-if="!inprogress && apiCallResult && apiCallResult.code !== 200" type="error" mt-12 title="Error while calling API">
<p><strong>Status code = {{ apiCallResult.code }}</strong></p>
<TextareaCopyable :value="apiCallResult.error" copy-placement="none" />
</c-alert>
<c-card v-if="!inprogress && apiCallResult && apiCallResult.code === 200" mt-12 title="API Call result">
<TextareaCopyable :value="apiCallResult.result" word-wrap />
</c-card>
</div>
</template>
12 changes: 12 additions & 0 deletions src/tools/api-tester/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { World } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'API Tester',
path: '/api-tester',
description: 'HTTP API Tester',
keywords: ['api', 'http', 'call', 'tester'],
component: () => import('./api-tester.vue'),
icon: World,
createdAt: new Date('2024-05-11'),
});
2 changes: 2 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { tool as regexTester } from './regex-tester';
import { tool as regexMemo } from './regex-memo';
import { tool as markdownToHtml } from './markdown-to-html';
import { tool as propertiesToYaml } from './properties-to-yaml';
import { tool as apiTester } from './api-tester';
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
import { tool as uuidConverter } from './uuid-converter';
import { tool as numeronymGenerator } from './numeronym-generator';
Expand Down Expand Up @@ -172,6 +173,7 @@ export const toolsByCategory: ToolCategory[] = [
httpStatusCodes,
jsonDiff,
safelinkDecoder,
apiTester,
],
},
{
Expand Down

0 comments on commit c716e0f

Please sign in to comment.