提交 4edda11e 编写于 作者: D DebugIsFalse

feature: 添加fetch请求

上级 b1c5e1c3
const BASE_URL = 'http://localhost:3000';
const useFetchRequest = (baseUrl) => {
baseUrl = baseUrl || BASE_URL;
const config = {
headers: {}
};
const loading = ref(false);
const fetchRequest = (url, fetchConfig) => {
url = `${BASE_URL}${url}`;
fetchConfig = fetchConfig || {};
fetchConfig.method = (fetchConfig.method || 'get').toLocaleUpperCase();
if (!fetchConfig['Content-Type']) {
fetchConfig['Content-Type'] = 'application/json';
}
fetchConfig = Object.assign(config, fetchConfig);
return window.fetch(url, fetchConfig).then(async (response) => {
let result = await response.text()
try {
result = JSON.parse(result)
} catch(error) {
console.log(error)
}
return result;
}).finally(() => {
loading.value = false
})
}
return {
loading,
fetchRequest
}
}
export default useFetchRequest;
\ No newline at end of file
<template>
<MdRenderer :content="mdContent" />
<div>
<UButton @click="fetchMarkdownContent">点击请求</UButton>
<UButton @click="fetchSseContent">sse请求</UButton>
</div>
</template>
<script setup>
const mdContent = ref('');
const { data } = await useFetch('https://ieditor-ai.inscode.cc/ai/md', { method: 'post' });
mdContent.value = data.value.content;
const { loading, fetchRequest } = useFetchRequest();
// const { data } = await useFetch('/api/fetchMarkdown')
// mdContent.value = data.value.content
async function fetchMarkdownContent () {
const { data } = await useFetch('/api/fetchMarkdown')
console.log(`data:`, data)
}
function fetchSseContent() {
fetchRequest('/api/mock/test').then((response) => {
console.log(response)
})
// const myHeaders = new Headers();
// myHeaders.append("Authorization", "Bearer fk188579-fptzRyRKlkoTE6T2bDjteXYor6ulTGeb");
// myHeaders.append("Content-Type", "application/json");
// const raw = JSON.stringify({
// model: "gpt-3.5-turbo",
// messages: [
// {
// "role": "user",
// "content": "讲个笑话"
// }
// ],
// safe_mode: false,
// stream: true
// });
// var requestOptions = {
// method: 'POST',
// headers: myHeaders,
// body: raw,
// redirect: 'follow'
// };
// window.fetch("https://oa.api2d.net/v1/chat/completions", requestOptions)
// .then(response => response.text())
// .then(result => {
// console.log(result)
// })
// .catch(error => console.log('error', error));
}
</script>
\ No newline at end of file
export default defineEventHandler(async (event) => {
// const config = useRuntimeConfig(event)
const cookie = event.headers.get('cookie');
// console.log(`config:`, event.headers.get('cookie'))
const repo = await $fetch('https://ieditor-ai.inscode.cc/ai/md', {
method: 'POST',
headers: {
cookie
}
})
return repo
})
\ No newline at end of file
import { createRouter, defineEventHandler, useBase } from 'h3'
const router = createRouter()
router.get('/test', defineEventHandler(() => {
return { data: 'Hello World' }
}))
export default useBase('/api/mock', router.handler)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册