Small enhancements, including a method to build an identififer param based on the type of identifier
parent
bffe5ca549
commit
e72f901119
|
|
@ -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,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()) {
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue