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

View File

@ -10,7 +10,7 @@ import {
} from "./ApiClient";
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
@ -139,3 +139,20 @@ export function findMissingProperties(
const keys = Object.keys(record);
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;
}
}
}