Small enhancements, including a method to build an identififer param based on the type of identifier
Former-commit-id: e72f901119
restapi
parent
ea1750ecd8
commit
b2b8c62389
|
|
@ -41,7 +41,7 @@ export interface LibreCcmApiClient {
|
|||
*/
|
||||
put(
|
||||
endpoint: string,
|
||||
body: RequestBody,
|
||||
body?: RequestBody,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse>;
|
||||
/**
|
||||
|
|
@ -303,14 +303,14 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
"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,7 +327,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
response.statusText,
|
||||
"get",
|
||||
"API responded with an error.",
|
||||
url,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
|
|
@ -336,7 +336,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
"n/a",
|
||||
"get",
|
||||
`Failed to execute get: ${err}`,
|
||||
url,
|
||||
url
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -356,7 +356,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
response.statusText,
|
||||
"get",
|
||||
"API responded with an error.",
|
||||
url,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
|
|
@ -365,7 +365,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
"n/a",
|
||||
"get",
|
||||
`Failed to execute get: ${err}`,
|
||||
url,
|
||||
url
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -382,7 +382,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
response.statusText,
|
||||
"get",
|
||||
"API responded with an error.",
|
||||
url,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
|
|
@ -391,7 +391,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
"n/a",
|
||||
"get",
|
||||
`Failed to execute get: ${err}`,
|
||||
url,
|
||||
url
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -411,7 +411,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
|||
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()) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue