Skip to content

Commit

Permalink
Merge branch 'main' into fix-one-of
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed Sep 17, 2022
2 parents 86c1412 + 4bfc7af commit fa2dcf8
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-ants-allow.md
@@ -0,0 +1,5 @@
---
"counterfact": minor
---

the open API document can now be loaded from a URL rather than a local path
10 changes: 5 additions & 5 deletions bin/counterfact.js
Expand Up @@ -72,13 +72,13 @@ async function main() {
}

if (command === "go" && process.argv.length === 5) {
const [, source, destination = "."] = process.argv
.slice(2)
.map((pathString) => nodePath.join(process.cwd(), pathString));
const [, source, destination = "."] = process.argv.slice(2);

await generate(source, destination);
await generate(source, nodePath.join(process.cwd(), destination));

const basePath = nodePath.resolve(destination);
const basePath = nodePath.resolve(
nodePath.join(process.cwd(), destination)
);

await start(basePath, 3100, source);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -32,7 +32,7 @@
"lint:quickfix": "eslint --fix . eslint --fix demo-ts --rule=\"import/namespace: 0,etc/no-deprecated:0,import/no-cycle:0,no-explicit-type-exports/no-explicit-type-exports:0,import/no-deprecated:0,import/no-self-import:0,import/default:0,import/no-named-as-default:0\"",
"generate:petstore": "yarn counterfact generate ./petstore.yaml out",
"generate:example": "yarn counterfact generate ./openapi-example.yaml out",
"go:petstore": "yarn counterfact go ./petstore.yaml out",
"go:petstore": "yarn counterfact go https://petstore3.swagger.io/api/v3/openapi.json out",
"go:example": "yarn counterfact go ./openapi-example.yaml out",
"counterfact": "./bin/counterfact.js"
},
Expand Down Expand Up @@ -69,6 +69,7 @@
"jsonwebtoken": "^8.5.1",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"node-fetch": "^3.2.10",
"prettier": "^2.7.1",
"typescript": "^4.8.2"
}
Expand Down
6 changes: 3 additions & 3 deletions src/counterfact.js
@@ -1,10 +1,12 @@
/* eslint-disable import/max-dependencies */
import fs from "node:fs/promises";
import nodePath from "node:path";
import os from "node:os";

import $RefParser from "json-schema-ref-parser";
import yaml from "js-yaml";

import { readFile } from "./read-file.js";
import { Registry } from "./registry.js";
import { Dispatcher } from "./dispatcher.js";
import { koaMiddleware } from "./koa-middleware.js";
Expand All @@ -13,9 +15,7 @@ import { Transpiler } from "./transpiler.js";

