Skip to content

Commit

Permalink
feat(request): CHP-6487 change response types from any to unknown (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanceaclark committed May 6, 2020
1 parent a9f7d43 commit 18c0226
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/coverage
/lib
/node_modules
.vscode
2 changes: 1 addition & 1 deletion src/payload-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class PayloadTransformer {
return options.body;
}

toResponse(xhr: XMLHttpRequest): Response {
toResponse<T = unknown>(xhr: XMLHttpRequest): Response<T> {
const headers = this._parseResponseHeaders(xhr.getAllResponseHeaders());

// Using `responseText` to support legacy IE
Expand Down
14 changes: 7 additions & 7 deletions src/request-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class RequestSender {
this._cache = this._options.cache || new DefaultCache();
}

sendRequest<T = any>(url: string, options?: RequestOptions): Promise<Response<T>> {
sendRequest<T = unknown>(url: string, options?: RequestOptions): Promise<Response<T>> {
const requestOptions = this._mergeDefaultOptions(url, options);
const cachedRequest = this._getCachedRequest<T>(url, requestOptions);

Expand All @@ -34,7 +34,7 @@ export default class RequestSender {

return new Promise((resolve, reject) => {
const requestHandler = () => {
const response = this._payloadTransformer.toResponse(request);
const response = this._payloadTransformer.toResponse<T>(request);

if (response.status >= 200 && response.status < 300) {
this._cacheRequest(url, requestOptions, response);
Expand Down Expand Up @@ -62,23 +62,23 @@ export default class RequestSender {
});
}

get<T = any>(url: string, options?: RequestOptions): Promise<Response<T>> {
get<T = unknown>(url: string, options?: RequestOptions): Promise<Response<T>> {
return this.sendRequest(url, { ...options, method: 'GET' });
}

post<T = any>(url: string, options?: RequestOptions): Promise<Response<T>> {
post<T = unknown>(url: string, options?: RequestOptions): Promise<Response<T>> {
return this.sendRequest(url, { ...options, method: 'POST' });
}

put<T = any>(url: string, options?: RequestOptions): Promise<Response<T>> {
put<T = unknown>(url: string, options?: RequestOptions): Promise<Response<T>> {
return this.sendRequest(url, { ...options, method: 'PUT' });
}

patch<T = any>(url: string, options?: RequestOptions): Promise<Response<T>> {
patch<T = unknown>(url: string, options?: RequestOptions): Promise<Response<T>> {
return this.sendRequest(url, { ...options, method: 'PATCH' });
}

delete<T = any>(url: string, options?: RequestOptions): Promise<Response<T>> {
delete<T = unknown>(url: string, options?: RequestOptions): Promise<Response<T>> {
return this.sendRequest(url, { ...options, method: 'DELETE' });
}

Expand Down
2 changes: 1 addition & 1 deletion src/response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Headers from './headers';

export default interface Response<T = any> {
export default interface Response<T> {
body: T;
headers: Headers;
status: number;
Expand Down
6 changes: 3 additions & 3 deletions src/responses.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function getResponse(
headers: Headers = {},
status: number = 200,
statusText: string = 'OK'
): Response {
): Response<any> {
return {
body,
headers: {
Expand All @@ -23,7 +23,7 @@ export function getErrorResponse(
headers: Headers = {},
status: number = 400,
statusText: string = 'Bad Request'
): Response {
): Response<any> {
return {
body,
headers: {
Expand All @@ -35,7 +35,7 @@ export function getErrorResponse(
};
}

export function getTimeoutResponse(): Response {
export function getTimeoutResponse(): Response<any> {
return {
body: '',
headers: {},
Expand Down

0 comments on commit 18c0226

Please sign in to comment.