Skip to content

Commit

Permalink
Allow http protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
JennieJi committed May 21, 2020
1 parent d241bc8 commit 74bc74c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
18 changes: 12 additions & 6 deletions src/SnippetRegistry.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -33,10 +35,14 @@ class SnippetRegistry {
}

public getSnippets(page?: number, perPage?: number): Promise<Snippet[]> {
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()
);
Expand Down
13 changes: 4 additions & 9 deletions src/addHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 74bc74c

Please sign in to comment.