fossyl 0.1.5 → 0.9.0

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.
@@ -1,143 +0,0 @@
1
- import { Params } from "./params.types";
2
- import {
3
- Authentication,
4
- AuthenticatedRoute,
5
- FullRoute,
6
- OpenRoute,
7
- ValidatedRoute,
8
- } from "./routes.types";
9
- import { RestMethod } from "./routes.types";
10
-
11
- export type ValidatorFunction<T extends unknown = unknown> = (data: unknown) => T;
12
- export type AuthenticationFunction<T extends Authentication> = (
13
- headers: Record<string, string>
14
- ) => T;
15
-
16
- /**
17
- * This is the most confusing part of the type system, but also key.
18
- * This type is used to force type inferenece when creating your endpoints.
19
- * This is the structure to provide `function overload`s. This is so you can have the same function
20
- * (in our case get,put,post,delete) but allow differnt configurations.
21
- *
22
- * These configurations need to be strongly typed force the developers into using the tools.
23
- *
24
- * Want to allow Auth? - Gotta pass in authenticator and the type system handles it
25
- *
26
- * Also, this is where the core of the Zod system is. We force the body to be a zod validated input.
27
- * Luckily, this is pretty easy to replace.
28
- * PART of me wanted to further abstract it such that you needed a translation function but
29
- * that seemed overengineerd
30
- *
31
- */
32
-
33
- /**
34
- * Configuration type for POST/PUT/DELETE endpoints.
35
- * These methods require a request body, so they always need a validator.
36
- *
37
- * Four variants (with/without auth × with/without query):
38
- * 1. Validated only, no query: validator + handler(params, body)
39
- * 2. Validated only, with query: validator + queryValidator + handler(params, body)
40
- * 3. Full (auth + validated), no query: authenticator + validator + handler(params, auth, body)
41
- * 4. Full (auth + validated), with query: authenticator + validator + queryValidator + handler(params, auth, body)
42
- */
43
- export type EndpointCreationFunction<Path extends string, Method extends RestMethod> = {
44
- // Validated route: no auth, no query
45
- <Res extends unknown, RequestBody extends unknown>(
46
- config: {
47
- authenticator?: never;
48
- validator: ValidatorFunction<RequestBody>;
49
- queryValidator?: never;
50
- handler: (params: { url: Params<Path> }, body: RequestBody) => Promise<Res>;
51
- }
52
- ): ValidatedRoute<Path, Method, Res, RequestBody, undefined>;
53
-
54
- // Validated route: no auth, with query
55
- <Res extends unknown, RequestBody extends unknown, Query extends unknown>(
56
- config: {
57
- authenticator?: never;
58
- validator: ValidatorFunction<RequestBody>;
59
- queryValidator: ValidatorFunction<Query>;
60
- handler: (
61
- params: { url: Params<Path>; query: Query },
62
- body: RequestBody
63
- ) => Promise<Res>;
64
- }
65
- ): ValidatedRoute<Path, Method, Res, RequestBody, Query>;
66
-
67
- // Full route: auth + body validation, no query
68
- <Res extends unknown, RequestBody extends unknown, Auth extends Authentication>(
69
- config: {
70
- authenticator: AuthenticationFunction<Auth>;
71
- validator: ValidatorFunction<RequestBody>;
72
- queryValidator?: never;
73
- handler: (params: { url: Params<Path> }, auth: Auth, body: RequestBody) => Promise<Res>;
74
- }
75
- ): FullRoute<Path, Method, Res, RequestBody, Auth, undefined>;
76
-
77
- // Full route: auth + body validation, with query
78
- <
79
- Res extends unknown,
80
- RequestBody extends unknown,
81
- Auth extends Authentication,
82
- Query extends unknown,
83
- >(
84
- config: {
85
- authenticator: AuthenticationFunction<Auth>;
86
- validator: ValidatorFunction<RequestBody>;
87
- queryValidator: ValidatorFunction<Query>;
88
- handler: (
89
- params: { url: Params<Path>; query: Query },
90
- auth: Auth,
91
- body: RequestBody
92
- ) => Promise<Res>;
93
- }
94
- ): FullRoute<Path, Method, Res, RequestBody, Auth, Query>;
95
- };
96
-
97
- /**
98
- * Configuration type for GET/DELETE endpoints.
99
- * These methods cannot have a request body, so no validator.
100
- *
101
- * Four variants (with/without auth × with/without query):
102
- * 1. Open, no query: just handler(params)
103
- * 2. Open, with query: queryValidator + handler(params)
104
- * 3. Authenticated, no query: authenticator + handler(params, auth)
105
- * 4. Authenticated, with query: authenticator + queryValidator + handler(params, auth)
106
- */
107
- export type GetEndpointCreationFunction<Path extends string, Method extends RestMethod> = {
108
- // Open route: no auth, no query
109
- <Res extends unknown>(
110
- config: {
111
- authenticator?: never;
112
- queryValidator?: never;
113
- handler: (params: { url: Params<Path> }) => Promise<Res>;
114
- }
115
- ): OpenRoute<Path, Method, Res, undefined>;
116
-
117
- // Open route: no auth, with query
118
- <Res extends unknown, Query extends unknown>(
119
- config: {
120
- authenticator?: never;
121
- queryValidator: ValidatorFunction<Query>;
122
- handler: (params: { url: Params<Path>; query: Query }) => Promise<Res>;
123
- }
124
- ): OpenRoute<Path, Method, Res, Query>;
125
-
126
- // Authenticated route: auth, no query
127
- <Res extends unknown, Auth extends Authentication>(
128
- config: {
129
- authenticator: AuthenticationFunction<Auth>;
130
- queryValidator?: never;
131
- handler: (params: { url: Params<Path> }, auth: Auth) => Promise<Res>;
132
- }
133
- ): AuthenticatedRoute<Path, Method, Res, Auth, undefined>;
134
-
135
- // Authenticated route: auth, with query
136
- <Res extends unknown, Auth extends Authentication, Query extends unknown>(
137
- config: {
138
- authenticator: AuthenticationFunction<Auth>;
139
- queryValidator: ValidatorFunction<Query>;
140
- handler: (params: { url: Params<Path>; query: Query }, auth: Auth) => Promise<Res>;
141
- }
142
- ): AuthenticatedRoute<Path, Method, Res, Auth, Query>;
143
- };
@@ -1,13 +0,0 @@
1
- type ExtractUrlParams<Path extends string> = Path extends `${string}/:${infer Param}/${infer Rest}`
2
- ? Param | ExtractUrlParams<Rest>
3
- : Path extends `${string}/:${infer Param}`
4
- ? Param
5
- : never;
6
-
7
- // Yields the full object. Otherwise, we end up with a bunch of & during type inference
8
- type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
9
-
10
- // Helper to use Expand with Path
11
- export type Params<Path extends string> = Expand<{
12
- [K in ExtractUrlParams<Path>]: string;
13
- }>;
@@ -1,36 +0,0 @@
1
- import { GetEndpointCreationFunction } from "./configuration.types";
2
- import { EndpointCreationFunction } from "./configuration.types";
3
-
4
- /* Endpoint seems more complex than it is from its type signature.
5
- * It has 4 Major functions on offer. The different HTTP methods
6
- *
7
- * These use the EndpointCreationFunction
8
- */
9
- export type Endpoint<Path extends string> = {
10
- get: GetEndpointCreationFunction<Path, "GET">;
11
- post: EndpointCreationFunction<Path, "POST">;
12
- put: EndpointCreationFunction<Path, "PUT">;
13
- delete: GetEndpointCreationFunction<Path, "DELETE">;
14
- };
15
-
16
- /**
17
- * Creates a router that ensures all endpoints extend the route.
18
- * IDK if this is actually useful, but I kinda like it.
19
- *
20
- * There def was a part of me that thought about pure generator functions.
21
- * In fact... they're provided!
22
- *
23
- * We can discuss that.
24
- */
25
- export type Router<BasePath extends string> = {
26
- /**
27
- * Creates an endpoint
28
- * @param path string, must extend Router's string
29
- */
30
- createEndpoint: <Path extends `${BasePath}${string}`>(path: Path) => Endpoint<Path>;
31
- /**
32
- * Creates a subrouter
33
- * @param path string, must extend Router's string
34
- */
35
- createSubrouter: <Path extends `${BasePath}${string}`>(path: Path) => Router<Path>;
36
- };
@@ -1,124 +0,0 @@
1
- import { AuthenticationFunction, ValidatorFunction } from "./configuration.types";
2
- import { Params } from "./params.types";
3
-
4
- declare const authBrand: unique symbol;
5
- export type Authentication = { readonly [authBrand]: "Auth" };
6
- export function authWrapper<T>(auth: T): T & Authentication {
7
- return {
8
- ...auth,
9
- [authBrand]: "Auth",
10
- };
11
- }
12
-
13
- export type RestMethod = "GET" | "POST" | "PUT" | "DELETE";
14
-
15
- // Combined Route
16
- export type BaseRoute<Path extends string> = {
17
- TRANSACTIONS: boolean;
18
- path: Path;
19
- logging?: (input: any) => void;
20
- };
21
-
22
- export type OpenRoute<
23
- Path extends string,
24
- Method extends RestMethod,
25
- Res extends unknown,
26
- Query extends unknown | undefined = undefined,
27
- > = {
28
- type: "open";
29
- path: Path;
30
- method: Method;
31
- validator?: never;
32
- authenticator?: never;
33
- handler: (params: { url: Params<Path>; query: Query }) => Promise<Res>;
34
- };
35
-
36
- export type AuthenticatedRoute<
37
- Path extends string,
38
- Method extends RestMethod,
39
- Res extends unknown,
40
- Auth extends Authentication,
41
- Query extends unknown | undefined = undefined,
42
- > = {
43
- type: "authenticated";
44
- path: Path;
45
- method: Method;
46
- validator?: never;
47
- authenticator: AuthenticationFunction<Auth>;
48
- handler: (
49
- params: {
50
- url: Params<Path>;
51
- query: Query;
52
- },
53
- auth: Auth
54
- ) => Promise<Res>;
55
- };
56
-
57
- export type ValidatedRoute<
58
- Path extends string,
59
- Method extends RestMethod,
60
- Res extends unknown,
61
- RequestBody extends unknown,
62
- Query extends unknown | undefined = undefined,
63
- > = {
64
- type: "validated";
65
- path: Path;
66
- method: Method;
67
- validator: ValidatorFunction<RequestBody>;
68
- authenticator?: never;
69
- handler: (
70
- params: {
71
- url: Params<Path>;
72
- query: Query;
73
- },
74
- body: RequestBody
75
- ) => Promise<Res>;
76
- };
77
-
78
- export type FullRoute<
79
- Path extends string,
80
- Method extends RestMethod,
81
- Res extends unknown,
82
- RequestBody extends unknown,
83
- Auth extends Authentication,
84
- Query extends unknown | undefined = undefined,
85
- > = {
86
- type: "full";
87
- path: Path;
88
- method: Method;
89
- validator: ValidatorFunction<RequestBody>;
90
- authenticator: AuthenticationFunction<Auth>;
91
- handler: Query extends undefined
92
- ? (
93
- params: {
94
- url: Params<Path>;
95
- },
96
- auth: Authentication,
97
- body: RequestBody
98
- ) => Promise<Res>
99
- : (
100
- params: {
101
- url: Params<Path>;
102
- query: Query;
103
- },
104
- auth: Authentication,
105
- body: RequestBody
106
- ) => Promise<Res>;
107
- };
108
-
109
- export type Route<
110
- Path extends string,
111
- Res extends unknown,
112
- Method extends RestMethod,
113
- RequestBody extends unknown | undefined = undefined,
114
- Auth extends Authentication | undefined = undefined,
115
- Query extends unknown | undefined = undefined,
116
- > = [RequestBody, Auth] extends [undefined, undefined]
117
- ? OpenRoute<Path, Method, Res, Query>
118
- : [RequestBody, Auth] extends [undefined, infer A extends Authentication]
119
- ? AuthenticatedRoute<Path, Method, Res, A, Query>
120
- : [RequestBody, Auth] extends [infer B, undefined]
121
- ? ValidatedRoute<Path, Method, Res, B, Query>
122
- : [RequestBody, Auth] extends [infer B, infer A extends Authentication]
123
- ? FullRoute<Path, Method, Res, B, A, Query>
124
- : never;