Skip to content

Commit

Permalink
HttpClient: Added debug mode (julienblin#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienblin authored Aug 27, 2018
1 parent 56fdc2c commit 9102028
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- SQSEventPublisher to publish events using AWS Simple Queue Service
- SQSEvent & SQSEventBatch as specialized handlers for Simple Queue Service-backed lambdas
- CLI: generate-schemas: added -config option to specify tsconfig.json file (Fix #60)
- HttpClient: Added debug mode (Fix #62)
- Added clear() and list() to KeyValueRepository (Fix #57)

## [0.49.0] - 2018-08-23
Expand Down
2 changes: 1 addition & 1 deletion packages/uno-serverless/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/uno-serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"base-58": "0.0.1",
"bcryptjs": "^2.4.3",
"chai": "^4.1.2",
"chalk": "^2.4.1",
"clean-webpack-plugin": "^0.1.19",
"json-stringify-safe": "^5.0.1",
"mocha": "^5.2.0",
Expand Down
129 changes: 87 additions & 42 deletions packages/uno-serverless/src/services/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Axios, * as axios from "axios";
import { isStatusCodeProvider, lazyAsync } from "../core";
import * as chalk from "chalk";
import { isStatusCodeProvider, lazyAsync, safeJSONStringify } from "../core";

export type PossiblePromise<T> = T | Promise<T>;

export interface HttpClientConfig {
adapter?: axios.AxiosAdapter;
auth?: PossiblePromise<axios.AxiosBasicCredentials>;
cancelToken?: axios.CancelToken;
debug?: PossiblePromise<boolean>;
baseURL?: PossiblePromise<string>;
headers?: PossiblePromise<Record<string, string>>;
httpAgent?: any;
Expand All @@ -32,9 +34,9 @@ export interface HttpClient {
head(url: string, config?: axios.AxiosRequestConfig | undefined): Promise<axios.AxiosResponse<any>>;
patch<T = any>(url: string, data?: any, config?: axios.AxiosRequestConfig | undefined);
post<T = any>(url: string, data?: any, config?: axios.AxiosRequestConfig | undefined)
: Promise<axios.AxiosResponse<T>>;
: Promise<axios.AxiosResponse<T>>;
put<T = any>(url: string, data?: any, config?: axios.AxiosRequestConfig | undefined)
: Promise<axios.AxiosResponse<T>>;
: Promise<axios.AxiosResponse<T>>;
request<T = any>(config: axios.AxiosRequestConfig): Promise<axios.AxiosResponse<T>>;
}

Expand Down Expand Up @@ -83,75 +85,75 @@ class AxiosHttpClient implements HttpClient {

private readonly lazyClient = lazyAsync(async () => this.clientBuilder());

public constructor(private readonly clientBuilder: () => Promise<axios.AxiosInstance>) {}
public constructor(private readonly clientBuilder: () => Promise<axios.AxiosInstance>) { }

public async delete(url: string, config?: axios.AxiosRequestConfig | undefined)
: Promise<axios.AxiosResponse<any>> {
try {
return await (await this.lazyClient()).delete(url, config);
} catch (error) {
throw httpClientError(error);
}
try {
return await (await this.lazyClient()).delete(url, config);
} catch (error) {
throw httpClientError(error);
}
}

public async get<T = any>(url: string, config?: axios.AxiosRequestConfig | undefined)
: Promise<axios.AxiosResponse<T>> {
try {
return await (await this.lazyClient()).get<T>(url, config);
} catch (error) {
throw httpClientError(error);
}
try {
return await (await this.lazyClient()).get<T>(url, config);
} catch (error) {
throw httpClientError(error);
}
}

public async head(url: string, config?: axios.AxiosRequestConfig | undefined)
: Promise<axios.AxiosResponse<any>> {
try {
return await (await this.lazyClient()).head(url, config);
} catch (error) {
throw httpClientError(error);
}
try {
return await (await this.lazyClient()).head(url, config);
} catch (error) {
throw httpClientError(error);
}
}

public async patch<T = any>(url: string, data?: any, config?: axios.AxiosRequestConfig | undefined)
: Promise<axios.AxiosResponse<T>> {
try {
return await (await this.lazyClient()).patch<T>(url, data, config);
} catch (error) {
throw httpClientError(error);
}
try {
return await (await this.lazyClient()).patch<T>(url, data, config);
} catch (error) {
throw httpClientError(error);
}
}

public async post<T = any>(url: string, data?: any, config?: axios.AxiosRequestConfig | undefined)
: Promise<axios.AxiosResponse<T>> {
try {
return await (await this.lazyClient()).post<T>(url, data, config);
} catch (error) {
throw httpClientError(error);
}
try {
return await (await this.lazyClient()).post<T>(url, data, config);
} catch (error) {
throw httpClientError(error);
}
}

public async put<T = any>(url: string, data?: any, config?: axios.AxiosRequestConfig | undefined)
: Promise<axios.AxiosResponse<T>> {
try {
return await (await this.lazyClient()).put<T>(url, data, config);
} catch (error) {
throw httpClientError(error);
}
try {
return await (await this.lazyClient()).put<T>(url, data, config);
} catch (error) {
throw httpClientError(error);
}
}

public async request<T = any>(config: axios.AxiosRequestConfig)
: Promise<axios.AxiosResponse<T>> {
try {
return await (await this.lazyClient()).request<T>(config);
} catch (error) {
throw httpClientError(error);
}
try {
return await (await this.lazyClient()).request<T>(config);
} catch (error) {
throw httpClientError(error);
}
}
}

export const httpClientFactory = (config: HttpClientConfig = {}): HttpClient =>
new AxiosHttpClient(async () =>
Axios.create({
new AxiosHttpClient(async () => {
const axiosInstance = Axios.create({
adapter: config.adapter ? config.adapter : Axios.defaults.adapter,
auth: config.auth ? await config.auth : Axios.defaults.auth,
baseURL: config.baseURL ? await config.baseURL : Axios.defaults.baseURL,
Expand All @@ -172,4 +174,47 @@ export const httpClientFactory = (config: HttpClientConfig = {}): HttpClient =>
transformResponse: config.transformResponse ? config.transformResponse : Axios.defaults.transformResponse,
validateStatus: config.validateStatus ? config.validateStatus : Axios.defaults.validateStatus,
withCredentials: config.withCredentials ? await config.withCredentials : Axios.defaults.withCredentials,
}));
});

if (await config.debug) {
const debug = (content, color) => {
console.log(chalk[color](
typeof content === "string" ? content : safeJSONStringify(content, undefined, 2)));
};

const curateRequest = (c) => {
if (typeof c === "string") {
return c;
}

return {
baseURL: c.baseURL,
data: c.data,
headers: c.headers || {},
method: c.method,
url: c.url,
};
};

const curateResponse = (response) => {
if (typeof response === "string") {
return response;
}

return {
data: response.data,
headers: response.headers || {},
status: response.status,
};
};

axiosInstance.interceptors.request.use(
(c) => { debug(curateRequest(c), "cyan"); return c; },
(error) => { debug(error, "red"); return Promise.reject(error); });
axiosInstance.interceptors.response.use(
(response) => { debug(curateResponse(response), "green"); return response; },
(error) => { debug(error, "red"); return Promise.reject(error); });
}

return axiosInstance;
});

0 comments on commit 9102028

Please sign in to comment.