Skip to content
This repository was archived by the owner on Sep 13, 2023. It is now read-only.

Commit bd8b2da

Browse files
author
Fernando B
authored
Merge pull request #1 from lauthieb/ghe-token-refactoring
Ghe token refactoring
2 parents fa256e4 + 2cdba3a commit bd8b2da

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/renderer/store/modules/Note.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import db from '@/datastore-notes';
22
import converter from '@/converter';
3+
import Octokit from '@octokit/rest';
34
import path from 'path';
45
// eslint-disable-next-line import/no-extraneous-dependencies
56
import { remote } from 'electron';
67
import packageJson from '../../../../package';
78

8-
const Octokit = require('@octokit/rest');
9-
let octokit = Octokit({
9+
const getOctokit = ({ githubPersonalAccessToken, githubEnterpriseUrl }) => new Octokit({
10+
auth: githubPersonalAccessToken,
1011
userAgent: 'code-notes'.concat(packageJson.version),
1112
mediaType: {
1213
format: 'application/vnd.github.v3+json',
1314
},
15+
...(githubEnterpriseUrl && { baseUrl: githubEnterpriseUrl }),
1416
});
1517

1618
const state = {
@@ -55,16 +57,8 @@ const actions = {
5557
store.commit('SELECT_LOADING', true);
5658
store.commit('LOAD_NOTES', []);
5759

58-
if (store.rootState.Settings.settings.githubEnterpriseUrl) {
59-
octokit = new Octokit({
60-
baseUrl: store.rootState.Settings.settings.githubEnterpriseUrl,
61-
auth: store.rootState.Settings.settings.githubPersonalAccessToken,
62-
});
63-
} else {
64-
octokit = new Octokit({
65-
auth: store.rootState.Settings.settings.githubPersonalAccessToken,
66-
});
67-
}
60+
const octokit = getOctokit(store.rootState.Settings.settings);
61+
6862
octokit.gists.list().then((res) => {
6963
const promises = [];
7064

@@ -99,6 +93,9 @@ const actions = {
9993
},
10094
addNote(store, note) {
10195
store.commit('SELECT_LOADING', true);
96+
97+
const octokit = getOctokit(store.rootState.Settings.settings);
98+
10299
if (store.state.gistsSelected) {
103100
octokit.gists.create(note).then(() => {
104101
store.dispatch('loadNotes');
@@ -115,6 +112,8 @@ const actions = {
115112
},
116113
updateNote(store, note) {
117114
if (store.state.gistsSelected) {
115+
const octokit = getOctokit(store.rootState.Settings.settings);
116+
118117
octokit.gists
119118
.update({
120119
gist_id: note.id,
@@ -133,6 +132,9 @@ const actions = {
133132
},
134133
deleteNote(store, note) {
135134
store.commit('SELECT_LOADING', true);
135+
136+
const octokit = getOctokit(store.rootState.Settings.settings);
137+
136138
if (store.state.gistsSelected) {
137139
octokit.gists.delete({ gist_id: note.id }).then(() => {
138140
store.commit('DELETE_NOTE', note);

0 commit comments

Comments
 (0)