From 16b791a3f82844b87d24a715f3fa267e335dc95d Mon Sep 17 00:00:00 2001 From: "Kull Zacharias (IT-SCF-F-BIM)" Date: Sat, 25 May 2024 23:39:49 +0200 Subject: [PATCH] fix makeRequest --- src/BitbucketServerProvider.tsx | 25 ++++++++++++++++--------- src/DataProvider.tsx | 4 ---- src/Table.tsx | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/BitbucketServerProvider.tsx b/src/BitbucketServerProvider.tsx index 8f0765b..146005d 100644 --- a/src/BitbucketServerProvider.tsx +++ b/src/BitbucketServerProvider.tsx @@ -12,6 +12,12 @@ interface LoginInput { password: string; } +interface RequestParams { + path: string + method?: string + body?: any +} + function LoginForm(props: {provider: BitbucketServerProvider, defaults: LoginInput, login: (LoginInput) => Promise, setProvider: (DataProvider) => void, showError: (string) => void}) { const { handleSubmit, register, formState } = useForm({ defaultValues: props.defaults, @@ -19,11 +25,10 @@ function LoginForm(props: {provider: BitbucketServerProvider, defaults: LoginInp const { errors } = formState; function submit(x: LoginInput) { - new Promise(() => console.log("start login")) + Promise.resolve() .then(() => props.login(x)) .then(() => props.setProvider(props.provider)) .catch(e => { - console.log("submit", String(e)); props.showError(String(e)); props.provider.reset(); throw e; @@ -75,16 +80,17 @@ export class BitbucketServerProvider extends DataProvider { } } - private makeRequest(path: string = "", method: string = 'GET', body?: any): Promise { + private makeRequest(request: RequestParams): Promise { + const {path, method, body} = request; // if project isn't specified, user name is taken as project const project = this.loginInput!.project || this.loginInput!.user; const repoUrl = this.baseUrl + "/projects/" + project + "/repos/" + this.loginInput!.repo; + console.log("makeRequest", repoUrl+path) // prepare headers const headers = new Headers(); - headers.append("Content-Type", "application/json"); headers.append("Authorization", this.authVal!); // fetch - return fetch(repoUrl+path, {method: method, headers: headers, body: (body ? JSON.stringify(body) : undefined)}) + return fetch(repoUrl+path, {method: method, headers: headers, body: body}) .then( response => { if (response.ok) return response; else throw Error( "Http error: " + response.status + " " + response.statusText + " (" + method + " "+repoUrl+path +")"); @@ -96,6 +102,7 @@ export class BitbucketServerProvider extends DataProvider { }; login(that: BitbucketServerProvider, input: LoginInput): Promise { + console.log("login"); that.loginInput = input; that.authVal = "Basic " + btoa( that.loginInput!.user + ":" + that.loginInput!.password ); // test request @@ -105,7 +112,7 @@ export class BitbucketServerProvider extends DataProvider { getData() { if(!this.loginInput) throw Error( "Connection parameters not set."); if(!this.data) { - this.data = this.makeRequest("/raw/"+this.loginInput!.path+"?at="+this.loginInput!.branch) + this.data = this.makeRequest({path: "/raw/"+this.loginInput!.path+"?at="+this.loginInput!.branch}) .then( response => response.json()) .then( data => { if (isArray(data)) return data as []; @@ -122,7 +129,7 @@ export class BitbucketServerProvider extends DataProvider { getSchema() { if(!this.loginInput) throw Error( "Connection parameters not set."); if(!this.config) { - this.config = this.makeRequest("/raw/"+this.getSchemaPath()+"?at="+this.loginInput!.branch) + this.config = this.makeRequest({path: "/raw/"+this.getSchemaPath()+"?at="+this.loginInput!.branch}) .then( response => response.json()) .then( data => { if (isObject(data)) return data as {} @@ -133,7 +140,7 @@ export class BitbucketServerProvider extends DataProvider { }; rememberDataCommitId() { - this.dataCommitId = this.makeRequest("/commits?path="+this.loginInput!.path+"&until="+this.loginInput!.branch+"&limit=1") + this.dataCommitId = this.makeRequest({path: "/commits?path="+this.loginInput!.path+"&until="+this.loginInput!.branch+"&limit=1"}) .then( response => response.json()) .then( data => { if (data.values.length>0) { @@ -161,7 +168,7 @@ export class BitbucketServerProvider extends DataProvider { postData.append("message", msg); postData.append("branch", this.loginInput!.branch!); postData.append("sourceCommitId", commitId!); - this.dataCommitId = this.makeRequest("/browse/"+this.loginInput!.path, "PUT", postData) + this.dataCommitId = this.makeRequest({path: "/browse/"+this.loginInput!.path, method: "PUT", body: postData}) .then( response => response.json()) .then( data => { console.log("new commitId", data); diff --git a/src/DataProvider.tsx b/src/DataProvider.tsx index bf1c937..a536573 100644 --- a/src/DataProvider.tsx +++ b/src/DataProvider.tsx @@ -16,10 +16,6 @@ export abstract class DataProvider { const idCols: string[] = this.getMetadata(schema, "idCols"); return data.forEach((e: any, idx) => this.addDataIdProperty(e, idCols.map(c => e[c]).join("-"))); }) - .catch(e => { - console.log("calcDataId Promise.all", String(e)); - throw e; - }); } addDataIdProperty(e: any, id: string) { // Object.defineProperty is used to create a property with enumerable: false, so that the artifical RECORD_ID property is not exported on save. diff --git a/src/Table.tsx b/src/Table.tsx index 044ff7a..8fc14a0 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -34,7 +34,7 @@ export default function Table(props: {show: boolean}) { showCommitDialog(false); // also refreshes button state // if msg is empty, commit dialog was cancelled if (msg) { - new Promise(() => {}) + Promise.resolve() .then(() => provider.saveData(msg)) .then(() => showSnackbar({msg: "Commit successfull", severity: "info"})) .catch(e => {