levelfour 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.fernignore +9 -0
- package/BaseClient.ts +56 -0
- package/Client.ts +90 -0
- package/LevelFourClient.ts +50 -0
- package/api/errors/UnprocessableEntityError.ts +22 -0
- package/api/errors/index.ts +1 -0
- package/api/index.ts +3 -0
- package/api/resources/apiKeys/client/Client.ts +298 -0
- package/api/resources/apiKeys/client/index.ts +1 -0
- package/api/resources/apiKeys/client/requests/CreateApiKeyRequest.ts +23 -0
- package/api/resources/apiKeys/client/requests/ListApiKeysApiV1ApiKeysGetRequest.ts +11 -0
- package/api/resources/apiKeys/client/requests/RevokeApiKeyApiV1ApiKeysKeyIdDeleteRequest.ts +14 -0
- package/api/resources/apiKeys/client/requests/RotateApiKeyApiV1ApiKeysKeyIdRotatePostRequest.ts +14 -0
- package/api/resources/apiKeys/client/requests/index.ts +4 -0
- package/api/resources/apiKeys/exports.ts +4 -0
- package/api/resources/apiKeys/index.ts +1 -0
- package/api/resources/evaluation/client/Client.ts +71 -0
- package/api/resources/evaluation/client/index.ts +1 -0
- package/api/resources/evaluation/exports.ts +4 -0
- package/api/resources/evaluation/index.ts +1 -0
- package/api/resources/health/client/Client.ts +108 -0
- package/api/resources/health/client/index.ts +1 -0
- package/api/resources/health/exports.ts +4 -0
- package/api/resources/health/index.ts +1 -0
- package/api/resources/index.ts +24 -0
- package/api/resources/prAnalysis/client/Client.ts +100 -0
- package/api/resources/prAnalysis/client/index.ts +1 -0
- package/api/resources/prAnalysis/client/requests/AnalyzePrRequest.ts +30 -0
- package/api/resources/prAnalysis/client/requests/index.ts +1 -0
- package/api/resources/prAnalysis/exports.ts +4 -0
- package/api/resources/prAnalysis/index.ts +1 -0
- package/api/resources/prAnalysisActions/client/Client.ts +418 -0
- package/api/resources/prAnalysisActions/client/index.ts +1 -0
- package/api/resources/prAnalysisActions/client/requests/DismissSuggestionsApiV1PrAnalysisActionsDismissPostRequest.ts +21 -0
- package/api/resources/prAnalysisActions/client/requests/GetIgnoredResourcesApiV1PrAnalysisActionsIgnoreGetRequest.ts +15 -0
- package/api/resources/prAnalysisActions/client/requests/GetPrStatusApiV1PrAnalysisActionsStatusGetRequest.ts +15 -0
- package/api/resources/prAnalysisActions/client/requests/IgnoreRequest.ts +21 -0
- package/api/resources/prAnalysisActions/client/requests/SnoozeSuggestionsApiV1PrAnalysisActionsSnoozePostRequest.ts +21 -0
- package/api/resources/prAnalysisActions/client/requests/index.ts +5 -0
- package/api/resources/prAnalysisActions/exports.ts +4 -0
- package/api/resources/prAnalysisActions/index.ts +1 -0
- package/api/resources/prAnalysisInstallations/client/Client.ts +410 -0
- package/api/resources/prAnalysisInstallations/client/index.ts +1 -0
- package/api/resources/prAnalysisInstallations/client/requests/AddReposRequest.ts +17 -0
- package/api/resources/prAnalysisInstallations/client/requests/DeleteInstallationApiV1PrAnalysisInstallationsInstallationIdDeleteRequest.ts +15 -0
- package/api/resources/prAnalysisInstallations/client/requests/ListInstallationsApiV1PrAnalysisInstallationsGetRequest.ts +13 -0
- package/api/resources/prAnalysisInstallations/client/requests/RegisterInstallationRequest.ts +16 -0
- package/api/resources/prAnalysisInstallations/client/requests/RemoveReposRequest.ts +17 -0
- package/api/resources/prAnalysisInstallations/client/requests/index.ts +5 -0
- package/api/resources/prAnalysisInstallations/exports.ts +4 -0
- package/api/resources/prAnalysisInstallations/index.ts +1 -0
- package/api/resources/prAnalysisQuery/client/Client.ts +169 -0
- package/api/resources/prAnalysisQuery/client/index.ts +1 -0
- package/api/resources/prAnalysisQuery/client/requests/ExplainRequest.ts +17 -0
- package/api/resources/prAnalysisQuery/client/requests/WhatifRequest.ts +21 -0
- package/api/resources/prAnalysisQuery/client/requests/index.ts +2 -0
- package/api/resources/prAnalysisQuery/exports.ts +4 -0
- package/api/resources/prAnalysisQuery/index.ts +1 -0
- package/api/resources/providers/client/Client.ts +991 -0
- package/api/resources/providers/client/index.ts +1 -0
- package/api/resources/providers/client/requests/GetProviderFilterOptionsApiV1ProvidersProviderIdSpendingFilterOptionsGetRequest.ts +17 -0
- package/api/resources/providers/client/requests/GetProviderPotentialSavingsBreakdownApiV1ProvidersProviderIdPotentialSavingsBreakdownGetRequest.ts +19 -0
- package/api/resources/providers/client/requests/GetProviderPotentialSavingsSummaryApiV1ProvidersProviderIdPotentialSavingsSummaryGetRequest.ts +11 -0
- package/api/resources/providers/client/requests/GetProviderRecommendationFilterOptionsApiV1ProvidersProviderIdRecommendationsFilterOptionsGetRequest.ts +11 -0
- package/api/resources/providers/client/requests/GetProviderRecommendationsBreakdownApiV1ProvidersProviderIdRecommendationsGetRequest.ts +33 -0
- package/api/resources/providers/client/requests/GetProviderRecommendationsOverviewApiV1ProvidersProviderIdRecommendationsOverviewGetRequest.ts +11 -0
- package/api/resources/providers/client/requests/GetProviderRecommendationsSummaryApiV1ProvidersProviderIdRecommendationsSummaryGetRequest.ts +11 -0
- package/api/resources/providers/client/requests/GetProviderSavedSummaryApiV1ProvidersProviderIdSavingsSummaryGetRequest.ts +11 -0
- package/api/resources/providers/client/requests/GetProviderServiceBreakdownApiV1ProvidersProviderIdSpendingBreakdownGetRequest.ts +45 -0
- package/api/resources/providers/client/requests/GetProviderSpendingSummaryApiV1ProvidersProviderIdSpendingSummaryGetRequest.ts +11 -0
- package/api/resources/providers/client/requests/GetProviderSpendingTimelineApiV1ProvidersProviderIdSpendingTimelineGetRequest.ts +17 -0
- package/api/resources/providers/client/requests/GetSavedByProviderBreakdownApiV1ProvidersProviderIdSavingsBreakdownGetRequest.ts +31 -0
- package/api/resources/providers/client/requests/index.ts +12 -0
- package/api/resources/providers/exports.ts +4 -0
- package/api/resources/providers/index.ts +2 -0
- package/api/resources/providers/types/GetProviderServiceBreakdownApiV1ProvidersProviderIdSpendingBreakdownGetResponse.ts +8 -0
- package/api/resources/providers/types/index.ts +1 -0
- package/api/resources/recommendations/client/Client.ts +815 -0
- package/api/resources/recommendations/client/index.ts +1 -0
- package/api/resources/recommendations/client/requests/AcceptTermsApiV1RecommendationsRecommendationIdAcceptTermsPostRequest.ts +11 -0
- package/api/resources/recommendations/client/requests/CompleteWizardApiV1RecommendationsRecommendationIdCompletePostRequest.ts +11 -0
- package/api/resources/recommendations/client/requests/GetRecommendationDetailsApiV1RecommendationsRecommendationIdDetailsGetRequest.ts +11 -0
- package/api/resources/recommendations/client/requests/GetRecommendationForWizardApiV1RecommendationsRecommendationIdGetRequest.ts +11 -0
- package/api/resources/recommendations/client/requests/GetRecommendationsApiV1RecommendationsGetRequest.ts +16 -0
- package/api/resources/recommendations/client/requests/ImplementationMethodRequest.ts +14 -0
- package/api/resources/recommendations/client/requests/SavingsDecisionRequest.ts +18 -0
- package/api/resources/recommendations/client/requests/UpdateTagRequest.ts +13 -0
- package/api/resources/recommendations/client/requests/index.ts +8 -0
- package/api/resources/recommendations/exports.ts +4 -0
- package/api/resources/recommendations/index.ts +1 -0
- package/api/resources/savings/client/Client.ts +231 -0
- package/api/resources/savings/client/index.ts +1 -0
- package/api/resources/savings/client/requests/ExecutionRequestBody.ts +15 -0
- package/api/resources/savings/client/requests/GetSavedBreakdownApiV1SavingsBreakdownGetRequest.ts +30 -0
- package/api/resources/savings/client/requests/index.ts +2 -0
- package/api/resources/savings/exports.ts +4 -0
- package/api/resources/savings/index.ts +1 -0
- package/api/resources/spending/client/Client.ts +288 -0
- package/api/resources/spending/client/index.ts +1 -0
- package/api/resources/spending/client/requests/GetCostBreakdownApiV1SpendingBreakdownGetRequest.ts +30 -0
- package/api/resources/spending/client/requests/GetDailySpendingBreakdownApiV1SpendingDailyBreakdownGetRequest.ts +12 -0
- package/api/resources/spending/client/requests/index.ts +2 -0
- package/api/resources/spending/exports.ts +4 -0
- package/api/resources/spending/index.ts +2 -0
- package/api/resources/spending/types/GetCostBreakdownApiV1SpendingBreakdownGetResponse.ts +9 -0
- package/api/resources/spending/types/index.ts +1 -0
- package/api/resources/webhooks/client/Client.ts +232 -0
- package/api/resources/webhooks/client/index.ts +1 -0
- package/api/resources/webhooks/client/requests/DeleteEndpointApiV1WebhooksEndpointsEndpointIdDeleteRequest.ts +14 -0
- package/api/resources/webhooks/client/requests/ListEndpointsApiV1WebhooksEndpointsGetRequest.ts +11 -0
- package/api/resources/webhooks/client/requests/RegisterEndpointRequest.ts +18 -0
- package/api/resources/webhooks/client/requests/index.ts +3 -0
- package/api/resources/webhooks/exports.ts +4 -0
- package/api/resources/webhooks/index.ts +1 -0
- package/api/types/AcceptTermsData.ts +10 -0
- package/api/types/AcceptTermsResponse.ts +12 -0
- package/api/types/ActionRequest.ts +14 -0
- package/api/types/ApiKeyCreatedData.ts +13 -0
- package/api/types/ApiKeyData.ts +12 -0
- package/api/types/ComparisonItem.ts +10 -0
- package/api/types/CompleteWizardData.ts +12 -0
- package/api/types/CompleteWizardResponse.ts +12 -0
- package/api/types/CostBreakdownData.ts +14 -0
- package/api/types/CostBreakdownItem.ts +27 -0
- package/api/types/CostBreakdownRawData.ts +15 -0
- package/api/types/CostBreakdownRawResponse.ts +12 -0
- package/api/types/CostBreakdownResponse.ts +12 -0
- package/api/types/CostsBreakdownChartData.ts +19 -0
- package/api/types/CostsBreakdownChartResponse.ts +12 -0
- package/api/types/CostsBreakdownDataPoint.ts +17 -0
- package/api/types/CostsBreakdownGroupedChartData.ts +21 -0
- package/api/types/CostsBreakdownGroupedChartResponse.ts +12 -0
- package/api/types/CostsBreakdownGroupedDataPoint.ts +13 -0
- package/api/types/CostsBreakdownTotals.ts +15 -0
- package/api/types/CreateApiKeyResponse.ts +9 -0
- package/api/types/DailySpendingBreakdownData.ts +17 -0
- package/api/types/DailySpendingBreakdownResponse.ts +12 -0
- package/api/types/DailySpendingDataPoint.ts +11 -0
- package/api/types/DashboardSummaryData.ts +29 -0
- package/api/types/DashboardSummaryResponse.ts +12 -0
- package/api/types/DeleteEndpointData.ts +6 -0
- package/api/types/DeleteEndpointResponse.ts +9 -0
- package/api/types/EndpointData.ts +9 -0
- package/api/types/EvaluationStatusData.ts +27 -0
- package/api/types/EvaluationStatusResponse.ts +12 -0
- package/api/types/ExecutionLogCommand.ts +7 -0
- package/api/types/ExecutionLogEntry.ts +10 -0
- package/api/types/ExecutionStepSchema.ts +9 -0
- package/api/types/HttpValidationError.ts +7 -0
- package/api/types/ImplementationMethodData.ts +10 -0
- package/api/types/ImplementationMethodResponse.ts +12 -0
- package/api/types/ImplementationStep.ts +8 -0
- package/api/types/ListApiKeysData.ts +8 -0
- package/api/types/ListApiKeysResponse.ts +9 -0
- package/api/types/ListEndpointsResponse.ts +9 -0
- package/api/types/MetricDataPoint.ts +8 -0
- package/api/types/MetricSeries.ts +15 -0
- package/api/types/MetricSeriesEntry.ts +9 -0
- package/api/types/MonthlySpendingData.ts +11 -0
- package/api/types/MonthlySpendingDataPoint.ts +11 -0
- package/api/types/MonthlySpendingResponse.ts +12 -0
- package/api/types/PaginationMeta.ts +19 -0
- package/api/types/ProcessingRecommendationItem.ts +23 -0
- package/api/types/ProcessingRecommendationsData.ts +12 -0
- package/api/types/ProcessingRecommendationsResponse.ts +12 -0
- package/api/types/Provider.ts +17 -0
- package/api/types/ProviderBreakdownData.ts +13 -0
- package/api/types/ProviderBreakdownItem.ts +31 -0
- package/api/types/ProviderBreakdownResponse.ts +12 -0
- package/api/types/ProviderFilterOptionsData.ts +19 -0
- package/api/types/ProviderFilterOptionsResponse.ts +12 -0
- package/api/types/ProviderPotentialSavingsSummaryData.ts +17 -0
- package/api/types/ProviderPotentialSavingsSummaryResponse.ts +12 -0
- package/api/types/ProviderRecommendationsSummaryData.ts +15 -0
- package/api/types/ProviderRecommendationsSummaryResponse.ts +12 -0
- package/api/types/ProviderSavedSummaryData.ts +21 -0
- package/api/types/ProviderSavedSummaryResponse.ts +12 -0
- package/api/types/ProviderServiceBreakdownData.ts +26 -0
- package/api/types/ProviderServiceBreakdownItem.ts +29 -0
- package/api/types/ProviderServiceBreakdownRawData.ts +25 -0
- package/api/types/ProviderServiceBreakdownRawResponse.ts +12 -0
- package/api/types/ProviderServiceBreakdownResponse.ts +12 -0
- package/api/types/ProviderServiceCostsBreakdownData.ts +25 -0
- package/api/types/ProviderServiceCostsBreakdownResponse.ts +12 -0
- package/api/types/ProviderServiceCostsDataPoint.ts +13 -0
- package/api/types/ProviderSpending.ts +23 -0
- package/api/types/ProviderSpendingSummaryData.ts +27 -0
- package/api/types/ProviderSpendingSummaryResponse.ts +12 -0
- package/api/types/ProviderTimelineData.ts +23 -0
- package/api/types/ProviderTimelineResponse.ts +12 -0
- package/api/types/ProviderTopService.ts +13 -0
- package/api/types/ProvidersSummaryData.ts +10 -0
- package/api/types/ProvidersSummaryResponse.ts +12 -0
- package/api/types/RecommendationActions.ts +28 -0
- package/api/types/RecommendationDetail.ts +22 -0
- package/api/types/RecommendationDetailResponse.ts +12 -0
- package/api/types/RecommendationFilterOptionsData.ts +15 -0
- package/api/types/RecommendationFilterOptionsResponse.ts +12 -0
- package/api/types/RecommendationItem.ts +19 -0
- package/api/types/RecommendationWizardData.ts +83 -0
- package/api/types/RecommendationWizardResponse.ts +12 -0
- package/api/types/RecommendationsBreakdownData.ts +11 -0
- package/api/types/RecommendationsBreakdownResponse.ts +12 -0
- package/api/types/RecommendationsOverviewData.ts +19 -0
- package/api/types/RecommendationsOverviewResponse.ts +12 -0
- package/api/types/RegisterEndpointResponse.ts +9 -0
- package/api/types/ResourceChange.ts +14 -0
- package/api/types/ResourceSnapshot.ts +7 -0
- package/api/types/RevokeApiKeyData.ts +6 -0
- package/api/types/RevokeApiKeyResponse.ts +9 -0
- package/api/types/RiskAssessment.ts +11 -0
- package/api/types/RiskFactor.ts +8 -0
- package/api/types/SavedBreakdownData.ts +13 -0
- package/api/types/SavedBreakdownResponse.ts +12 -0
- package/api/types/SavedByProviderBreakdownData.ts +13 -0
- package/api/types/SavedByProviderBreakdownResponse.ts +12 -0
- package/api/types/SavedByProviderItem.ts +29 -0
- package/api/types/SavedItem.ts +20 -0
- package/api/types/SavedProviderSummary.ts +13 -0
- package/api/types/SavedSummaryData.ts +23 -0
- package/api/types/SavedSummaryResponse.ts +12 -0
- package/api/types/SavingsDecisionData.ts +13 -0
- package/api/types/SavingsDecisionResponse.ts +12 -0
- package/api/types/SpendingByDateItem.ts +13 -0
- package/api/types/TopRecommendation.ts +21 -0
- package/api/types/TriggerInfo.ts +8 -0
- package/api/types/UpdateTagData.ts +6 -0
- package/api/types/UpdateTagResponse.ts +9 -0
- package/api/types/ValidationError.ts +17 -0
- package/api/types/index.ts +115 -0
- package/core/exports.ts +1 -0
- package/core/fetcher/APIResponse.ts +23 -0
- package/core/fetcher/BinaryResponse.ts +34 -0
- package/core/fetcher/EndpointMetadata.ts +13 -0
- package/core/fetcher/EndpointSupplier.ts +14 -0
- package/core/fetcher/Fetcher.ts +398 -0
- package/core/fetcher/Headers.ts +93 -0
- package/core/fetcher/HttpResponsePromise.ts +116 -0
- package/core/fetcher/RawResponse.ts +61 -0
- package/core/fetcher/Supplier.ts +11 -0
- package/core/fetcher/createRequestUrl.ts +6 -0
- package/core/fetcher/getErrorResponseBody.ts +33 -0
- package/core/fetcher/getFetchFn.ts +3 -0
- package/core/fetcher/getHeader.ts +8 -0
- package/core/fetcher/getRequestBody.ts +20 -0
- package/core/fetcher/getResponseBody.ts +58 -0
- package/core/fetcher/index.ts +11 -0
- package/core/fetcher/makeRequest.ts +70 -0
- package/core/fetcher/requestWithRetries.ts +64 -0
- package/core/fetcher/signals.ts +26 -0
- package/core/headers.ts +33 -0
- package/core/index.ts +4 -0
- package/core/json.ts +27 -0
- package/core/logging/exports.ts +19 -0
- package/core/logging/index.ts +1 -0
- package/core/logging/logger.ts +203 -0
- package/core/runtime/index.ts +1 -0
- package/core/runtime/runtime.ts +134 -0
- package/core/url/encodePathParam.ts +18 -0
- package/core/url/index.ts +3 -0
- package/core/url/join.ts +79 -0
- package/core/url/qs.ts +74 -0
- package/errors/LevelFourError.ts +58 -0
- package/errors/LevelFourTimeoutError.ts +13 -0
- package/errors/handleNonStatusCodeError.ts +37 -0
- package/errors/index.ts +2 -0
- package/errors.ts +100 -0
- package/exports.ts +1 -0
- package/index.ts +22 -0
- package/jest.config.js +17 -0
- package/package.json +35 -0
- package/pagination.ts +54 -0
- package/telemetry.ts +64 -0
- package/tests/auth.test.ts +64 -0
- package/tests/errors.test.ts +92 -0
- package/tests/pagination.test.ts +72 -0
- package/tests/telemetry.test.ts +110 -0
- package/tsconfig.json +22 -0
- package/webhooks.ts +77 -0
package/errors.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type * as core from "./core/index.js";
|
|
2
|
+
import { LevelFourError } from "./errors/LevelFourError.js";
|
|
3
|
+
|
|
4
|
+
export { LevelFourError };
|
|
5
|
+
|
|
6
|
+
export class AuthenticationError extends LevelFourError {
|
|
7
|
+
constructor(message: string, rawResponse?: core.RawResponse) {
|
|
8
|
+
super({ message, statusCode: 401, rawResponse });
|
|
9
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
10
|
+
this.name = "AuthenticationError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class AuthorizationError extends LevelFourError {
|
|
15
|
+
constructor(message: string, body?: unknown, rawResponse?: core.RawResponse) {
|
|
16
|
+
super({ message, statusCode: 403, body, rawResponse });
|
|
17
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
18
|
+
this.name = "AuthorizationError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class NotFoundError extends LevelFourError {
|
|
23
|
+
constructor(message: string, body?: unknown, rawResponse?: core.RawResponse) {
|
|
24
|
+
super({ message, statusCode: 404, body, rawResponse });
|
|
25
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
26
|
+
this.name = "NotFoundError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class BadRequestError extends LevelFourError {
|
|
31
|
+
constructor(message: string, body?: unknown, rawResponse?: core.RawResponse) {
|
|
32
|
+
super({ message, statusCode: 400, body, rawResponse });
|
|
33
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
34
|
+
this.name = "BadRequestError";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class ConflictError extends LevelFourError {
|
|
39
|
+
constructor(message: string, body?: unknown, rawResponse?: core.RawResponse) {
|
|
40
|
+
super({ message, statusCode: 409, body, rawResponse });
|
|
41
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
42
|
+
this.name = "ConflictError";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class RateLimitError extends LevelFourError {
|
|
47
|
+
public readonly retryAfter?: string;
|
|
48
|
+
|
|
49
|
+
constructor(message: string, retryAfter?: string, body?: unknown, rawResponse?: core.RawResponse) {
|
|
50
|
+
super({ message, statusCode: 429, body, rawResponse });
|
|
51
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
52
|
+
this.name = "RateLimitError";
|
|
53
|
+
this.retryAfter = retryAfter;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class ValidationError extends LevelFourError {
|
|
58
|
+
constructor(message: string, body?: unknown, rawResponse?: core.RawResponse) {
|
|
59
|
+
super({ message, statusCode: 422, body, rawResponse });
|
|
60
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
61
|
+
this.name = "ValidationError";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class InternalServerError extends LevelFourError {
|
|
66
|
+
constructor(message: string, body?: unknown, rawResponse?: core.RawResponse) {
|
|
67
|
+
super({ message, statusCode: 500, body, rawResponse });
|
|
68
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
69
|
+
this.name = "InternalServerError";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const STATUS_TO_ERROR: Record<number, new (message: string, body?: unknown) => LevelFourError> = {
|
|
74
|
+
400: BadRequestError,
|
|
75
|
+
401: AuthenticationError as new (message: string, body?: unknown) => LevelFourError,
|
|
76
|
+
403: AuthorizationError,
|
|
77
|
+
404: NotFoundError,
|
|
78
|
+
409: ConflictError,
|
|
79
|
+
422: ValidationError,
|
|
80
|
+
429: RateLimitError as new (message: string, body?: unknown) => LevelFourError,
|
|
81
|
+
500: InternalServerError,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export function mapError(err: LevelFourError): LevelFourError {
|
|
85
|
+
if (!err.statusCode) return err;
|
|
86
|
+
const ErrorClass = STATUS_TO_ERROR[err.statusCode];
|
|
87
|
+
if (!ErrorClass) return err;
|
|
88
|
+
|
|
89
|
+
const body = err.body as { error?: { message?: string } } | undefined;
|
|
90
|
+
const message = body?.error?.message ?? err.message;
|
|
91
|
+
|
|
92
|
+
if (err.statusCode === 429) {
|
|
93
|
+
return new RateLimitError(message, undefined, err.body, err.rawResponse);
|
|
94
|
+
}
|
|
95
|
+
if (err.statusCode === 401) {
|
|
96
|
+
return new AuthenticationError(message, err.rawResponse);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return new ErrorClass(message, err.body);
|
|
100
|
+
}
|
package/exports.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./core/exports.js";
|
package/index.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export * as LevelFour from "./api/index.js";
|
|
2
|
+
export type { BaseClientOptions, BaseRequestOptions } from "./BaseClient.js";
|
|
3
|
+
export { LevelFourClient } from "./LevelFourClient.js";
|
|
4
|
+
export type { LevelFourClientOptions } from "./LevelFourClient.js";
|
|
5
|
+
export { LevelFourClient as BaseLevelFourClient } from "./Client.js";
|
|
6
|
+
export { LevelFourError, LevelFourTimeoutError } from "./errors/index.js";
|
|
7
|
+
export {
|
|
8
|
+
AuthenticationError,
|
|
9
|
+
AuthorizationError,
|
|
10
|
+
NotFoundError,
|
|
11
|
+
BadRequestError,
|
|
12
|
+
ConflictError,
|
|
13
|
+
RateLimitError,
|
|
14
|
+
ValidationError,
|
|
15
|
+
InternalServerError,
|
|
16
|
+
mapError,
|
|
17
|
+
} from "./errors.js";
|
|
18
|
+
export { PageIterator } from "./pagination.js";
|
|
19
|
+
export type { PaginatedResponse, FetchPage } from "./pagination.js";
|
|
20
|
+
export { WebhookVerifier, WebhookVerificationError } from "./webhooks.js";
|
|
21
|
+
export { createTracedFetch } from "./telemetry.js";
|
|
22
|
+
export * from "./exports.js";
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
preset: "ts-jest",
|
|
3
|
+
testEnvironment: "node",
|
|
4
|
+
roots: ["<rootDir>/tests"],
|
|
5
|
+
moduleNameMapper: {
|
|
6
|
+
"^(\\.\\.?/.*)\\.js$": "$1",
|
|
7
|
+
},
|
|
8
|
+
transform: {
|
|
9
|
+
"^.+\\.tsx?$": [
|
|
10
|
+
"ts-jest",
|
|
11
|
+
{
|
|
12
|
+
tsconfig: { isolatedModules: true },
|
|
13
|
+
diagnostics: { ignoreCodes: [151002] },
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
},
|
|
17
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "levelfour",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"test": "jest"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/jest": "^30.0.0",
|
|
19
|
+
"@types/node": "^25.3.5",
|
|
20
|
+
"jest": "^30.2.0",
|
|
21
|
+
"ts-jest": "^29.4.6",
|
|
22
|
+
"typescript": "^5.3.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@opentelemetry/api": ">=1.4.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependenciesMeta": {
|
|
28
|
+
"@opentelemetry/api": {
|
|
29
|
+
"optional": true
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/pagination.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export interface PaginatedResponse<T> {
|
|
2
|
+
data: {
|
|
3
|
+
items: T[];
|
|
4
|
+
pagination: {
|
|
5
|
+
total_items: number;
|
|
6
|
+
total_pages: number;
|
|
7
|
+
current_page: number;
|
|
8
|
+
page_size: number;
|
|
9
|
+
has_next: boolean;
|
|
10
|
+
has_previous: boolean;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type FetchPage<T> = (page: number, pageSize: number) => Promise<PaginatedResponse<T>>;
|
|
16
|
+
|
|
17
|
+
export class PageIterator<T> {
|
|
18
|
+
private readonly fetchPage: FetchPage<T>;
|
|
19
|
+
private readonly pageSize: number;
|
|
20
|
+
private currentPage: number;
|
|
21
|
+
private done: boolean;
|
|
22
|
+
|
|
23
|
+
constructor(fetchPage: FetchPage<T>, pageSize: number = 20) {
|
|
24
|
+
this.fetchPage = fetchPage;
|
|
25
|
+
this.pageSize = pageSize;
|
|
26
|
+
this.currentPage = 1;
|
|
27
|
+
this.done = false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async *[Symbol.asyncIterator](): AsyncIterableIterator<T> {
|
|
31
|
+
while (!this.done) {
|
|
32
|
+
const response = await this.fetchPage(this.currentPage, this.pageSize);
|
|
33
|
+
const items = response.data.items;
|
|
34
|
+
|
|
35
|
+
for (const item of items) {
|
|
36
|
+
yield item;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!response.data.pagination.has_next) {
|
|
40
|
+
this.done = true;
|
|
41
|
+
} else {
|
|
42
|
+
this.currentPage++;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async toArray(): Promise<T[]> {
|
|
48
|
+
const results: T[] = [];
|
|
49
|
+
for await (const item of this) {
|
|
50
|
+
results.push(item);
|
|
51
|
+
}
|
|
52
|
+
return results;
|
|
53
|
+
}
|
|
54
|
+
}
|
package/telemetry.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
type Tracer = {
|
|
2
|
+
startActiveSpan: <T>(name: string, fn: (span: Span) => T) => T;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
type Span = {
|
|
6
|
+
setAttribute: (key: string, value: string | number | boolean) => void;
|
|
7
|
+
recordException: (error: unknown) => void;
|
|
8
|
+
end: () => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
let _tracer: Tracer | null | undefined;
|
|
12
|
+
|
|
13
|
+
function getTracer(): Tracer | null {
|
|
14
|
+
if (_tracer !== undefined) {
|
|
15
|
+
return _tracer;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
const otel = require("@opentelemetry/api");
|
|
19
|
+
_tracer = otel.trace.getTracer("levelfour-sdk") as Tracer;
|
|
20
|
+
} catch {
|
|
21
|
+
_tracer = null;
|
|
22
|
+
}
|
|
23
|
+
return _tracer;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createTracedFetch(fetchFn?: typeof fetch): typeof fetch | undefined {
|
|
27
|
+
if (!fetchFn) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const tracer = getTracer();
|
|
31
|
+
if (!tracer) {
|
|
32
|
+
return fetchFn;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return function tracedFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
|
|
36
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
37
|
+
const method = init?.method ?? "GET";
|
|
38
|
+
|
|
39
|
+
return tracer.startActiveSpan(`levelfour.http ${method}`, (span: Span) => {
|
|
40
|
+
span.setAttribute("http.method", method);
|
|
41
|
+
span.setAttribute("http.url", url);
|
|
42
|
+
|
|
43
|
+
return fetchFn(input, init).then(
|
|
44
|
+
(response) => {
|
|
45
|
+
span.setAttribute("http.status_code", response.status);
|
|
46
|
+
span.setAttribute("levelfour.success", response.status < 400);
|
|
47
|
+
span.end();
|
|
48
|
+
return response;
|
|
49
|
+
},
|
|
50
|
+
(error) => {
|
|
51
|
+
span.setAttribute("levelfour.success", false);
|
|
52
|
+
span.setAttribute("levelfour.error.type", error?.constructor?.name ?? "Error");
|
|
53
|
+
span.recordException(error);
|
|
54
|
+
span.end();
|
|
55
|
+
throw error;
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
} as typeof fetch;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function _resetTracer(): void {
|
|
63
|
+
_tracer = undefined;
|
|
64
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { LevelFourClient } from "../LevelFourClient";
|
|
2
|
+
import { AuthenticationError } from "../errors";
|
|
3
|
+
|
|
4
|
+
const ORIGINAL_ENV = process.env.LEVELFOUR_API_KEY;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
delete process.env.LEVELFOUR_API_KEY;
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
afterAll(() => {
|
|
11
|
+
if (ORIGINAL_ENV !== undefined) {
|
|
12
|
+
process.env.LEVELFOUR_API_KEY = ORIGINAL_ENV;
|
|
13
|
+
} else {
|
|
14
|
+
delete process.env.LEVELFOUR_API_KEY;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("LevelFourClient auth", () => {
|
|
19
|
+
test("accepts explicit live key", () => {
|
|
20
|
+
const client = new LevelFourClient({ apiKey: "l4_live_test123" });
|
|
21
|
+
expect(client).toBeDefined();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("accepts explicit test key", () => {
|
|
25
|
+
const client = new LevelFourClient({ apiKey: "l4_test_abc" });
|
|
26
|
+
expect(client).toBeDefined();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("falls back to LEVELFOUR_API_KEY env var", () => {
|
|
30
|
+
process.env.LEVELFOUR_API_KEY = "l4_live_from_env";
|
|
31
|
+
const client = new LevelFourClient();
|
|
32
|
+
expect(client).toBeDefined();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("throws AuthenticationError when no key provided", () => {
|
|
36
|
+
expect(() => new LevelFourClient()).toThrow(AuthenticationError);
|
|
37
|
+
expect(() => new LevelFourClient()).toThrow("No API key provided");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("throws AuthenticationError for invalid prefix", () => {
|
|
41
|
+
expect(() => new LevelFourClient({ apiKey: "sk_bad_key" })).toThrow(
|
|
42
|
+
AuthenticationError,
|
|
43
|
+
);
|
|
44
|
+
expect(() => new LevelFourClient({ apiKey: "sk_bad_key" })).toThrow(
|
|
45
|
+
"Invalid API key format",
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("explicit key overrides env var", () => {
|
|
50
|
+
process.env.LEVELFOUR_API_KEY = "l4_live_env_key";
|
|
51
|
+
const client = new LevelFourClient({ apiKey: "l4_test_explicit" });
|
|
52
|
+
expect(client).toBeDefined();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("AuthenticationError has status 401", () => {
|
|
56
|
+
try {
|
|
57
|
+
new LevelFourClient();
|
|
58
|
+
fail("should have thrown");
|
|
59
|
+
} catch (e) {
|
|
60
|
+
expect(e).toBeInstanceOf(AuthenticationError);
|
|
61
|
+
expect((e as AuthenticationError).statusCode).toBe(401);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthenticationError,
|
|
3
|
+
AuthorizationError,
|
|
4
|
+
BadRequestError,
|
|
5
|
+
ConflictError,
|
|
6
|
+
InternalServerError,
|
|
7
|
+
NotFoundError,
|
|
8
|
+
RateLimitError,
|
|
9
|
+
ValidationError,
|
|
10
|
+
mapError,
|
|
11
|
+
} from "../errors";
|
|
12
|
+
import { LevelFourError } from "../errors/LevelFourError";
|
|
13
|
+
|
|
14
|
+
describe("error hierarchy", () => {
|
|
15
|
+
const errorClasses = [
|
|
16
|
+
{ Cls: AuthenticationError, status: 401, name: "AuthenticationError" },
|
|
17
|
+
{ Cls: AuthorizationError, status: 403, name: "AuthorizationError" },
|
|
18
|
+
{ Cls: BadRequestError, status: 400, name: "BadRequestError" },
|
|
19
|
+
{ Cls: NotFoundError, status: 404, name: "NotFoundError" },
|
|
20
|
+
{ Cls: ConflictError, status: 409, name: "ConflictError" },
|
|
21
|
+
{ Cls: ValidationError, status: 422, name: "ValidationError" },
|
|
22
|
+
{ Cls: InternalServerError, status: 500, name: "InternalServerError" },
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
test.each(errorClasses)(
|
|
26
|
+
"$name extends LevelFourError",
|
|
27
|
+
({ Cls, status }) => {
|
|
28
|
+
const err = new Cls("test message");
|
|
29
|
+
expect(err).toBeInstanceOf(LevelFourError);
|
|
30
|
+
expect(err).toBeInstanceOf(Error);
|
|
31
|
+
expect(err.statusCode).toBe(status);
|
|
32
|
+
},
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
test("RateLimitError has retryAfter", () => {
|
|
36
|
+
const err = new RateLimitError("slow down", "30");
|
|
37
|
+
expect(err).toBeInstanceOf(LevelFourError);
|
|
38
|
+
expect(err.statusCode).toBe(429);
|
|
39
|
+
expect(err.retryAfter).toBe("30");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("error name is set correctly", () => {
|
|
43
|
+
const err = new NotFoundError("gone");
|
|
44
|
+
expect(err.name).toBe("NotFoundError");
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("mapError", () => {
|
|
49
|
+
test.each([
|
|
50
|
+
[400, BadRequestError],
|
|
51
|
+
[401, AuthenticationError],
|
|
52
|
+
[403, AuthorizationError],
|
|
53
|
+
[404, NotFoundError],
|
|
54
|
+
[409, ConflictError],
|
|
55
|
+
[422, ValidationError],
|
|
56
|
+
[429, RateLimitError],
|
|
57
|
+
[500, InternalServerError],
|
|
58
|
+
])("maps status %i to correct error class", (status, ErrorClass) => {
|
|
59
|
+
const base = new LevelFourError({
|
|
60
|
+
message: "test",
|
|
61
|
+
statusCode: status,
|
|
62
|
+
body: { error: { message: "server message" } },
|
|
63
|
+
});
|
|
64
|
+
const mapped = mapError(base);
|
|
65
|
+
expect(mapped).toBeInstanceOf(ErrorClass);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("extracts message from error body", () => {
|
|
69
|
+
const base = new LevelFourError({
|
|
70
|
+
message: "fallback",
|
|
71
|
+
statusCode: 404,
|
|
72
|
+
body: { error: { message: "Resource not found" } },
|
|
73
|
+
});
|
|
74
|
+
const mapped = mapError(base);
|
|
75
|
+
expect(mapped.message).toContain("Resource not found");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("returns original error for unknown status", () => {
|
|
79
|
+
const base = new LevelFourError({
|
|
80
|
+
message: "teapot",
|
|
81
|
+
statusCode: 418,
|
|
82
|
+
});
|
|
83
|
+
const mapped = mapError(base);
|
|
84
|
+
expect(mapped).toBe(base);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("returns original error when no statusCode", () => {
|
|
88
|
+
const base = new LevelFourError({ message: "unknown" });
|
|
89
|
+
const mapped = mapError(base);
|
|
90
|
+
expect(mapped).toBe(base);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { PageIterator, PaginatedResponse } from "../pagination";
|
|
2
|
+
|
|
3
|
+
function makePage<T>(
|
|
4
|
+
items: T[],
|
|
5
|
+
hasNext: boolean,
|
|
6
|
+
pageNum = 1,
|
|
7
|
+
totalItems?: number,
|
|
8
|
+
): PaginatedResponse<T> {
|
|
9
|
+
return {
|
|
10
|
+
data: {
|
|
11
|
+
items,
|
|
12
|
+
pagination: {
|
|
13
|
+
total_items: totalItems ?? items.length,
|
|
14
|
+
total_pages: 3,
|
|
15
|
+
current_page: pageNum,
|
|
16
|
+
page_size: items.length,
|
|
17
|
+
has_next: hasNext,
|
|
18
|
+
has_previous: pageNum > 1,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("PageIterator", () => {
|
|
25
|
+
test("iterates single page", async () => {
|
|
26
|
+
const fetchPage = async () => makePage(["a", "b", "c"], false);
|
|
27
|
+
const iter = new PageIterator(fetchPage, 10);
|
|
28
|
+
const items = await iter.toArray();
|
|
29
|
+
expect(items).toEqual(["a", "b", "c"]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("iterates multiple pages", async () => {
|
|
33
|
+
const pages: Record<number, PaginatedResponse<number>> = {
|
|
34
|
+
1: makePage([1, 2], true, 1, 5),
|
|
35
|
+
2: makePage([3, 4], true, 2, 5),
|
|
36
|
+
3: makePage([5], false, 3, 5),
|
|
37
|
+
};
|
|
38
|
+
const fetchPage = async (page: number) => pages[page];
|
|
39
|
+
const iter = new PageIterator(fetchPage, 2);
|
|
40
|
+
const items = await iter.toArray();
|
|
41
|
+
expect(items).toEqual([1, 2, 3, 4, 5]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("handles empty first page", async () => {
|
|
45
|
+
const fetchPage = async () => makePage([], false);
|
|
46
|
+
const iter = new PageIterator(fetchPage, 10);
|
|
47
|
+
const items = await iter.toArray();
|
|
48
|
+
expect(items).toEqual([]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("supports async iteration", async () => {
|
|
52
|
+
const fetchPage = async () => makePage([10, 20], false);
|
|
53
|
+
const iter = new PageIterator(fetchPage, 10);
|
|
54
|
+
const items: number[] = [];
|
|
55
|
+
for await (const item of iter) {
|
|
56
|
+
items.push(item);
|
|
57
|
+
}
|
|
58
|
+
expect(items).toEqual([10, 20]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("tracks page number correctly", async () => {
|
|
62
|
+
const requestedPages: number[] = [];
|
|
63
|
+
const fetchPage = async (page: number) => {
|
|
64
|
+
requestedPages.push(page);
|
|
65
|
+
if (page === 1) return makePage([1], true, 1);
|
|
66
|
+
return makePage([2], false, 2);
|
|
67
|
+
};
|
|
68
|
+
const iter = new PageIterator(fetchPage, 1);
|
|
69
|
+
await iter.toArray();
|
|
70
|
+
expect(requestedPages).toEqual([1, 2]);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { _resetTracer } from "../telemetry.js";
|
|
2
|
+
|
|
3
|
+
beforeEach(() => {
|
|
4
|
+
_resetTracer();
|
|
5
|
+
jest.resetModules();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
function mockOtel() {
|
|
9
|
+
const mockSpan = {
|
|
10
|
+
setAttribute: jest.fn(),
|
|
11
|
+
recordException: jest.fn(),
|
|
12
|
+
end: jest.fn(),
|
|
13
|
+
};
|
|
14
|
+
const mockTracer = {
|
|
15
|
+
startActiveSpan: jest.fn((name: string, fn: (span: typeof mockSpan) => unknown) => fn(mockSpan)),
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
jest.doMock("@opentelemetry/api", () => ({
|
|
19
|
+
trace: {
|
|
20
|
+
getTracer: () => mockTracer,
|
|
21
|
+
},
|
|
22
|
+
}), { virtual: true });
|
|
23
|
+
|
|
24
|
+
return { mockSpan, mockTracer };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("createTracedFetch", () => {
|
|
28
|
+
it("returns undefined when no fetch provided", () => {
|
|
29
|
+
const { createTracedFetch } = require("../telemetry.js");
|
|
30
|
+
expect(createTracedFetch(undefined)).toBeUndefined();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("returns original fetch when @opentelemetry/api is not installed", () => {
|
|
34
|
+
const { createTracedFetch } = require("../telemetry.js");
|
|
35
|
+
const mockFetch = jest.fn() as unknown as typeof fetch;
|
|
36
|
+
const result = createTracedFetch(mockFetch);
|
|
37
|
+
expect(result).toBe(mockFetch);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("returns wrapped fetch when @opentelemetry/api is available", () => {
|
|
41
|
+
mockOtel();
|
|
42
|
+
const { createTracedFetch } = require("../telemetry.js");
|
|
43
|
+
const mockFetch = jest.fn() as unknown as typeof fetch;
|
|
44
|
+
const result = createTracedFetch(mockFetch);
|
|
45
|
+
expect(result).not.toBe(mockFetch);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("sets span attributes on successful request", async () => {
|
|
49
|
+
const { mockSpan, mockTracer } = mockOtel();
|
|
50
|
+
const { createTracedFetch } = require("../telemetry.js");
|
|
51
|
+
|
|
52
|
+
const mockResponse = { status: 200 } as Response;
|
|
53
|
+
const mockFetch = jest.fn().mockResolvedValue(mockResponse) as unknown as typeof fetch;
|
|
54
|
+
|
|
55
|
+
const traced = createTracedFetch(mockFetch);
|
|
56
|
+
const response = await traced("https://api.levelfour.ai/health", { method: "GET" });
|
|
57
|
+
|
|
58
|
+
expect(response).toBe(mockResponse);
|
|
59
|
+
expect(mockTracer.startActiveSpan).toHaveBeenCalledWith("levelfour.http GET", expect.any(Function));
|
|
60
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith("http.method", "GET");
|
|
61
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith("http.url", "https://api.levelfour.ai/health");
|
|
62
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith("http.status_code", 200);
|
|
63
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith("levelfour.success", true);
|
|
64
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("records error status codes", async () => {
|
|
68
|
+
const { mockSpan } = mockOtel();
|
|
69
|
+
const { createTracedFetch } = require("../telemetry.js");
|
|
70
|
+
|
|
71
|
+
const mockResponse = { status: 404 } as Response;
|
|
72
|
+
const mockFetch = jest.fn().mockResolvedValue(mockResponse) as unknown as typeof fetch;
|
|
73
|
+
|
|
74
|
+
const traced = createTracedFetch(mockFetch);
|
|
75
|
+
const response = await traced("https://api.levelfour.ai/api/v1/missing");
|
|
76
|
+
|
|
77
|
+
expect(response.status).toBe(404);
|
|
78
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith("levelfour.success", false);
|
|
79
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("records exceptions on fetch failure", async () => {
|
|
83
|
+
const { mockSpan } = mockOtel();
|
|
84
|
+
const { createTracedFetch } = require("../telemetry.js");
|
|
85
|
+
|
|
86
|
+
const networkError = new TypeError("fetch failed");
|
|
87
|
+
const mockFetch = jest.fn().mockRejectedValue(networkError) as unknown as typeof fetch;
|
|
88
|
+
|
|
89
|
+
const traced = createTracedFetch(mockFetch);
|
|
90
|
+
await expect(traced("https://api.levelfour.ai/health")).rejects.toThrow("fetch failed");
|
|
91
|
+
|
|
92
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith("levelfour.success", false);
|
|
93
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith("levelfour.error.type", "TypeError");
|
|
94
|
+
expect(mockSpan.recordException).toHaveBeenCalledWith(networkError);
|
|
95
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("defaults method to GET when not specified", async () => {
|
|
99
|
+
const { mockSpan, mockTracer } = mockOtel();
|
|
100
|
+
const { createTracedFetch } = require("../telemetry.js");
|
|
101
|
+
|
|
102
|
+
const mockFetch = jest.fn().mockResolvedValue({ status: 200 } as Response) as unknown as typeof fetch;
|
|
103
|
+
|
|
104
|
+
const traced = createTracedFetch(mockFetch);
|
|
105
|
+
await traced("https://api.levelfour.ai/health");
|
|
106
|
+
|
|
107
|
+
expect(mockTracer.startActiveSpan).toHaveBeenCalledWith("levelfour.http GET", expect.any(Function));
|
|
108
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith("http.method", "GET");
|
|
109
|
+
});
|
|
110
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "Node16",
|
|
5
|
+
"moduleResolution": "Node16",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": ".",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true
|
|
13
|
+
},
|
|
14
|
+
"include": [
|
|
15
|
+
"**/*.ts"
|
|
16
|
+
],
|
|
17
|
+
"exclude": [
|
|
18
|
+
"node_modules",
|
|
19
|
+
"dist",
|
|
20
|
+
"tests"
|
|
21
|
+
]
|
|
22
|
+
}
|