Skip to content

Commit

Permalink
Add self signed certificate path to the rest config (#18)
Browse files Browse the repository at this point in the history
* Support self signed sertificates.

Signed-off-by: Oleksandr Andriienko <[email protected]>
  • Loading branch information
AndrienkoAleksandr authored and evidolob committed Jul 1, 2019
1 parent 3133df3 commit eaf6156
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {Resources} from './rest/resources';
import {Backend, IBackend} from './rest/backend';
import {IWorkspaceMasterApi, WorkspaceMasterApi} from './json-rpc/workspace-master-api';
import {WebSocketClient} from './json-rpc/web-socket-client';

import * as fs from 'fs';
import * as https from 'https';

export * from './rest/backend';
export * from './rest/remote-api';
Expand All @@ -24,12 +25,12 @@ export * from './json-rpc/workspace-master-api';
export interface IRestAPIConfig {
baseUrl?: string;
headers?: any;
// path to self signed certificate
ssCrtPath?: string;
}

export default class WorkspaceClient {

private static axiosInstance: AxiosInstance = axios;

public static getRestApi(config: IRestAPIConfig = {}): IRemoteAPI {
let baseUrl = config.baseUrl;
if (!baseUrl) {
Expand All @@ -42,17 +43,31 @@ export default class WorkspaceClient {

const headers = config.headers || {};

const resources = new Resources(this.axiosInstance, baseUrl, headers);
const resources = new Resources(this.createAxiosInstance(config), baseUrl, headers);
return new RemoteAPI(resources);
}

public static getRestBackend(): IBackend {
return new Backend(this.axiosInstance, moxios);
return new Backend(axios, moxios);
}

public static getJsonRpcApi(entryPoint: string): IWorkspaceMasterApi {
const transport = new WebSocketClient();
return new WorkspaceMasterApi(transport, entryPoint);
}

private static createAxiosInstance(config: IRestAPIConfig): AxiosInstance {
if (config.ssCrtPath && this.isItNode() && fs.existsSync(config.ssCrtPath)) {
const agent = new https.Agent({
ca: fs.readFileSync(config.ssCrtPath)
});
return axios.create({httpsAgent: agent});
}

return axios;
}

private static isItNode() {
return (typeof process !== 'undefined') && (typeof process.versions.node !== 'undefined')
}
}
3 changes: 3 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ var client = {
extensions: ['.ts', '.js']
},
target: 'web',
node: {
fs: 'empty',
},
output: {
filename: 'client.js',
library: 'workspace-client',
Expand Down

0 comments on commit eaf6156

Please sign in to comment.