@kaito-http/core 1.3.3 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/dist/declarations/src/index.d.ts +1 -0
- package/dist/declarations/src/server.d.ts +101 -0
- package/dist/kaito-http-core.cjs.d.ts +1 -0
- package/dist/kaito-http-core.cjs.dev.js +245 -0
- package/dist/kaito-http-core.cjs.js +7 -0
- package/dist/kaito-http-core.cjs.prod.js +245 -0
- package/dist/kaito-http-core.esm.js +233 -0
- package/package.json +40 -55
- package/dist/index.cjs +0 -256
- package/dist/index.d.ts +0 -115
- package/dist/index.js +0 -256
package/dist/index.js
DELETED
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
3
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
4
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {enumerable: true, configurable: true, writable: true, value}) : obj[key] = value;
|
|
6
|
-
var __assign = (a, b) => {
|
|
7
|
-
for (var prop in b || (b = {}))
|
|
8
|
-
if (__hasOwnProp.call(b, prop))
|
|
9
|
-
__defNormalProp(a, prop, b[prop]);
|
|
10
|
-
if (__getOwnPropSymbols)
|
|
11
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
-
if (__propIsEnum.call(b, prop))
|
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
|
14
|
-
}
|
|
15
|
-
return a;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
// src/Kaito.ts
|
|
19
|
-
import "reflect-metadata";
|
|
20
|
-
|
|
21
|
-
// src/types.ts
|
|
22
|
-
var MetadataKeys;
|
|
23
|
-
(function(MetadataKeys2) {
|
|
24
|
-
MetadataKeys2["HTTP_METHOD"] = "kaito:http:method";
|
|
25
|
-
MetadataKeys2["SCHEMA"] = "kaito:route:schema";
|
|
26
|
-
MetadataKeys2["QUERY_SCHEMA"] = "kaito:route:schema:query";
|
|
27
|
-
MetadataKeys2["CONTROLLER_PATH"] = "kaito:controller:path";
|
|
28
|
-
MetadataKeys2["AVAILABLE_ROUTE_METHODS"] = "kaito:route:methods";
|
|
29
|
-
MetadataKeys2["ROUTE_PATH"] = "kaito:route:path";
|
|
30
|
-
})(MetadataKeys || (MetadataKeys = {}));
|
|
31
|
-
|
|
32
|
-
// src/utils/url.ts
|
|
33
|
-
var FORWARD_SLASH_CHAR_CODE = 47;
|
|
34
|
-
function normalizePath(base, path) {
|
|
35
|
-
return lead(base) + lead(path);
|
|
36
|
-
}
|
|
37
|
-
function lead(path) {
|
|
38
|
-
return path.charCodeAt(0) === FORWARD_SLASH_CHAR_CODE ? path : "/" + path;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// src/utils/metadata.ts
|
|
42
|
-
function readControllerMetadata(controller) {
|
|
43
|
-
const base = Reflect.getMetadata(MetadataKeys.CONTROLLER_PATH, controller.constructor);
|
|
44
|
-
const classMethods = Reflect.getMetadata(MetadataKeys.AVAILABLE_ROUTE_METHODS, controller) || [];
|
|
45
|
-
return {
|
|
46
|
-
base,
|
|
47
|
-
routes: classMethods.map((methodKey) => {
|
|
48
|
-
const method2 = Reflect.getMetadata(MetadataKeys.HTTP_METHOD, controller, methodKey);
|
|
49
|
-
const schema = Reflect.getMetadata(MetadataKeys.SCHEMA, controller, methodKey);
|
|
50
|
-
const querySchema = Reflect.getMetadata(MetadataKeys.QUERY_SCHEMA, controller, methodKey);
|
|
51
|
-
const routePath = Reflect.getMetadata(MetadataKeys.ROUTE_PATH, controller, methodKey);
|
|
52
|
-
const path = normalizePath(base, routePath);
|
|
53
|
-
return {
|
|
54
|
-
path,
|
|
55
|
-
schema,
|
|
56
|
-
method: method2,
|
|
57
|
-
routePath,
|
|
58
|
-
querySchema,
|
|
59
|
-
methodName: methodKey
|
|
60
|
-
};
|
|
61
|
-
})
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// src/Kaito.ts
|
|
66
|
-
import {App} from "@tinyhttp/app";
|
|
67
|
-
|
|
68
|
-
// src/exceptions.ts
|
|
69
|
-
var HttpException = class extends Error {
|
|
70
|
-
constructor(code, message) {
|
|
71
|
-
super(typeof message === "string" ? message : message.join(", "));
|
|
72
|
-
this.code = code;
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
var NotFoundException = class extends HttpException {
|
|
76
|
-
constructor() {
|
|
77
|
-
super(404, "That resource was not found");
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
// src/utils/errors.ts
|
|
82
|
-
import {ZodError} from "zod";
|
|
83
|
-
function defaultErrorHandler(err, ctx) {
|
|
84
|
-
ctx.res.setHeader("Content-Type", "application/json");
|
|
85
|
-
if (err instanceof HttpException) {
|
|
86
|
-
const body = JSON.stringify({
|
|
87
|
-
message: err.message,
|
|
88
|
-
code: err.code,
|
|
89
|
-
error: "HttpException"
|
|
90
|
-
});
|
|
91
|
-
ctx.res.writeHead(err.code, {
|
|
92
|
-
"Content-Type": "application/json"
|
|
93
|
-
});
|
|
94
|
-
ctx.res.end(body);
|
|
95
|
-
} else if (err instanceof ZodError) {
|
|
96
|
-
const body = JSON.stringify({
|
|
97
|
-
message: err.errors.map((error) => error.message).join(", "),
|
|
98
|
-
code: 422,
|
|
99
|
-
error: "ZodError"
|
|
100
|
-
});
|
|
101
|
-
ctx.res.writeHead(422, {
|
|
102
|
-
"Content-Type": "application/json"
|
|
103
|
-
});
|
|
104
|
-
ctx.res.end(body);
|
|
105
|
-
} else {
|
|
106
|
-
ctx.res.statusCode = 500;
|
|
107
|
-
const body = JSON.stringify({
|
|
108
|
-
message: "Something went wrong on our side",
|
|
109
|
-
error: err.constructor.name,
|
|
110
|
-
code: 500
|
|
111
|
-
});
|
|
112
|
-
ctx.res.end(body);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// src/Kaito.ts
|
|
117
|
-
import {blue, bold} from "colorette";
|
|
118
|
-
var Kaito = class extends App {
|
|
119
|
-
constructor(options) {
|
|
120
|
-
super({
|
|
121
|
-
settings: {
|
|
122
|
-
xPoweredBy: "kaito.cloud"
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
this.server = null;
|
|
126
|
-
this.addControllers(options.controllers);
|
|
127
|
-
this.kaitoOptions = options;
|
|
128
|
-
this.use = this.use.bind(this);
|
|
129
|
-
}
|
|
130
|
-
async parseBody(req) {
|
|
131
|
-
var _a;
|
|
132
|
-
const get = async () => {
|
|
133
|
-
let chunks = "";
|
|
134
|
-
for await (const chunk of req)
|
|
135
|
-
chunks += chunk;
|
|
136
|
-
return chunks;
|
|
137
|
-
};
|
|
138
|
-
switch ((_a = req.headers["content-type"]) == null ? void 0 : _a.toLowerCase()) {
|
|
139
|
-
case "application/json": {
|
|
140
|
-
return JSON.parse(await get());
|
|
141
|
-
}
|
|
142
|
-
default:
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
listen(port) {
|
|
147
|
-
const parsed = (typeof port === "string" ? parseInt(port) : port) || process.env.PORT && parseInt(process.env.PORT) || void 0;
|
|
148
|
-
this.server = super.listen(parsed, () => this.log(`starting on ${parsed}`));
|
|
149
|
-
return this.server;
|
|
150
|
-
}
|
|
151
|
-
log(...args) {
|
|
152
|
-
if (this.kaitoOptions.logging) {
|
|
153
|
-
console.log(blue(bold("[kaito/core]")), ...args);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
close(cb) {
|
|
157
|
-
this.log("shutting down");
|
|
158
|
-
if (!this.server) {
|
|
159
|
-
this.log("trying to close a server that was never started");
|
|
160
|
-
cb == null ? void 0 : cb(new Error("Could not close a server that was never started"));
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
try {
|
|
164
|
-
this.server.removeAllListeners();
|
|
165
|
-
this.server.close(cb);
|
|
166
|
-
this.server = null;
|
|
167
|
-
cb == null ? void 0 : cb();
|
|
168
|
-
} catch (e) {
|
|
169
|
-
cb == null ? void 0 : cb(e);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
addControllers(controllers) {
|
|
173
|
-
for (const controller of controllers) {
|
|
174
|
-
const {routes} = readControllerMetadata(controller);
|
|
175
|
-
for (const route of routes) {
|
|
176
|
-
const {method: method2, schema, path, methodName, querySchema} = route;
|
|
177
|
-
if (method2 === "get" && schema) {
|
|
178
|
-
throw new Error(`Method ${methodName} (${path}) cannot have a schema as it is a GET only route.`);
|
|
179
|
-
}
|
|
180
|
-
const handler = controller[methodName];
|
|
181
|
-
this[method2](path, async (req, res) => {
|
|
182
|
-
var _a, _b;
|
|
183
|
-
const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress || req.ip;
|
|
184
|
-
const ctx = {
|
|
185
|
-
body: await this.parseBody(req),
|
|
186
|
-
params: req.params,
|
|
187
|
-
path: req.path,
|
|
188
|
-
query: req.query,
|
|
189
|
-
url: req.url,
|
|
190
|
-
req,
|
|
191
|
-
res,
|
|
192
|
-
ip
|
|
193
|
-
};
|
|
194
|
-
try {
|
|
195
|
-
if (querySchema) {
|
|
196
|
-
ctx.query = await querySchema.parseAsync(ctx.query);
|
|
197
|
-
}
|
|
198
|
-
if (schema) {
|
|
199
|
-
ctx.body = await schema.parseAsync(ctx.body);
|
|
200
|
-
}
|
|
201
|
-
const result = await handler(ctx);
|
|
202
|
-
res.writeHead((_a = result == null ? void 0 : result.status) != null ? _a : 200, __assign({"Content-Type": "application/json"}, result == null ? void 0 : result.headers)).end(JSON.stringify((_b = result == null ? void 0 : result.body) != null ? _b : "OK"));
|
|
203
|
-
} catch (e) {
|
|
204
|
-
if (this.kaitoOptions.onError) {
|
|
205
|
-
this.kaitoOptions.onError(e, ctx);
|
|
206
|
-
} else {
|
|
207
|
-
defaultErrorHandler(e, ctx);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
// src/decorators.ts
|
|
217
|
-
var method = (method2) => (path = "/") => (target, property) => {
|
|
218
|
-
Reflect.defineMetadata(MetadataKeys.HTTP_METHOD, method2, target, property);
|
|
219
|
-
Reflect.defineMetadata(MetadataKeys.ROUTE_PATH, path, target, property);
|
|
220
|
-
const existing = Reflect.getMetadata(MetadataKeys.AVAILABLE_ROUTE_METHODS, target) || [];
|
|
221
|
-
Reflect.defineMetadata(MetadataKeys.AVAILABLE_ROUTE_METHODS, [...existing, property], target);
|
|
222
|
-
};
|
|
223
|
-
var Schema = (schema) => (target, property) => {
|
|
224
|
-
Reflect.defineMetadata(MetadataKeys.SCHEMA, schema, target, property);
|
|
225
|
-
};
|
|
226
|
-
var QuerySchema = (schema) => (target, property) => {
|
|
227
|
-
Reflect.defineMetadata(MetadataKeys.QUERY_SCHEMA, schema, target, property);
|
|
228
|
-
};
|
|
229
|
-
var Controller = (path = "/") => (target) => {
|
|
230
|
-
if (!path.startsWith("/")) {
|
|
231
|
-
throw new Error(`Path must start with / for ${target}`);
|
|
232
|
-
}
|
|
233
|
-
Reflect.defineMetadata(MetadataKeys.CONTROLLER_PATH, path, target);
|
|
234
|
-
};
|
|
235
|
-
var Get = method("get");
|
|
236
|
-
var Post = method("post");
|
|
237
|
-
var Patch = method("patch");
|
|
238
|
-
var Put = method("put");
|
|
239
|
-
var Delete = method("delete");
|
|
240
|
-
var Method = (m) => method(m);
|
|
241
|
-
export {
|
|
242
|
-
Controller,
|
|
243
|
-
Delete,
|
|
244
|
-
Get,
|
|
245
|
-
HttpException,
|
|
246
|
-
Kaito,
|
|
247
|
-
MetadataKeys,
|
|
248
|
-
Method,
|
|
249
|
-
NotFoundException,
|
|
250
|
-
Patch,
|
|
251
|
-
Post,
|
|
252
|
-
Put,
|
|
253
|
-
QuerySchema,
|
|
254
|
-
Schema,
|
|
255
|
-
method
|
|
256
|
-
};
|