Skip to content

Commit

Permalink
replace Octokit by native fetch calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
ideadapt committed Sep 22, 2024
1 parent ea101e8 commit ebd435f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"dependencies": {
"alpinejs": "^3.10.3",
"mocha": "^10.0.0",
"nodemon": "^2.0.20",
"octokit": "^2.0.7"
"nodemon": "^2.0.20"
}
}
28 changes: 12 additions & 16 deletions server/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Status } from "https://deno.land/[email protected]/http/http_status.ts";
import { Application, Context, Request, Response, Router } from "https://deno.land/x/[email protected]/mod.ts";
import { proxy } from "https://deno.land/x/[email protected]/mod.ts";
import { Octokit } from "https://cdn.skypack.dev/octokit";
import { config } from "https://deno.land/x/[email protected]/mod.ts";


Expand All @@ -22,8 +21,11 @@ const editor_password = getConfig('editor_password')

const app = new Application()
const router = new Router()
const octokit = new Octokit({ auth: gh_gist_token })
const rawOctokit = new Octokit({ auth: gh_gist_token, baseUrl: 'https://gist.githubusercontent.com' })
const ghApiOpts = {
headers: new Headers({'Authorization': gh_gist_token, "X-GitHub-Api-Version": "2022-11-28", 'accept': 'application/json'})
}
const octokit = (path: string, method = 'GET', body: object | null = null) => fetch('https://api.github.com'+path, { method, ...ghApiOpts, body: body ? JSON.stringify(body) : null})
const rawOctokit = (path: string) => fetch('https://gist.githubusercontent.com'+path, ghApiOpts)

function isAuthorized(request: Request){
const auth = request.headers.get('Authorization')
Expand Down Expand Up @@ -89,23 +91,18 @@ router
router.get('/gists/:gist_id', async (context) => {
console.log('GET gist')

const gist = await octokit.request(`GET /gists/${context.params.gist_id}`, {
gist_id: context.params.gist_id
})
const dbFile = gist.data.files['db.json'];
let gistText = "";
const gist = await (await octokit(`/gists/${context.params.gist_id}`)).json();
const dbFile = gist.files['db.json'];
let gistJson = { articles: [], findings: []};
// if db.json content is >1MB, github will truncate
// and only serve full content via raw_url.
if(dbFile.truncated === true){
const rawUrl = new URL(dbFile.raw_url);
const rawGist = await rawOctokit.request(`GET ${rawUrl.pathname}`, {
gist_id: context.params.gist_id
})
gistText = rawGist.data;
const rawGist = await (await rawOctokit(`${rawUrl.pathname}`)).json();
gistJson = rawGist;
}else{
gistText = dbFile.content;
gistJson = JSON.parse(dbFile.content);
}
const gistJson = JSON.parse(gistText)

// TODO should be on client side?
if(!("findings" in gistJson)){
Expand Down Expand Up @@ -133,8 +130,7 @@ router
}

const state = await request.body({ type: 'json'}).value
await octokit.request(`PATCH /gists/${params.gist_id}`, {
gist_id: params.gist_id,
await octokit(`/gists/${params.gist_id}`, 'PATCH', {
description: 'spelljack-db-' + dict_name,
files: {
'db.json': { content: JSON.stringify(state) }
Expand Down

0 comments on commit ebd435f

Please sign in to comment.