Skip to content

Commit

Permalink
Upgrade to 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
WinUP committed May 14, 2018
1 parent f00b34c commit f03a693
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 86 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ Resource provider for Angular HttpClient.
| ```server.contentType``` | ```'application/json'``` | Default content type |
| ```assets.address``` | ```''``` | Default address from ```assets``` protocol |

```typescript
SerializableNode.set(StorageProtocol.config, StorageProtocol.configKeys.root.cache, 'ROOT');
SerializableNode.set(StorageProtocol.config, StorageProtocol.configKeys.root.local, 'DLCS');
```

### Supported protocol

* ```remote``` for access default server defined in Configuration.
Expand Down Expand Up @@ -65,10 +60,10 @@ resourceManager.registerProtocol(new AngularHttpProtocol(this.httpClient)); // A
// Request data
resourceManager.request.to(`http://www.google.com`).tag('google').send();
// Request using default server
SerializableNode.set(AngularHttpProtocol.config, AngularHttpProtocol.configKeys.server.address, 'http://www.a.org');
http.config.server.address = 'http://www.a.org';
resourceManager.request.to(`remote:///test.txt`).tag('test').send();
// Request static file
SerializableNode.set(AngularHttpProtocol.config, AngularHttpProtocol.configKeys.assets.address, '/assets');
http.config.assets.address = '/assets';
resourceManager.request.to(`assets:///test.txt`).tag('test_local').send();
// POST data
resourceManager.request.to(`http://www.test.org`).param({ method: XHRMethod.POST }).tag('google').send();
Expand Down
51 changes: 26 additions & 25 deletions package-lock.json

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

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@dlcs/provider-angular-http",
"version": "1.1.1",
"description": "Angular 5+ HttpClient provider for Deus Legem Creation System",
"version": "1.2.0",
"description": "Angular 6+ HttpClient provider for Deus Legem Creation System",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"private": false,
Expand All @@ -24,11 +24,11 @@
"typescript": "2.8.3"
},
"dependencies": {
"@angular/common": ">=5.0.0",
"@angular/core": ">=5.0.0",
"@dlcs/core": "~1.1.0",
"@dlcs/tools": "~1.0.8",
"rxjs": "^5.5.10"
"@angular/common": ">=6.0.0",
"@angular/core": ">=6.0.0",
"@dlcs/core": "~1.2.0",
"@dlcs/tools": "~1.2.1",
"rxjs": ">=6.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion src/AngularHttpRequestParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface AngularHttpRequestParams {
/**
* Query parameters.
*/
querys?: { [key: string]: string };
queries?: { [key: string]: string };
/**
* Request body. Only affects POST/PUT/PATCH.
*/
Expand Down
70 changes: 24 additions & 46 deletions src/angular-http.protocol.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { HttpClient, HttpHeaders, HttpParams, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { SerializableNode, autoname, toPascalCase, isValueAvailable } from '@dlcs/tools';
import { HttpClient, HttpHeaders, HttpParams, HttpErrorResponse } from '@angular/common/http';
import { ResourceProtocol, ResourceRequest, InjectorTimepoint } from '@dlcs/core';
import { TimeoutError } from 'rxjs/util/TimeoutError';
import { catchError } from 'rxjs/operators/catchError';
import { Observable } from 'rxjs/Observable';
import { timeout } from 'rxjs/operators/timeout';
import { _throw } from 'rxjs/observable/throw';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { catchError, timeout } from 'rxjs/operators';
import { isValueAvailable } from '@dlcs/tools';

import { AngularHttpRequestParams } from './AngularHttpRequestParams';
import { AngularHttpRequestData } from './AngularHttpRequestData';
Expand All @@ -15,7 +12,7 @@ import { XHRMethod } from './XHRMethod';
/**
* Configuration keys for StorageProtocol
*/
export interface AngularHttpProtocolConfigKeys {
export interface AngularHttpProtocolConfig {
/**
* Default server configuration for protocol ```remote```.
*/
Expand All @@ -29,7 +26,7 @@ export interface AngularHttpProtocolConfigKeys {
* Default resposne type, only allows ```ResponseType``` value
* @default ResponseType.JSON
*/
responseType: string,
responseType: ResponseType,
/**
* Default content type
* @default 'application/json'
Expand All @@ -49,23 +46,14 @@ export interface AngularHttpProtocolConfigKeys {
}

export class AngularHttpProtocol extends ResourceProtocol {
private static _config: SerializableNode = SerializableNode.create('AngularHttpProtocol', undefined);
private static _configKeys: AngularHttpProtocolConfigKeys = {
server: { address: '', responseType: '', contentType: '' },
private _config: AngularHttpProtocolConfig = {
server: { address: '', responseType: ResponseType.JSON, contentType: '' },
assets: { address: '' }
};

public static initialize(): void {
autoname(AngularHttpProtocol._configKeys, '/', toPascalCase);
SerializableNode.set(AngularHttpProtocol.config, AngularHttpProtocol.configKeys.server.address, '');
SerializableNode.set(AngularHttpProtocol.config, AngularHttpProtocol.configKeys.server.contentType, 'application/json');
SerializableNode.set(AngularHttpProtocol.config, AngularHttpProtocol.configKeys.server.responseType, ResponseType.JSON);
SerializableNode.set(AngularHttpProtocol.config, AngularHttpProtocol.configKeys.assets.address, '');
}

/**
* Create new AngularHttpProtocol
* @param httpClient Angular 5+ HttpClient service
* @param httpClient Angular 6+ HttpClient service
*/
public constructor(private httpClient: HttpClient) {
super('remote', 'assets', 'http', 'https');
Expand All @@ -74,23 +62,16 @@ export class AngularHttpProtocol extends ResourceProtocol {
/**
* Get configuration
*/
public static get config(): SerializableNode {
return AngularHttpProtocol._config;
public get config(): AngularHttpProtocolConfig {
return this._config;
}

/**
* Get configuration keys
*/
public static get configKeys(): Readonly<AngularHttpProtocolConfigKeys> {
return AngularHttpProtocol._configKeys;
}

private static packRequest(raw: ResourceRequest): AngularHttpRequestData {
private packRequest(raw: ResourceRequest): AngularHttpRequestData {
const request: { params: HttpParams, responseType: ResponseType, headers: HttpHeaders, observe: 'response' | 'body' } = {
params: new HttpParams(),
headers: new HttpHeaders(),
observe: 'body',
responseType: SerializableNode.get<ResponseType>(AngularHttpProtocol.config, AngularHttpProtocol.configKeys.server.responseType)
responseType: this.config.server.responseType
};
if (!isValueAvailable(request.responseType)) {
request.responseType = ResponseType.JSON;
Expand All @@ -104,8 +85,7 @@ export class AngularHttpProtocol extends ResourceProtocol {
if (rawParams.contentType) { // 请求格式
request.headers = request.headers.set('Content-Type', rawParams.contentType);
} else {
const defaultContentType =
SerializableNode.get<string>(AngularHttpProtocol.config, AngularHttpProtocol.configKeys.server.contentType);
const defaultContentType = this.config.server.contentType;
if (defaultContentType) {
request.headers = request.headers.append('Content-Type', defaultContentType);
}
Expand All @@ -117,9 +97,9 @@ export class AngularHttpProtocol extends ResourceProtocol {
request.responseType = rawParams.responseType;
}
}
if (rawParams.querys) { // 查询参数
Object.keys(rawParams.querys).forEach(key => {
const value = rawParams.querys![key];
if (rawParams.queries) { // 查询参数
Object.keys(rawParams.queries).forEach(key => {
const value = rawParams.queries![key];
if (isValueAvailable(value)) {
request.params = request.params.set(key, value);
}
Expand All @@ -131,23 +111,23 @@ export class AngularHttpProtocol extends ResourceProtocol {
body: rawParams.body,
timeout: rawParams.timeout || 0,
method: method,
url: AngularHttpProtocol.assembleUrl(raw, method)
url: this.assembleUrl(raw, method)
};
}

private static assembleUrl(request: ResourceRequest, method: XHRMethod): string {
private assembleUrl(request: ResourceRequest, method: XHRMethod): string {
let url = request.protocol;
if (request.protocol === 'assets') {
if (method !== XHRMethod.GET) {
throw new TypeError(`Cannot request asset file ${request.address}: Only GET method is allowed for this protocol`);
}
const base = SerializableNode.get<string>(AngularHttpProtocol.config, AngularHttpProtocol.configKeys.assets.address);
const base = this.config.assets.address;
if (!isValueAvailable(base)) {
throw new ReferenceError(`Cannot request asset file ${request.address}: No assets directory provided`);
}
url = `${base}${request.address}`;
} else if (request.protocol === 'remote') {
const base = SerializableNode.get<string>(AngularHttpProtocol.config, AngularHttpProtocol.configKeys.server.address);
const base = this.config.server.address;
if (!isValueAvailable(base)) {
throw new ReferenceError(`Cannot request asset file ${request.address}: No default server provided`);
}
Expand Down Expand Up @@ -188,23 +168,21 @@ export class AngularHttpProtocol extends ResourceProtocol {
}

public request(request: ResourceRequest, injector: (data: any, timepoint: InjectorTimepoint) => any): Observable<any> {
let data = AngularHttpProtocol.packRequest(request);
let data = this.packRequest(request);
injector && (data = injector(data, InjectorTimepoint.BeforeSend));
let result: Observable<any> = this.makeRequest(data);
result = result.pipe(catchError(e => {
return e instanceof TimeoutError
? _throw(new HttpErrorResponse({
? throwError(new HttpErrorResponse({
error: e.message,
status: 408,
headers: data.info.headers,
statusText: e.name,
url: request.address
}))
: _throw(e);
: throwError(e);
}));
injector && (result = injector(result, InjectorTimepoint.AfterSent));
return result;
}
}

AngularHttpProtocol.initialize();

0 comments on commit f03a693

Please sign in to comment.