@kaito-http/core 3.0.0-beta.7 → 3.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/src/util.ts ADDED
@@ -0,0 +1,83 @@
1
+ import type {KaitoHead} from './head.ts';
2
+ import type {KaitoRequest} from './request.ts';
3
+ import {Router} from './router/router.ts';
4
+
5
+ /**
6
+ * A helper to check if the environment is Node.js-like and the NODE_ENV is development
7
+ */
8
+ export const isNodeLikeDev =
9
+ typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env.NODE_ENV === 'development';
10
+
11
+ export type ErroredAPIResponse = {success: false; data: null; message: string};
12
+ export type SuccessfulAPIResponse<T> = {success: true; data: T; message: 'OK'};
13
+ export type APIResponse<T> = ErroredAPIResponse | SuccessfulAPIResponse<T>;
14
+ export type AnyResponse = APIResponse<unknown>;
15
+ export type MakeOptional<T, K extends keyof T> = T extends T ? Omit<T, K> & Partial<Pick<T, K>> : never;
16
+
17
+ export type ExtractRouteParams<T extends string> = string extends T
18
+ ? Record<string, string>
19
+ : T extends `${string}:${infer Param}/${infer Rest}`
20
+ ? {[k in Param | keyof ExtractRouteParams<Rest>]: string}
21
+ : T extends `${string}:${infer Param}`
22
+ ? {[k in Param]: string}
23
+ : {};
24
+
25
+ /**
26
+ * A function that is called to get the context for a request.
27
+ *
28
+ * This is useful for things like authentication, to pass in a database connection, etc.
29
+ *
30
+ * It's fine for this function to throw; if it does, the error is passed to the `onError` function.
31
+ *
32
+ * @param req - The kaito request object, which contains the request method, url, headers, etc
33
+ * @param head - The kaito head object, which contains getters and setters for headers and status
34
+ * @returns The context for your routes
35
+ */
36
+ export type GetContext<Result> = (req: KaitoRequest, head: KaitoHead) => Promise<Result>;
37
+
38
+ /**
39
+ * A helper function to create typed necessary functions
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * const {router, getContext} = createUtilities(async (req, res) => {
44
+ * // Return context here
45
+ * })
46
+ *
47
+ * const app = router().get('/', async () => "hello");
48
+ *
49
+ * const server = createKaitoHandler({
50
+ * router: app,
51
+ * getContext,
52
+ * // ...
53
+ * });
54
+ * ```
55
+ */
56
+ export function createUtilities<Context>(getContext: GetContext<Context>): {
57
+ getContext: GetContext<Context>;
58
+ router: () => Router<Context, Context, never>;
59
+ } {
60
+ return {
61
+ getContext,
62
+ router: () => Router.create<Context>(),
63
+ };
64
+ }
65
+
66
+ export interface Parsable<Output = any, Input = Output> {
67
+ _input: Input;
68
+ parse: (value: unknown) => Output;
69
+ }
70
+
71
+ export type InferParsable<T> =
72
+ T extends Parsable<infer Output, infer Input>
73
+ ? {
74
+ input: Input;
75
+ output: Output;
76
+ }
77
+ : never;
78
+
79
+ export function parsable<T>(parse: (value: unknown) => T): Parsable<T, T> {
80
+ return {
81
+ parse,
82
+ } as Parsable<T, T>;
83
+ }