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

Former-commit-id: e72f901119
restapi
Jens Pelzetter 2020-08-02 11:42:11 +02:00
parent ea1750ecd8
commit b2b8c62389
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>;
/** /**
@ -303,14 +303,14 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
"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,7 +327,7 @@ 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) {
@ -336,7 +336,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
"n/a", "n/a",
"get", "get",
`Failed to execute get: ${err}`, `Failed to execute get: ${err}`,
url, url
); );
} }
} }
@ -356,7 +356,7 @@ 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) {
@ -365,7 +365,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
"n/a", "n/a",
"get", "get",
`Failed to execute get: ${err}`, `Failed to execute get: ${err}`,
url, url
); );
} }
} }
@ -382,7 +382,7 @@ 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) {
@ -391,7 +391,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
"n/a", "n/a",
"get", "get",
`Failed to execute get: ${err}`, `Failed to execute get: ${err}`,
url, url
); );
} }
} }
@ -411,7 +411,7 @@ 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) {
@ -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,7 +546,8 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
resolve(apiResponse); resolve(apiResponse);
}); });
}); });
request.on("error", (error) => reject( request.on("error", (error) =>
reject(
new ApiError( new ApiError(
-1, -1,
"n/a", "n/a",
@ -554,7 +555,8 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
`Failed to do GET: ${error}`, `Failed to do GET: ${error}`,
url 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) =>
reject(
new ApiError(
-1, -1,
"n/a", "n/a",
"post", "post",
`Failed to do POST: ${error}`, `Failed to do POST: ${error}`,
url url
))); )
)
);
if (body) {
request.write(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) =>
reject(
new ApiError(
-1, -1,
"n/a", "n/a",
"put", "put",
`Failed to do PUT: ${error}`, `Failed to do PUT: ${error}`,
url url
))); )
)
);
if (body) {
request.write(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) =>
reject(
new ApiError(
-1, -1,
"n/a", "n/a",
"delete", "delete",
`Failed to do DELETE: ${error}`, `Failed to do DELETE: ${error}`,
url 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) =>
reject(
new ApiError(
-1, -1,
"n/a", "n/a",
"head", "head",
`Failed to do HEAD: ${error}`, `Failed to do HEAD: ${error}`,
url 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) =>
reject(
new ApiError(
-1, -1,
"n/a", "n/a",
"options", "options",
`Failed to do OPTIONS: ${error}`, `Failed to do OPTIONS: ${error}`,
url 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;
}
}
}