Skip to content

Commit

Permalink
Add a new optional step to set a custom api path
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Weirich <matthias.weirich@selectcode.de>
  • Loading branch information
vavido committed Jan 4, 2021
1 parent 328fae4 commit 4b6525d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
14 changes: 14 additions & 0 deletions javascript/lib/api/src/client/builder-steps.ts
Expand Up @@ -68,6 +68,14 @@ interface ProtocolStep<T extends BuildStep, U extends BuildStep> extends BuildSt
* @param <U> - type of API 2 Build steps.
*/
interface EnvironmentStep<T extends BuildStep, U extends BuildStep> extends BuildStep {

/**
* Sets a custom path for the api endpoint (optional step)
*
* @param path custom ditto api path, e.g. /custom - will be used instead of the default /api
*/
withCustomPath(path: string): EnvironmentStep<T, U>;

/**
* Sets a custom domain to use for requests.
*
Expand Down Expand Up @@ -212,6 +220,7 @@ interface CustomSearchHandleStep<R, H extends SearchHandle> extends BuildStep {
abstract class AbstractBuilder<T extends BuildStep, U extends BuildStep> implements ProtocolStep<T, U>, EnvironmentStep<T, U>,
AuthenticationStep<T, U>, ApiVersionStep<T, U> {
protected domain!: string;
protected customPath?: string;
protected apiVersion!: ApiVersion;
protected authProviders!: AuthProvider[];
protected tls!: boolean;
Expand All @@ -227,6 +236,11 @@ abstract class AbstractBuilder<T extends BuildStep, U extends BuildStep> impleme
return this;
}

withCustomPath(path: string): EnvironmentStep<T, U> {
this.customPath = path;
return this;
}

withDomain(url: string): AuthenticationStep<T, U> {
this.domain = url;
return this;
Expand Down
11 changes: 9 additions & 2 deletions javascript/lib/api/src/client/http-client-builder.ts
Expand Up @@ -13,7 +13,13 @@

import { HttpThingsHandle, HttpThingsHandleV1, HttpThingsHandleV2 } from './handles/things.interfaces';
import { HttpRequester, HttpRequestSenderBuilder } from './request-factory/http-request-sender';
import { AllDittoHttpHandles, DefaultDittoHttpClient, DittoHttpClient, DittoHttpClientV1, DittoHttpClientV2 } from './ditto-client-http';
import {
AllDittoHttpHandles,
DefaultDittoHttpClient,
DittoHttpClient,
DittoHttpClientV1,
DittoHttpClientV2
} from './ditto-client-http';
import { HttpMessagesHandle } from './handles/messages-http';
import { PoliciesHandle } from './handles/policies';
import { SearchHandle } from './handles/search';
Expand Down Expand Up @@ -122,7 +128,8 @@ export class HttpClientBuilder extends AbstractBuilder<BuildStepApi1, BuildStepA

private buildUrl(): DittoURL {
const protocol = this.tls ? 'https' : 'http';
return ImmutableURL.newInstance(protocol, this.domain, `/api/${this.apiVersion}`);
const path = this.customPath === undefined ? this.customPath : '/api';
return ImmutableURL.newInstance(protocol, this.domain, `${path}/${this.apiVersion}`);
}

withCustomThingsHandle(factory: (requestSenderBuilder: HttpRequestSenderBuilder,
Expand Down
4 changes: 3 additions & 1 deletion javascript/lib/api/src/client/websocket-client-builder.ts
Expand Up @@ -189,9 +189,11 @@ export class WebSocketClientBuilder extends AbstractBuilder<WebSocketBufferStep,

build(): DefaultDittoWebSocketClient {
const protocol = this.tls ? 'wss' : 'ws';
const path = this.customPath === undefined ? this.customPath : '/ws';

const resilienceHandlerFactory = this.resilienceFactory.withContext(
this.builder.withConnectionDetails(
ImmutableURL.newInstance(protocol, this.domain, `/ws/${this.apiVersion}`),
ImmutableURL.newInstance(protocol, this.domain, `${path}/${this.apiVersion}`),
this.authProviders),
this.stateHandler);
const requester = new WebSocketRequestHandler(resilienceHandlerFactory);
Expand Down

0 comments on commit 4b6525d

Please sign in to comment.