@counterfact/generator 0.1.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/License.md +21 -0
- package/README.md +18 -0
- package/dist/code-generator.d.ts +60 -0
- package/dist/code-generator.js +182 -0
- package/dist/coder.d.ts +107 -0
- package/dist/coder.js +132 -0
- package/dist/context-file-token.d.ts +1 -0
- package/dist/context-file-token.js +1 -0
- package/dist/ensure-directory-exists.d.ts +1 -0
- package/dist/ensure-directory-exists.js +12 -0
- package/dist/forward-slash-path.d.ts +9 -0
- package/dist/forward-slash-path.js +13 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/jsdoc.d.ts +5 -0
- package/dist/jsdoc.js +49 -0
- package/dist/openapi-path.d.ts +11 -0
- package/dist/openapi-path.js +23 -0
- package/dist/operation-coder.d.ts +23 -0
- package/dist/operation-coder.js +73 -0
- package/dist/operation-type-coder.d.ts +140 -0
- package/dist/operation-type-coder.js +387 -0
- package/dist/operation-type-name-mapping.d.ts +7 -0
- package/dist/operation-type-name-mapping.js +53 -0
- package/dist/parameter-export-type-coder.d.ts +13 -0
- package/dist/parameter-export-type-coder.js +27 -0
- package/dist/parameters-type-coder.d.ts +10 -0
- package/dist/parameters-type-coder.js +44 -0
- package/dist/printers.d.ts +2 -0
- package/dist/printers.js +10 -0
- package/dist/prune.d.ts +20 -0
- package/dist/prune.js +146 -0
- package/dist/read-only-comments.d.ts +1 -0
- package/dist/read-only-comments.js +5 -0
- package/dist/repository.d.ts +87 -0
- package/dist/repository.js +264 -0
- package/dist/requirement.d.ts +104 -0
- package/dist/requirement.js +191 -0
- package/dist/reserved-words.d.ts +1 -0
- package/dist/reserved-words.js +50 -0
- package/dist/response-type-coder.d.ts +16 -0
- package/dist/response-type-coder.js +104 -0
- package/dist/responses-type-coder.d.ts +11 -0
- package/dist/responses-type-coder.js +43 -0
- package/dist/scenario-file-generator.d.ts +31 -0
- package/dist/scenario-file-generator.js +368 -0
- package/dist/schema-coder.d.ts +10 -0
- package/dist/schema-coder.js +49 -0
- package/dist/schema-type-coder.d.ts +20 -0
- package/dist/schema-type-coder.js +121 -0
- package/dist/script.d.ts +130 -0
- package/dist/script.js +322 -0
- package/dist/specification.d.ts +40 -0
- package/dist/specification.js +58 -0
- package/dist/templates/counterfact-types/cookie-options.ts +14 -0
- package/dist/templates/counterfact-types/counterfact-response.ts +15 -0
- package/dist/templates/counterfact-types/example-names.ts +13 -0
- package/dist/templates/counterfact-types/example.ts +14 -0
- package/dist/templates/counterfact-types/generic-response-builder.ts +167 -0
- package/dist/templates/counterfact-types/http-status-code.ts +62 -0
- package/dist/templates/counterfact-types/if-has-key.ts +19 -0
- package/dist/templates/counterfact-types/index.ts +30 -0
- package/dist/templates/counterfact-types/maybe-promise.ts +6 -0
- package/dist/templates/counterfact-types/media-type.ts +6 -0
- package/dist/templates/counterfact-types/middleware.ts +87 -0
- package/dist/templates/counterfact-types/omit-all.ts +11 -0
- package/dist/templates/counterfact-types/omit-value-when-never.ts +11 -0
- package/dist/templates/counterfact-types/open-api-content.ts +8 -0
- package/dist/templates/counterfact-types/open-api-header.ts +4 -0
- package/dist/templates/counterfact-types/open-api-operation.ts +36 -0
- package/dist/templates/counterfact-types/open-api-parameters.ts +26 -0
- package/dist/templates/counterfact-types/open-api-response.ts +22 -0
- package/dist/templates/counterfact-types/random-function.ts +9 -0
- package/dist/templates/counterfact-types/response-builder-factory.ts +16 -0
- package/dist/templates/counterfact-types/response-builder.ts +36 -0
- package/dist/templates/counterfact-types/wide-operation-argument.ts +20 -0
- package/dist/templates/counterfact-types/wide-response-builder.ts +28 -0
- package/dist/type-coder.d.ts +5 -0
- package/dist/type-coder.js +9 -0
- package/dist/versions-ts-generator.d.ts +10 -0
- package/dist/versions-ts-generator.js +82 -0
- package/dist/wait-for-event.d.ts +2 -0
- package/dist/wait-for-event.js +17 -0
- package/dist/watch-options.d.ts +5 -0
- package/dist/watch-options.js +5 -0
- package/dist/windows-escape.d.ts +1 -0
- package/dist/windows-escape.js +9 -0
- package/examples/generate-routes.mjs +40 -0
- package/package.json +61 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A unique symbol used as a brand for the `COUNTERFACT_RESPONSE` type.
|
|
3
|
+
* This prevents arbitrary objects from being accidentally treated as a
|
|
4
|
+
* completed response value.
|
|
5
|
+
*/
|
|
6
|
+
const counterfactResponse = Symbol("Counterfact Response");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The terminal value type returned by the fluent response builder once all
|
|
10
|
+
* required fields (body, headers, etc.) have been provided. When a route
|
|
11
|
+
* handler returns this type, Counterfact treats the response as complete.
|
|
12
|
+
*/
|
|
13
|
+
export type COUNTERFACT_RESPONSE = {
|
|
14
|
+
[counterfactResponse]: typeof counterfactResponse;
|
|
15
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { OpenApiResponse } from "./open-api-response.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Extracts the union of named example keys defined on an OpenAPI response.
|
|
5
|
+
* Resolves to `never` when the response has no named examples.
|
|
6
|
+
* Used to constrain the argument to the `.example(name)` method on the
|
|
7
|
+
* response builder.
|
|
8
|
+
*/
|
|
9
|
+
export type ExampleNames<Response extends OpenApiResponse> = Response extends {
|
|
10
|
+
examples: infer E;
|
|
11
|
+
}
|
|
12
|
+
? keyof E & string
|
|
13
|
+
: never;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a named example defined in an OpenAPI document.
|
|
3
|
+
* Examples can be referenced by route handlers via the `.example(name)` method
|
|
4
|
+
* on the response builder.
|
|
5
|
+
*
|
|
6
|
+
* OpenAPI 3.2 adds `dataValue` as a structured alternative to `value`.
|
|
7
|
+
* When present, `dataValue` is preferred over `value`.
|
|
8
|
+
*/
|
|
9
|
+
export interface Example {
|
|
10
|
+
dataValue?: unknown;
|
|
11
|
+
description: string;
|
|
12
|
+
summary: string;
|
|
13
|
+
value?: unknown;
|
|
14
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { COUNTERFACT_RESPONSE } from "./counterfact-response.js";
|
|
2
|
+
import type { CookieOptions } from "./cookie-options.js";
|
|
3
|
+
import type { ExampleNames } from "./example-names.js";
|
|
4
|
+
import type { IfHasKey } from "./if-has-key.js";
|
|
5
|
+
import type { MediaType } from "./media-type.js";
|
|
6
|
+
import type { OmitAll } from "./omit-all.js";
|
|
7
|
+
import type { OmitValueWhenNever } from "./omit-value-when-never.js";
|
|
8
|
+
import type { OpenApiResponse } from "./open-api-response.js";
|
|
9
|
+
import type { RandomFunction } from "./random-function.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Returns `never` when `Record` is an empty object type (`{}`), signalling
|
|
13
|
+
* that there are no remaining choices available on the response builder.
|
|
14
|
+
*/
|
|
15
|
+
type NeverIfEmpty<Record> = object extends Record ? never : Record;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Extracts the union of schema types from a map of media-type content entries.
|
|
19
|
+
* Used to type the body argument of shortcut methods like `.json()` or `.html()`.
|
|
20
|
+
*/
|
|
21
|
+
type SchemasOf<T extends { [key: string]: { schema: unknown } }> = {
|
|
22
|
+
[K in keyof T]: T[K]["schema"];
|
|
23
|
+
}[keyof T];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Produces a builder method for a shortcut (e.g. `.json()`, `.html()`) when
|
|
27
|
+
* the response contains at least one of the given `ContentTypes`, and `never`
|
|
28
|
+
* otherwise. Calling the method narrows the builder by removing those content
|
|
29
|
+
* types from the remaining options.
|
|
30
|
+
*/
|
|
31
|
+
type MaybeShortcut<
|
|
32
|
+
ContentTypes extends MediaType[],
|
|
33
|
+
Response extends OpenApiResponse,
|
|
34
|
+
> = IfHasKey<
|
|
35
|
+
Response["content"],
|
|
36
|
+
ContentTypes,
|
|
37
|
+
(body: SchemasOf<Response["content"]>) => GenericResponseBuilder<{
|
|
38
|
+
content: NeverIfEmpty<OmitAll<Response["content"], ContentTypes>>;
|
|
39
|
+
headers: Response["headers"];
|
|
40
|
+
requiredHeaders: Response["requiredHeaders"];
|
|
41
|
+
}>,
|
|
42
|
+
never
|
|
43
|
+
>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The type of the `.match(contentType, body)` method on the generic response
|
|
47
|
+
* builder. Calling it narrows the builder by removing the chosen content type
|
|
48
|
+
* from the remaining options.
|
|
49
|
+
*/
|
|
50
|
+
type MatchFunction<Response extends OpenApiResponse> = <
|
|
51
|
+
ContentType extends MediaType & keyof Response["content"],
|
|
52
|
+
>(
|
|
53
|
+
contentType: ContentType,
|
|
54
|
+
body: Response["content"][ContentType]["schema"],
|
|
55
|
+
) => GenericResponseBuilder<{
|
|
56
|
+
content: NeverIfEmpty<Omit<Response["content"], ContentType>>;
|
|
57
|
+
headers: Response["headers"];
|
|
58
|
+
requiredHeaders: Response["requiredHeaders"];
|
|
59
|
+
}>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The type of the `.header(name, value)` method on the generic response
|
|
63
|
+
* builder. Calling it narrows the builder by removing the satisfied header
|
|
64
|
+
* from the set of required headers.
|
|
65
|
+
*/
|
|
66
|
+
type HeaderFunction<Response extends OpenApiResponse> = <
|
|
67
|
+
Header extends string & keyof Response["headers"],
|
|
68
|
+
>(
|
|
69
|
+
header: Header,
|
|
70
|
+
value: Response["headers"][Header]["schema"],
|
|
71
|
+
) => GenericResponseBuilder<{
|
|
72
|
+
content: NeverIfEmpty<Response["content"]>;
|
|
73
|
+
headers: NeverIfEmpty<Omit<Response["headers"], Header>>;
|
|
74
|
+
requiredHeaders: Exclude<Response["requiredHeaders"], Header>;
|
|
75
|
+
}>;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The inner shape of the generic response builder, listing all methods that
|
|
79
|
+
* are currently available given the remaining response constraints.
|
|
80
|
+
* Methods whose type resolves to `never` are stripped by `OmitValueWhenNever`.
|
|
81
|
+
*
|
|
82
|
+
* Note: `[T] extends [never]` (non-distributive tuple wrapping) is used
|
|
83
|
+
* alongside `[keyof T] extends [never]` to correctly handle both `T = never`
|
|
84
|
+
* (spec-generated no-body) and `T = {}` (all content types consumed) cases.
|
|
85
|
+
* TypeScript evaluates `keyof never` as `string | number | symbol`, so a
|
|
86
|
+
* direct `[keyof never] extends [never]` check would incorrectly return false.
|
|
87
|
+
*/
|
|
88
|
+
export type GenericResponseBuilderInner<
|
|
89
|
+
Response extends OpenApiResponse = OpenApiResponse,
|
|
90
|
+
> = OmitValueWhenNever<{
|
|
91
|
+
binary: MaybeShortcut<["application/octet-stream"], Response>;
|
|
92
|
+
cookie: (
|
|
93
|
+
name: string,
|
|
94
|
+
value: string,
|
|
95
|
+
options?: CookieOptions,
|
|
96
|
+
) => GenericResponseBuilder<Response>;
|
|
97
|
+
empty: [Response["content"]] extends [never]
|
|
98
|
+
? () => COUNTERFACT_RESPONSE
|
|
99
|
+
: [keyof Response["content"]] extends [never]
|
|
100
|
+
? () => COUNTERFACT_RESPONSE
|
|
101
|
+
: never;
|
|
102
|
+
header: [Response["headers"]] extends [never]
|
|
103
|
+
? never
|
|
104
|
+
: [keyof Response["headers"]] extends [never]
|
|
105
|
+
? never
|
|
106
|
+
: HeaderFunction<Response>;
|
|
107
|
+
html: MaybeShortcut<["text/html"], Response>;
|
|
108
|
+
json: MaybeShortcut<
|
|
109
|
+
[
|
|
110
|
+
"application/json",
|
|
111
|
+
"text/json",
|
|
112
|
+
"text/x-json",
|
|
113
|
+
"application/xml",
|
|
114
|
+
"text/xml",
|
|
115
|
+
],
|
|
116
|
+
Response
|
|
117
|
+
>;
|
|
118
|
+
match: [Response["content"]] extends [never]
|
|
119
|
+
? never
|
|
120
|
+
: [keyof Response["content"]] extends [never]
|
|
121
|
+
? never
|
|
122
|
+
: MatchFunction<Response>;
|
|
123
|
+
random: [Response["content"]] extends [never]
|
|
124
|
+
? never
|
|
125
|
+
: [keyof Response["content"]] extends [never]
|
|
126
|
+
? never
|
|
127
|
+
: RandomFunction;
|
|
128
|
+
example: [ExampleNames<Response>] extends [never]
|
|
129
|
+
? never
|
|
130
|
+
: (name: ExampleNames<Response>) => COUNTERFACT_RESPONSE;
|
|
131
|
+
text: MaybeShortcut<["text/plain"], Response>;
|
|
132
|
+
xml: MaybeShortcut<["application/xml", "text/xml"], Response>;
|
|
133
|
+
stream: MaybeShortcut<
|
|
134
|
+
["text/event-stream", "application/jsonl", "application/json-seq"],
|
|
135
|
+
Response
|
|
136
|
+
>;
|
|
137
|
+
}>;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The strongly-typed, fluent response builder generated for each operation in
|
|
141
|
+
* a route handler. Its available methods are derived from the OpenAPI response
|
|
142
|
+
* schema: as methods are called, the builder type narrows until all required
|
|
143
|
+
* content and headers have been provided, at which point it resolves to
|
|
144
|
+
* `COUNTERFACT_RESPONSE`.
|
|
145
|
+
*
|
|
146
|
+
* When a Response type carries an `examples` key it is a spec-generated
|
|
147
|
+
* response (either the initial no-body builder or a builder that still has
|
|
148
|
+
* content/headers to satisfy). Those always go through
|
|
149
|
+
* `GenericResponseBuilderInner`, which exposes `empty()` when `content` is
|
|
150
|
+
* `never`.
|
|
151
|
+
*
|
|
152
|
+
* When a Response type has no `examples` key it is a narrowed type produced
|
|
153
|
+
* by a method call (e.g. `.json()` sets the body and returns a type without
|
|
154
|
+
* `examples`). Those go through the existing collapse logic so that
|
|
155
|
+
* fully-satisfied responses resolve directly to `COUNTERFACT_RESPONSE`.
|
|
156
|
+
*/
|
|
157
|
+
export type GenericResponseBuilder<
|
|
158
|
+
Response extends OpenApiResponse = OpenApiResponse,
|
|
159
|
+
> = "examples" extends keyof Response
|
|
160
|
+
? GenericResponseBuilderInner<Response>
|
|
161
|
+
: object extends OmitValueWhenNever<Omit<Response, "examples">>
|
|
162
|
+
? COUNTERFACT_RESPONSE
|
|
163
|
+
: keyof OmitValueWhenNever<Omit<Response, "examples">> extends "headers"
|
|
164
|
+
? COUNTERFACT_RESPONSE & {
|
|
165
|
+
header: HeaderFunction<Response>;
|
|
166
|
+
}
|
|
167
|
+
: GenericResponseBuilderInner<Response>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A union of all standard HTTP status codes.
|
|
3
|
+
* Used to constrain the status code argument in response builder calls and
|
|
4
|
+
* generated route handler types.
|
|
5
|
+
*/
|
|
6
|
+
export type HttpStatusCode =
|
|
7
|
+
| 100
|
|
8
|
+
| 101
|
|
9
|
+
| 102
|
|
10
|
+
| 200
|
|
11
|
+
| 201
|
|
12
|
+
| 202
|
|
13
|
+
| 203
|
|
14
|
+
| 204
|
|
15
|
+
| 205
|
|
16
|
+
| 206
|
|
17
|
+
| 207
|
|
18
|
+
| 226
|
|
19
|
+
| 300
|
|
20
|
+
| 301
|
|
21
|
+
| 302
|
|
22
|
+
| 303
|
|
23
|
+
| 304
|
|
24
|
+
| 305
|
|
25
|
+
| 307
|
|
26
|
+
| 308
|
|
27
|
+
| 400
|
|
28
|
+
| 401
|
|
29
|
+
| 402
|
|
30
|
+
| 403
|
|
31
|
+
| 404
|
|
32
|
+
| 405
|
|
33
|
+
| 406
|
|
34
|
+
| 407
|
|
35
|
+
| 408
|
|
36
|
+
| 409
|
|
37
|
+
| 410
|
|
38
|
+
| 411
|
|
39
|
+
| 412
|
|
40
|
+
| 413
|
|
41
|
+
| 414
|
|
42
|
+
| 415
|
|
43
|
+
| 416
|
|
44
|
+
| 417
|
|
45
|
+
| 418
|
|
46
|
+
| 422
|
|
47
|
+
| 423
|
|
48
|
+
| 424
|
|
49
|
+
| 426
|
|
50
|
+
| 428
|
|
51
|
+
| 429
|
|
52
|
+
| 431
|
|
53
|
+
| 451
|
|
54
|
+
| 500
|
|
55
|
+
| 501
|
|
56
|
+
| 502
|
|
57
|
+
| 503
|
|
58
|
+
| 504
|
|
59
|
+
| 505
|
|
60
|
+
| 506
|
|
61
|
+
| 507
|
|
62
|
+
| 511;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conditional type that resolves to `Yes` when `SomeObject` has at least one
|
|
3
|
+
* key that contains any string from `Keys` as a substring, and `No` otherwise.
|
|
4
|
+
* Used to determine whether a shortcut method (e.g. `.json()`, `.html()`)
|
|
5
|
+
* should be present on the response builder for a given response type.
|
|
6
|
+
*/
|
|
7
|
+
export type IfHasKey<
|
|
8
|
+
SomeObject,
|
|
9
|
+
Keys extends readonly string[],
|
|
10
|
+
Yes,
|
|
11
|
+
No,
|
|
12
|
+
> = Keys extends [
|
|
13
|
+
infer FirstKey extends string,
|
|
14
|
+
...infer RestKeys extends string[],
|
|
15
|
+
]
|
|
16
|
+
? Extract<keyof SomeObject, `${string}${FirstKey}${string}`> extends never
|
|
17
|
+
? IfHasKey<SomeObject, RestKeys, Yes, No>
|
|
18
|
+
: Yes
|
|
19
|
+
: No;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type { CookieOptions } from "./cookie-options.js";
|
|
2
|
+
export type { COUNTERFACT_RESPONSE } from "./counterfact-response.js";
|
|
3
|
+
export type { ExampleNames } from "./example-names.js";
|
|
4
|
+
export type {
|
|
5
|
+
GenericResponseBuilder,
|
|
6
|
+
GenericResponseBuilderInner,
|
|
7
|
+
} from "./generic-response-builder.js";
|
|
8
|
+
export type { HttpStatusCode } from "./http-status-code.js";
|
|
9
|
+
export type { IfHasKey } from "./if-has-key.js";
|
|
10
|
+
export type { MaybePromise } from "./maybe-promise.js";
|
|
11
|
+
export type { MediaType } from "./media-type.js";
|
|
12
|
+
export type {
|
|
13
|
+
Middleware,
|
|
14
|
+
MiddlewareRequest,
|
|
15
|
+
MiddlewareResponse,
|
|
16
|
+
MiddlewareResponseBuilderFactory,
|
|
17
|
+
MiddlewareResponseObject,
|
|
18
|
+
MiddlewareTools,
|
|
19
|
+
RespondTo,
|
|
20
|
+
} from "./middleware.js";
|
|
21
|
+
export type { OmitAll } from "./omit-all.js";
|
|
22
|
+
export type { OmitValueWhenNever } from "./omit-value-when-never.js";
|
|
23
|
+
export type { OpenApiHeader } from "./open-api-header.js";
|
|
24
|
+
export type { OpenApiOperation } from "./open-api-operation.js";
|
|
25
|
+
export type { OpenApiParameters } from "./open-api-parameters.js";
|
|
26
|
+
export type { OpenApiResponse } from "./open-api-response.js";
|
|
27
|
+
export type { ResponseBuilder } from "./response-builder.js";
|
|
28
|
+
export type { ResponseBuilderFactory } from "./response-builder-factory.js";
|
|
29
|
+
export type { WideOperationArgument } from "./wide-operation-argument.js";
|
|
30
|
+
export type { WideResponseBuilder } from "./wide-response-builder.js";
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { COUNTERFACT_RESPONSE } from "./counterfact-response.js";
|
|
2
|
+
import type { HttpStatusCode } from "./http-status-code.js";
|
|
3
|
+
import type { MaybePromise } from "./maybe-promise.js";
|
|
4
|
+
import type { MediaType } from "./media-type.js";
|
|
5
|
+
import type { ResponseBuilder } from "./response-builder.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The untyped response object that Counterfact passes between middleware and
|
|
9
|
+
* route handlers.
|
|
10
|
+
*/
|
|
11
|
+
export interface MiddlewareResponseObject {
|
|
12
|
+
appendedHeaders?: [string, string][];
|
|
13
|
+
body?: AsyncIterable<unknown> | Uint8Array | string;
|
|
14
|
+
content?: { body: unknown; type: MediaType }[];
|
|
15
|
+
contentType?: string;
|
|
16
|
+
headers?: { [key: string]: number | string | string[] };
|
|
17
|
+
status?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Utilities available to middleware through `$.tools`. */
|
|
21
|
+
export interface MiddlewareTools {
|
|
22
|
+
accepts: (contentType: string) => boolean;
|
|
23
|
+
oneOf: <Item>(array: Item[]) => Item;
|
|
24
|
+
randomFromSchema: (schema: object) => Promise<unknown>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A response-builder factory with every standard status code available. */
|
|
28
|
+
export type MiddlewareResponseBuilderFactory = {
|
|
29
|
+
[StatusCode in HttpStatusCode]: ResponseBuilder;
|
|
30
|
+
} & { [key: number | `${number} ${string}`]: ResponseBuilder };
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The request argument passed to a custom middleware function.
|
|
34
|
+
*
|
|
35
|
+
* Supply the context type from the nearest `_.context.ts` file to retain
|
|
36
|
+
* autocomplete and checking for `$.context`.
|
|
37
|
+
*/
|
|
38
|
+
export interface MiddlewareRequest<Context = unknown> {
|
|
39
|
+
auth?: { apiKey?: string; password?: string; username?: string };
|
|
40
|
+
body?: unknown;
|
|
41
|
+
context: Context;
|
|
42
|
+
cookie: { [name: string]: string | undefined };
|
|
43
|
+
delay: (milliseconds: number, maxMilliseconds?: number) => Promise<void>;
|
|
44
|
+
headers: { [key: string]: boolean | number | string };
|
|
45
|
+
matchedPath?: string;
|
|
46
|
+
path?: { [key: string]: boolean | number | string };
|
|
47
|
+
proxy: (url: string) => Promise<{
|
|
48
|
+
body: string;
|
|
49
|
+
contentType: string;
|
|
50
|
+
headers: { [key: string]: string };
|
|
51
|
+
status: number;
|
|
52
|
+
}>;
|
|
53
|
+
query: { [key: string]: unknown };
|
|
54
|
+
response: MiddlewareResponseBuilderFactory;
|
|
55
|
+
tools: MiddlewareTools;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A response value that middleware may return or receive from `respondTo`. */
|
|
59
|
+
export type MiddlewareResponse =
|
|
60
|
+
COUNTERFACT_RESPONSE | MiddlewareResponseObject | ResponseBuilder;
|
|
61
|
+
|
|
62
|
+
/** Passes a request to the next matching middleware or route handler. */
|
|
63
|
+
export type RespondTo<Context = unknown> = (
|
|
64
|
+
request: MiddlewareRequest<Context>,
|
|
65
|
+
) => Promise<MiddlewareResponse>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A custom Counterfact middleware function.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import type { Middleware } from "../counterfact-types/index.js";
|
|
73
|
+
* import type { Context } from "./_.context.js";
|
|
74
|
+
*
|
|
75
|
+
* export const middleware: Middleware<Context> = async ($, respondTo) => {
|
|
76
|
+
* if (!$.context.isAuthorized($.auth?.apiKey)) {
|
|
77
|
+
* return $.response[401].json({ error: "Unauthorized" });
|
|
78
|
+
* }
|
|
79
|
+
*
|
|
80
|
+
* return respondTo($);
|
|
81
|
+
* };
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export type Middleware<Context = unknown> = (
|
|
85
|
+
request: MiddlewareRequest<Context>,
|
|
86
|
+
respondTo: RespondTo<Context>,
|
|
87
|
+
) => MaybePromise<MiddlewareResponse>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes all keys from `T` whose names contain any of the strings in `K`
|
|
3
|
+
* as a substring (prefix, suffix, or exact match).
|
|
4
|
+
* Used internally to narrow the set of available content-type methods on the
|
|
5
|
+
* response builder after one has already been called.
|
|
6
|
+
*/
|
|
7
|
+
export type OmitAll<T, K extends readonly string[]> = {
|
|
8
|
+
[
|
|
9
|
+
P in keyof T as P extends `${string}${K[number]}${string}` ? never : P
|
|
10
|
+
]: T[P];
|
|
11
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new type from `Base` that omits any keys whose value type is
|
|
3
|
+
* `never`. This is used to strip unavailable builder methods (those that
|
|
4
|
+
* don't apply to the current response shape) from the fluent response builder.
|
|
5
|
+
*/
|
|
6
|
+
export type OmitValueWhenNever<Base> = Pick<
|
|
7
|
+
Base,
|
|
8
|
+
{
|
|
9
|
+
[Key in keyof Base]: [Base[Key]] extends [never] ? never : Key;
|
|
10
|
+
}[keyof Base]
|
|
11
|
+
>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Example } from "./example.js";
|
|
2
|
+
import type { OpenApiHeader } from "./open-api-header.js";
|
|
3
|
+
import type { OpenApiParameters } from "./open-api-parameters.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Describes a single HTTP operation (e.g. `GET /pets`) as defined in an
|
|
7
|
+
* OpenAPI document. Used internally to derive the strongly-typed argument
|
|
8
|
+
* and response builder types for generated route handler functions.
|
|
9
|
+
*/
|
|
10
|
+
export interface OpenApiOperation {
|
|
11
|
+
parameters?: OpenApiParameters[];
|
|
12
|
+
produces?: string[];
|
|
13
|
+
requestBody?: {
|
|
14
|
+
content?: {
|
|
15
|
+
[mediaType: string]: {
|
|
16
|
+
schema: { [key: string]: unknown };
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
required?: boolean;
|
|
20
|
+
};
|
|
21
|
+
responses: {
|
|
22
|
+
[status: string]: {
|
|
23
|
+
content?: {
|
|
24
|
+
[type: number | string]: {
|
|
25
|
+
examples?: { [key: string]: Example };
|
|
26
|
+
schema: { [key: string]: unknown };
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
examples?: { [key: string]: unknown };
|
|
30
|
+
headers?: {
|
|
31
|
+
[name: string]: OpenApiHeader;
|
|
32
|
+
};
|
|
33
|
+
schema?: { [key: string]: unknown };
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Describes a single parameter (path, query, header, cookie, body, or
|
|
3
|
+
* formData) as defined in an OpenAPI document. Used internally to type the
|
|
4
|
+
* `path`, `query`, `headers`, and `body` properties of a route handler's
|
|
5
|
+
* argument object.
|
|
6
|
+
*/
|
|
7
|
+
export interface OpenApiParameters {
|
|
8
|
+
explode?: boolean;
|
|
9
|
+
in:
|
|
10
|
+
| "body"
|
|
11
|
+
| "cookie"
|
|
12
|
+
| "formData"
|
|
13
|
+
| "header"
|
|
14
|
+
| "path"
|
|
15
|
+
| "query"
|
|
16
|
+
| "querystring";
|
|
17
|
+
name: string;
|
|
18
|
+
required?: boolean;
|
|
19
|
+
schema?: {
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
properties?: Record<string, unknown>;
|
|
22
|
+
type?: string;
|
|
23
|
+
};
|
|
24
|
+
style?: string;
|
|
25
|
+
type?: "string" | "number" | "integer" | "boolean";
|
|
26
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { MediaType } from "./media-type.js";
|
|
2
|
+
import type { OpenApiContent } from "./open-api-content.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Describes a single HTTP response as modelled in an OpenAPI document.
|
|
6
|
+
* Contains the allowed content types, optional named examples, and the
|
|
7
|
+
* required/optional response headers for that response.
|
|
8
|
+
*/
|
|
9
|
+
export interface OpenApiResponse {
|
|
10
|
+
content: { [key: MediaType]: OpenApiContent };
|
|
11
|
+
examples?: { [key: string]: unknown };
|
|
12
|
+
headers: { [key: string]: { schema: unknown } };
|
|
13
|
+
requiredHeaders: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A map of HTTP status codes (or `"default"`) to their corresponding
|
|
18
|
+
* `OpenApiResponse` definitions for a given operation.
|
|
19
|
+
*/
|
|
20
|
+
export interface OpenApiResponses {
|
|
21
|
+
[key: string]: OpenApiResponse;
|
|
22
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { COUNTERFACT_RESPONSE } from "./counterfact-response.js";
|
|
2
|
+
import type { MaybePromise } from "./maybe-promise.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The type of the `.random()` method on the response builder.
|
|
6
|
+
* When called, it randomly selects one of the available content-type examples
|
|
7
|
+
* and returns a completed `COUNTERFACT_RESPONSE`.
|
|
8
|
+
*/
|
|
9
|
+
export type RandomFunction = () => MaybePromise<COUNTERFACT_RESPONSE>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { GenericResponseBuilder } from "./generic-response-builder.js";
|
|
2
|
+
import type { OpenApiResponses } from "./open-api-response.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Maps each HTTP status code (or `"default"`) in an OpenAPI operation's
|
|
6
|
+
* response definitions to the corresponding `GenericResponseBuilder`.
|
|
7
|
+
* This is the type of the `response` property in a generated route handler's
|
|
8
|
+
* argument object, allowing handlers to call e.g. `response[200].json(body)`.
|
|
9
|
+
*/
|
|
10
|
+
export type ResponseBuilderFactory<
|
|
11
|
+
Responses extends OpenApiResponses = OpenApiResponses,
|
|
12
|
+
> = {
|
|
13
|
+
[StatusCode in keyof Responses]: GenericResponseBuilder<
|
|
14
|
+
Responses[StatusCode]
|
|
15
|
+
>;
|
|
16
|
+
} & { [key: string]: GenericResponseBuilder<Responses["default"]> };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { CookieOptions } from "./cookie-options.js";
|
|
2
|
+
import type { MaybePromise } from "./maybe-promise.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A loosely-typed, chainable response builder used in non-generated contexts
|
|
6
|
+
* (e.g. middleware or wide/catch-all route handlers) where the exact response
|
|
7
|
+
* shape is not statically known. For generated route handlers, prefer the
|
|
8
|
+
* strongly-typed `GenericResponseBuilder`.
|
|
9
|
+
*/
|
|
10
|
+
export interface ResponseBuilder {
|
|
11
|
+
[status: number | `${number} ${string}`]: ResponseBuilder;
|
|
12
|
+
binary: (body: Uint8Array | string) => ResponseBuilder;
|
|
13
|
+
content?: { body: unknown; type: string }[];
|
|
14
|
+
cookie: (
|
|
15
|
+
name: string,
|
|
16
|
+
value: string,
|
|
17
|
+
options?: CookieOptions,
|
|
18
|
+
) => ResponseBuilder;
|
|
19
|
+
empty: () => ResponseBuilder;
|
|
20
|
+
example: (name: string) => ResponseBuilder;
|
|
21
|
+
header: (name: string, value: string) => ResponseBuilder;
|
|
22
|
+
headers: { [name: string]: string | string[] };
|
|
23
|
+
html: (body: unknown) => ResponseBuilder;
|
|
24
|
+
json: (body: unknown) => ResponseBuilder;
|
|
25
|
+
match: (contentType: string, body: unknown) => ResponseBuilder;
|
|
26
|
+
random: () => MaybePromise<ResponseBuilder>;
|
|
27
|
+
randomLegacy: () => MaybePromise<ResponseBuilder>;
|
|
28
|
+
status?: number;
|
|
29
|
+
stream: (iterable: AsyncIterable<unknown>) => {
|
|
30
|
+
body: AsyncIterable<unknown>;
|
|
31
|
+
contentType: string;
|
|
32
|
+
status?: number;
|
|
33
|
+
};
|
|
34
|
+
text: (body: unknown) => ResponseBuilder;
|
|
35
|
+
xml: (body: unknown) => ResponseBuilder;
|
|
36
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { HttpStatusCode } from "./http-status-code.js";
|
|
2
|
+
import type { WideResponseBuilder } from "./wide-response-builder.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The loosely-typed argument object passed to wide (catch-all) route handlers.
|
|
6
|
+
* Unlike the generated operation argument types, all fields are typed as
|
|
7
|
+
* `unknown` or broad index signatures. Use this when writing handlers that
|
|
8
|
+
* should accept any request without compile-time schema enforcement.
|
|
9
|
+
*/
|
|
10
|
+
export interface WideOperationArgument {
|
|
11
|
+
body: unknown;
|
|
12
|
+
context: unknown;
|
|
13
|
+
headers: { [key: string]: string };
|
|
14
|
+
path: { [key: string]: string };
|
|
15
|
+
proxy: (url: string) => { proxyUrl: string };
|
|
16
|
+
query: { [key: string]: string };
|
|
17
|
+
response: { [StatusCode in HttpStatusCode]: WideResponseBuilder } & {
|
|
18
|
+
[key: number]: WideResponseBuilder;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { COUNTERFACT_RESPONSE } from "./counterfact-response.js";
|
|
2
|
+
import type { CookieOptions } from "./cookie-options.js";
|
|
3
|
+
import type { MaybePromise } from "./maybe-promise.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A loosely-typed response builder used in wide (catch-all) route handlers
|
|
7
|
+
* where the response shape is not known at compile time. Unlike the generated
|
|
8
|
+
* `GenericResponseBuilder`, this interface accepts `unknown` for all body
|
|
9
|
+
* arguments and does not enforce content-type constraints.
|
|
10
|
+
*/
|
|
11
|
+
export type WideResponseBuilder = COUNTERFACT_RESPONSE & {
|
|
12
|
+
binary: (body: Uint8Array | string) => WideResponseBuilder;
|
|
13
|
+
empty: () => WideResponseBuilder;
|
|
14
|
+
example: (name: string) => WideResponseBuilder;
|
|
15
|
+
cookie: (
|
|
16
|
+
name: string,
|
|
17
|
+
value: string,
|
|
18
|
+
options?: CookieOptions,
|
|
19
|
+
) => WideResponseBuilder;
|
|
20
|
+
header: (name: string, value: string) => WideResponseBuilder;
|
|
21
|
+
html: (body: unknown) => WideResponseBuilder;
|
|
22
|
+
json: (body: unknown) => WideResponseBuilder;
|
|
23
|
+
match: (contentType: string, body: unknown) => WideResponseBuilder;
|
|
24
|
+
random: () => MaybePromise<WideResponseBuilder>;
|
|
25
|
+
text: (body: unknown) => WideResponseBuilder;
|
|
26
|
+
xml: (body: unknown) => WideResponseBuilder;
|
|
27
|
+
stream: (body: AsyncIterable<unknown>) => COUNTERFACT_RESPONSE;
|
|
28
|
+
};
|