diff --git a/solidjs-tailwind/src/services/get-gists.js b/solidjs-tailwind/src/services/get-gists.js new file mode 100644 index 000000000..c41f31f47 --- /dev/null +++ b/solidjs-tailwind/src/services/get-gists.js @@ -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; diff --git a/solidjs-tailwind/src/services/queries/gists.js b/solidjs-tailwind/src/services/queries/gists.js new file mode 100644 index 000000000..7a7b38641 --- /dev/null +++ b/solidjs-tailwind/src/services/queries/gists.js @@ -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 + } + } + } + } + } +`;