Skip to content

Commit d0289dd

Browse files
committed
update
1 parent 5dabb35 commit d0289dd

File tree

6 files changed

+292
-45
lines changed

6 files changed

+292
-45
lines changed

.gitignore

+200-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,206 @@
1-
node_modules
2-
dist/
31
.pnp.*
42
.yarn/*
53
!.yarn/patches
64
!.yarn/plugins
75
!.yarn/releases
86
!.yarn/sdks
9-
!.yarn/versions
7+
!.yarn/versions
8+
9+
# Created by https://www.toptal.com/developers/gitignore/api/macos,vim,node
10+
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,vim,node
11+
12+
### macOS ###
13+
# General
14+
.DS_Store
15+
.AppleDouble
16+
.LSOverride
17+
18+
# Icon must end with two \r
19+
Icon
20+
21+
22+
# Thumbnails
23+
._*
24+
25+
# Files that might appear in the root of a volume
26+
.DocumentRevisions-V100
27+
.fseventsd
28+
.Spotlight-V100
29+
.TemporaryItems
30+
.Trashes
31+
.VolumeIcon.icns
32+
.com.apple.timemachine.donotpresent
33+
34+
# Directories potentially created on remote AFP share
35+
.AppleDB
36+
.AppleDesktop
37+
Network Trash Folder
38+
Temporary Items
39+
.apdisk
40+
41+
### macOS Patch ###
42+
# iCloud generated files
43+
*.icloud
44+
45+
### Node ###
46+
# Logs
47+
logs
48+
*.log
49+
npm-debug.log*
50+
yarn-debug.log*
51+
yarn-error.log*
52+
lerna-debug.log*
53+
.pnpm-debug.log*
54+
55+
# Diagnostic reports (https://nodejs.org/api/report.html)
56+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
57+
58+
# Runtime data
59+
pids
60+
*.pid
61+
*.seed
62+
*.pid.lock
63+
64+
# Directory for instrumented libs generated by jscoverage/JSCover
65+
lib-cov
66+
67+
# Coverage directory used by tools like istanbul
68+
coverage
69+
*.lcov
70+
71+
# nyc test coverage
72+
.nyc_output
73+
74+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
75+
.grunt
76+
77+
# Bower dependency directory (https://bower.io/)
78+
bower_components
79+
80+
# node-waf configuration
81+
.lock-wscript
82+
83+
# Compiled binary addons (https://nodejs.org/api/addons.html)
84+
build/Release
85+
86+
# Dependency directories
87+
node_modules/
88+
jspm_packages/
89+
90+
# Snowpack dependency directory (https://snowpack.dev/)
91+
web_modules/
92+
93+
# TypeScript cache
94+
*.tsbuildinfo
95+
96+
# Optional npm cache directory
97+
.npm
98+
99+
# Optional eslint cache
100+
.eslintcache
101+
102+
# Optional stylelint cache
103+
.stylelintcache
104+
105+
# Microbundle cache
106+
.rpt2_cache/
107+
.rts2_cache_cjs/
108+
.rts2_cache_es/
109+
.rts2_cache_umd/
110+
111+
# Optional REPL history
112+
.node_repl_history
113+
114+
# Output of 'npm pack'
115+
*.tgz
116+
117+
# Yarn Integrity file
118+
.yarn-integrity
119+
120+
# dotenv environment variable files
121+
.env
122+
.env.development.local
123+
.env.test.local
124+
.env.production.local
125+
.env.local
126+
127+
# parcel-bundler cache (https://parceljs.org/)
128+
.cache
129+
.parcel-cache
130+
131+
# Next.js build output
132+
.next
133+
out
134+
135+
# Nuxt.js build / generate output
136+
.nuxt
137+
dist
138+
139+
# Gatsby files
140+
.cache/
141+
# Comment in the public line in if your project uses Gatsby and not Next.js
142+
# https://nextjs.org/blog/next-9-1#public-directory-support
143+
# public
144+
145+
# vuepress build output
146+
.vuepress/dist
147+
148+
# vuepress v2.x temp and cache directory
149+
.temp
150+
151+
# Docusaurus cache and generated files
152+
.docusaurus
153+
154+
# Serverless directories
155+
.serverless/
156+
157+
# FuseBox cache
158+
.fusebox/
159+
160+
# DynamoDB Local files
161+
.dynamodb/
162+
163+
# TernJS port file
164+
.tern-port
165+
166+
# Stores VSCode versions used for testing VSCode extensions
167+
.vscode-test
168+
169+
# yarn v2
170+
.yarn/cache
171+
.yarn/unplugged
172+
.yarn/build-state.yml
173+
.yarn/install-state.gz
174+
.pnp.*
175+
176+
### Node Patch ###
177+
# Serverless Webpack directories
178+
.webpack/
179+
180+
# Optional stylelint cache
181+
182+
# SvelteKit build / generate output
183+
.svelte-kit
184+
185+
### Vim ###
186+
# Swap
187+
[._]*.s[a-v][a-z]
188+
!*.svg # comment out if you don't need vector files
189+
[._]*.sw[a-p]
190+
[._]s[a-rt-v][a-z]
191+
[._]ss[a-gi-z]
192+
[._]sw[a-p]
193+
194+
# Session
195+
Session.vim
196+
Sessionx.vim
197+
198+
# Temporary
199+
.netrwhist
200+
*~
201+
# Auto-generated tag files
202+
tags
203+
# Persistent undo
204+
[._]*.un~
205+
206+
# End of https://www.toptal.com/developers/gitignore/api/macos,vim,node

index.d.ts

-3
This file was deleted.

index.html

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
</head>
99

1010
<body>
11-
<pre id="app"></pre>
1211
<form id="formElem">
1312
<input type="text" name="name" value="konfig">
1413
<input type="submit">
1514
</form>
16-
<script type="module" src="/test/index.ts"></script>
15+
<form id="chat-gpt">
16+
<input id="chat-input" type="text" value="write me a django REST api that mimics the pet store example">
17+
<input type="submit">
18+
</form>
19+
<pre id="app"></pre>
20+
<script type="module" src="/script.ts"></script>
1721
</body>
1822

1923
</html>

index.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios, {
1+
import {
22
AxiosAdapter,
33
AxiosError,
44
AxiosRequestConfig,
@@ -36,10 +36,7 @@ const fetchAdapter: AxiosAdapter = async (config) => {
3636
if (data instanceof Error) {
3737
reject(data);
3838
} else {
39-
Object.prototype.toString.call((config as any).settle) ===
40-
"[object Function]"
41-
? (config as any).settle(resolve, reject, data)
42-
: settle(resolve, reject, data);
39+
settle(resolve, reject, data);
4340
}
4441
});
4542
};
@@ -48,7 +45,10 @@ const fetchAdapter: AxiosAdapter = async (config) => {
4845
* Fetch API stage two is to get response body. This funtion tries to retrieve
4946
* response body based on response's type
5047
*/
51-
async function getResponse(request: Request, config: AxiosRequestConfig) {
48+
async function getResponse(
49+
request: Request,
50+
config: AxiosRequestConfig
51+
): Promise<AxiosResponse | Error> {
5252
let stageOne: Response;
5353
try {
5454
stageOne = await fetch(request);
@@ -68,6 +68,9 @@ async function getResponse(request: Request, config: AxiosRequestConfig) {
6868
case "json":
6969
data = await stageOne.json();
7070
break;
71+
case "stream":
72+
data = await stageOne.body?.getReader().read();
73+
break;
7174
default:
7275
data = await stageOne.text();
7376
break;

script.ts

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import axios from "axios";
2+
import fetchAdapter from "./index";
3+
4+
window["axios"] = axios;
5+
window.onload = async function () {
6+
try {
7+
const data = await axios.request({
8+
url: "/package.json",
9+
method: "get",
10+
adapter: fetchAdapter,
11+
});
12+
document.getElementById("app")?.append(JSON.stringify(data, null, 4));
13+
} catch (e) {
14+
console.log(e);
15+
}
16+
};
17+
const formElem = document.getElementById("formElem");
18+
if (formElem && formElem instanceof HTMLFormElement) {
19+
formElem.onsubmit = async (e) => {
20+
e.preventDefault();
21+
22+
let response = await axios.request({
23+
url: "https://httpbin.org/post",
24+
method: "post",
25+
data: new FormData(formElem),
26+
adapter: fetchAdapter,
27+
});
28+
29+
console.log(response);
30+
};
31+
}
32+
33+
const chatForm = document.getElementById("chat-gpt");
34+
const chatInput = document.getElementById("chat-input");
35+
if (chatForm && chatInput && chatInput instanceof HTMLInputElement) {
36+
chatForm.onsubmit = async (e) => {
37+
e.preventDefault();
38+
39+
const input = {
40+
model: { id: "gpt-3.5-turbo", name: "GPT-3.5" },
41+
systemPrompt:
42+
"You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.",
43+
messages: [
44+
{
45+
role: "user",
46+
content: chatInput.value,
47+
},
48+
],
49+
};
50+
51+
const res = await axios.post(
52+
`https://api.openai.com/v1/chat/completions`,
53+
JSON.stringify({
54+
model: input.model.id,
55+
messages: [
56+
{
57+
role: "system",
58+
content: input.systemPrompt,
59+
},
60+
...input.messages,
61+
],
62+
max_tokens: 1000,
63+
temperature: 1,
64+
stream: true,
65+
}),
66+
{
67+
headers: {
68+
"Content-Type": "application/json",
69+
Authorization: `Bearer ${import.meta.env.VITE_OPENAI_API_KEY}`,
70+
},
71+
adapter: fetchAdapter,
72+
}
73+
);
74+
75+
console.log(res.data);
76+
};
77+
}

test/index.ts

-31
This file was deleted.

0 commit comments

Comments
 (0)