counterfact 0.43.1 → 0.43.2

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.
@@ -14,6 +14,10 @@ interface Example {
14
14
 
15
15
  type MediaType = `${string}/${string}`;
16
16
 
17
+ type OmitAll<T, K extends (keyof T)[]> = {
18
+ [P in keyof T as P extends K[number] ? never : P]: T[P];
19
+ };
20
+
17
21
  type OmitValueWhenNever<Base> = Pick<
18
22
  Base,
19
23
  {
@@ -24,25 +28,34 @@ type OmitValueWhenNever<Base> = Pick<
24
28
  interface OpenApiResponse {
25
29
  content: { [key: MediaType]: OpenApiContent };
26
30
  headers: { [key: string]: OpenApiHeader };
27
- requiredHeaders: string
31
+ requiredHeaders: string;
28
32
  }
29
33
 
30
34
  interface OpenApiResponses {
31
35
  [key: string]: OpenApiResponse;
32
36
  }
33
37
 
34
- type IfHasKey<SomeObject, Key, Yes, No> = Key extends keyof SomeObject
35
- ? Yes
38
+ type IfHasKey<SomeObject, Keys extends (keyof any)[], Yes, No> = Keys extends [
39
+ infer FirstKey,
40
+ ...infer RestKeys,
41
+ ]
42
+ ? FirstKey extends keyof SomeObject
43
+ ? Yes
44
+ : RestKeys extends (keyof any)[]
45
+ ? IfHasKey<SomeObject, RestKeys, Yes, No>
46
+ : No
36
47
  : No;
37
48
 
38
49
  type MaybeShortcut<
39
- ContentType extends MediaType,
50
+ ContentTypes extends MediaType[],
40
51
  Response extends OpenApiResponse,
41
52
  > = IfHasKey<
42
53
  Response["content"],
43
- ContentType,
44
- (body: Response["content"][ContentType]["schema"]) => GenericResponseBuilder<{
45
- content: NeverIfEmpty<Omit<Response["content"], ContentType>>;
54
+ ContentTypes,
55
+ (
56
+ body: Response["content"][ArrayToUnion<ContentTypes>]["schema"],
57
+ ) => GenericResponseBuilder<{
58
+ content: NeverIfEmpty<OmitAll<Response["content"], ContentTypes>>;
46
59
  headers: Response["headers"];
47
60
  requiredHeaders: Response["requiredHeaders"];
48
61
  }>,
@@ -55,7 +68,7 @@ type MatchFunction<Response extends OpenApiResponse> = <
55
68
  ContentType extends MediaType & keyof Response["content"],
56
69
  >(
57
70
  contentType: ContentType,
58
- body: Response["content"][ContentType]["schema"]
71
+ body: Response["content"][ContentType]["schema"],
59
72
  ) => GenericResponseBuilder<{
60
73
  content: NeverIfEmpty<Omit<Response["content"], ContentType>>;
61
74
  headers: Response["headers"];
@@ -66,7 +79,7 @@ type HeaderFunction<Response extends OpenApiResponse> = <
66
79
  Header extends string & keyof Response["headers"],
67
80
  >(
68
81
  header: Header,
69
- value: Response["headers"][Header]["schema"]
82
+ value: Response["headers"][Header]["schema"],
70
83
  ) => GenericResponseBuilder<{
71
84
  content: NeverIfEmpty<Response["content"]>;
72
85
  headers: NeverIfEmpty<Omit<Response["headers"], Header>>;
@@ -92,27 +105,36 @@ interface ResponseBuilder {
92
105
  xml: (body: unknown) => ResponseBuilder;
93
106
  }
94
107
 
108
+ type ArrayToUnion<T extends readonly any[]> = T[number];
109
+
95
110
  type GenericResponseBuilderInner<
96
111
  Response extends OpenApiResponse = OpenApiResponse,
97
112
  > = OmitValueWhenNever<{
98
113
  header: [keyof Response["headers"]] extends [never]
99
114
  ? never
100
115
  : HeaderFunction<Response>;
101
- html: MaybeShortcut<"text/html", Response>;
102
- json: MaybeShortcut<"application/json", Response>;
116
+ html: MaybeShortcut<["text/html"], Response>;
117
+ json: MaybeShortcut<["application/json", "text/json", "text/x-json", "application/xml", "text/xml"], Response>;
103
118
  match: [keyof Response["content"]] extends [never]
104
119
  ? never
105
120
  : MatchFunction<Response>;
106
121
  random: [keyof Response["content"]] extends [never]
107
122
  ? never
108
123
  : RandomFunction<Response>;
109
- text: MaybeShortcut<"text/plain", Response>;
110
- xml: MaybeShortcut<"application/xml", Response>;
124
+ text: MaybeShortcut<["text/plain"], Response>;
125
+ xml: MaybeShortcut<["application/xml", "text/xml"], Response>;
111
126
  }>;
112
127
 
113
128
  type GenericResponseBuilder<
114
129
  Response extends OpenApiResponse = OpenApiResponse,
115
- > = {} extends OmitValueWhenNever<Response> ? "COUNTERFACT_RESPONSE" : keyof OmitValueWhenNever<Response> extends "headers" ? { header: HeaderFunction<Response>, ALL_REMAINING_HEADERS_ARE_OPTIONAL: "COUNTERFACT_RESPONSE" } : GenericResponseBuilderInner<Response>;
130
+ > = {} extends OmitValueWhenNever<Response>
131
+ ? "COUNTERFACT_RESPONSE"
132
+ : keyof OmitValueWhenNever<Response> extends "headers"
133
+ ? {
134
+ ALL_REMAINING_HEADERS_ARE_OPTIONAL: "COUNTERFACT_RESPONSE";
135
+ header: HeaderFunction<Response>;
136
+ }
137
+ : GenericResponseBuilderInner<Response>;
116
138
 
117
139
  type ResponseBuilderFactory<
118
140
  Responses extends OpenApiResponses = OpenApiResponses,
@@ -205,7 +227,7 @@ interface OpenApiOperation {
205
227
  };
206
228
  }
207
229
 
208
- type WideResponseBuilder = {
230
+ interface WideResponseBuilder {
209
231
  header: (body: unknown) => WideResponseBuilder;
210
232
  html: (body: unknown) => WideResponseBuilder;
211
233
  json: (body: unknown) => WideResponseBuilder;
@@ -215,26 +237,24 @@ type WideResponseBuilder = {
215
237
  xml: (body: unknown) => WideResponseBuilder;
216
238
  }
217
239
 
218
- type WideOperationArgument = {
219
- path: Record<string, string>;
220
- query: Record<string, string>;
221
- header: Record<string, string>;
240
+ interface WideOperationArgument {
222
241
  body: unknown;
223
- response: Record<number, WideResponseBuilder>;
242
+ context: unknown;
243
+ header: { [key: string]: string };
244
+ path: { [key: string]: string };
224
245
  proxy: (url: string) => { proxyUrl: string };
225
- context: unknown
226
- };
246
+ query: { [key: string]: string };
247
+ response: { [key: number]: WideResponseBuilder };
248
+ }
227
249
 
228
250
  export type {
229
251
  HttpStatusCode,
230
252
  MediaType,
253
+ OmitValueWhenNever,
231
254
  OpenApiOperation,
232
255
  OpenApiParameters,
233
256
  OpenApiResponse,
234
257
  ResponseBuilder,
235
258
  ResponseBuilderFactory,
236
259
  WideOperationArgument,
237
- OmitValueWhenNever
238
260
  };
239
-
240
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "counterfact",
3
- "version": "0.43.1",
3
+ "version": "0.43.2",
4
4
  "description": "a library for building a fake REST API for testing",
5
5
  "type": "module",
6
6
  "main": "./src/server/counterfact.js",