async function loadOpenApiDocument(source) {
try {
return $RefParser.dereference(
await yaml.load(await fs.readFile(source, "utf8"))
);
return $RefParser.dereference(await yaml.load(await readFile(source)));
} catch {
return undefined;
}
Expand Down
17 changes: 17 additions & 0 deletions src/read-file.js
@@ -0,0 +1,17 @@
import fs from "node:fs/promises";

import nodeFetch from "node-fetch";

export async function readFile(urlOrPath) {
if (urlOrPath.startsWith("http")) {
const response = await nodeFetch(urlOrPath);

return response.text();
}

if (urlOrPath.startsWith("file")) {
return fs.readFile(new URL(urlOrPath), "utf8");
}

return fs.readFile(urlOrPath, "utf8");
}
13 changes: 7 additions & 6 deletions src/typescript-generator/specification.js
@@ -1,8 +1,9 @@
import fs from "node:fs/promises";
import nodePath from "node:path";

import yaml from "js-yaml";

import { readFile } from "../read-file.js";

import { Requirement } from "./requirement.js";

export class Specification {
Expand All @@ -21,16 +22,16 @@ export class Specification {
return rootRequirement.select(path.slice(1));
}

async loadFile(url) {
if (this.cache.has(url)) {
return this.cache.get(url);
async loadFile(urlOrPath) {
if (this.cache.has(urlOrPath)) {
return this.cache.get(urlOrPath);
}

const source = await fs.readFile(url, "utf8");
const source = await readFile(urlOrPath, "utf8");

const data = await yaml.load(source);

this.cache.set(url, data);
this.cache.set(urlOrPath, data);

return data;
}
Expand Down
42 changes: 42 additions & 0 deletions test/typescript-generator/specification.test.js
@@ -1,3 +1,7 @@
import { pathToFileURL } from "node:url";

import Koa from "koa";

import { withTemporaryFiles } from "../lib/with-temporary-files.js";
import { Requirement } from "../../src/typescript-generator/requirement.js";
import { Specification } from "../../src/typescript-generator/specification.js";
Expand All @@ -18,6 +22,44 @@ describe("a Specification", () => {
);
});

it("loads a file from disk with a file URL", async () => {
await withTemporaryFiles(
{ "openapi.yaml": "hello:\n world" },
async (temporaryDirectory, { path }) => {
const specification = new Specification();

const url = pathToFileURL(path("openapi.yaml")).href;

await specification.loadFile(url);

expect(specification.cache.get(url)).toStrictEqual({
hello: "world",
});
}
);
});

it("loads a file from the web", async () => {
const app = new Koa();

app.use(({ response }) => {
response.body = "hello: world";
});

const server = app.listen(0);
const url = `http://localhost:${server.address().port}/`;

const specification = new Specification();

await specification.loadFile(url);

expect(specification.cache.get(url)).toStrictEqual({
hello: "world",
});

await server.close();
});

it("returns a requirement for a URL", async () => {
const specification = new Specification();

Expand Down
39 changes: 39 additions & 0 deletions yarn.lock
Expand Up @@ -3568,6 +3568,11 @@ damerau-levenshtein@^1.0.8:
resolved "https://registry.yarnpkg.com/damerau-levenshtein/-/damerau-levenshtein-1.0.8.tgz#b43d286ccbd36bc5b2f7ed41caf2d0aba1f8a6e7"
integrity sha512-sdQSFB7+llfUcQHUQO3+B8ERRj0Oa4w9POWMI/puGtuf7gFywGmkaLCElnudfTiKZV+NvHqL0ifzdrI8Ro7ESA==

data-uri-to-buffer@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz#b5db46aea50f6176428ac05b73be39a57701a64b"
integrity sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==

date-format@^4.0.13:
version "4.0.13"
resolved "https://registry.yarnpkg.com/date-format/-/date-format-4.0.13.tgz#87c3aab3a4f6f37582c5f5f63692d2956fa67890"
Expand Down Expand Up @@ -4716,6 +4721,14 @@ fb-watchman@^2.0.0:
dependencies:
bser "2.1.1"

fetch-blob@^3.1.2, fetch-blob@^3.1.4:
version "3.2.0"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9"
integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==
dependencies:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"

figures@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962"
Expand Down Expand Up @@ -4837,6 +4850,13 @@ format-util@^1.0.3:
resolved "https://registry.yarnpkg.com/format-util/-/format-util-1.0.5.tgz#1ffb450c8a03e7bccffe40643180918cc297d271"
integrity sha512-varLbTj0e0yVyRpqQhuWV+8hlePAgaoFRhNFj50BNjEIrw1/DphHSObtqwskVCPWNgzwPoQrZAbfa/SBiicNeg==

formdata-polyfill@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
dependencies:
fetch-blob "^3.1.2"

formidable@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/formidable/-/formidable-2.0.1.tgz#4310bc7965d185536f9565184dee74fbb75557ff"
Expand Down Expand Up @@ -7080,6 +7100,20 @@ no-case@^3.0.4:
lower-case "^2.0.2"
tslib "^2.0.3"

node-domexception@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==

node-fetch@^3.2.10:
version "3.2.10"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.2.10.tgz#e8347f94b54ae18b57c9c049ef641cef398a85c8"
integrity sha512-MhuzNwdURnZ1Cp4XTazr69K0BTizsBroX7Zx3UgDSVcZYKF/6p0CBe4EUb/hLqmzVhl0UpYfgRljQ4yxE+iCxA==
dependencies:
data-uri-to-buffer "^4.0.0"
fetch-blob "^3.1.4"
formdata-polyfill "^4.0.10"

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down Expand Up @@ -9477,6 +9511,11 @@ weapon-regex@~1.0.2:
resolved "https://registry.yarnpkg.com/weapon-regex/-/weapon-regex-1.0.3.tgz#56b5cd6054bb23323bdde640c09062b5aebe024e"
integrity sha512-V8X6hPIzY1juvrSVREmtRhK9AHn/8c2z8XxaibESU+jyG/RinZ9x9x6aw8qEuFAi7R6Kl/EWGbU2Yq/9u6TTjw==

web-streams-polyfill@^3.0.3:
version "3.2.1"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6"
integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==

which-boxed-primitive@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6"
Expand Down

0 comments on commit fa2dcf8

Please sign in to comment.