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