diff --git a/.changeset/late-terms-taste.md b/.changeset/late-terms-taste.md new file mode 100644 index 00000000..dd5392be --- /dev/null +++ b/.changeset/late-terms-taste.md @@ -0,0 +1,5 @@ +--- +"counterfact": patch +--- + +fix types: $.header should be $.headers (#1160) diff --git a/docs/usage.md b/docs/usage.md index 3a29bd70..e1b47e44 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -121,11 +121,11 @@ return $.response[200].header("x-coolness-level", 10).text("This is cool!")`. ### Request parameters -Most of the time, the server's response depends on input from various parts of the request, which are accessible through `$.path`, `$.query`, `$.header`, and `$.body`. The best way to explain is with an example: +Most of the time, the server's response depends on input from various parts of the request, which are accessible through `$.path`, `$.query`, `$.headers`, and `$.body`. The best way to explain is with an example: ```ts export const GET: HTTP_GET = ($) => { - if ($.header['x-token'] !== 'super-secret') { + if ($.headers['x-token'] !== 'super-secret') { return $.response[401].text('unauthorized'); } diff --git a/src/server/types.ts b/src/server/types.ts index 066ec344..20396880 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -259,7 +259,7 @@ interface WideResponseBuilder { interface WideOperationArgument { body: unknown; context: unknown; - header: { [key: string]: string }; + headers: { [key: string]: string }; path: { [key: string]: string }; proxy: (url: string) => { proxyUrl: string }; query: { [key: string]: string }; diff --git a/src/typescript-generator/operation-type-coder.js b/src/typescript-generator/operation-type-coder.js index ffe581ac..8f3fd984 100644 --- a/src/typescript-generator/operation-type-coder.js +++ b/src/typescript-generator/operation-type-coder.js @@ -110,7 +110,7 @@ export class OperationTypeCoder extends TypeCoder { const pathType = new ParametersTypeCoder(parameters, "path").write(script); - const headerType = new ParametersTypeCoder(parameters, "header").write( + const headersType = new ParametersTypeCoder(parameters, "header").write( script, ); @@ -139,7 +139,7 @@ export class OperationTypeCoder extends TypeCoder { const proxyType = "(url: string) => COUNTERFACT_RESPONSE"; - return `($: OmitValueWhenNever<{ query: ${queryType}, path: ${pathType}, header: ${headerType}, body: ${bodyType}, context: ${contextTypeImportName}, response: ${responseType}, x: ${xType}, proxy: ${proxyType}, user: ${this.userType()} }>) => ${this.responseTypes( + return `($: OmitValueWhenNever<{ query: ${queryType}, path: ${pathType}, headers: ${headersType}, body: ${bodyType}, context: ${contextTypeImportName}, response: ${responseType}, x: ${xType}, proxy: ${proxyType}, user: ${this.userType()} }>) => ${this.responseTypes( script, )} | { status: 415, contentType: "text/plain", body: string } | COUNTERFACT_RESPONSE | { ALL_REMAINING_HEADERS_ARE_OPTIONAL: COUNTERFACT_RESPONSE }`; } diff --git a/test/typescript-generator/__snapshots__/generate.test.ts.snap b/test/typescript-generator/__snapshots__/generate.test.ts.snap index 887459c8..9c531892 100644 --- a/test/typescript-generator/__snapshots__/generate.test.ts.snap +++ b/test/typescript-generator/__snapshots__/generate.test.ts.snap @@ -30,7 +30,7 @@ export type HTTP_POST = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: Pet; context: Context; response: ResponseBuilderFactory<{ @@ -78,7 +78,7 @@ export type HTTP_PUT = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: Pet; context: Context; response: ResponseBuilderFactory<{ @@ -165,7 +165,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: { status?: "available" | "pending" | "sold" }; path: never; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -236,7 +236,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: { tags?: Array }; path: never; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -317,7 +317,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: never; path: { petId: number }; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -373,7 +373,7 @@ export type HTTP_POST = ( $: OmitValueWhenNever<{ query: { name?: number; status?: string }; path: { petId: number }; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -399,7 +399,7 @@ export type HTTP_DELETE = ( $: OmitValueWhenNever<{ query: never; path: { petId: string }; - header: { api_key?: string }; + headers: { api_key?: string }; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -448,7 +448,7 @@ export type HTTP_POST = ( $: OmitValueWhenNever<{ query: { additionalMetadata?: number }; path: { petId: number }; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -502,7 +502,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -557,7 +557,7 @@ export type HTTP_POST = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: Order; context: Context; response: ResponseBuilderFactory<{ @@ -625,7 +625,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: never; path: { orderId: number }; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -681,7 +681,7 @@ export type HTTP_DELETE = ( $: OmitValueWhenNever<{ query: never; path: { orderId: number }; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -739,7 +739,7 @@ export type HTTP_POST = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: User; context: Context; response: ResponseBuilderFactory<{ @@ -803,7 +803,7 @@ export type HTTP_POST = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: Array; context: Context; response: ResponseBuilderFactory<{ @@ -873,7 +873,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: { username?: string; password?: string }; path: never; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -947,7 +947,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -1007,7 +1007,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: never; path: { username: string }; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -1063,7 +1063,7 @@ export type HTTP_PUT = ( $: OmitValueWhenNever<{ query: never; path: { username: string }; - header: never; + headers: never; body: User; context: Context; response: ResponseBuilderFactory<{ @@ -1089,7 +1089,7 @@ export type HTTP_DELETE = ( $: OmitValueWhenNever<{ query: never; path: { username: string }; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -1212,7 +1212,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -1267,7 +1267,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -1322,7 +1322,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -1377,7 +1377,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: never; path: { name: string }; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ @@ -1436,7 +1436,7 @@ export type HTTP_GET = ( $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: never; context: Context; response: ResponseBuilderFactory<{ diff --git a/test/typescript-generator/__snapshots__/operation-type-coder.test.js.snap b/test/typescript-generator/__snapshots__/operation-type-coder.test.js.snap index d19be651..9d12a350 100644 --- a/test/typescript-generator/__snapshots__/operation-type-coder.test.js.snap +++ b/test/typescript-generator/__snapshots__/operation-type-coder.test.js.snap @@ -5,7 +5,7 @@ exports[`an OperationTypeCoder falls back to root level produces (OpenAPI 2) 1`] $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: never; context: ExternalType; response: ResponseBuilderFactory<{ @@ -40,7 +40,7 @@ exports[`an OperationTypeCoder generates a complex post operation (OpenAPI 2 wit $: OmitValueWhenNever<{ query: { name?: string }; path: { id?: string }; - header: { name?: string }; + headers: { name?: string }; body: Type; context: ExternalType; response: ResponseBuilderFactory<{ @@ -103,7 +103,7 @@ exports[`an OperationTypeCoder generates a complex post operation (OpenAPI 2) 1` $: OmitValueWhenNever<{ query: { name?: string }; path: { id?: string }; - header: { name?: string }; + headers: { name?: string }; body: Type; context: ExternalType; response: ResponseBuilderFactory<{ @@ -166,7 +166,7 @@ exports[`an OperationTypeCoder generates a complex post operation (OpenAPI 2, co $: OmitValueWhenNever<{ query: { name?: string }; path: { id?: string }; - header: { name?: string }; + headers: { name?: string }; body: Type; context: ExternalType; response: ResponseBuilderFactory<{ @@ -229,7 +229,7 @@ exports[`an OperationTypeCoder generates a complex post operation 1`] = ` $: OmitValueWhenNever<{ query: { name?: string }; path: { id?: string }; - header: { name?: string }; + headers: { name?: string }; body: Type; context: ExternalType; response: ResponseBuilderFactory<{ @@ -292,7 +292,7 @@ exports[`an OperationTypeCoder generates a simple get operation (OpenAPI 2) 1`] $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: never; context: ExternalType; response: ResponseBuilderFactory<{ @@ -327,7 +327,7 @@ exports[`an OperationTypeCoder generates a simple get operation 1`] = ` $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: never; context: ExternalType; response: ResponseBuilderFactory<{ @@ -362,7 +362,7 @@ exports[`an OperationTypeCoder generates an auth object for basic authentication $: OmitValueWhenNever<{ query: never; path: never; - header: never; + headers: never; body: never; context: ExternalType; response: ResponseBuilderFactory<{