Skip to content

Commit

Permalink
fix: Changed Read API Method for Github from Graphql API to Rest API (#…
Browse files Browse the repository at this point in the history
…253)

* github read from rest instead of graphql

* Better empty content message
  • Loading branch information
Sidd27 committed May 20, 2024
1 parent 7b7904b commit a1abe62
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions cli/src/core/getOpenAPISourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,48 +53,38 @@ export const getOpenAPISourceFile = async (
const { default: got } = await import("got");

try {
const raw = await got
.post("https://api.github.com/graphql", {
const raw = await got(
`https://api.github.com/repos/${options.owner}/${options.repository}/contents/${options.specPath}?ref=${options.ref}`,
{
headers: {
"content-type": "application/json",
"user-agent": "openapi-codegen",
authorization: `bearer ${token}`,
},
body: JSON.stringify({
query: `query {
repository(name: "${options.repository}", owner: "${options.owner}") {
object(expression: "${options.ref}:${options.specPath}") {
... on Blob {
text
}
}
}
}`,
}),
})
.json<{
data: { repository: { object: { text: string } | null } };
errors?: [
{
message: string;
}
];
}>();
}
).json<{
content: string;
encoding: string | null;
}>();

prompt.close();
if (raw.errors) {
throw new UsageError(raw.errors[0].message);
}
if (raw.data.repository.object === null) {
throw new UsageError(`No file found at "${options.specPath}"`);

if (!raw.content) {
throw new UsageError(`No content found at "${options.specPath}"`);
}

const encoding: BufferEncoding =
(raw.encoding as BufferEncoding) || "base64";
const textContent = Buffer.from(raw.content, encoding).toString(
"utf-8"
);

let format: OpenAPISourceFile["format"] = "yaml";
if (options.specPath.toLowerCase().endsWith("json")) {
format = "json";
}

return { text: raw.data.repository.object.text, format };
return { text: textContent, format };
} catch (e) {
if (
e instanceof HTTPError &&
Expand All @@ -112,6 +102,10 @@ export const getOpenAPISourceFile = async (
return await getOpenAPISourceFile(options);
}
}

if (e instanceof HTTPError && e.response.statusCode === 404) {
throw new UsageError(`No file found at "${options.specPath}"`);
}
throw e;
}
}
Expand Down

0 comments on commit a1abe62

Please sign in to comment.