Repository: pmcelhaney/counterfact
Pull request #191 (2022-09-10 18:24:23)

add headers to generated response type


Patch:
diff --git a/.changeset/dry-paws-dream.md b/.changeset/dry-paws-dream.md
new file mode 100644
index 0000000..ce44598
--- /dev/null
+++ b/.changeset/dry-paws-dream.md
@@ -0,0 +1,5 @@
+---
+"counterfact": patch
+---
+
+add headers to the generated response type
diff --git a/openapi-example.yaml b/openapi-example.yaml
index fb0f057..c5f9de0 100644
--- a/openapi-example.yaml
+++ b/openapi-example.yaml
@@ -47,6 +47,10 @@ paths:
       responses:
         default:
           description: Successful response
+          headers:
+            x-test:
+              schema:
+                type: string
           content:
             application/json:
               schema: 
diff --git a/src/typescript-generator/response-type-coder.js b/src/typescript-generator/response-type-coder.js
index 6739bae..9a5283c 100644
--- a/src/typescript-generator/response-type-coder.js
+++ b/src/typescript-generator/response-type-coder.js
@@ -39,6 +39,19 @@ export class ResponseTypeCoder extends Coder {
     }`;
   }
 
+  buildHeaders(script, responseCode) {
+    const response = this.requirement.select(responseCode);
+
+    const properties = Object.keys(response.data.headers ?? {}).map(
+      (name) =>
+        `"${name}": { schema: ${new SchemaTypeCoder(
+          response.select("headers").select(name).select("schema")
+        ).write(script)}}`
+    );
+
+    return `{${properties.join(";")}}`;
+  }
+
   buildResponseObjectType(script) {
     const responses = this.requirement.data;
 
@@ -49,7 +62,7 @@ export class ResponseTypeCoder extends Coder {
             responseCode,
             responses
           )}: {
-          headers: {};
+          headers: ${this.buildHeaders(script, responseCode)};
           content: ${this.buildContentObjectType(
             script,
             responseCode,


Test patch:
diff --git a/test/typescript-generator/__snapshots__/end-to-end.test.js.snap b/test/typescript-generator/__snapshots__/end-to-end.test.js.snap
index 707bfd9..3ca924f 100644
--- a/test/typescript-generator/__snapshots__/end-to-end.test.js.snap
+++ b/test/typescript-generator/__snapshots__/end-to-end.test.js.snap
@@ -4777,7 +4777,7 @@ Map {
             "HTTP_GET" => Object {
               "code": "({ query, path, header, body, context, tools }: { query: {username?: string, password?: string}, path: never, header: never, body: undefined, context: Context, response: ResponseBuilderBuilder<{
       200: {
-          headers: {};
+          headers: {\\"X-Rate-Limit\\": { schema: number};\\"X-Expires-After\\": { schema: string}};
           content: {
       \\"application/xml\\": { 
               schema:  string
@@ -4896,7 +4896,7 @@ Map {
       "HTTP_GET" => Object {
         "code": "({ query, path, header, body, context, tools }: { query: {username?: string, password?: string}, path: never, header: never, body: undefined, context: Context, response: ResponseBuilderBuilder<{
       200: {
-          headers: {};
+          headers: {\\"X-Rate-Limit\\": { schema: number};\\"X-Expires-After\\": { schema: string}};
           content: {
       \\"application/xml\\": { 
               schema:  string