Repository: pmcelhaney/counterfact
Pull request #231 (2022-09-29 01:07:37)

227 when adding a server url add the full url


Patch:
diff --git a/.changeset/five-dancers-taste.md b/.changeset/five-dancers-taste.md
new file mode 100644
index 0000000..880bcf0
--- /dev/null
+++ b/.changeset/five-dancers-taste.md
@@ -0,0 +1,5 @@
+---
+"counterfact": patch
+---
+
+fix swagger-ui when the openapi doc has a host property
diff --git a/.changeset/nine-masks-attend.md b/.changeset/nine-masks-attend.md
new file mode 100644
index 0000000..04c4f6b
--- /dev/null
+++ b/.changeset/nine-masks-attend.md
@@ -0,0 +1,5 @@
+---
+"counterfact": patch
+---
+
+ensure a $context.ts file is created in every directory
diff --git a/src/typescript-generator/context-coder.js b/src/typescript-generator/context-coder.js
index 50e5bca..7682e73 100644
--- a/src/typescript-generator/context-coder.js
+++ b/src/typescript-generator/context-coder.js
@@ -3,31 +3,37 @@ import nodePath from "node:path";
 import { Coder } from "./coder.js";
 
 export class ContextCoder extends Coder {
-  isRoot() {
-    return this.requirement.url.split("/").at(-2).split("~1").length === 2;
+  pathString() {
+    return this.requirement.url
+      .split("/")
+      .at(-2)
+      .replaceAll("~1", "/")
+      .replaceAll("~0", "~");
   }
 
   names() {
     return super.names("Context");
   }
 
-  write() {
-    if (this.isRoot()) {
+  write(script) {
+    if (script.path === "paths/$context.ts") {
       return "{}";
     }
 
+    const parentPath = nodePath.normalize(
+      nodePath.join(script.path, "../../$context.ts")
+    );
+
+    script.repository.get(parentPath).exportDefault(this);
+
     return { raw: 'export { default } from "../$context.js"' };
   }
 
   modulePath() {
-    const pathString = this.requirement.url
-      .split("/")
-      .at(-2)
-      .replaceAll("~1", "/");
-
-    return `${nodePath.join(
+    return nodePath.join(
       "paths",
-      nodePath.dirname(pathString)
-    )}/$context.ts`;
+      nodePath.dirname(this.pathString()),
+      "$context.ts"
+    );
   }
 }


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 c261a97..9cb9386 100644
--- a/test/typescript-generator/__snapshots__/end-to-end.test.js.snap
+++ b/test/typescript-generator/__snapshots__/end-to-end.test.js.snap
@@ -45,9 +45,9 @@ Map {
           "cache": Map {
             "OperationTypeCoder@petstore.yaml#/paths/~1pet/post@[object Object]:true" => "HTTP_POST",
             "OperationTypeCoder@petstore.yaml#/paths/~1pet/put@[object Object]:true" => "HTTP_PUT",
-            "ContextCoder@petstore.yaml#/paths/~1pet/post@paths//$context.ts:false:true" => "Context",
+            "ContextCoder@petstore.yaml#/paths/~1pet/post@paths/$context.ts:false:true" => "Context",
             "SchemaTypeCoder@undefined@components/Pet.ts:true:false" => "Pet",
-            "ContextCoder@petstore.yaml#/paths/~1pet/put@paths//$context.ts:false:true" => "Context2",
+            "ContextCoder@petstore.yaml#/paths/~1pet/put@paths/$context.ts:false:true" => "Context2",
           },
           "exports": Map {
             "HTTP_POST" => Object {
@@ -190,7 +190,7 @@ Map {
                 },
                 "externalImports": Map {},
                 "imports": Map {},
-                "path": "paths//$context.ts",
+                "path": "paths/$context.ts",
                 "repository": Repository {
                   "scripts": [Circular],
                   "writeFiles": [Function],
@@ -313,7 +313,7 @@ Map {
                 },
                 "externalImports": Map {},
                 "imports": Map {},
-                "path": "paths//$context.ts",
+                "path": "paths/$context.ts",
                 "repository": Repository {
                   "scripts": [Circular],
                   "writeFiles": [Function],
@@ -338,9 +338,9 @@ Map {
           "cache": Map {
             "OperationTypeCoder@petstore.yaml#/paths/~1pet/post@[object Object]:true" => "HTTP_POST",
             "OperationTypeCoder@petstore.yaml#/paths/~1pet/put@[object Object]:true" => "HTTP_PUT",
-            "ContextCoder@petstore.yaml#/paths/~1pet/post@paths//$context.ts:false:true" => "Context",
+            "ContextCoder@petstore.yaml#/paths/~1pet/post@paths/$context.ts:false:true" => "Context",
             "SchemaTypeCoder@undefined@components/Pet.ts:true:false" => "Pet",
-            "ContextCoder@petstore.yaml#/paths/~1pet/put@paths//$context.ts:false:true" => "Context2",
+            "ContextCoder@petstore.yaml#/paths/~1pet/put@paths/$context.ts:false:true" => "Context2",
           },
           "exports": Map {
             "HTTP_POST" => Object {
@@ -483,7 +483,7 @@ Map {
                 },
                 "externalImports": Map {},
                 "imports": Map {},
-                "path": "paths//$context.ts",
+                "path": "paths/$context.ts",
                 "repository": Repository {
                   "scripts": [Circular],
                   "writeFiles": [Function],
@@ -606,7 +606,7 @@ Map {
                 },
                 "externalImports": Map {},
                 "imports": Map {},
-                "path": "paths//$context.ts",
+                "path": "paths/$context.ts",
                 "repository": Repository {
                   "scripts": [Circular],
                   "writeFiles": [Function],
@@ -635,9 +635,9 @@ Map {
     "cache": Map {
       "OperationTypeCoder@petstore.yaml#/paths/~1pet/post@[object Object]:true" => "HTTP_POST",
       "OperationTypeCoder@petstore.yaml#/paths/~1pet/put@[object Object]:true" => "HTTP_PUT",
-      "ContextCoder@petstore.yaml#/paths/~1pet/post@paths//$context.ts:false:true" => "Context",
+      "ContextCoder@petstore.yaml#/paths/~1pet/post@paths/$context.ts:false:true" => "Context",
       "SchemaTypeCoder@undefined@components/Pet.ts:true:false" => "Pet",
-      "ContextCoder@petstore.yaml#/paths/~1pet/put@paths//$context.ts:false:true" => "Context2",
+      "ContextCoder@petstore.yaml#/paths/~1pet/put@paths/$context.ts:false:true" => "Context2",
     },
     "exports": Map {
       "HTTP_POST" => Object {
@@ -780,7 +780,7 @@ Map {
           },
           "externalImports": Map {},
           "imports": Map {},
-          "path": "paths//$context.ts",
+          "path": "paths/$context.ts",
           "repository": Repository {
             "scripts": [Circular],
             "writeFiles": [Function],
@@ -903,7 +903,7 @@ Map {
           },
           "externalImports": Map {},
           "imports": Map {},
-          "path": "paths//$context.ts",
+          "path": "paths/$context.ts",
           "repository": Repository {
             "scripts": [Circular],
             "writeFiles": [Function],
@@ -4561,7 +4561,7 @@ Map {
         "script": Script {
           "cache": Map {
             "OperationTypeCoder@petstore.yaml#/paths/~1user/post@[object Object]:true" => "HTTP_POST",
-            "ContextCoder@petstore.yaml#/paths/~1user/post@paths//$context.ts:false:true" => "Context",
+            "ContextCoder@petstore.yaml#/paths/~1user/post@paths/$context.ts:false:true" => "Context",
             "SchemaTypeCoder@undefined@components/User.ts:true:false" => "User",
           },
           "exports": Map {
@@ -4632,7 +4632,7 @@ Map {
                 },
                 "externalImports": Map {},
                 "imports": Map {},
-                "path": "paths//$context.ts",
+                "path": "paths/$context.ts",
                 "repository": Repository {
                   "scripts": [Circular],
                   "writeFiles": [Function],
@@ -4690,7 +4690,7 @@ Map {
   "path-types/user.types.ts" => Script {
     "cache": Map {
       "OperationTypeCoder@petstore.yaml#/paths/~1user/post@[object Object]:true" => "HTTP_POST",
-      "ContextCoder@petstore.yaml#/paths/~1user/post@paths//$context.ts:false:true" => "Context",
+      "ContextCoder@petstore.yaml#/paths/~1user/post@paths/$context.ts:false:true" => "Context",
       "SchemaTypeCoder@undefined@components/User.ts:true:false" => "User",
     },
     "exports": Map {
@@ -4761,7 +4761,7 @@ Map {
           },
           "externalImports": Map {},
           "imports": Map {},
-          "path": "paths//$context.ts",
+          "path": "paths/$context.ts",
           "repository": Repository {
             "scripts": [Circular],
             "writeFiles": [Function],
@@ -6667,7 +6667,7 @@ Map {
     },
     "typeCache": Map {},
   },
-  "paths//$context.ts" => Script {
+  "paths/$context.ts" => Script {
     "cache": Map {
       "default" => "Context",
     },
@@ -6685,7 +6685,7 @@ Map {
     },
     "externalImports": Map {},
     "imports": Map {},
-    "path": "paths//$context.ts",
+    "path": "paths/$context.ts",
     "repository": Repository {
       "scripts": [Circular],
       "writeFiles": [Function],