import { HttpClient } from '@angular/common/http'; import { CyHttpMessages } from 'cypress/types/net-stubbing'; import { Observable } from 'rxjs'; import { AbstractEndpoint, Endpoint } from './endpoint'; import { Headers, RouteConfig } from './routing'; import { SpyHttpClient } from './spy-http-client'; /** * A client with a list of endpoints. * Can be used to implement your own specific client that is not based on a generated client. */ export abstract class AbstractClientStub { // AbstractEndpoint needed for endpoints without parameter abstract endpoints: { [endpointName: string]: AbstractEndpoint | AbstractEndpoint; }; protected constructor(public name: string) {} /** * Automatically affect the name of each endpoint */ init(): this { Object.keys(this.endpoints).forEach((endpointName) => (this.endpoints[endpointName].endpointName = endpointName)); return this; } get allDefaultConfigs(): RouteConfig[] { return Object.values(this.endpoints).map((r) => r.defaultConfig()); } } /** * A client with endpoints based on generated client's endpoints */ export abstract class ClientStub extends AbstractClientStub { protected readonly spyHttpClient = new SpyHttpClient(); protected readonly client: C; protected constructor(clientConstructor: new (http: HttpClient, baseUrl?: string) => C) { super(clientConstructor.name); this.client = new clientConstructor(this.spyHttpClient as unknown as HttpClient); } protected createEndpoint( actualEndpoint: (...otherParams: IN) => Observable, status: number, fixtureOrFixtureBuilder: OUT | ((req: CyHttpMessages.IncomingHttpRequest) => OUT), headers: Headers = {} ): Endpoint { return new Endpoint( this.spyHttpClient, this.client, actualEndpoint, this.name, status, fixtureOrFixtureBuilder, headers ); } }