Permalink
Browse files

feat(project): proper types

  • Loading branch information...
doktordirk committed Aug 17, 2016
1 parent 980a0d0 commit 320b7986a88e9fb652f36b060531a1eb591c5a79
Showing with 89 additions and 40 deletions.
  1. +1 −0 bower.json
  2. +43 −8 config.js
  3. +3 −0 package.json
  4. +2 −1 src/aurelia-api.js
  5. +12 −10 src/config.js
  6. +5 −4 src/endpoint.js
  7. +23 −17 src/rest.js
@@ -25,6 +25,7 @@
"dependencies": {
"aurelia-dependency-injection": "^1.0.0-rc.1.0.0",
"aurelia-fetch-client": "^1.0.0-rc.1.0.0",
"aurelia-framework": "^1.0.0-rc.1.0.0",
"aurelia-path": "^1.0.0-rc.1.0.0",
"extend": "^3.0.0"
}
@@ -7,21 +7,56 @@ System.config({
},

map: {
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0-rc.1.0.1",
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-fetch-client": "npm:aurelia-fetch-client@1.0.0-rc.1.0.1",
"aurelia-path": "npm:aurelia-path@1.0.0-rc.1.0.0",
"aurelia-framework": "npm:aurelia-framework@1.0.1",
"aurelia-path": "npm:aurelia-path@1.0.0",
"aurelia-polyfills": "npm:aurelia-polyfills@1.0.0-rc.1.0.0",
"extend": "npm:extend@3.0.0",
"fetch": "github:github/fetch@1.0.0",
"npm:aurelia-dependency-injection@1.0.0-rc.1.0.1": {
"aurelia-metadata": "npm:aurelia-metadata@1.0.0-rc.1.0.1",
"aurelia-pal": "npm:aurelia-pal@1.0.0-rc.1.0.0"
"npm:aurelia-binding@1.0.1": {
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0",
"aurelia-task-queue": "npm:aurelia-task-queue@1.0.0"
},
"npm:aurelia-metadata@1.0.0-rc.1.0.1": {
"aurelia-pal": "npm:aurelia-pal@1.0.0-rc.1.0.0"
"npm:aurelia-dependency-injection@1.0.0": {
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-framework@1.0.1": {
"aurelia-binding": "npm:aurelia-binding@1.0.1",
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-loader": "npm:aurelia-loader@1.0.0",
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0",
"aurelia-path": "npm:aurelia-path@1.0.0",
"aurelia-task-queue": "npm:aurelia-task-queue@1.0.0",
"aurelia-templating": "npm:aurelia-templating@1.0.0"
},
"npm:aurelia-loader@1.0.0": {
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-path": "npm:aurelia-path@1.0.0"
},
"npm:aurelia-metadata@1.0.0": {
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-polyfills@1.0.0-rc.1.0.0": {
"aurelia-pal": "npm:aurelia-pal@1.0.0-rc.1.0.0"
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-task-queue@1.0.0": {
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-templating@1.0.0": {
"aurelia-binding": "npm:aurelia-binding@1.0.1",
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-loader": "npm:aurelia-loader@1.0.0",
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0",
"aurelia-path": "npm:aurelia-path@1.0.0",
"aurelia-task-queue": "npm:aurelia-task-queue@1.0.0"
}
}
});
@@ -37,12 +37,14 @@
"dependencies": {
"aurelia-dependency-injection": "^1.0.0-rc.1.0.0",
"aurelia-fetch-client": "^1.0.0-rc.1.0.0",
"aurelia-framework": "^1.0.0-rc.1.0.0",
"aurelia-path": "^1.0.0-rc.1.0.0",
"extend": "^3.0.0"
},
"peerDependencies": {
"aurelia-dependency-injection": "^1.0.0-rc.1.0.0",
"aurelia-fetch-client": "^1.0.0-rc.1.0.0",
"aurelia-framework": "^1.0.0-rc.1.0.0",
"aurelia-path": "^1.0.0-rc.1.0.0",
"extend": "^3.0.0"
},
@@ -54,6 +56,7 @@
"dependencies": {
"aurelia-dependency-injection": "^1.0.0-rc.1.0.0",
"aurelia-fetch-client": "^1.0.0-rc.1.0.0",
"aurelia-framework": "^1.0.0-rc.1.0.0",
"aurelia-path": "^1.0.0-rc.1.0.0",
"extend": "^3.0.0"
},
@@ -1,6 +1,7 @@
import {Aurelia} from 'aurelia-framework';
import {Config} from './config';

export function configure(aurelia, configCallback) {
export function configure(aurelia: Aurelia, configCallback: Function): void {
let config = aurelia.container.get(Config);

configCallback(config);
@@ -7,32 +7,34 @@ import {Rest} from './rest';
export class Config {
/**
* Collection of configures endpionts
* @param {Object} Key: endpoint name, value: Rest client
* @param {{}} Key: endpoint name, value: Rest client
*/
endpoints = {};
endpoints: {} = {};

/**
* Current default endpoint if set
* @param {[Rest]} Default Rest client
*/
defaultEndpoint = null;
defaultEndpoint: Rest = null;

/**
* Register a new endpoint.
*
* @param {string} name The name of the new endpoint.
* @param {function|string} [configureMethod] Configure method or endpoint.
* @param {function|string} [configureMethod] Endpoint url or configure method for client.configure().
* @param {{}} [defaults] New defaults for the HttpClient
*
* @see http://aurelia.io/docs.html#/aurelia/fetch-client/latest/doc/api/class/HttpClientConfiguration
* @return {Config}
*/
registerEndpoint(name, configureMethod, defaults) {
registerEndpoint(name: string, configureMethod?: string|Function, defaults?: {}): Config {
let newClient = new HttpClient();
this.endpoints[name] = new Rest(newClient, name);

// set custom defaults to Rest
if (defaults !== undefined) this.endpoints[name].defaults = defaults;
if (defaults !== undefined) {
this.endpoints[name].defaults = defaults;
}

// Manual configure of client.
if (typeof configureMethod === 'function') {
@@ -41,7 +43,7 @@ export class Config {
return this;
}

// Base url is self.
// Base url is self / current host.
if (typeof configureMethod !== 'string') {
return this;
}
@@ -61,7 +63,7 @@ export class Config {
*
* @return {Rest|null}
*/
getEndpoint(name) {
getEndpoint(name: string): Rest {
if (!name) {
return this.defaultEndpoint || null;
}
@@ -76,7 +78,7 @@ export class Config {
*
* @return {boolean}
*/
endpointExists(name) {
endpointExists(name: string): boolean {
return !!this.endpoints[name];
}

@@ -87,7 +89,7 @@ export class Config {
*
* @return {Config}
*/
setDefaultEndpoint(name) {
setDefaultEndpoint(name: string): Config {
this.defaultEndpoint = this.getEndpoint(name);

return this;
@@ -1,5 +1,6 @@
import {resolver} from 'aurelia-dependency-injection';
import {Container, resolver} from 'aurelia-dependency-injection';
import {Config} from './config';
import {Rest} from './rest';

/**
* Endpoint class. A resolver for endpoints which allows injection of the corresponding Rest client into a class
@@ -12,7 +13,7 @@ export class Endpoint {
*
* @param {string} key
*/
constructor(key) {
constructor(key: string) {
this._key = key;
}

@@ -23,7 +24,7 @@ export class Endpoint {
*
* @return {Rest}
*/
get(container) {
get(container: Container): Rest {
return container.get(Config).getEndpoint(this._key);
}

@@ -34,7 +35,7 @@ export class Endpoint {
*
* @return {Endpoint} Resolves to the Rest client for this endpoint
*/
static of(key) {
static of(key: string): Endpoint {
return new Endpoint(key);
}
}
@@ -1,12 +1,18 @@
import {buildQueryString} from 'aurelia-path';
import {HttpClient} from 'aurelia-fetch-client';
import extend from 'extend';

/**
* Rest class. A simple rest client to fetch resources
*/
export class Rest {

defaults = {
/**
* The defaults to apply to anc request
*
* @param {{}} defaults The fetch client options
*/
defaults: {} = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
@@ -19,7 +25,7 @@ export class Rest {
* @param {HttpClient} httpClient The httpClient to use
* @param {string} [endpoint] The endpoint name
*/
constructor(httpClient, endpoint) {
constructor(httpClient: HttpClient, endpoint: string) {
this.client = httpClient;
this.endpoint = endpoint;
}
@@ -32,10 +38,10 @@ export class Rest {
* @param {{}} [body] The body to send if applicable
* @param {{}} [options] Fetch options overwrites
*
* @return {Promise<Object>|Promise<Error>} Server response as Object
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
request(method, path, body, options = {}) {
let requestOptions = extend(true, {headers: {}}, this.defaults, options, {method, body});
request(method: string, path: string, body?: {}, options?: {}): Promise<any|Error> {
let requestOptions = extend(true, {headers: {}}, this.defaults, options || {}, {method, body});

let contentType = requestOptions.headers['Content-Type'] || requestOptions.headers['content-type'];

@@ -61,9 +67,9 @@ export class Rest {
* @param {{}|string|Number} criteria Object for where clause, string / number for id.
* @param {{}} [options] Extra fetch options.
*
* @return {Promise<Object>|Promise<Error>} Server response as Object
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
find(resource, criteria, options) {
find(resource: string, criteria?: {}, options?: {}): Promise<any|Error> {
return this.request('GET', getRequestPath(resource, criteria), undefined, options);
}

@@ -74,9 +80,9 @@ export class Rest {
* @param {{}} body The data to post (as Object)
* @param {{}} [options] Extra fetch options.
*
* @return {Promise<Object>|Promise<Error>} Server response as Object
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
post(resource, body, options) {
post(resource: string, body?: {}, options?: {}): Promise<any|Error> {
return this.request('POST', resource, body, options);
}

@@ -88,9 +94,9 @@ export class Rest {
* @param {object} body New data for provided criteria.
* @param {{}} [options] Extra fetch options.
*
* @return {Promise<Object>|Promise<Error>} Server response as Object
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
update(resource, criteria, body, options) {
update(resource: string, criteria?: {}|string|Number, body?: {}, options?: {}): Promise<any|Error> {
return this.request('PUT', getRequestPath(resource, criteria), body, options);
}

@@ -102,9 +108,9 @@ export class Rest {
* @param {object} body Data to patch for provided criteria.
* @param {{}} [options] Extra fetch options.
*
* @return {Promise<Object>|Promise<Error>} Server response as Object
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
patch(resource, criteria, body, options) {
patch(resource: string, criteria?: {}|string|Number, body?: {}, options?: {}): Promise<any|Error> {
return this.request('PATCH', getRequestPath(resource, criteria), body, options);
}

@@ -115,9 +121,9 @@ export class Rest {
* @param {{}|string|Number} criteria Object for where clause, string / number for id.
* @param {{}} [options] Extra fetch options.
*
* @return {Promise<Object>|Promise<Error>} Server response as Object
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
destroy(resource, criteria, options) {
destroy(resource: string, criteria?: {}|string|Number, options?: {}): Promise<any|Error> {
return this.request('DELETE', getRequestPath(resource, criteria), undefined, options);
}

@@ -128,9 +134,9 @@ export class Rest {
* @param {{}} body The data to post (as Object)
* @param {{}} [options] Extra fetch options.
*
* @return {Promise<Object>|Promise<Error>} Server response as Object
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
create(resource, body, options) {
create(resource: string, body?: {}, options?: {}): Promise<any|Error> {
return this.post(...arguments);
}
}

0 comments on commit 320b798

Please sign in to comment.