UNPKG

@altostra/core

Version:

Core library for shared types and logic

62 lines (61 loc) 3.5 kB
import type { WrapParams as UserFacingErrorWrapParams } from "../../../common/Errors/UserFacingError"; import type { Logger, OperationLoggingSeverity } from "../../../common/Logging"; import type { EmptyObject } from "../../../common/Utils/Object"; import type { AnyTypeValidation, Assertion } from '@altostra/type-validations'; import type { AxiosInstance, AxiosResponse } from 'axios'; import type { Dict } from "../../Common"; import type { ErrorMapping } from "../common"; export interface ServiceClientBaseParams { serviceName?: string; axios?: AxiosInstance; logger?: Logger<OperationLoggingSeverity>; /** * An array of strings that would be sent the the server to signal that a certain * feature is supported for the current call. * * This allow using "breaking" features by exposing them only for calls that contain * certain features */ supportedFeatures?: string[]; errorMapping?: ErrorMapping; wrapAuthorizationErrors?: boolean; } /** * Unauthenticated service client - to handle response validation and error mapping */ export declare abstract class ServiceClientBase { #private; protected readonly _logger: Logger<OperationLoggingSeverity>; readonly serviceName: string; protected _errorMapping: ErrorMapping; constructor({ serviceName, axios, logger, supportedFeatures, errorMapping, wrapAuthorizationErrors, }?: ServiceClientBaseParams); protected abstract _getHeaders(): Promise<Dict<string>>; protected _withLogging<T>(operationName: string, operation: () => Promise<T>, successMessage?: (val: T) => string, errMessage?: (err: any) => string): Promise<T>; protected _request<T>({ request, errorWrapping, verboseErrorData, ...validation }: RequestOptions<T>): Promise<T>; } export declare type RequestOptions<T> = _RequestOptions<T> | LegacyRequestOptions<T>; interface _RequestOptions<T> extends RequestOptionsBase { dataValidation: AnyTypeValidation<T>; } interface LegacyRequestOptions<T> extends RequestOptionsBase { /** * A type assertion to validate the response data * @deprecated - Please use `dataValidation` instead */ dataValidator: Assertion<T>; } export interface RequestOptionsBase { request: (axios: AxiosInstance) => Promise<AxiosResponse<unknown>>; errorWrapping?: Partial<UserFacingErrorWrapParams>; verboseErrorData?: Record<any, unknown>; } export declare type AbstractServiceClientClass = abstract new (...args: any) => ServiceClientBase; export declare type ConcreteServiceClientClass = new (...args: any) => ServiceClientBase; export declare type ServiceClientMixin<T extends AbstractServiceClientClass, Mixin, Args extends object = EmptyObject> = T extends ConcreteServiceClientClass ? ConcreteServiceClientMixin<T, Mixin, Args> : T extends AbstractServiceClientClass ? AbstractServiceClientMixin<T, Mixin, Args> : never; declare type AbstractServiceClientMixin<T extends AbstractServiceClientClass, Mixin, Args extends object> = (abstract new (config: MixinCtorParam<T, Args>) => (InstanceType<T> & Mixin)) & ObjectFacet<T>; declare type ConcreteServiceClientMixin<T extends ConcreteServiceClientClass, Mixin, Args extends object> = (new (config: MixinCtorParam<T, Args>) => (InstanceType<T> & Mixin)) & ObjectFacet<T>; export declare type MixinCtorParam<T extends AbstractServiceClientClass, Args extends object> = Args & ConstructorParameters<T>[0]; declare type ObjectFacet<T> = { [K in keyof T]: T[K]; }; export {};