@alxia/create 0.1.5 → 0.1.6
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 +43 -18
- package/dist/index.js +29 -19
- package/dist/index.js.map +7 -6
- package/dist/main.d.ts +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/packument.d.ts +18 -0
- package/dist/packument.d.ts.map +1 -0
- package/dist/registry.d.ts +19 -10
- package/dist/registry.d.ts.map +1 -1
- package/dist/versions.d.ts +1 -1
- package/dist/versions.d.ts.map +1 -1
- package/docs/README.md +6 -5
- package/docs/guide.md +371 -53
- package/docs/roadmap.md +36 -4
- package/docs/troubleshooting.md +182 -4
- package/package.json +5 -4
- package/templates/api/README.md +53 -8
- package/templates/api/_biome.json +1 -1
- package/templates/api/openapi-codegen.config.ts +11 -0
- package/templates/api/openapi.yaml +116 -0
- package/templates/api/package.json +5 -3
- package/templates/api/src/app.spec.ts +33 -14
- package/templates/api/src/app.ts +5 -30
- package/templates/api/src/context.ts +19 -0
- package/templates/api/src/generated/alxia.ts +61 -0
- package/templates/api/src/generated/operations.ts +133 -0
- package/templates/api/src/generated/paths.ts +170 -0
- package/templates/api/src/generated/types.ts +170 -0
- package/templates/api/src/generated/zod.ts +31 -0
- package/templates/api/src/routes/todos.ts +31 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Generated by @nxgt/openapi-codegen from ../../openapi.yaml. Do not edit.
|
|
2
|
+
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { zNewTodo, zNotFound, zTodo, zUnauthorized, zValidationError } from './zod';
|
|
5
|
+
|
|
6
|
+
/** A number in a path, a query or a header: digits, where z.coerce.number() would read '' as 0. */
|
|
7
|
+
const numeric = z
|
|
8
|
+
.string()
|
|
9
|
+
.regex(/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/)
|
|
10
|
+
.transform(Number);
|
|
11
|
+
|
|
12
|
+
/** Every todo */
|
|
13
|
+
export const listTodos = {
|
|
14
|
+
method: 'GET',
|
|
15
|
+
path: '/todos',
|
|
16
|
+
schema: {
|
|
17
|
+
response: {
|
|
18
|
+
200: z.array(zTodo),
|
|
19
|
+
},
|
|
20
|
+
detail: { operationId: 'listTodos', summary: 'Every todo' },
|
|
21
|
+
},
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
/** Add a todo */
|
|
25
|
+
export const createTodo = {
|
|
26
|
+
method: 'POST',
|
|
27
|
+
path: '/todos',
|
|
28
|
+
schema: {
|
|
29
|
+
body: zNewTodo,
|
|
30
|
+
response: {
|
|
31
|
+
201: zTodo,
|
|
32
|
+
400: zValidationError,
|
|
33
|
+
401: zUnauthorized,
|
|
34
|
+
},
|
|
35
|
+
detail: { operationId: 'createTodo', summary: 'Add a todo' },
|
|
36
|
+
},
|
|
37
|
+
} as const;
|
|
38
|
+
|
|
39
|
+
/** One todo */
|
|
40
|
+
export const getTodo = {
|
|
41
|
+
method: 'GET',
|
|
42
|
+
path: '/todos/:id',
|
|
43
|
+
schema: {
|
|
44
|
+
params: z.object({
|
|
45
|
+
id: numeric.pipe(z.int().min(1)),
|
|
46
|
+
}),
|
|
47
|
+
response: {
|
|
48
|
+
200: zTodo,
|
|
49
|
+
400: zValidationError,
|
|
50
|
+
404: zNotFound,
|
|
51
|
+
},
|
|
52
|
+
detail: { operationId: 'getTodo', summary: 'One todo' },
|
|
53
|
+
},
|
|
54
|
+
} as const;
|
|
55
|
+
|
|
56
|
+
/** Every operation alxia routes, by operationId: `app.route(operations.listTodos, handler)`. */
|
|
57
|
+
export const operations = {
|
|
58
|
+
listTodos,
|
|
59
|
+
createTodo,
|
|
60
|
+
getTodo,
|
|
61
|
+
} as const;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Generated by @nxgt/openapi-codegen from ../../openapi.yaml. Do not edit.
|
|
2
|
+
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { zNewTodo, zNotFound, zTodo, zUnauthorized, zValidationError } from './zod';
|
|
5
|
+
import type { ClientOperations, Operations } from './types';
|
|
6
|
+
|
|
7
|
+
/** How a server reads a parameter, and how a client writes it. */
|
|
8
|
+
export interface ParameterSpec {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly in: 'path' | 'query' | 'header';
|
|
11
|
+
readonly required: boolean;
|
|
12
|
+
/** A query list as `?a=1&a=2` (true) or `?a=1,2` (false). */
|
|
13
|
+
readonly explode: boolean;
|
|
14
|
+
/** Validated as a list: every value of a repeated query key, or one split on commas. */
|
|
15
|
+
readonly list: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface MediaSpec {
|
|
19
|
+
readonly kind: 'json' | 'form' | 'text' | 'binary' | 'sse' | 'jsonl';
|
|
20
|
+
/** Absent for binary content, which is passed through unvalidated, and for a stream. */
|
|
21
|
+
readonly schema?: z.ZodType;
|
|
22
|
+
/** `sse`: each event's data, by name: a schema for JSON, `null` for text. Absent: any event, as text. */
|
|
23
|
+
readonly events?: { readonly [event: string]: z.ZodType | null };
|
|
24
|
+
/** `jsonl`: each item. Absent: any JSON. */
|
|
25
|
+
readonly item?: z.ZodType;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** An operation of the table; `Client` is its entry of `ClientOperations`. */
|
|
29
|
+
export interface OperationSpec<Client = unknown> {
|
|
30
|
+
readonly method: 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace' | 'query';
|
|
31
|
+
readonly path: string;
|
|
32
|
+
readonly honoPath: string;
|
|
33
|
+
readonly tags: readonly string[];
|
|
34
|
+
readonly parameters: readonly ParameterSpec[];
|
|
35
|
+
/** Validates the path parameters, each read as a string. */
|
|
36
|
+
readonly param: z.ZodType;
|
|
37
|
+
/** Validates the query: a string per parameter, or every value of a list. */
|
|
38
|
+
readonly query: z.ZodType;
|
|
39
|
+
/** Validates the headers, keyed by lowercased name. */
|
|
40
|
+
readonly header: z.ZodType;
|
|
41
|
+
readonly body?: {
|
|
42
|
+
readonly required: boolean;
|
|
43
|
+
readonly content: { readonly [mediaType: string]: MediaSpec };
|
|
44
|
+
};
|
|
45
|
+
readonly responses: {
|
|
46
|
+
readonly [status: number]: { readonly [mediaType: string]: MediaSpec };
|
|
47
|
+
};
|
|
48
|
+
/** Never set: carries `Client`, so `createOpenApiClient(http, operations)` reads the spec's types off the table. */
|
|
49
|
+
readonly '~client'?: Client;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A location with no parameters declared: whatever arrives there is dropped. */
|
|
53
|
+
const none = z.object({});
|
|
54
|
+
|
|
55
|
+
/** A number in a path, a query or a header: digits, where z.coerce.number() would read '' as 0. */
|
|
56
|
+
const numeric = z
|
|
57
|
+
.string()
|
|
58
|
+
.regex(/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/)
|
|
59
|
+
.transform(Number);
|
|
60
|
+
|
|
61
|
+
export const zGetTodoParam = z.object({
|
|
62
|
+
id: numeric.pipe(z.int().min(1)),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
export const operations: {
|
|
66
|
+
readonly [K in keyof Operations]: OperationSpec<ClientOperations[K]>;
|
|
67
|
+
} = {
|
|
68
|
+
listTodos: {
|
|
69
|
+
method: 'get',
|
|
70
|
+
path: '/todos',
|
|
71
|
+
honoPath: '/todos',
|
|
72
|
+
tags: [],
|
|
73
|
+
parameters: [],
|
|
74
|
+
param: none,
|
|
75
|
+
query: none,
|
|
76
|
+
header: none,
|
|
77
|
+
responses: {
|
|
78
|
+
200: {
|
|
79
|
+
'application/json': { kind: 'json', schema: z.array(zTodo) },
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
createTodo: {
|
|
84
|
+
method: 'post',
|
|
85
|
+
path: '/todos',
|
|
86
|
+
honoPath: '/todos',
|
|
87
|
+
tags: [],
|
|
88
|
+
parameters: [],
|
|
89
|
+
param: none,
|
|
90
|
+
query: none,
|
|
91
|
+
header: none,
|
|
92
|
+
body: {
|
|
93
|
+
required: true,
|
|
94
|
+
content: {
|
|
95
|
+
'application/json': { kind: 'json', schema: zNewTodo },
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
responses: {
|
|
99
|
+
201: {
|
|
100
|
+
'application/json': { kind: 'json', schema: zTodo },
|
|
101
|
+
},
|
|
102
|
+
400: {
|
|
103
|
+
'application/json': { kind: 'json', schema: zValidationError },
|
|
104
|
+
},
|
|
105
|
+
401: {
|
|
106
|
+
'application/json': { kind: 'json', schema: zUnauthorized },
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
getTodo: {
|
|
111
|
+
method: 'get',
|
|
112
|
+
path: '/todos/{id}',
|
|
113
|
+
honoPath: '/todos/:id',
|
|
114
|
+
tags: [],
|
|
115
|
+
parameters: [
|
|
116
|
+
{ name: 'id', in: 'path', required: true, explode: false, list: false },
|
|
117
|
+
],
|
|
118
|
+
param: zGetTodoParam,
|
|
119
|
+
query: none,
|
|
120
|
+
header: none,
|
|
121
|
+
responses: {
|
|
122
|
+
200: {
|
|
123
|
+
'application/json': { kind: 'json', schema: zTodo },
|
|
124
|
+
},
|
|
125
|
+
400: {
|
|
126
|
+
'application/json': { kind: 'json', schema: zValidationError },
|
|
127
|
+
},
|
|
128
|
+
404: {
|
|
129
|
+
'application/json': { kind: 'json', schema: zNotFound },
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// Generated by @nxgt/openapi-codegen from ../../openapi.yaml. Do not edit.
|
|
2
|
+
|
|
3
|
+
import type { NewTodo, NotFound, Todo, Unauthorized, ValidationError } from './types';
|
|
4
|
+
|
|
5
|
+
export interface paths {
|
|
6
|
+
'/todos': {
|
|
7
|
+
parameters: {
|
|
8
|
+
query?: never;
|
|
9
|
+
header?: never;
|
|
10
|
+
path?: never;
|
|
11
|
+
cookie?: never;
|
|
12
|
+
};
|
|
13
|
+
/** Every todo */
|
|
14
|
+
get: operations['listTodos'];
|
|
15
|
+
put?: never;
|
|
16
|
+
/** Add a todo */
|
|
17
|
+
post: operations['createTodo'];
|
|
18
|
+
delete?: never;
|
|
19
|
+
options?: never;
|
|
20
|
+
head?: never;
|
|
21
|
+
patch?: never;
|
|
22
|
+
trace?: never;
|
|
23
|
+
};
|
|
24
|
+
'/todos/{id}': {
|
|
25
|
+
parameters: {
|
|
26
|
+
query?: never;
|
|
27
|
+
header?: never;
|
|
28
|
+
path: {
|
|
29
|
+
id: number;
|
|
30
|
+
};
|
|
31
|
+
cookie?: never;
|
|
32
|
+
};
|
|
33
|
+
/** One todo */
|
|
34
|
+
get: operations['getTodo'];
|
|
35
|
+
put?: never;
|
|
36
|
+
post?: never;
|
|
37
|
+
delete?: never;
|
|
38
|
+
options?: never;
|
|
39
|
+
head?: never;
|
|
40
|
+
patch?: never;
|
|
41
|
+
trace?: never;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type webhooks = Record<string, never>;
|
|
46
|
+
|
|
47
|
+
export interface components {
|
|
48
|
+
schemas: {
|
|
49
|
+
Todo: Todo;
|
|
50
|
+
NewTodo: NewTodo;
|
|
51
|
+
Unauthorized: Unauthorized;
|
|
52
|
+
NotFound: NotFound;
|
|
53
|
+
ValidationError: ValidationError;
|
|
54
|
+
};
|
|
55
|
+
responses: never;
|
|
56
|
+
parameters: never;
|
|
57
|
+
requestBodies: never;
|
|
58
|
+
headers: never;
|
|
59
|
+
pathItems: never;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type $defs = Record<string, never>;
|
|
63
|
+
|
|
64
|
+
export interface operations {
|
|
65
|
+
/** Every todo */
|
|
66
|
+
listTodos: {
|
|
67
|
+
parameters: {
|
|
68
|
+
query?: never;
|
|
69
|
+
header?: never;
|
|
70
|
+
path?: never;
|
|
71
|
+
cookie?: never;
|
|
72
|
+
};
|
|
73
|
+
requestBody?: never;
|
|
74
|
+
responses: {
|
|
75
|
+
/** The todos, oldest first */
|
|
76
|
+
200: {
|
|
77
|
+
headers: {
|
|
78
|
+
[name: string]: unknown;
|
|
79
|
+
};
|
|
80
|
+
content: {
|
|
81
|
+
'application/json': Todo[];
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
/** Add a todo */
|
|
87
|
+
createTodo: {
|
|
88
|
+
parameters: {
|
|
89
|
+
query?: never;
|
|
90
|
+
header?: never;
|
|
91
|
+
path?: never;
|
|
92
|
+
cookie?: never;
|
|
93
|
+
};
|
|
94
|
+
requestBody: {
|
|
95
|
+
content: {
|
|
96
|
+
'application/json': NewTodo;
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
responses: {
|
|
100
|
+
/** The todo, created */
|
|
101
|
+
201: {
|
|
102
|
+
headers: {
|
|
103
|
+
[name: string]: unknown;
|
|
104
|
+
};
|
|
105
|
+
content: {
|
|
106
|
+
'application/json': Todo;
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
/** alxia's own 400, for a request the schemas refuse */
|
|
110
|
+
400: {
|
|
111
|
+
headers: {
|
|
112
|
+
[name: string]: unknown;
|
|
113
|
+
};
|
|
114
|
+
content: {
|
|
115
|
+
'application/json': ValidationError;
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
/** No x-api-key header, or not the key */
|
|
119
|
+
401: {
|
|
120
|
+
headers: {
|
|
121
|
+
[name: string]: unknown;
|
|
122
|
+
};
|
|
123
|
+
content: {
|
|
124
|
+
'application/json': Unauthorized;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
/** One todo */
|
|
130
|
+
getTodo: {
|
|
131
|
+
parameters: {
|
|
132
|
+
query?: never;
|
|
133
|
+
header?: never;
|
|
134
|
+
path: {
|
|
135
|
+
id: number;
|
|
136
|
+
};
|
|
137
|
+
cookie?: never;
|
|
138
|
+
};
|
|
139
|
+
requestBody?: never;
|
|
140
|
+
responses: {
|
|
141
|
+
/** The todo */
|
|
142
|
+
200: {
|
|
143
|
+
headers: {
|
|
144
|
+
[name: string]: unknown;
|
|
145
|
+
};
|
|
146
|
+
content: {
|
|
147
|
+
'application/json': Todo;
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
/** alxia's own 400, for a request the schemas refuse */
|
|
151
|
+
400: {
|
|
152
|
+
headers: {
|
|
153
|
+
[name: string]: unknown;
|
|
154
|
+
};
|
|
155
|
+
content: {
|
|
156
|
+
'application/json': ValidationError;
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
/** No todo has this id */
|
|
160
|
+
404: {
|
|
161
|
+
headers: {
|
|
162
|
+
[name: string]: unknown;
|
|
163
|
+
};
|
|
164
|
+
content: {
|
|
165
|
+
'application/json': NotFound;
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// Generated by @nxgt/openapi-codegen from ../../openapi.yaml. Do not edit.
|
|
2
|
+
|
|
3
|
+
export interface Todo {
|
|
4
|
+
id: number;
|
|
5
|
+
title: string;
|
|
6
|
+
done: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface NewTodo {
|
|
10
|
+
title: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Unauthorized {
|
|
14
|
+
error: 'unauthorized';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface NotFound {
|
|
18
|
+
error: 'not_found';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ValidationError {
|
|
22
|
+
error: 'validation';
|
|
23
|
+
issues: {
|
|
24
|
+
target: 'params' | 'query' | 'headers' | 'cookies' | 'body';
|
|
25
|
+
path: (string | number)[];
|
|
26
|
+
code: string;
|
|
27
|
+
message: string;
|
|
28
|
+
}[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface GetTodoParam {
|
|
32
|
+
id: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface Operations {
|
|
36
|
+
/** Every todo */
|
|
37
|
+
listTodos: {
|
|
38
|
+
method: 'get';
|
|
39
|
+
path: '/todos';
|
|
40
|
+
honoPath: '/todos';
|
|
41
|
+
param: {};
|
|
42
|
+
query: {};
|
|
43
|
+
header: {};
|
|
44
|
+
responses: {
|
|
45
|
+
/** The todos, oldest first */
|
|
46
|
+
200: {
|
|
47
|
+
'application/json': Todo[];
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
/** Add a todo */
|
|
52
|
+
createTodo: {
|
|
53
|
+
method: 'post';
|
|
54
|
+
path: '/todos';
|
|
55
|
+
honoPath: '/todos';
|
|
56
|
+
param: {};
|
|
57
|
+
query: {};
|
|
58
|
+
header: {};
|
|
59
|
+
json: NewTodo;
|
|
60
|
+
responses: {
|
|
61
|
+
/** The todo, created */
|
|
62
|
+
201: {
|
|
63
|
+
'application/json': Todo;
|
|
64
|
+
};
|
|
65
|
+
/** alxia's own 400, for a request the schemas refuse */
|
|
66
|
+
400: {
|
|
67
|
+
'application/json': ValidationError;
|
|
68
|
+
};
|
|
69
|
+
/** No x-api-key header, or not the key */
|
|
70
|
+
401: {
|
|
71
|
+
'application/json': Unauthorized;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
/** One todo */
|
|
76
|
+
getTodo: {
|
|
77
|
+
method: 'get';
|
|
78
|
+
path: '/todos/{id}';
|
|
79
|
+
honoPath: '/todos/:id';
|
|
80
|
+
param: GetTodoParam;
|
|
81
|
+
query: {};
|
|
82
|
+
header: {};
|
|
83
|
+
responses: {
|
|
84
|
+
/** The todo */
|
|
85
|
+
200: {
|
|
86
|
+
'application/json': Todo;
|
|
87
|
+
};
|
|
88
|
+
/** alxia's own 400, for a request the schemas refuse */
|
|
89
|
+
400: {
|
|
90
|
+
'application/json': ValidationError;
|
|
91
|
+
};
|
|
92
|
+
/** No todo has this id */
|
|
93
|
+
404: {
|
|
94
|
+
'application/json': NotFound;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** What a client sends and gets back: `args` after the operationId, and each declared `reply`. */
|
|
101
|
+
export interface ClientOperations {
|
|
102
|
+
/** Every todo */
|
|
103
|
+
listTodos: {
|
|
104
|
+
method: 'get';
|
|
105
|
+
path: '/todos';
|
|
106
|
+
args: [];
|
|
107
|
+
reply: { status: 200; type: 'application/json'; data: Todo[] };
|
|
108
|
+
wire: { status: 200; type: 'application/json'; data: Todo[] };
|
|
109
|
+
};
|
|
110
|
+
/** Add a todo */
|
|
111
|
+
createTodo: {
|
|
112
|
+
method: 'post';
|
|
113
|
+
path: '/todos';
|
|
114
|
+
args: [input: {
|
|
115
|
+
json: NewTodo;
|
|
116
|
+
}];
|
|
117
|
+
reply:
|
|
118
|
+
| { status: 201; type: 'application/json'; data: Todo }
|
|
119
|
+
| { status: 400; type: 'application/json'; data: ValidationError }
|
|
120
|
+
| { status: 401; type: 'application/json'; data: Unauthorized };
|
|
121
|
+
wire:
|
|
122
|
+
| { status: 201; type: 'application/json'; data: Todo }
|
|
123
|
+
| { status: 400; type: 'application/json'; data: ValidationError }
|
|
124
|
+
| { status: 401; type: 'application/json'; data: Unauthorized };
|
|
125
|
+
};
|
|
126
|
+
/** One todo */
|
|
127
|
+
getTodo: {
|
|
128
|
+
method: 'get';
|
|
129
|
+
path: '/todos/{id}';
|
|
130
|
+
args: [input: {
|
|
131
|
+
param: {
|
|
132
|
+
id: number;
|
|
133
|
+
};
|
|
134
|
+
}];
|
|
135
|
+
reply:
|
|
136
|
+
| { status: 200; type: 'application/json'; data: Todo }
|
|
137
|
+
| { status: 400; type: 'application/json'; data: ValidationError }
|
|
138
|
+
| { status: 404; type: 'application/json'; data: NotFound };
|
|
139
|
+
wire:
|
|
140
|
+
| { status: 200; type: 'application/json'; data: Todo }
|
|
141
|
+
| { status: 400; type: 'application/json'; data: ValidationError }
|
|
142
|
+
| { status: 404; type: 'application/json'; data: NotFound };
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The operation behind each route: `routes.put(path)` finds it here. */
|
|
147
|
+
export interface OperationsByRoute {
|
|
148
|
+
'get /todos': 'listTodos';
|
|
149
|
+
'post /todos': 'createTodo';
|
|
150
|
+
'get /todos/{id}': 'getTodo';
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The paths with an operation for each method. */
|
|
154
|
+
export interface PathsByMethod {
|
|
155
|
+
get: '/todos' | '/todos/{id}';
|
|
156
|
+
put: never;
|
|
157
|
+
post: '/todos';
|
|
158
|
+
delete: never;
|
|
159
|
+
options: never;
|
|
160
|
+
head: never;
|
|
161
|
+
patch: never;
|
|
162
|
+
trace: never;
|
|
163
|
+
query: never;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The operations under each tag. */
|
|
167
|
+
export interface OperationsByTag {}
|
|
168
|
+
|
|
169
|
+
/** `PathsByMethod`, for each tag. */
|
|
170
|
+
export interface PathsByTag {}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Generated by @nxgt/openapi-codegen from ../../openapi.yaml. Do not edit.
|
|
2
|
+
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
export const zTodo = z.object({
|
|
6
|
+
id: z.int(),
|
|
7
|
+
title: z.string(),
|
|
8
|
+
done: z.boolean(),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
export const zNewTodo = z.object({
|
|
12
|
+
title: z.string().min(1),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const zUnauthorized = z.object({
|
|
16
|
+
error: z.literal('unauthorized'),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export const zNotFound = z.object({
|
|
20
|
+
error: z.literal('not_found'),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export const zValidationError = z.object({
|
|
24
|
+
error: z.literal('validation'),
|
|
25
|
+
issues: z.array(z.object({
|
|
26
|
+
target: z.enum(['params', 'query', 'headers', 'cookies', 'body']),
|
|
27
|
+
path: z.array(z.union([z.string(), z.int()])),
|
|
28
|
+
code: z.string(),
|
|
29
|
+
message: z.string(),
|
|
30
|
+
})),
|
|
31
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { defineMiddleware, defineRoutes } from "@alxia/core";
|
|
2
|
+
import { apiKey } from "../context";
|
|
3
|
+
import { operations } from "../generated/alxia";
|
|
4
|
+
|
|
5
|
+
// A middleware of the routes it is given to: it answers 401 without the key,
|
|
6
|
+
// before the body is read. openapi.yaml declares that 401.
|
|
7
|
+
const requireKey = defineMiddleware(({ request, reply }, next) =>
|
|
8
|
+
request.headers.get("x-api-key") === apiKey
|
|
9
|
+
? next()
|
|
10
|
+
: reply(401, { error: "unauthorized" as const }),
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
// Each route is an operation of openapi.yaml, generated into
|
|
14
|
+
// src/generated/alxia.ts: its method, path and schemas come from the spec,
|
|
15
|
+
// so the handler is all that is written here. The request is validated
|
|
16
|
+
// just before the handler, and its reply against the spec's responses.
|
|
17
|
+
// `todos` is the registered context's: defineRoutes() reads it, and the
|
|
18
|
+
// app that mounts these routes must give it.
|
|
19
|
+
export const todoRoutes = defineRoutes()
|
|
20
|
+
.route(operations.listTodos, ({ todos, reply }) => reply.ok(todos))
|
|
21
|
+
.route(operations.createTodo, requireKey, ({ body, todos, reply }) => {
|
|
22
|
+
const todo = { id: todos.length + 1, title: body.title, done: false };
|
|
23
|
+
todos.push(todo);
|
|
24
|
+
return reply.created(todo);
|
|
25
|
+
})
|
|
26
|
+
.route(operations.getTodo, ({ params, todos, reply }) => {
|
|
27
|
+
const todo = todos.find(({ id }) => id === params.id);
|
|
28
|
+
return todo
|
|
29
|
+
? reply.ok(todo)
|
|
30
|
+
: reply.notFound({ error: "not_found" as const });
|
|
31
|
+
});
|