Skip to content

Commit a1abe62

Browse files
authored
fix: Changed Read API Method for Github from Graphql API to Rest API (#253)
* github read from rest instead of graphql * Better empty content message
1 parent 7b7904b commit a1abe62

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

cli/src/core/getOpenAPISourceFile.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,48 +53,38 @@ export const getOpenAPISourceFile = async (
5353
const { default: got } = await import("got");
5454

5555
try {
56-
const raw = await got
57-
.post("https://api.github.com/graphql", {
56+
const raw = await got(
57+
`https://api.github.com/repos/${options.owner}/${options.repository}/contents/${options.specPath}?ref=${options.ref}`,
58+
{
5859
headers: {
5960
"content-type": "application/json",
6061
"user-agent": "openapi-codegen",
6162
authorization: `bearer ${token}`,
6263
},
63-
body: JSON.stringify({
64-
query: `query {
65-
repository(name: "${options.repository}", owner: "${options.owner}") {
66-
object(expression: "${options.ref}:${options.specPath}") {
67-
... on Blob {
68-
text
69-
}
70-
}
71-
}
72-
}`,
73-
}),
74-
})
75-
.json<{
76-
data: { repository: { object: { text: string } | null } };
77-
errors?: [
78-
{
79-
message: string;
80-
}
81-
];
82-
}>();
64+
}
65+
).json<{
66+
content: string;
67+
encoding: string | null;
68+
}>();
8369

8470
prompt.close();
85-
if (raw.errors) {
86-
throw new UsageError(raw.errors[0].message);
87-
}
88-
if (raw.data.repository.object === null) {
89-
throw new UsageError(`No file found at "${options.specPath}"`);
71+
72+
if (!raw.content) {
73+
throw new UsageError(`No content found at "${options.specPath}"`);
9074
}
9175

76+
const encoding: BufferEncoding =
77+
(raw.encoding as BufferEncoding) || "base64";
78+
const textContent = Buffer.from(raw.content, encoding).toString(
79+
"utf-8"
80+
);
81+
9282
let format: OpenAPISourceFile["format"] = "yaml";
9383
if (options.specPath.toLowerCase().endsWith("json")) {
9484
format = "json";
9585
}
9686

97-
return { text: raw.data.repository.object.text, format };
87+
return { text: textContent, format };
9888
} catch (e) {
9989
if (
10090
e instanceof HTTPError &&
@@ -112,6 +102,10 @@ export const getOpenAPISourceFile = async (
112102
return await getOpenAPISourceFile(options);
113103
}
114104
}
105+
106+
if (e instanceof HTTPError && e.response.statusCode === 404) {
107+
throw new UsageError(`No file found at "${options.specPath}"`);
108+
}
115109
throw e;
116110
}
117111
}

0 commit comments

Comments
 (0)