Skip to content

Commit

Permalink
feat: opensearch authorization ok #14
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Jan 9, 2024
1 parent e8cd72e commit 1e639ab
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 24 deletions.
41 changes: 31 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"prepare": "husky install"
},
"dependencies": {
"buffer": "^6.0.3",
"debug": "^4.3.4",
"electron-store": "^8.1.0",
"monaco-editor": "^0.45.0",
Expand Down
19 changes: 17 additions & 2 deletions src/common/httpClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CustomError } from './customError';
import { Buffer } from 'buffer';

const catchHandler = (err: unknown) => {
if (err instanceof CustomError) {
Expand All @@ -18,13 +19,27 @@ const buildURL = (host: string, port: number, path?: string, queryParameters?: s
return url;
};

export const loadHttpClient = (host: string, port: number) => ({
export const loadHttpClient = ({
host,
port,
username,
password,
}: {
host: string;
port: number;
username?: string;
password?: string;
}) => ({
get: async (path?: string, queryParameters?: string) => {
const authorization =
username && password
? `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`
: undefined;
const url = buildURL(host, port, path, queryParameters);
try {
const result = await fetch(url, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
headers: { 'Content-Type': 'application/json', authorization } as unknown as Headers,
});
const data = await result.json();
if (!result.ok) new CustomError(result.status, data);
Expand Down
2 changes: 1 addition & 1 deletion src/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ contextBridge.exposeInMainWorld('sourceFileAPI', {
saveFile: async (content: string) =>
ipcRenderer.invoke('sourceFileAPI', { method: 'SAVE_FILE', content }),
readFile: async () => ipcRenderer.invoke('sourceFileAPI', { method: 'READ_FILE' }),
onSaveChortcut: (callback: (value: unknown) => void) =>
onSaveShortcut: (callback: (value: unknown) => void) =>
ipcRenderer.on('save-shortcout', (_event, value) => callback(value)),
});
22 changes: 15 additions & 7 deletions src/store/connectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ export const useConnectionStore = defineStore('connectionStore', {
async fetchConnections() {
this.connections = await storeAPI.get('connections', []);
},
async testConnection({ host, port }: Connection) {
const client = loadHttpClient(host, parseInt(`${port}`, 10));
async testConnection({ port, ...con }: Connection) {
const client = loadHttpClient({
...con,
port: parseInt(`${port}`, 10),
});

return await client.get();
},
saveConnection(connection: Connection) {
Expand All @@ -79,7 +83,10 @@ export const useConnectionStore = defineStore('connectionStore', {
},
async establishConnection(connection: Connection) {
await this.testConnection(connection);
const client = loadHttpClient(connection?.host, parseInt(`${connection?.port}`, 10));
const client = loadHttpClient({
...connection,
port: parseInt(`${connection.port}`, 10),
});

const data = await client.get('/_cat/indices', 'format=json');
const indices = data.map((index: { [key: string]: string }) => ({
Expand Down Expand Up @@ -109,10 +116,11 @@ export const useConnectionStore = defineStore('connectionStore', {
index?: string;
qdsl?: string;
}) {
const client = loadHttpClient(
this.established?.host || '',
parseInt(`${this.established?.port || 9200}`, 10),
);
if (!this.established) throw new Error('no connection established');
const client = loadHttpClient({
...this.established,
port: parseInt(`${this.established.port}`, 10),
});

const reqPath = buildPath(index, path);
const body = qdsl ? JSON.parse(qdsl) : undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/views/connect/components/connect-dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<script setup lang="ts">
import { Close } from '@vicons/carbon';
import { CustomError } from '../../../common';
import { Connection, useConnectionStore } from '../../../store/connectionStore';
import { Connection, useConnectionStore } from '../../../store';
import { useLang } from '../../../lang';
import { FormValidationError } from 'naive-ui';
Expand Down
2 changes: 1 addition & 1 deletion src/views/connect/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import ConnectModal from './components/connect-dialog.vue';
import connectList from './components/connect-list.vue';
import collectionSelector from './components/collection-selector.vue';
import Editor from '../editor/index.vue';
import { useAppStore } from './../../store';
import { useAppStore } from '../../store';
const appStore = useAppStore();
// DOM
Expand Down
5 changes: 4 additions & 1 deletion src/views/editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ onMounted(async () => {
});
const { sourceFileAPI } = window;
sourceFileAPI.onSaveChortcut(async () => {
sourceFileAPI.onSaveShortcut(async () => {
if (!queryEditor) {
return;
}
await saveSourceToFile(queryEditor.getModel()!.getValue() || '');
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface IStoreAPI {
export interface ISourceFileAPI {
saveFile: (content: string) => Promise<void>;
readFile: () => Promise<string>;
onSaveChortcut: () => string;
onSaveShortcut: (fn: () => void) => string;
}

declare global {
Expand Down

0 comments on commit 1e639ab

Please sign in to comment.