Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type fix: header should be headers (#1160) #1161

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/late-terms-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

fix types: $.header should be $.headers (#1160)
4 changes: 2 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
4 changes: 2 additions & 2 deletions src/typescript-generator/operation-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down Expand Up @@ -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 }`;
}
Expand Down
48 changes: 24 additions & 24 deletions test/typescript-generator/__snapshots__/generate.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type HTTP_POST = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: Pet;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -78,7 +78,7 @@ export type HTTP_PUT = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: Pet;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -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<{
Expand Down Expand Up @@ -236,7 +236,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: { tags?: Array<string> };
path: never;
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -317,7 +317,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: never;
path: { petId: number };
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -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<{
Expand All @@ -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<{
Expand Down Expand Up @@ -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<{
Expand Down Expand Up @@ -502,7 +502,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -557,7 +557,7 @@ export type HTTP_POST = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: Order;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -625,7 +625,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: never;
path: { orderId: number };
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -681,7 +681,7 @@ export type HTTP_DELETE = (
$: OmitValueWhenNever<{
query: never;
path: { orderId: number };
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -739,7 +739,7 @@ export type HTTP_POST = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: User;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -803,7 +803,7 @@ export type HTTP_POST = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: Array<User>;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -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<{
Expand Down Expand Up @@ -947,7 +947,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -1007,7 +1007,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: never;
path: { username: string };
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -1063,7 +1063,7 @@ export type HTTP_PUT = (
$: OmitValueWhenNever<{
query: never;
path: { username: string };
header: never;
headers: never;
body: User;
context: Context;
response: ResponseBuilderFactory<{
Expand All @@ -1089,7 +1089,7 @@ export type HTTP_DELETE = (
$: OmitValueWhenNever<{
query: never;
path: { username: string };
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -1212,7 +1212,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -1267,7 +1267,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -1322,7 +1322,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -1377,7 +1377,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: never;
path: { name: string };
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down Expand Up @@ -1436,7 +1436,7 @@ export type HTTP_GET = (
$: OmitValueWhenNever<{
query: never;
path: never;
header: never;
headers: never;
body: never;
context: Context;
response: ResponseBuilderFactory<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<{
Expand Down Expand Up @@ -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<{
Expand Down Expand Up @@ -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<{
Expand Down Expand Up @@ -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<{
Expand Down Expand Up @@ -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<{
Expand Down Expand Up @@ -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<{
Expand Down Expand Up @@ -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<{
Expand Down Expand Up @@ -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<{
Expand Down
Loading