Skip to content

Commit 2b0c2a8

Browse files
committed
setup initial project
1 parent d7660a5 commit 2b0c2a8

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed

src/App.vue

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,39 @@
22
<div>
33
hello world
44
</div>
5-
<button>login</button>
5+
<button @click="login">login</button>
6+
<button @click="listQuery">list</button>
67
</template>
78

89
<script setup lang="ts">
10+
import {ref, computed} from 'vue';
11+
import {auth} from './auth';
12+
import listGql from './List.gql';
13+
import { graphql } from '@octokit/graphql';
14+
15+
const accessToken = ref<string | null>(null);
16+
17+
const graphQlClient = computed(() => {
18+
const headers = accessToken.value ? {authorization: `token ${accessToken.value}`}: {};
19+
alert(headers);
20+
return graphql.defaults({
21+
headers
22+
});
23+
});
24+
25+
export const login = async () => {
26+
const {access_token} = await auth();
27+
accessToken.value = access_token;
28+
alert('logged in');
29+
};
30+
31+
export const listQuery = async () => {
32+
if(!graphQlClient.value){
33+
return;
34+
}
35+
const result = await graphQlClient.value(listGql);
36+
alert(JSON.stringify(result, null, 2));
37+
};
938
1039
</script>
1140

src/List.gql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
query List {
2+
viewer {
3+
gists (first: 100, privacy:ALL, orderBy: {field: CREATED_AT, direction: DESC} ) {
4+
edges {
5+
node {
6+
description
7+
name
8+
files {
9+
name
10+
text
11+
}
12+
}
13+
}
14+
}
15+
}
16+
}

src/auth.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const client_id = "39a009625c0b1f336d94";
2+
const client_secret = "2e363c1a9fced7a931af9244d5abf07dfd9192ac";
3+
4+
export const launchWebAuthFlow = (): Promise<string | undefined> => new Promise((resolve) => {
5+
const redirect_uri = `https://${chrome.runtime.id}.chromiumapp.org/provider_cb`;
6+
chrome.identity.launchWebAuthFlow(
7+
{
8+
url: `https://github.com/login/oauth/authorize?client_id=${client_id}&redirect_uri=${redirect_uri}&scope=gist`,
9+
interactive: true
10+
},
11+
(redirect_url) => resolve(redirect_url)
12+
);
13+
});
14+
15+
export const auth = async (): Promise<{access_token: string}>=> {
16+
const redirectUrl = await launchWebAuthFlow();
17+
if (!redirectUrl) {
18+
throw new Error("login failed");
19+
}
20+
const code = new URL(redirectUrl).searchParams.get("code");
21+
if(!code){
22+
throw new Error("login failed");
23+
}
24+
const { access_token } = await fetch(`https://github.com/login/oauth/access_token?client_id=${client_id}&client_secret=${client_secret}&code=${code}`, {
25+
headers: {
26+
Accept: "application/json"
27+
},
28+
method: "GET"
29+
}).then((r) => r.json() as Promise<{access_token: string}>).catch(e => {
30+
console.warn(e);
31+
return Promise.reject('wtf');
32+
});
33+
34+
return {
35+
access_token: access_token
36+
};
37+
};
38+
39+
void auth();

src/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"devtools_page": "devtools.html",
1313
"content_security_policy": "script-src 'self' 'unsafe-eval' http://localhost:8098; object-src 'self'",
1414
"permissions": [
15-
"identity"
15+
"identity",
16+
"https://*.github.com/*"
1617
]
1718
}

vue.config.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ module.exports = {
1414
const manifest = JSON.parse(content);
1515

1616
if (process.env.INCLUDE_KEY === 'true') {
17-
manifest.key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApuOA55Y/VfUyydcI2cWcezmCtvDjZ0ANF7eyq5SCvWiVvCca+qInh91GipobJQb3iFw5GXN0as4pJKbZg6pNRKn285Fo0uyegsdGMnXy//2tZ/yOgX7QiI0LM5XBc+NUPVER+QBsE/gbflKb0g3Z5aUcSJWreHxPqs8+PD/o2v6Z8YEmnEwDhn8w/YXmOiqnDrW4+4wEo49oOQYuVvehfFpE4dm8h9W/47tQNI2lmPrN7U8FTqdZnksmaUBUXaUUzIxhCDA3krYK/h+PrQq4i1F9hGt3YPDRhvkmNrZQsclfS+BdYE3yuWoNIQTJYO2xIhsrub4vBhvyl9W9SmPdyQIDAQAB";
17+
manifest.key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAg8eYUxVzvqDLHWq3z93P"+
18+
"tJ/9gOv84r7yLJ3r311VBZW9GmzMmgFsPKAF9Bfcr2dNYCwU6W72Z+stxYNnfgz8" +
19+
"5HXBELbearnsDpzF56TlSJNhWqE2/6HTn32qZ6dI5Pnq7oEhUITkbWESePvw3k2j" +
20+
"V0vTp6yJSFIyP+2+dP7/m6KOfcBhuJZi6RsDdfIw35XOJ+ORnfVJ3v1d5m0wAGF4" +
21+
"aUlgHsN8SIn043YZb7w+upuiCrmxHjhi6a+NY80tlMZDG7Fyi25KBSsjvrsC2HWR" +
22+
"lF6snolockJWWpwr+tmLIgazpXV9Z1YLZWExBKq2iiSPLKEdTjep1mngeNqYvkuZ" +
23+
"nQIDAQAB";
1824
}
1925

2026
manifest.version = `${version}.0`;
@@ -31,7 +37,8 @@ module.exports = {
3137
.plugin('reloader').use(ExtensionReloader).end()
3238
.plugin('monacoEditor').use(MonacoWebpackPlugin).end()
3339
.plugin('copy').use(CopyPlugin, [{patterns: copyPluginPatterns}]).end()
34-
.optimization.delete('splitChunks').end()
40+
.optimization.delete('splitChunks').end();
41+
3542

3643
// .gql files
3744
config.module.rule('gql')

0 commit comments

Comments
 (0)