Skip to content

Commit cce87ba

Browse files
authored
feat: support loading input from uri (#150)
initial support for loading input schemas from urls. support for following relative links to other urls probably doesn't work yet. relates #43
1 parent d2fda51 commit cce87ba

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

packages/openapi-code-generator/src/core/file-loader.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,48 @@ async function loadLocalFile(file: string): Promise<[string, any]> {
2222
file = path.resolve(file)
2323
const raw = fs.readFileSync(file, {encoding: "utf-8"})
2424

25-
let result: any | undefined
25+
return [file, await parseFile(raw, file)]
26+
}
27+
28+
async function loadRemoteFile(uri: string): Promise<[string, any]> {
29+
const res = await fetch(uri)
30+
31+
if (!res.ok) {
32+
throw new Error(`failed to fetch remote file '${uri}'`)
33+
}
34+
35+
const raw = await res.text()
2636

27-
if (file.endsWith(".json")) {
37+
return [uri, await parseFile(raw, uri)]
38+
}
39+
40+
async function parseFile(raw: string, filepath: string): Promise<unknown> {
41+
let result: unknown | undefined
42+
43+
// TODO: sniff format from raw text
44+
if (filepath.endsWith(".json")) {
2845
try {
2946
result = JSON.parse(raw)
3047
} catch (err: any) {
3148
logger.error("error parsing json", err.stack)
32-
throw new Error(`failed to parse json from file '${file}'`)
49+
throw new Error(`failed to parse json from '${filepath}'`)
3350
}
3451
}
3552

36-
if (file.endsWith(".yaml") || file.endsWith(".yml")) {
53+
if (filepath.endsWith(".yaml") || filepath.endsWith(".yml")) {
3754
try {
3855
result = yaml.load(raw)
3956
} catch (err: any) {
4057
logger.error("error parsing yaml", err.stack)
41-
throw new Error(`failed to parse yaml from file '${file}'`)
58+
throw new Error(`failed to parse yaml from '${filepath}'`)
4259
}
4360
}
4461

4562
if (!result) {
46-
throw new Error(`failed to load file '${file}'`)
63+
throw new Error(`failed to parse '${filepath}'`)
4764
}
4865

49-
return [file, result]
50-
}
51-
52-
async function loadRemoteFile(uri: string): Promise<[string, any]> {
53-
// todo: https://github.com/mnahkies/openapi-code-generator/issues/43
54-
throw new Error(`could not load ${uri} - not implemented,`)
66+
return result
5567
}
5668

5769
export function isRemote(location: string): boolean {

packages/openapi-code-generator/src/core/openapi-loader.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export class OpenapiLoader {
153153
throw new Error(`invalid $ref '${$ref}`)
154154
}
155155

156+
// TODO: support relative urls
156157
if (isRemote(file)) {
157158
return $ref
158159
}
@@ -192,7 +193,7 @@ export class OpenapiLoader {
192193
entryPoint: string,
193194
validator: OpenapiValidator,
194195
): Promise<OpenapiLoader> {
195-
entryPoint = path.resolve(entryPoint)
196+
entryPoint = isRemote(entryPoint) ? entryPoint : path.resolve(entryPoint)
196197
const loader = new OpenapiLoader(entryPoint, validator)
197198

198199
await loader.loadFile(entryPoint)

0 commit comments

Comments
 (0)