mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-09-20 00:27:17 +03:00
Refactor parameters in httpClient.ts
- it's convention for first param to be url
This commit is contained in:
parent
7aa30724e7
commit
92b12ebad7
@ -11,8 +11,7 @@ export class ButlerAIClient implements AIClient {
|
|||||||
async evaluate(prompt: string) {
|
async evaluate(prompt: string) {
|
||||||
const messages: PromptMessage[] = [{ role: MessageRole.User, content: prompt }];
|
const messages: PromptMessage[] = [{ role: MessageRole.User, content: prompt }];
|
||||||
|
|
||||||
const response = await this.cloud.post<{ message: string }>({
|
const response = await this.cloud.post<{ message: string }>('evaluate_prompt/predict.json', {
|
||||||
path: 'evaluate_prompt/predict.json',
|
|
||||||
body: {
|
body: {
|
||||||
messages,
|
messages,
|
||||||
max_tokens: 400,
|
max_tokens: 400,
|
||||||
|
@ -7,32 +7,23 @@ export const DEFAULT_HEADERS = {
|
|||||||
|
|
||||||
export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
||||||
|
|
||||||
function getApiUrl(path: string) {
|
type RequestOptions = {
|
||||||
return new URL(path, API_URL);
|
headers?: Record<string, string | undefined>;
|
||||||
}
|
body?: FormData | object;
|
||||||
|
token?: string;
|
||||||
|
};
|
||||||
|
|
||||||
export class HttpClient {
|
export class HttpClient {
|
||||||
constructor(public fetch = window.fetch) {}
|
constructor(public fetch = window.fetch) {}
|
||||||
|
|
||||||
private formatBody(body?: FormData | object) {
|
private async request<T>(
|
||||||
if (body instanceof FormData) {
|
path: string,
|
||||||
return body;
|
opts: RequestOptions & { method: RequestMethod }
|
||||||
} else if (body) {
|
): Promise<T> {
|
||||||
return JSON.stringify(body);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async request<T>(params: {
|
|
||||||
path: string;
|
|
||||||
method: RequestMethod;
|
|
||||||
token?: string;
|
|
||||||
body?: FormData | object;
|
|
||||||
headers?: Record<string, string | undefined>;
|
|
||||||
}): Promise<T> {
|
|
||||||
const butlerHeaders = new Headers(DEFAULT_HEADERS);
|
const butlerHeaders = new Headers(DEFAULT_HEADERS);
|
||||||
|
|
||||||
if (params.headers) {
|
if (opts.headers) {
|
||||||
Object.entries(params.headers).forEach(([key, value]) => {
|
Object.entries(opts.headers).forEach(([key, value]) => {
|
||||||
if (value) {
|
if (value) {
|
||||||
butlerHeaders.set(key, value);
|
butlerHeaders.set(key, value);
|
||||||
} else {
|
} else {
|
||||||
@ -41,58 +32,42 @@ export class HttpClient {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.token) butlerHeaders.set('X-Auth-Token', params.token);
|
if (opts.token) butlerHeaders.set('X-Auth-Token', opts.token);
|
||||||
|
|
||||||
const response = await this.fetch(getApiUrl(params.path), {
|
const response = await this.fetch(getApiUrl(path), {
|
||||||
method: params.method || 'GET',
|
method: opts.method,
|
||||||
headers: butlerHeaders,
|
headers: butlerHeaders,
|
||||||
body: this.formatBody(params.body)
|
body: formatBody(opts.body)
|
||||||
});
|
});
|
||||||
|
|
||||||
return parseResponseJSON(response);
|
return parseResponseJSON(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
get<T>(params: { path: string; token?: string; headers?: Record<string, string | undefined> }) {
|
async get<T>(path: string, opts?: Omit<RequestOptions, 'body'>) {
|
||||||
return this.request<T>({ ...params, method: 'GET' });
|
return await this.request<T>(path, { ...opts, method: 'GET' });
|
||||||
}
|
}
|
||||||
|
|
||||||
post<T>(params: {
|
async post<T>(path: string, opts?: RequestOptions) {
|
||||||
path: string;
|
return await this.request<T>(path, { ...opts, method: 'POST' });
|
||||||
token?: string;
|
|
||||||
body?: FormData | object;
|
|
||||||
headers?: Record<string, string | undefined>;
|
|
||||||
}) {
|
|
||||||
return this.request<T>({ ...params, method: 'POST' });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
put<T>(params: {
|
async put<T>(path: string, opts?: RequestOptions) {
|
||||||
path: string;
|
return this.request<T>(path, { ...opts, method: 'PUT' });
|
||||||
token?: string;
|
|
||||||
body?: FormData | object;
|
|
||||||
headers?: Record<string, string | undefined>;
|
|
||||||
}) {
|
|
||||||
return this.request<T>({ ...params, method: 'PUT' });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
patch<T>(params: {
|
async patch<T>(path: string, opts?: RequestOptions) {
|
||||||
path: string;
|
return this.request<T>(path, { ...opts, method: 'PATCH' });
|
||||||
token?: string;
|
|
||||||
body?: FormData | object;
|
|
||||||
headers?: Record<string, string | undefined>;
|
|
||||||
}) {
|
|
||||||
return this.request<T>({ ...params, method: 'PATCH' });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete<T>(params: {
|
async delete<T>(path: string, opts?: RequestOptions) {
|
||||||
path: string;
|
return await this.request<T>(path, { ...opts, method: 'DELETE' });
|
||||||
token?: string;
|
|
||||||
body?: FormData | object;
|
|
||||||
headers?: Record<string, string | undefined>;
|
|
||||||
}) {
|
|
||||||
return this.request<T>({ ...params, method: 'DELETE' });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getApiUrl(path: string) {
|
||||||
|
return new URL(path, API_URL);
|
||||||
|
}
|
||||||
|
|
||||||
async function parseResponseJSON(response: Response) {
|
async function parseResponseJSON(response: Response) {
|
||||||
if (response.status === 204 || response.status === 205) {
|
if (response.status === 204 || response.status === 205) {
|
||||||
return null;
|
return null;
|
||||||
@ -102,3 +77,8 @@ async function parseResponseJSON(response: Response) {
|
|||||||
return await response.json();
|
return await response.json();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatBody(body?: FormData | object) {
|
||||||
|
if (!body) return;
|
||||||
|
return body instanceof FormData ? body : JSON.stringify(body);
|
||||||
|
}
|
||||||
|
@ -134,8 +134,7 @@ export class ProjectService {
|
|||||||
uid?: string;
|
uid?: string;
|
||||||
}
|
}
|
||||||
): Promise<CloudProject> {
|
): Promise<CloudProject> {
|
||||||
return this.httpClient.post({
|
return this.httpClient.post('projects.json', {
|
||||||
path: 'projects.json',
|
|
||||||
body: params,
|
body: params,
|
||||||
token
|
token
|
||||||
});
|
});
|
||||||
@ -149,16 +148,14 @@ export class ProjectService {
|
|||||||
description?: string;
|
description?: string;
|
||||||
}
|
}
|
||||||
): Promise<CloudProject> {
|
): Promise<CloudProject> {
|
||||||
return this.httpClient.put({
|
return this.httpClient.put(`projects/${repositoryId}.json`, {
|
||||||
path: `projects/${repositoryId}.json`,
|
|
||||||
body: params,
|
body: params,
|
||||||
token
|
token
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getCloudProject(token: string, repositoryId: string): Promise<CloudProject> {
|
getCloudProject(token: string, repositoryId: string): Promise<CloudProject> {
|
||||||
return this.httpClient.get({
|
return this.httpClient.get(`projects/${repositoryId}.json`, {
|
||||||
path: `projects/${repositoryId}.json`,
|
|
||||||
token
|
token
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,7 @@
|
|||||||
if (params.data) formData.append('data', params.data);
|
if (params.data) formData.append('data', params.data);
|
||||||
|
|
||||||
// Content Type must be unset for the right form-data border to be set automatically
|
// Content Type must be unset for the right form-data border to be set automatically
|
||||||
return httpClient.put({
|
return httpClient.put('feedback', {
|
||||||
path: 'feedback',
|
|
||||||
body: formData,
|
body: formData,
|
||||||
headers: { 'Content-Type': undefined },
|
headers: { 'Content-Type': undefined },
|
||||||
token
|
token
|
||||||
|
@ -81,7 +81,7 @@ export class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async createLoginToken(): Promise<LoginToken> {
|
async createLoginToken(): Promise<LoginToken> {
|
||||||
const token = await this.httpClient.post<LoginToken>({ path: 'login/token.json' });
|
const token = await this.httpClient.post<LoginToken>('login/token.json');
|
||||||
const url = new URL(token.url);
|
const url = new URL(token.url);
|
||||||
url.host = API_URL.host;
|
url.host = API_URL.host;
|
||||||
return {
|
return {
|
||||||
@ -104,11 +104,11 @@ export class UserService {
|
|||||||
|
|
||||||
// TODO: Remove token from URL, we don't want that leaking into logs.
|
// TODO: Remove token from URL, we don't want that leaking into logs.
|
||||||
getLoginUser(token: string): Promise<User> {
|
getLoginUser(token: string): Promise<User> {
|
||||||
return this.httpClient.get({ path: `login/user/${token}.json` });
|
return this.httpClient.get(`login/user/${token}.json`);
|
||||||
}
|
}
|
||||||
|
|
||||||
getUser(token: string): Promise<User> {
|
getUser(token: string): Promise<User> {
|
||||||
return this.httpClient.get({ path: 'user.json', token });
|
return this.httpClient.get('user.json', { token });
|
||||||
}
|
}
|
||||||
|
|
||||||
updateUser(token: string, params: { name?: string; picture?: File }): Promise<any> {
|
updateUser(token: string, params: { name?: string; picture?: File }): Promise<any> {
|
||||||
@ -117,8 +117,7 @@ export class UserService {
|
|||||||
if (params.picture) formData.append('avatar', params.picture);
|
if (params.picture) formData.append('avatar', params.picture);
|
||||||
|
|
||||||
// Content Type must be unset for the right form-data border to be set automatically
|
// Content Type must be unset for the right form-data border to be set automatically
|
||||||
return this.httpClient.put({
|
return this.httpClient.put('user.json', {
|
||||||
path: 'user.json',
|
|
||||||
body: formData,
|
body: formData,
|
||||||
headers: { 'Content-Type': undefined },
|
headers: { 'Content-Type': undefined },
|
||||||
token
|
token
|
||||||
|
Loading…
Reference in New Issue
Block a user