Skip to content

Commit e13a9f1

Browse files
committed
fixup! schema input from stdin
1 parent 2b98406 commit e13a9f1

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

bin/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async function main() {
143143
// handle stdin schema, exit
144144
if (pathToSpec === "-") {
145145
if (output !== "." && output === OUTPUT_FILE) fs.mkdirSync(path.dirname(flags.output), { recursive: true });
146-
await generateSchema("/dev/stdin");
146+
await generateSchema(process.stdin);
147147
return;
148148
}
149149

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { URL } from "url";
66
import load, { resolveSchema, VIRTUAL_JSON_URL } from "./load.js";
77
import { swaggerVersion } from "./utils.js";
88
import { transformAll } from "./transform/index.js";
9+
import { Readable } from "stream";
910
export * from "./types.js"; // expose all types to consumers
1011

1112
export const WARNING_MESSAGE = `/**
@@ -30,7 +31,7 @@ export const WARNING_MESSAGE = `/**
3031
* @return {Promise<string>} {Promise<string>} Parsed file schema
3132
*/
3233
async function openapiTS(
33-
schema: string | URL | OpenAPI2 | OpenAPI3 | Record<string, SchemaObject>,
34+
schema: string | URL | OpenAPI2 | OpenAPI3 | Record<string, SchemaObject> | Readable,
3435
options: SwaggerToTSOptions = {} as Partial<SwaggerToTSOptions>
3536
): Promise<string> {
3637
const ctx: GlobalContext = {

src/load.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { URL } from "url";
77
import mime from "mime";
88
import yaml from "js-yaml";
99
import { parseRef } from "./utils.js";
10+
import { Readable } from "stream";
1011

1112
type PartialSchema = Record<string, any>; // not a very accurate type, but this is easier to deal with before we know we’re dealing with a valid spec
1213
type SchemaMap = { [url: string]: PartialSchema };
@@ -95,13 +96,13 @@ interface LoadOptions extends GlobalContext {
9596

9697
/** Load a schema from local path or remote URL */
9798
export default async function load(
98-
schema: URL | PartialSchema,
99+
schema: URL | PartialSchema | Readable,
99100
options: LoadOptions
100101
): Promise<{ [url: string]: PartialSchema }> {
101102
const urlCache = options.urlCache || new Set<string>();
102103

103-
const isJSON = schema instanceof URL === false; // if this is dynamically-passed-in JSON, we’ll have to change a few things
104-
let schemaID = isJSON ? new URL(VIRTUAL_JSON_URL).href : (schema.href as string);
104+
const isJSON = schema instanceof URL === false && !(schema instanceof Readable); // if this is dynamically-passed-in JSON, we’ll have to change a few things
105+
let schemaID = isJSON || schema instanceof Readable ? new URL(VIRTUAL_JSON_URL).href : (schema.href as string);
105106

106107
const schemas = options.schemas;
107108

@@ -116,9 +117,25 @@ export default async function load(
116117

117118
let contents = "";
118119
let contentType = "";
119-
const schemaURL = schema as URL; // helps TypeScript
120-
121-
if (isFile(schemaURL)) {
120+
const schemaURL = schema instanceof Readable ? new URL(VIRTUAL_JSON_URL) : (schema as URL); // helps TypeScript
121+
122+
if (schema instanceof Readable) {
123+
const readable = schema;
124+
contents = await new Promise<string>((resolve) => {
125+
readable.resume();
126+
readable.setEncoding("utf8");
127+
128+
let content = "";
129+
readable.on("data", (chunk: string) => {
130+
content += chunk;
131+
});
132+
133+
readable.on("end", () => {
134+
resolve(content);
135+
});
136+
});
137+
contentType = "text/yaml";
138+
} else if (isFile(schemaURL)) {
122139
// load local
123140
contents = fs.readFileSync(schemaURL, "utf8");
124141
contentType = mime.getType(schemaID) || "";

0 commit comments

Comments
 (0)