import { ApiRequestError, NetworkError, RetryRequestError } from '../errors'; import type { Entity, DoLookupUserOptions, NetworkContext, NetworkProxy } from '@polarityio/integration-types'; export type { NetworkContext, NetworkProxy }; /** * @public */ export type ConfigRequestProxyOptions = { ca?: undefined | string; cert?: undefined | string; key?: undefined | string; passphrase?: undefined | string; rejectUnauthorized?: undefined | boolean; proxy?: undefined | string; json?: undefined | boolean; }; export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS'; /** * @public */ export type HttpRequestOptions = { /** * The URL to make the request to. */ url?: string; /** * The HTTP method to use for the request. * * @defaultValue 'GET' */ method?: HttpMethod; /** * If true, sets body to JSON representation of value and adds Content-type: application/json header. * Additionally, parses the response body as JSON. * * @defaultValue true */ json?: boolean; /** * An object containing the headers to include in the request. * @example * Here is an example of setting the "X-Api-Key" `headers` property: * ``` * { * headers: { * 'X-Api-Key': '1234567890' * } * } * ``` */ headers?: object; /** * An object containing querystring parameters to include in the request. * @example * Here is an example of setting the `qs` property: * ``` * { * qs: { * search: 'foo' * } * } * ``` */ qs?: object; /** * When passed an object or a querystring, this sets body to a querystring representation of value, * and adds Content-type: application/x-www-form-urlencoded header. */ form?: object; /** * The body of the request. */ body?: object; /** * The authentication options to use for the request */ auth?: { username: string; password: string; sendImmediately?: boolean; } | { bearer: string; sendImmediately?: boolean; }; [key: string]: unknown; } & ({ entity: Entity; entities?: never; requestId?: never; } | { entities: Entity[]; entity?: never; requestId?: never; } | { requestId: string | unknown; entity?: never; entities?: never; } | { entity?: never; entities?: never; requestId?: never; }); /** * @public */ export type RunInParallelOptions = { /** * Array of HttpRequestOptions that will be run in parallel as specified by * the `maxConcurrentRequests` property. */ allRequestOptions: HttpRequestOptions[]; /** * Maximum number of requests to run in parallel * * @defaultValue 5 */ maxConcurrentRequests?: number; /** * If true, any errors thrown during the request will be returned in the response object on the `error` property * of the returned `HttpRequestResponse` object. If false, any errors thrown will be thrown and should be handled by * * * @defaultValue false */ returnErrors?: boolean; }; /** * @public */ export type HttpRequestResponse = { /** * The HTTP status code of the response. */ statusCode: number; request: { uri: unknown; method: string; headers: unknown; [key: string]: unknown; }; /** * The body of the response. */ body: unknown; /** * The response headers */ headers: unknown; /** * The error object if an error occurred during the request. */ error?: ApiRequestError | NetworkError | RetryRequestError; /** * The entity that the request was made for. The `entity` property matches the * `entity` property set on the {@link HttpRequestOptions} object associated with this HttpRequestResponse. */ entity?: Entity; /** * An array of entities that the request was made for. The `entities` property * matches the `entities` property set on the {@link HttpRequestOptions} object associated * with this HttpRequestResponse. */ entities?: Entity[]; /** * A custom request id that can be used to identify the request. The `requestId` matches * the `requestId` property set on the {@link HttpRequestOptions} object. */ requestId?: string | unknown; [key: string]: unknown; }; /** * @public */ export type IsApiErrorResult = { /** * Indicates whether the response is an API error. */ isApiError: boolean; /** * Optional message providing additional information about the API error. The * returned `message` will be used as the `message` property on the {@link ApiRequestError} object * thrown by the request. */ message?: string; }; /** * @public */ export type IsApiErrorFunction = (response: HttpRequestResponse, requestOptions: HttpRequestOptions, userOptions: DoLookupUserOptions) => IsApiErrorResult; /** * Minimal interface for a rate limiter compatible with PolarityRequest. * Only requires the `schedule` method, which is the sole method used internally. * The Polarity server provides a full `Limiter` instance (from * `@polarityio/integration-types`) that satisfies this interface, but consumers * may also pass simpler objects or mocks that only implement `schedule`. * * @public */ export interface PolarityRequestLimiter { schedule(fn: (...args: unknown[]) => PromiseLike, ...args: unknown[]): Promise; } /** * @deprecated Use {@link PolarityRequestLimiter} instead. This alias will be removed in a future major version. * @public */ export type Limiter = PolarityRequestLimiter; /** * Hook that runs before an HTTP request is made. Each hook receives the output * of the previous hook, allowing request options to be modified in a chain. * * Typically used for adding authentication headers or conditionally modifying * request options based on user options. * * @public */ export type BeforeRequestHook = (requestOptions: HttpRequestOptions, userOptions: DoLookupUserOptions) => Promise; /** * Hook that runs after a successful HTTP response. Each hook receives the output * of the previous hook, allowing the response to be modified in a chain. * * Typically used to extract specific fields from the response body or to * transform the response into a more useful shape. * * @public */ export type AfterResponseHook = (response: HttpRequestResponse, requestOptions: HttpRequestOptions, userOptions: DoLookupUserOptions) => Promise; /** * Hook that runs when an API error is detected (non-success status code or error * properties found in the response body). Receives the full HTTP response so the * hook can inspect status codes, headers, and body. * * If all registered hooks return without throwing, the error is suppressed and * the HTTP response is returned to the caller. To propagate or replace the error, * throw from within the hook. * * @public */ export type OnApiErrorHook = (error: ApiRequestError, response: HttpRequestResponse, requestOptions: HttpRequestOptions, userOptions: DoLookupUserOptions) => Promise; /** * Hook that runs when a network error or rate-limiting error occurs during a request. * * If all registered hooks return without throwing, the error is suppressed. * To propagate or replace the error, throw from within the hook. * * @public */ export type OnNetworkErrorHook = (error: NetworkError | RetryRequestError, requestOptions: HttpRequestOptions, userOptions: DoLookupUserOptions) => Promise; /** * Hooks for customizing the {@link PolarityRequest} lifecycle. All hook arrays * execute in order. `beforeRequest` and `afterResponse` hooks chain their output, * while error hooks run sequentially and can suppress errors by returning without * throwing. * * @public */ export interface PolarityRequestHooks { /** * Hooks that run before each HTTP request. Each hook receives a copy of the request * options and the return value is passed to the next hook (or used for the request). */ beforeRequest?: BeforeRequestHook[]; /** * Hooks that run after a successful HTTP response. Each hook receives the response * from the previous hook and the return value is passed to the next hook (or returned * to the caller). */ afterResponse?: AfterResponseHook[]; /** * Hooks that run when an API error is detected. Each hook receives the error and the * full HTTP response. If all hooks return without throwing, the error is suppressed. */ onApiError?: OnApiErrorHook[]; /** * Hooks that run when a network or rate-limiting error occurs. If all hooks return * without throwing, the error is suppressed. */ onNetworkError?: OnNetworkErrorHook[]; } /** * @public */ export interface PolarityRequestOptions { defaults?: ConfigRequestProxyOptions; /** * Per-integration network configuration from `context.network`. When provided, * proxy and TLS settings from this object override values from `defaults`. * * Can also be set (or updated) after construction via the {@link PolarityRequest.network} * property to reflect per-request runtime config. */ network?: NetworkContext; isApiError?: IsApiErrorFunction; roundedSuccessStatusCodes?: number[]; httpResponseErrorProperties?: string[]; httpResponseErrorMessageProperties?: string[]; requestOptionsToSanitize?: string[]; hooks?: PolarityRequestHooks; limiter?: PolarityRequestLimiter; } /** * A utility class for making HTTP requests * @group Requests * @public */ export declare class PolarityRequest { /** * Instance of a Bunyan logger */ private logger; /** * postman-request library request object with default values set. Used internally for * making HTTP requests directly via the postman-request library */ private readonly requestWithDefaults; readonly roundedSuccessStatusCodes: number[]; /** * One or more HTTP response properties specified using JSON dot notation. If the * specified path exists within the `body` property of the HTTP Response, an * ApiRequestError will be thrown. * * By default, this value is an empty array and response properties are not used to * detect errors. * @defaultValue [] */ readonly httpResponseErrorProperties: string[]; /** * One or more HTTP response properties specified using JSON dot notation that * point to an error message that should be displayed to the user in the event * of an API error. * * The property should be a string value. If the property does not exist or is not * a string value, a default error message will be used instead. */ readonly httpResponseErrorMessageProperties: string[]; /** * Optional method that can be implemented to determine if an API error * was encountered after an HTTP request is made. * * If the `isApiError` method is implemented * the property `roundedSuccessStatusCodes` and `httpResponseErrorProperties` are not * used to determine API errors. * * @returns An object indicating whether an API error was encountered and an optional message. */ readonly isApiError: IsApiErrorFunction; /** * An array of JSON dot notation paths to omit from the request options when logging. * * This property can be used to sanitize sensitive request properties that should not * appear in logging. * * Note that the `requestOptions` object is automatically sanitized to remove properties that * typically contain sensitive API key and passwords. For a list of properties that are * automatically sanitized, see the {@link sanitizeRequestOptions} method. */ readonly requestOptionsToSanitize: string[]; userOptions: DoLookupUserOptions; /** * An optional rate limiter instance used to throttle HTTP requests. * When set, all requests made via {@link PolarityRequest.run} are scheduled * through this limiter. Typically provided by the Polarity server via the * integration context. */ limiter: PolarityRequestLimiter | null; /** * Per-integration network configuration from `context.network`. * * When set, proxy and TLS settings are applied dynamically on each * {@link PolarityRequest.run} call, overriding any static values from * the constructor `defaults`. Set this before each `run()` call to * reflect the latest runtime config. * * @example * ```typescript * polarityRequest.network = context.network; * const response = await polarityRequest.run(requestOptions); * ``` */ network: NetworkContext | null; /** * Lifecycle hooks for customizing request behavior. Hooks are configured via the * {@link PolarityRequestOptions.hooks} property when creating a new instance of the * {@link PolarityRequest} class. * * @see {@link PolarityRequestHooks} for hook type details. */ readonly hooks: Required; constructor(options?: PolarityRequestOptions); private configFieldIsValid; /** * Makes a single HTTP request and returns the response or throws an error * * @param requestOptions - request options used to make the HTTP request * @returns The HTTP response */ run(requestOptions: HttpRequestOptions): Promise | never; /** * Checks whether the HTTP response is an API error and returns an ApiRequestError if it is. * * @param httpResponse - The HTTP response from the Postman request. * @param requestOptions - The options used for the request. * @returns An ApiRequestError if the response indicates an API error, undefined otherwise. * * @throws {@link LibraryUsageError} * Throws if the `isApiError` function returns an invalid result. */ private detectApiError; /** * Returns true if the `httpStatusCode` is not one of the rounded HTTP status codes * specified in the PolarityRequest `roundedSuccessStatusCodes` property. * * @param httpStatusCode - A numeric HTTP Status Code * @returns true if the provided `httpStatusCode` is an error code */ private isHttpStatusCodeError; /** * Returns true indicating that the API returned an error if the `httpBody` contains * one of the paths specified by the PolarityRequest `httpResponseErrorProperties` * property. * * @param httpBody - body property from the HttpRequestResponse * @returns `true` if the httpBody property contains properties specified in `httpResponseErrorProperties` */ private hasHttpResponseErrorProperty; /** * Returns an error message based on the `httpResponseErrorMessageProperties` first. If no * message is found, it then uses the `httpResponseErrorProperties` to attempt to find * a suitable error message. If no message is still found, the `defaultMessage` is returned. * * @param httpBody - JSON Object returned by an HTTP Request * @param defaultMessage - A default error message to use if no specific error messages are found * @returns An error message */ private getErrorMessageFromHttpResponse; /** * Given a list of `properties` which are strings representing JSON dot notation, this * method returns the first string property found at the given JSON path * in the given `object`. * * @param object - An object to find properties in * @param properties - a list of JSON dot notation properties to look for within `object` * @returns A string value of the property found within the given object or undefined if no value is found */ private maybeGetStringPropertyValue; /** * Runs multiple requests in parallel with a limit on the maximum number of concurrent requests. * * When running multiple request at once it is often useful to be able to tie a specific request * back to the entity the request is for. To support this, the `HttpRequestOptions` object accepts * an optional `entity` property which can be assigned to the Entity the request is being made for. * The `HttpRequestResponse` object returned by this method will include the same `entity` property * making it easy to match the response to the entity. * * Alternatively, for requests that are made for multiple entities at once (e.g., a query that can * search multiple entities at a time), the `HttpRequestOptions` object also has an `entities` * property which accept an array of entity objects. Similar to the `entity`, the * `entities` property will be set on the `HttpRequestResponse` object. * * Finally, if you are looking to pass through a custom request id you can do that using the * `requestId` property. * * @param options - An array of request options for running requests in parallel. * @returns A promise that resolves to an array of responses in the same order as the input * request options. If the `returnErrors` property is set to `true`, the response objects will * have their `error` property set to the thrown error. If `onNetworkError` hooks are configured * and suppress an error (return without throwing), the corresponding entry will be `undefined`. */ runInParallel(options: RunInParallelOptions): Promise<(HttpRequestResponse | undefined)[]>; } //# sourceMappingURL=polarity-request.d.ts.map