UNPKG

dfx-openapi

Version:

dfx-openapi is a type-safe Angular HttpClient that pulls in your OpenAPI schema. It has virtually zero runtime and is fully compatible with Http Interceptors.

38 lines (37 loc) 2.91 kB
import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { FilterKeys, HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf } from 'openapi-typescript-helpers'; import { HttpClientResponse } from './shared/http-client-response'; import { QuerySerializer } from './shared/query-serializer'; import { QuerySerializerOptions } from './shared/query-serializer-options'; import { RequestOptions } from './shared/request-options'; /** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */ type MaybeOptionalInit<Params, Location extends keyof Params> = RequiredKeysOf<RequestOptions<FilterKeys<Params, Location>>> extends never ? RequestOptions<FilterKeys<Params, Location>> | undefined : RequestOptions<FilterKeys<Params, Location>>; type InitParam<Init> = RequiredKeysOf<Init> extends never ? [(Init & { [key: string]: unknown; })?] : [Init & { [key: string]: unknown; }]; type ClientMethod<Paths extends Record<string, Record<HttpMethod, any>>, Method extends HttpMethod, Media extends MediaType> = <Path extends PathsWithMethod<Paths, Method>, Init extends MaybeOptionalInit<Paths[Path], Method>>(url: Path, ...init: InitParam<Init>) => Observable<HttpClientResponse<Paths[Path][Method], Media>>; export type ClientRequestMethod<Paths extends Record<string, Record<HttpMethod, any>>, Media extends MediaType> = <Method extends HttpMethod, Path extends PathsWithMethod<Paths, Method>, Init extends MaybeOptionalInit<Paths[Path], Method>>(method: Method, url: Path, ...init: InitParam<Init>) => Observable<HttpClientResponse<Paths[Path][Method], Media>>; export interface OpenAPIHttpClientOptions { baseUrl: string; querySerializer?: QuerySerializer<unknown> | QuerySerializerOptions; } export declare function createOpenAPIHttpClient<Paths extends Record<string, any>>(httpClient: HttpClient, options: OpenAPIHttpClientOptions): OpenAPIHttpClient<Paths, `${string}/${string}`>; export declare class OpenAPIHttpClient<Paths extends Record<string, any>, Media extends MediaType = MediaType> { private http; private readonly baseUrl; private readonly querySerializer?; constructor(http: HttpClient, { baseUrl, querySerializer }: OpenAPIHttpClientOptions); core<Path extends PathsWithMethod<Paths, Method>, Init extends MaybeOptionalInit<Paths[Path], Method>, Method extends HttpMethod>(method: Method, path: Path, options: InitParam<Init>): Observable<HttpClientResponse<Paths[Path], Media>>; request: ClientRequestMethod<Paths, Media>; get: ClientMethod<Paths, 'get', Media>; put: ClientMethod<Paths, 'put', Media>; post: ClientMethod<Paths, 'post', Media>; delete: ClientMethod<Paths, 'delete', Media>; options: ClientMethod<Paths, 'options', Media>; head: ClientMethod<Paths, 'head', Media>; patch: ClientMethod<Paths, 'patch', Media>; } export {};