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

[solidjs-pod]: Get Gists from GitHub API #838

Merged
merged 19 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 18 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
4 changes: 4 additions & 0 deletions solidjs-tailwind/src/components/Icons/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export { default as TwitterIcon } from './TwitterIcon';
export { default as CaretIcon } from './caret';
export { default as CloseIcon } from './close';
export { default as CorrectIcon } from './correct';
export { default as RepoBookIcon } from './repo-book';
25 changes: 25 additions & 0 deletions solidjs-tailwind/src/services/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const FetchApi = ({url, query, variables, headersOptions}) => {
return new Promise((resolve, reject) => {
fetch(url, {
method: "POST",
headers: {
...headersOptions,
Accept: 'application/vnd.github+json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
variables
})
})
.then((res) => res.json())
.then((result) => {
resolve(result);
})
.catch((error) => {
reject(error);
});
});
};

export default FetchApi;
44 changes: 44 additions & 0 deletions solidjs-tailwind/src/services/get-gists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import FetchApi from "./api";
import { useAuth } from "../auth";
import { USER_GISTS_QUERY } from "./queries/gists";


const getGists = async ({url}) => {
const { authStore } = useAuth();

const data = {
url,
query: USER_GISTS_QUERY,
variable: null,
headersOptions: {
authorization: `Bearer ${authStore.token}`,
}
}
const resp = await FetchApi(data);
const gists = resp.viewer.gists.nodes?.reduce((acc, gist) => {
if (!gist) {
return acc;
}
const files = gist.files ?? [];
const gists = files.reduce(
(_acc, file) =>
file ?
[
..._acc,
{
id: gist.id,
description: gist.description,
name: file.name || gist.name,
url: gist.url,
},
] :
acc,
[],
);
return [...acc, ...gists];
}, []);

return gists;
};

export default getGists;
17 changes: 17 additions & 0 deletions solidjs-tailwind/src/services/queries/gists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const USER_GISTS_QUERY =`
query UserGists {
viewer {
gists(last: 20, orderBy: { field: CREATED_AT, direction: DESC }) {
nodes {
id
description
url
name
files {
name
}
}
}
}
}
`;