Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Changed Read API Method for Github from Graphql API to Rest API #253

Merged
merged 2 commits into from
May 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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