Skip to content

Commit

Permalink
feat(all): add type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanrsmith committed Jul 28, 2015
1 parent 23f9ceb commit 15fbbbd
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build/tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var tools = require('aurelia-tools');

gulp.task('build-index', function(){
var importsToAdd = [];
var files = ['util.js', 'http-client-configuration.js', 'http-client.js'].map(function(file){
var files = ['util.js', 'http-client-configuration.js', 'http-client.js', 'interfaces.js'].map(function(file){
return paths.root + file;
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"devDependencies": {
"aurelia-tools": "^0.1.7",
"babel-dts-generator": "^0.2.5",
"babel-dts-generator": "^0.2.6",
"babel-eslint": "^3.1.19",
"conventional-changelog": "0.0.11",
"del": "^1.1.0",
Expand Down
17 changes: 9 additions & 8 deletions src/http-client-configuration.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {IRequestInit, IInterceptor} from './interfaces';
import 'core-js';

/**
Expand All @@ -11,7 +12,7 @@ export class HttpClientConfiguration {
* The base URL to be prepended to each Request's url before sending.
* @type {String}
*/
baseUrl = '';
baseUrl: string = '';

/**
* Default values to apply to init objects when creating Requests. Note that
Expand All @@ -20,13 +21,13 @@ export class HttpClientConfiguration {
* See also https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
* @type {Object}
*/
defaults = {};
defaults: IRequestInit = {};

/**
* Interceptors to be added to the HttpClient.
* @type {Array}
*/
interceptors = [];
interceptors: IInterceptor[] = [];

/**
* Sets the baseUrl.
Expand All @@ -35,7 +36,7 @@ export class HttpClientConfiguration {
* @returns {HttpClientConfiguration}
* @chainable
*/
withBaseUrl(baseUrl) {
withBaseUrl(baseUrl: string): HttpClientConfiguration {
this.baseUrl = baseUrl;
return this;
}
Expand All @@ -47,7 +48,7 @@ export class HttpClientConfiguration {
* @returns {HttpClientConfiguration}
* @chainable
*/
withDefaults(defaults) {
withDefaults(defaults: IRequestInit): HttpClientConfiguration {
this.defaults = defaults;
return this;
}
Expand All @@ -63,7 +64,7 @@ export class HttpClientConfiguration {
* @returns {HttpClientConfiguration}
* @chainable
*/
withInterceptor(interceptor) {
withInterceptor(interceptor: IInterceptor): HttpClientConfiguration {
this.interceptors.push(interceptor);
return this;
}
Expand All @@ -75,7 +76,7 @@ export class HttpClientConfiguration {
* @returns {HttpClientConfiguration}
* @chainable
*/
useStandardConfiguration() {
useStandardConfiguration(): HttpClientConfiguration {
let standardConfig = { credentials: 'same-origin' };
Object.assign(this.defaults, standardConfig, this.defaults);
return this.rejectErrorResponses();
Expand All @@ -92,7 +93,7 @@ export class HttpClientConfiguration {
* @returns {HttpClientConfiguration}
* @chainable
*/
rejectErrorResponses() {
rejectErrorResponses(): HttpClientConfiguration {
return this.withInterceptor({ response: rejectOnError });
}
}
Expand Down
24 changes: 16 additions & 8 deletions src/http-client.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import {HttpClientConfiguration} from './http-client-configuration';
import {mergeHeaders} from './util';
import {IRequestInit, IInterceptor} from './interfaces';
import 'core-js';

type ConfigOrCallback = IRequestInit|(config: HttpClientConfiguration) => void|string;

/**
* An HTTP client based on the Fetch API.
*
* @class HttpClient
* @constructor
*/
export class HttpClient {
activeRequestCount = 0;
isRequesting = false;
interceptors = [];
isConfigured = false;
baseUrl = '';
defaults = null;
activeRequestCount: number = 0;

isRequesting: boolean = false;

interceptors: IInterceptor[] = [];

isConfigured: boolean = false;

baseUrl: string = '';

defaults: IRequestInit = null;

/**
* Configure this client with default settings to be used by all requests.
Expand All @@ -24,7 +32,7 @@ export class HttpClient {
* @returns {HttpClient}
* @chainable
*/
configure(config) {
configure(config: ConfigOrCallback): HttpClient {
let normalizedConfig;

if (typeof config === 'string') {
Expand Down Expand Up @@ -60,7 +68,7 @@ export class HttpClient {
* the Request.
* @return {Promise} - A Promise that resolves with the Response.
*/
fetch(input, init) {
fetch(input: Request|string, init?: IRequestInit): Promise<Response> {
this::trackRequestStart();

let request = Promise.resolve().then(() => this::buildRequest(input, init, this.defaults));
Expand Down
25 changes: 25 additions & 0 deletions src/interfaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable */

interface IInterceptor {
request?: (request: Request) => Request|Response|Promise<Request|Response>;

requestError?: (error: any) => Request|Response|Promise<Request|Response>;

response?: (response: Response) => Response|Promise<Response>;

responseError?: (error: any) => Response|Promise<Response>;
}

interface IRequestInit {
method?: string;

headers?: Headers;

body?: Blob|BufferSource|FormData|URLSearchParams|string;

mode?: string;

credentials?: string;

cache?: string;
}
4 changes: 2 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @param {*} body - [description]
* @return {Blob} - A blob containing the JSON-serialized body.
*/
export function json(body) {
export function json(body: any): Blob {
return new Blob([JSON.stringify(body)], { type: 'application/json' });
}

Expand All @@ -20,7 +20,7 @@ export function json(body) {
* @return {Headers} - A Headers instance containing the headers from
* both objects.
*/
export function mergeHeaders(first, second) {
export function mergeHeaders(first: Headers|Object, second: Headers|Object): Headers {
let headers = new Headers(first || {});
(new Headers(second || {})).forEach((value, name) => {
headers.set(name, value);
Expand Down

0 comments on commit 15fbbbd

Please sign in to comment.