Use Error instances for throw
parent
68e4208dc1
commit
edcc000d57
|
|
@ -2,6 +2,7 @@ import {
|
||||||
ApiResponse,
|
ApiResponse,
|
||||||
LibreCcmApiClient,
|
LibreCcmApiClient,
|
||||||
ListView,
|
ListView,
|
||||||
|
ApiClientError,
|
||||||
ApiError,
|
ApiError,
|
||||||
} from "@libreccm/ccm-apiclient-commons";
|
} from "@libreccm/ccm-apiclient-commons";
|
||||||
|
|
||||||
|
|
@ -34,14 +35,12 @@ export class WebApiClient {
|
||||||
limit: number,
|
limit: number,
|
||||||
offset: number
|
offset: number
|
||||||
): Promise<ListView<CcmApplication>> {
|
): Promise<ListView<CcmApplication>> {
|
||||||
|
const url = `${this.#APPS_API_PREFIX}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.get(
|
const response: ApiResponse = await this.#apiClient.get(url, {
|
||||||
`${this.#APPS_API_PREFIX}`,
|
limit: limit.toString(),
|
||||||
{
|
offset: offset.toString(),
|
||||||
limit: limit.toString(),
|
});
|
||||||
offset: offset.toString(),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const result: Record<
|
const result: Record<
|
||||||
|
|
@ -63,53 +62,86 @@ export class WebApiClient {
|
||||||
offset: result.offset as number,
|
offset: result.offset as number,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to get application instances:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"get",
|
||||||
|
"Failed to get application instances",
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to get application instances: ${err}`;
|
if (err instanceof ApiClientError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to get application instances: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getApplication(
|
async getApplication(
|
||||||
appIdentifier: string | number
|
appIdentifier: string | number
|
||||||
): Promise<CcmApplication> {
|
): Promise<CcmApplication> {
|
||||||
|
const url = `${this.#APPS_API_PREFIX}/${buildIdentifierParam(
|
||||||
|
appIdentifier
|
||||||
|
)}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.get(
|
const response: ApiResponse = await this.#apiClient.get(url);
|
||||||
`${this.#APPS_API_PREFIX}/${buildIdentifierParam(
|
|
||||||
appIdentifier
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return buildCcmApplicationFromRecord(
|
return buildCcmApplicationFromRecord(
|
||||||
(await response.json()) as Record<string, unknown>
|
(await response.json()) as Record<string, unknown>
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to get application instance ${appIdentifier}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"get",
|
||||||
|
`Failed to get application instance ${appIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to get application instance ${appIdentifier}: ${err}`;
|
if (err instanceof ApiClientError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to get application instance ${appIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async createApplication(application: CcmApplication): Promise<void> {
|
async createApplication(application: CcmApplication): Promise<void> {
|
||||||
|
const url = `${this.#APPS_API_PREFIX}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.post(
|
const response: ApiResponse = await this.#apiClient.post(
|
||||||
`${this.#APPS_API_PREFIX}`,
|
url,
|
||||||
JSON.stringify(application)
|
JSON.stringify(application)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to create new application instance of
|
throw new ApiError(
|
||||||
type ${application.applicationType}:
|
response.status,
|
||||||
${response.status} ${response.statusText}`;
|
response.statusText,
|
||||||
|
"post",
|
||||||
|
`Failed to create new application instance of type
|
||||||
|
${application.applicationType}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to create new application instance of
|
if (err instanceof ApiError) {
|
||||||
type ${application.applicationType}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to create new application instance of type
|
||||||
|
${application.applicationType}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -117,44 +149,64 @@ export class WebApiClient {
|
||||||
appIdentifier: string | number,
|
appIdentifier: string | number,
|
||||||
application: CcmApplication
|
application: CcmApplication
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
const url = `${this.#APPS_API_PREFIX}/${buildIdentifierParam(
|
||||||
|
appIdentifier
|
||||||
|
)}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.put(
|
const response: ApiResponse = await this.#apiClient.put(
|
||||||
`${this.#APPS_API_PREFIX}/${buildIdentifierParam(
|
url,
|
||||||
appIdentifier
|
|
||||||
)}`,
|
|
||||||
JSON.stringify(application)
|
JSON.stringify(application)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to update pplication instance ${appIdentifier}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"put",
|
||||||
|
`Failed to update application instance ${appIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to update application instance ${appIdentifier}:
|
if (err instanceof ApiError) {
|
||||||
${application.applicationType}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to update application instance ${appIdentifier}:
|
||||||
|
${application.applicationType}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteApplication(appIdentifier: string | number): Promise<void> {
|
async deleteApplication(appIdentifier: string | number): Promise<void> {
|
||||||
|
const url = `${this.#APPS_API_PREFIX}/${buildIdentifierParam(
|
||||||
|
appIdentifier
|
||||||
|
)}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.delete(
|
const response: ApiResponse = await this.#apiClient.delete(url);
|
||||||
`${this.#APPS_API_PREFIX}/${buildIdentifierParam(
|
|
||||||
appIdentifier
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to delete application
|
throw new ApiError(
|
||||||
instance ${appIdentifier}:
|
response.status,
|
||||||
${response.status} ${response.statusText}`;
|
response.statusText,
|
||||||
|
"delete",
|
||||||
|
`Failed to delete application instance ${appIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to delete application
|
if (err instanceof ApiError) {
|
||||||
instance ${appIdentifier}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to delete application instance ${appIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -163,16 +215,14 @@ export class WebApiClient {
|
||||||
limit: number,
|
limit: number,
|
||||||
offset: number
|
offset: number
|
||||||
): Promise<ListView<DomainOwnership>> {
|
): Promise<ListView<DomainOwnership>> {
|
||||||
|
const url = `${this.#APPS_API_PREFIX}/${buildIdentifierParam(
|
||||||
|
appIdentifier
|
||||||
|
)}/domains`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.get(
|
const response: ApiResponse = await this.#apiClient.get(url, {
|
||||||
`${this.#APPS_API_PREFIX}/${buildIdentifierParam(
|
limit: limit.toString(),
|
||||||
appIdentifier
|
offset: offset.toString(),
|
||||||
)}/domains`,
|
});
|
||||||
{
|
|
||||||
limit: limit.toString(),
|
|
||||||
offset: offset.toString(),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const result: Record<
|
const result: Record<
|
||||||
|
|
@ -194,13 +244,22 @@ export class WebApiClient {
|
||||||
offset: result.limit as number,
|
offset: result.limit as number,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to get domains of application
|
throw new ApiError(
|
||||||
instance ${appIdentifier}:
|
response.status,
|
||||||
${response.status} ${response.statusText}`;
|
response.statusText,
|
||||||
|
"get",
|
||||||
|
`Failed to get domains of application instance ${appIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to get domains of application
|
if (err instanceof ApiError) {
|
||||||
instance ${appIdentifier}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to get domains of application instance ${appIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -208,24 +267,33 @@ export class WebApiClient {
|
||||||
appIdentifier: string | number,
|
appIdentifier: string | number,
|
||||||
domainIdenfifier: string | number
|
domainIdenfifier: string | number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
const appParam = buildIdentifierParam(appIdentifier);
|
||||||
|
const domainParam = buildIdentifierParam(domainIdenfifier);
|
||||||
|
const url = `${
|
||||||
|
this.#APPS_API_PREFIX
|
||||||
|
}/${appParam}/domains/${domainParam}`;
|
||||||
try {
|
try {
|
||||||
const appParam = buildIdentifierParam(appIdentifier);
|
const response: ApiResponse = await this.#apiClient.put(url);
|
||||||
const domainParam = buildIdentifierParam(domainIdenfifier);
|
|
||||||
|
|
||||||
const response: ApiResponse = await this.#apiClient.put(
|
|
||||||
`${this.#APPS_API_PREFIX}/${appParam}/domains/${domainParam}`
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to add domain ${domainIdenfifier} to
|
throw new ApiError(
|
||||||
application ${appIdentifier}:
|
response.status,
|
||||||
${response.status} ${response.statusText}`;
|
response.statusText,
|
||||||
|
"put",
|
||||||
|
`Failed to add domain ${domainIdenfifier} to application ${appIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to add domain ${domainIdenfifier} to
|
if (err instanceof ApiError) {
|
||||||
application ${appIdentifier}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to add domain ${domainIdenfifier} to application ${appIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -233,24 +301,33 @@ export class WebApiClient {
|
||||||
appIdentifier: string | number,
|
appIdentifier: string | number,
|
||||||
domainIdenfifier: string | number
|
domainIdenfifier: string | number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
const appParam = buildIdentifierParam(appIdentifier);
|
||||||
|
const domainParam = buildIdentifierParam(domainIdenfifier);
|
||||||
|
const url = `${
|
||||||
|
this.#APPS_API_PREFIX
|
||||||
|
}/${appParam}/domains/${domainParam}`;
|
||||||
try {
|
try {
|
||||||
const appParam = buildIdentifierParam(appIdentifier);
|
const response: ApiResponse = await this.#apiClient.delete(url);
|
||||||
const domainParam = buildIdentifierParam(domainIdenfifier);
|
|
||||||
|
|
||||||
const response: ApiResponse = await this.#apiClient.delete(
|
|
||||||
`${this.#APPS_API_PREFIX}/${appParam}/domains/${domainParam}`
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to remove domain ${domainIdenfifier} from
|
throw new ApiError(
|
||||||
application ${appIdentifier}:
|
response.status,
|
||||||
${response.status} ${response.statusText}`;
|
response.statusText,
|
||||||
|
"delete",
|
||||||
|
`Failed to remove domain ${domainIdenfifier} from application instance ${appIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to remove domain ${domainIdenfifier} from
|
if (err instanceof ApiError) {
|
||||||
application ${appIdentifier}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to remove domain ${domainIdenfifier} from application ${appIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue