Small enhancements, including a method to build an identififer param based on the type of identifier

Jens Pelzetter 2020-08-02 11:42:11 +02:00
parent bffe5ca549
commit e72f901119
2 changed files with 110 additions and 67 deletions

View File

@ -41,7 +41,7 @@ export interface LibreCcmApiClient {
*/ */
put( put(
endpoint: string, endpoint: string,
body: RequestBody, body?: RequestBody,
searchParams?: Record<string, string> searchParams?: Record<string, string>
): Promise<ApiResponse>; ): Promise<ApiResponse>;
/** /**
@ -298,19 +298,19 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
); );
} }
} catch (err) { } catch (err) {
throw new ApiError ( throw new ApiError(
-1, -1,
"n/a", "n/a",
"get", "get",
`Failed to execute get: ${err}`, `Failed to execute get: ${err}`,
url, url
); );
} }
} }
async put( async put(
endpoint: string, endpoint: string,
body: RequestBody, body?: RequestBody,
searchParams?: Record<string, string> searchParams?: Record<string, string>
): Promise<ApiResponse> { ): Promise<ApiResponse> {
const url = buildUrl(this.#baseUrl, endpoint, searchParams); const url = buildUrl(this.#baseUrl, endpoint, searchParams);
@ -327,16 +327,16 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
response.statusText, response.statusText,
"get", "get",
"API responded with an error.", "API responded with an error.",
url, url
); );
} }
} catch (err) { } catch (err) {
throw new ApiError ( throw new ApiError(
-1, -1,
"n/a", "n/a",
"get", "get",
`Failed to execute get: ${err}`, `Failed to execute get: ${err}`,
url, url
); );
} }
} }
@ -351,21 +351,21 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
if (response.ok) { if (response.ok) {
return new FetchResponse(response); return new FetchResponse(response);
} else { } else {
throw new ApiError ( throw new ApiError(
response.status, response.status,
response.statusText, response.statusText,
"get", "get",
"API responded with an error.", "API responded with an error.",
url, url
); );
} }
} catch (err) { } catch (err) {
throw new ApiError ( throw new ApiError(
-1, -1,
"n/a", "n/a",
"get", "get",
`Failed to execute get: ${err}`, `Failed to execute get: ${err}`,
url, url
); );
} }
} }
@ -377,21 +377,21 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
if (response.ok) { if (response.ok) {
return new FetchResponse(response); return new FetchResponse(response);
} else { } else {
throw new ApiError ( throw new ApiError(
response.status, response.status,
response.statusText, response.statusText,
"get", "get",
"API responded with an error.", "API responded with an error.",
url, url
); );
} }
} catch (err) { } catch (err) {
throw new ApiError ( throw new ApiError(
-1, -1,
"n/a", "n/a",
"get", "get",
`Failed to execute get: ${err}`, `Failed to execute get: ${err}`,
url, url
); );
} }
} }
@ -406,12 +406,12 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
if (response.ok) { if (response.ok) {
return new FetchResponse(response); return new FetchResponse(response);
} else { } else {
throw new ApiError ( throw new ApiError(
response.status, response.status,
response.statusText, response.statusText,
"get", "get",
"API responded with an error.", "API responded with an error.",
url, url
); );
} }
} catch (err) { } catch (err) {
@ -420,7 +420,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
"n/a", "n/a",
"get", "get",
`Failed to execute get: ${err}`, `Failed to execute get: ${err}`,
url, url
); );
} }
} }
@ -546,15 +546,17 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
resolve(apiResponse); resolve(apiResponse);
}); });
}); });
request.on("error", (error) => reject( request.on("error", (error) =>
new ApiError( reject(
-1, new ApiError(
"n/a", -1,
"get", "n/a",
`Failed to do GET: ${error}`, "get",
url `Failed to do GET: ${error}`,
url
)
) )
)); );
request.end(); request.end();
}); });
@ -593,15 +595,21 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
}); });
}); });
request.on("error", (error) => reject(new ApiError( request.on("error", (error) =>
-1, reject(
"n/a", new ApiError(
"post", -1,
`Failed to do POST: ${error}`, "n/a",
url "post",
))); `Failed to do POST: ${error}`,
url
)
)
);
request.write(body); if (body) {
request.write(body);
}
request.end(); request.end();
}); });
@ -609,7 +617,7 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
put( put(
endpoint: string, endpoint: string,
body: ArrayBuffer, body?: ArrayBuffer,
searchParams?: Record<string, string> searchParams?: Record<string, string>
): Promise<ApiResponse> { ): Promise<ApiResponse> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -640,15 +648,21 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
}); });
}); });
request.on("error", (error) => reject(new ApiError( request.on("error", (error) =>
-1, reject(
"n/a", new ApiError(
"put", -1,
`Failed to do PUT: ${error}`, "n/a",
url "put",
))); `Failed to do PUT: ${error}`,
url
)
)
);
request.write(body); if (body) {
request.write(body);
}
request.end(); request.end();
}); });
@ -686,13 +700,17 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
}); });
}); });
request.on("error", (error) => reject(new ApiError( request.on("error", (error) =>
-1, reject(
"n/a", new ApiError(
"delete", -1,
`Failed to do DELETE: ${error}`, "n/a",
url "delete",
))); `Failed to do DELETE: ${error}`,
url
)
)
);
request.end(); request.end();
}); });
@ -732,13 +750,17 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
resolve(apiResponse); resolve(apiResponse);
}); });
}); });
request.on("error", (error) => reject(new ApiError( request.on("error", (error) =>
-1, reject(
"n/a", new ApiError(
"head", -1,
`Failed to do HEAD: ${error}`, "n/a",
url "head",
))); `Failed to do HEAD: ${error}`,
url
)
)
);
request.end(); request.end();
}); });
@ -778,13 +800,17 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
resolve(apiResponse); resolve(apiResponse);
}); });
}); });
request.on("error", (error) => reject(new ApiError( request.on("error", (error) =>
-1, reject(
"n/a", new ApiError(
"options", -1,
`Failed to do OPTIONS: ${error}`, "n/a",
url "options",
))); `Failed to do OPTIONS: ${error}`,
url
)
)
);
request.end(); request.end();
}); });
@ -830,7 +856,7 @@ export class IsomorphicClientImpl implements LibreCcmApiClient {
put( put(
endpoint: string, endpoint: string,
body: ArrayBuffer, body?: ArrayBuffer,
searchParams?: Record<string, string> searchParams?: Record<string, string>
): Promise<ApiResponse> { ): Promise<ApiResponse> {
if (isFetchAvailable()) { if (isFetchAvailable()) {

View File

@ -10,7 +10,7 @@ import {
} from "./ApiClient"; } from "./ApiClient";
export * from "./entities"; export * from "./entities";
export { LibreCcmApiClient, ApiResponse, ApiError as ApiError, buildUrl }; export { LibreCcmApiClient, ApiResponse, ApiError, buildUrl };
/** /**
* Build an client for the LibreCCM RESTful API suitable for use in * Build an client for the LibreCCM RESTful API suitable for use in
@ -139,3 +139,20 @@ export function findMissingProperties(
const keys = Object.keys(record); const keys = Object.keys(record);
return properties.filter((property) => !keys.includes(property)); return properties.filter((property) => !keys.includes(property));
} }
export function buildIdentifierParam(identifier: string | number): string {
if (typeof identifier === "number") {
return `ID-${identifier}`;
} else {
const strIdentifier: string = identifier as string;
if (
strIdentifier.match(
/[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/g
)
) {
return `UUID-${identifier}`;
} else {
return identifier;
}
}
}