diff --git a/src/SnippetRegistry.ts b/src/SnippetRegistry.ts index 485874a..a9a27b7 100644 --- a/src/SnippetRegistry.ts +++ b/src/SnippetRegistry.ts @@ -1,6 +1,7 @@ import fetch from 'node-fetch'; import { Host, Snippet, NewSnippet } from './types'; -import { URLSearchParams } from 'url'; +import { URLSearchParams, URL } from 'url'; +import { window } from 'vscode'; class SnippetRegistry { public host: Host; @@ -16,7 +17,8 @@ class SnippetRegistry { private endpoint(name: string) { const { host, version } = this.host; - return `${host}/api/v${version}/${name}`; + const ret = new URL(`/api/v${version}/${name}`, host); + return ret; } private get(endpoint: string) { @@ -33,10 +35,14 @@ class SnippetRegistry { } public getSnippets(page?: number, perPage?: number): Promise { - const search = new URLSearchParams({ - page: page ? page.toString() : undefined, - per_page: perPage ? perPage.toString() : undefined, - }); + const params = {} as { [key: string]: string }; + if (page) { + params.page = page.toString(); + } + if (perPage) { + params.perPage = perPage.toString(); + } + const search = new URLSearchParams(params); return this.get('snippets/public?' + search.toString()).then((res) => res.json() ); diff --git a/src/addHost.ts b/src/addHost.ts index 1615ee8..85e95e4 100644 --- a/src/addHost.ts +++ b/src/addHost.ts @@ -2,26 +2,21 @@ import { window, Memento } from 'vscode'; import SnippetRegistry from './SnippetRegistry'; import hostManager from './hostManager'; -const PROTOCOL = 'https://'; - export default async function addHost(state: Memento, defaultValue?: string) { let host = await window.showInputBox({ ignoreFocusOut: true, prompt: 'Enter your gitlab host', - value: defaultValue || PROTOCOL, + value: defaultValue || 'https://', validateInput(value) { - if (value && value.replace(PROTOCOL, '').trim()) { - return null; + if (!/https?:\/\//.test(value)) { + return 'Must start with "http://" or "https://" protocal'; } - return 'Must enter a valid host'; + return null; }, }); if (!host) { return; } - if (!host.startsWith(PROTOCOL)) { - host = PROTOCOL + host; - } const token = await window.showInputBox({ ignoreFocusOut: true, prompt: 'Enter your gitlab token',