-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
192 lines (181 loc) · 4.48 KB
/
example.ts
File metadata and controls
192 lines (181 loc) · 4.48 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { Jetflare, createRoutes } from "./dist/index.js";
// Define your API routes
export const routes = {
GET_users: {
path: "/users",
method: "get",
title: "Get all users",
query: {
id: "string",
limit: 0,
isActive: false,
tags: [] as string[],
},
response: {
a: "eggs",
b: "eggs",
c: "eggs",
},
},
GET_user_by_id: {
path: "/users/:id",
method: "get",
params: {
id: "string",
},
query: {
includePosts: false,
},
},
POST_user: {
path: "/users",
method: "post",
title: "Create new user",
body: {
name: "string",
email: "string",
age: 0,
address: {
street: "string",
city: "string",
},
},
query: {
dryRun: false,
},
},
PUT_user_by_id: {
path: "/users/:id",
method: "put",
params: {
id: "string",
},
body: {
name: "string",
email: "string",
},
},
DELETE_user_by_id: {
path: "/users/:id",
method: "delete",
params: {
id: "string",
},
},
WS_chat: {
path: "/chat",
method: "websocket",
},
SSE_updates: {
path: "/updates",
method: "sse",
query: {
topic: "string",
},
},
} as const; // <-- KEEP this 'as const' if defined separately,
// or remove it if you pass it directly to createRoutes()
const jetflare = Jetflare("https://api.example.com", createRoutes(routes));
// Type test: the response type of GET_users is inferred from the route definition
async function typeTest() {
const res = await jetflare.GET_users();
const data = await res.json();
// The following line should have correct type inference:
// data.a, data.b, data.c should all be string ("eggs")
// @ts-expect-error: Property 'notAKey' does not exist
data.notAKey;
// Uncomment to see types:
// type Inferred = typeof data;
}
// Examples of how the new types enforce usage:
// GET request - query params are enforced
jetflare
.GET_users({
query: {
id: "123",
limit: 10, // This should now work!
isActive: true,
tags: ["admin", "developer"],
// invalidParam: true, // This would still be a type error!
},
// body: {} // This would still be a type error!
})
.then((response) => {
console.log("GET_users response:", response);
})
.catch((error) => {
console.error("GET_users error:", error);
});
// GET request with params - params are enforced
jetflare
.GET_user_by_id({
params: {
id: "user123",
// anotherParam: "abc" // Type error: anotherParam is not defined
},
query: {
includePosts: true,
},
})
.then((response) => response.json())
.then((data) => console.log("GET_user_by_id data:", data))
.catch((error) => console.error("GET_user_by_id error:", error));
// POST request - body and query are enforced
jetflare
.POST_user({
body: {
name: "John Doe",
email: "john.doe@example.com",
age: 30,
address: {
street: "123 Main St",
city: "Any town",
},
// extraField: "value" // This would still be a type error!
},
query: {
dryRun: false,
},
})
.then((response) => {
console.log("POST_user response:", response);
})
.catch((error) => {
console.error("POST_user error:", error);
});
// PUT request - body and params enforced
jetflare
.PUT_user_by_id({
params: {
id: "user456",
},
body: {
name: "Jane Doe Updated",
email: "jane.doe.updated@example.com",
},
})
.then((response) => console.log("PUT_user_by_id response:", response))
.catch((error) => console.error("PUT_user_by_id error:", error));
// DELETE request - params enforced, no body allowed
jetflare
.DELETE_user_by_id({
params: {
id: "user789",
},
// body: { data: "test" } // Type error: Body not allowed for DELETE
})
.then((response) => console.log("DELETE_user_by_id response:", response))
.catch((error) => console.error("DELETE_user_by_id error:", error));
// WebSocket example
const chatWs = jetflare.WS_chat();
chatWs.connect();
chatWs.on("open", () => console.log("WebSocket connected!"));
chatWs.send("Hello from client!");
chatWs.close();
// SSE example
const sse = jetflare.SSE_updates({ query: { topic: "news" } });
sse.connect();
sse.on("message", (event) => console.log("SSE message:", event.data));
sse.on("custom-event", (event) => console.log("Custom event:", event.data));
sse.on("close", () => console.log("SSE connection closed."));
sse.close();