Unlisted
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { GptApi } from "https://esm.town/v/xkonti/gptApiFramework";
import { Hono } from "npm:hono@3";
import { z } from "npm:zod";
/**
* INITIALIZE API
*/
const api = new GptApi({
url: "https://xkonti-tempsimplegptexample.web.val.run",
title: "Some API",
description: "API for something",
version: "1.0.0",
});
// Example of action that doesn't accept any input, but returns something
const ResponseCommandSchema = z.object({
feedback: z.string().describe("Feedback regarding submitted action"),
data: z.string().optional().describe("Additional data related to the given command"),
}).describe("Contains feedback regarding submitted action");
export type ResponseCommand = z.infer<typeof ResponseCommandSchema>;
api.nothingToJson<ResponseCommand>({
verb: "post",
path: "/someendpoint",
operationId: "doSomething",
desc: "Does something to do something",
requestSchema: null,
requestDesc: null,
responseSchema: ResponseCommandSchema,
responseDesc: "Summary of executed action",
}, async (ctx) => {
// TODO: Do something / call something
return {
feedback: "Did something.",
data: "124 miles",
};
});
// Example of action that accept some data and returns something
const ThingsRequestSchema = z.object({
things: z.array(z.string().describe("A thing to do something with")).describe("A list of things "),
}).describe("Input for the action containing some things");
type ThingsRequest = z.infer<typeof ThingsRequestSchema>;
api.jsonToJson<ThingsRequest, ResponseCommand>({
verb: "post",
path: "/submitthings",
operationId: "submit-things",
desc: "Endpoint for submitting some things",
requestSchema: ThingsRequestSchema,
requestDesc: "Things to do things with",
responseSchema: ResponseCommandSchema,
responseDesc: "Feedback on things",
}, async (ctx, dto) => {
// TODO: Do things to things
return {
feedback: "Things have been done to things.",
data: "15 things processed",
};
});
// Serve the API
export default api.serve();
xkonti-tempsimplegptexample.web.val.run
June 7, 2024