Skip to content

Commit

Permalink
fix send request with query parameters (#85)
Browse files Browse the repository at this point in the history
fix: to support send request with query parameters

Refs: #77
  • Loading branch information
AnnChord authored Aug 15, 2024
1 parent 5d6f9db commit b97891e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/common/monaco/tokenlizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export const buildSearchToken = (lines: Array<{ lineNumber: number; lineContent:
const commands = lines.filter(({ lineContent }) => executeActions.regexp.test(lineContent));

return commands.map(({ lineContent, lineNumber }, index, commands) => {
const rawCmd = lineContent.split(/[/\s]+/);
const [rawPath, queryParams] = lineContent.split('?');
const rawCmd = rawPath.split(/[\/\s]+/);
const method = rawCmd[0]?.toUpperCase();
const indexName = rawCmd[1]?.startsWith('_') ? undefined : rawCmd[1];
const path = rawCmd.slice(indexName ? 2 : 1, rawCmd.length).join('/');

const nexCommandLineNumber = commands[index + 1]?.lineNumber
? commands[index + 1]?.lineNumber - 1
: lines.length;
Expand All @@ -28,6 +30,7 @@ export const buildSearchToken = (lines: Array<{ lineNumber: number; lineContent:
method,
index: indexName,
path,
queryParams,
actionPosition: {
startLineNumber: lineNumber,
endLineNumber: lineNumber,
Expand Down
1 change: 1 addition & 0 deletions src/common/monaco/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type SearchAction = {
method: string;
index: string;
path: string;
queryParams: string | null;
};

export enum ActionType {
Expand Down
14 changes: 9 additions & 5 deletions src/store/connectionStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineStore } from 'pinia';
import { pureObject } from '../common';
import { loadHttpClient, storeApi } from '../datasources';

export type Connection = {
id?: number;
name: string;
Expand Down Expand Up @@ -131,14 +132,17 @@ export const useConnectionStore = defineStore('connectionStore', {
path,
index,
qdsl,
queryParams,
}: {
method: string;
path: string;
queryParams?: string;
index?: string;
qdsl?: string;
}) {
if (!this.established) throw new Error('no connection established');
const client = loadHttpClient(this.established);
const queryParameters = queryParams ? `${queryParams}&format=json` : 'format=json';
// refresh the index mapping
try {
if (index && index !== this.established.activeIndex?.index) {
Expand All @@ -147,7 +151,7 @@ export const useConnectionStore = defineStore('connectionStore', {
);
if (newIndex) {
if (!newIndex.mapping) {
newIndex.mapping = await client.get(`/${index}/_mapping`, 'format=json');
newIndex.mapping = await client.get(`/${index}/_mapping`, queryParameters);
}
this.established = { ...this.established, activeIndex: newIndex };
}
Expand All @@ -157,11 +161,11 @@ export const useConnectionStore = defineStore('connectionStore', {
const reqPath = buildPath(index, path);

const dispatch: { [method: string]: () => Promise<unknown> } = {
POST: async () => client.post(reqPath, undefined, qdsl),
PUT: async () => client.put(reqPath, undefined, qdsl),
DELETE: async () => client.delete(reqPath, undefined, qdsl),
POST: async () => client.post(reqPath, queryParameters, qdsl),
PUT: async () => client.put(reqPath, queryParameters, qdsl),
DELETE: async () => client.delete(reqPath, queryParameters, qdsl),
GET: async () =>
qdsl ? client.post(reqPath, undefined, qdsl) : client.get(reqPath, 'format=json'),
qdsl ? client.post(reqPath, queryParams, qdsl) : client.get(reqPath, queryParameters),
};
return dispatch[method]();
},
Expand Down
1 change: 1 addition & 0 deletions src/views/editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ const executeQueryAction = async (
}
const data = await searchQDSL({
...action,
queryParams: action.queryParams ?? undefined,
qdsl: action.qdsl ?? undefined,
index: action.index || established.value?.activeIndex?.index,
});
Expand Down

0 comments on commit b97891e

Please sign in to comment.