@cubicweb/client
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -8,5 +8,6 @@ import { RawSchema } from "../schema/SchemaJSON"; | ||
export declare type ResultSet = Array<RqlRow>; | ||
export declare type ApiError = { | ||
error: string; | ||
declare type ApiError = { | ||
title: string; | ||
message: string; | ||
data?: Record<string, unknown>; | ||
}; | ||
@@ -28,2 +29,3 @@ export declare type ApiErrorResponse = { | ||
} | ||
export {}; | ||
//# sourceMappingURL=Api.d.ts.map |
@@ -14,4 +14,10 @@ "use strict"; | ||
constructor(apiUrl) { | ||
this.transactionApiUrl = { | ||
begin: "", | ||
execute: "", | ||
commit: "", | ||
rollback: "", | ||
}; | ||
this.apiUrl = `${apiUrl}/v1`; | ||
this.transactionApiUrl = `${this.apiUrl}/transaction`; | ||
Object.keys(this.transactionApiUrl).forEach((k) => (this.transactionApiUrl[k] = `${this.apiUrl}/transaction/${k}`)); | ||
} | ||
@@ -51,5 +57,4 @@ execute(query, params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return nonNullFetchApi(this.transactionApiUrl, { | ||
return nonNullFetchApi(this.transactionApiUrl.begin, { | ||
method: "POST", | ||
body: JSON.stringify({ action: "begin" }), | ||
credentials: "include", | ||
@@ -61,5 +66,5 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return nonNullFetchApi(this.transactionApiUrl, { | ||
return nonNullFetchApi(this.transactionApiUrl.execute, { | ||
method: "POST", | ||
body: JSON.stringify({ action: "execute", uuid, query, params }), | ||
body: JSON.stringify({ uuid, query, params }), | ||
credentials: "include", | ||
@@ -71,5 +76,5 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return fetchApi(this.transactionApiUrl, { | ||
return fetchApi(this.transactionApiUrl.commit, { | ||
method: "POST", | ||
body: JSON.stringify({ action: "commit", uuid }), | ||
body: JSON.stringify({ uuid }), | ||
credentials: "include", | ||
@@ -81,5 +86,5 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return fetchApi(this.transactionApiUrl, { | ||
return fetchApi(this.transactionApiUrl.rollback, { | ||
method: "POST", | ||
body: JSON.stringify({ action: "commit", uuid }), | ||
body: JSON.stringify({ uuid }), | ||
credentials: "include", | ||
@@ -97,3 +102,3 @@ }); | ||
status: 500, | ||
error: "Unexpected null response", | ||
title: "Unexpected null response", | ||
message: "Response was null but a value was expected", | ||
@@ -110,3 +115,7 @@ }; | ||
const fetchApi = (input, init) => { | ||
return new Promise((resolve, reject) => fetch(input, init).then((response) => { | ||
let headers = init === null || init === void 0 ? void 0 : init.headers; | ||
if ((init === null || init === void 0 ? void 0 : init.method) && init.method !== "GET" && init.method !== "DELETE") { | ||
headers = Object.assign({ "Content-Type": "application/json;charset=UTF-8" }, init === null || init === void 0 ? void 0 : init.headers); | ||
} | ||
return new Promise((resolve, reject) => fetch(input, Object.assign(Object.assign({}, init), { headers })).then((response) => { | ||
const status = response.status; | ||
@@ -129,7 +138,3 @@ if (status >= 200 && status < 300) { | ||
response.json().then((data) => { | ||
reject({ | ||
status, | ||
error: data.error, | ||
message: data.message, | ||
}); | ||
reject(Object.assign({ status }, data)); | ||
}); | ||
@@ -136,0 +141,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"author": "Logilab", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"license": "LGPL-3.0-or-later", | ||
@@ -8,0 +8,0 @@ "main": "lib/index.js", |
@@ -11,3 +11,7 @@ import { RawSchema } from "../schema/SchemaJSON"; | ||
export type ResultSet = Array<RqlRow>; | ||
export type ApiError = { error: string; message: string }; | ||
type ApiError = { | ||
title: string; | ||
message: string; | ||
data?: Record<string, unknown>; | ||
}; | ||
export type ApiErrorResponse = { | ||
@@ -19,7 +23,17 @@ status: number; | ||
private apiUrl: string; | ||
private transactionApiUrl: string; | ||
private transactionApiUrl = { | ||
begin: "", | ||
execute: "", | ||
commit: "", | ||
rollback: "", | ||
}; | ||
constructor(apiUrl: string) { | ||
this.apiUrl = `${apiUrl}/v1`; | ||
this.transactionApiUrl = `${this.apiUrl}/transaction`; | ||
Object.keys(this.transactionApiUrl).forEach( | ||
(k) => | ||
(this.transactionApiUrl[ | ||
k as keyof typeof this.transactionApiUrl | ||
] = `${this.apiUrl}/transaction/${k}`) | ||
); | ||
} | ||
@@ -56,5 +70,4 @@ | ||
async transactionBegin(): Promise<string> { | ||
return nonNullFetchApi<string>(this.transactionApiUrl, { | ||
return nonNullFetchApi<string>(this.transactionApiUrl.begin, { | ||
method: "POST", | ||
body: JSON.stringify({ action: "begin" }), | ||
credentials: "include", | ||
@@ -69,5 +82,5 @@ }); | ||
): Promise<ResultSet> { | ||
return nonNullFetchApi<ResultSet>(this.transactionApiUrl, { | ||
return nonNullFetchApi<ResultSet>(this.transactionApiUrl.execute, { | ||
method: "POST", | ||
body: JSON.stringify({ action: "execute", uuid, query, params }), | ||
body: JSON.stringify({ uuid, query, params }), | ||
credentials: "include", | ||
@@ -78,5 +91,5 @@ }); | ||
async transactionCommit(uuid: string): Promise<null> { | ||
return fetchApi<null>(this.transactionApiUrl, { | ||
return fetchApi<null>(this.transactionApiUrl.commit, { | ||
method: "POST", | ||
body: JSON.stringify({ action: "commit", uuid }), | ||
body: JSON.stringify({ uuid }), | ||
credentials: "include", | ||
@@ -87,5 +100,5 @@ }); | ||
async transactionRollback(uuid: string): Promise<null> { | ||
return fetchApi<null>(this.transactionApiUrl, { | ||
return fetchApi<null>(this.transactionApiUrl.rollback, { | ||
method: "POST", | ||
body: JSON.stringify({ action: "commit", uuid }), | ||
body: JSON.stringify({ uuid }), | ||
credentials: "include", | ||
@@ -107,3 +120,3 @@ }); | ||
status: 500, | ||
error: "Unexpected null response", | ||
title: "Unexpected null response", | ||
message: "Response was null but a value was expected", | ||
@@ -121,2 +134,9 @@ }; | ||
const fetchApi = <R = unknown>(input: RequestInfo, init?: RequestInit) => { | ||
let headers = init?.headers; | ||
if (init?.method && init.method !== "GET" && init.method !== "DELETE") { | ||
headers = { | ||
"Content-Type": "application/json;charset=UTF-8", | ||
...init?.headers, | ||
}; | ||
} | ||
return new Promise( | ||
@@ -127,3 +147,6 @@ ( | ||
) => | ||
fetch(input, init).then((response) => { | ||
fetch(input, { | ||
...init, | ||
headers, | ||
}).then((response) => { | ||
const status = response.status; | ||
@@ -146,4 +169,3 @@ if (status >= 200 && status < 300) { | ||
status, | ||
error: data.error, | ||
message: data.message, | ||
...data, | ||
}); | ||
@@ -150,0 +172,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
79015
1593