@ffflorian/api-client
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -6,2 +6,10 @@ # Change Log | ||
## [0.3.2](https://github.com/ffflorian/api-clients/tree/master/packages/api-client/compare/@ffflorian/api-client@0.3.1...@ffflorian/api-client@0.3.2) (2019-04-03) | ||
**Note:** Version bump only for package @ffflorian/api-client | ||
## [0.3.1](https://github.com/ffflorian/api-clients/tree/master/packages/api-client/compare/@ffflorian/api-client@0.3.0...@ffflorian/api-client@0.3.1) (2019-03-31) | ||
@@ -8,0 +16,0 @@ |
@@ -1,7 +0,7 @@ | ||
import { InjectorFn, RequestService } from './RequestService'; | ||
export interface ClientOptions { | ||
import { RequestInjectorFn, RequestService } from './RequestService'; | ||
export interface ClientOptions<T> { | ||
/** The API URL (e.g. "https://example.com/api/v1"). */ | ||
apiUrl: string; | ||
/** An optional injector which alters every Axios request configuration before the request is sent. */ | ||
requestInjector?: InjectorFn; | ||
requestInjector?: RequestInjectorFn<T>; | ||
} | ||
@@ -11,3 +11,3 @@ export declare class APIClient<T = any> { | ||
constructor(apiUrl: string); | ||
constructor(options: ClientOptions); | ||
constructor(options: ClientOptions<T>); | ||
/** | ||
@@ -14,0 +14,0 @@ * Set a new API URL. |
export * from './APIClient'; | ||
export * from './APIException'; | ||
export * from './RequestService'; | ||
export { AxiosRequestConfig } from 'axios'; |
@@ -1,14 +0,20 @@ | ||
import { AxiosRequestConfig } from 'axios'; | ||
import { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import { ClientOptions } from './APIClient'; | ||
export declare type InjectorFn = (baseConfig: AxiosRequestConfig) => AxiosRequestConfig; | ||
export declare type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; | ||
export declare type AxiosResponseWithoutData<T> = Omit<AxiosResponse<unknown>, 'data'> & T; | ||
export interface AxiosConfigWithData<T> extends AxiosRequestConfig { | ||
data?: T; | ||
} | ||
export declare type RequestInjectorFn<T> = (baseConfig: AxiosConfigWithData<T>) => AxiosConfigWithData<T> | Promise<AxiosConfigWithData<T>>; | ||
export declare type ResponseInjectorFn<T> = (response: AxiosResponseWithoutData<T>) => AxiosResponseWithoutData<T> | Promise<AxiosResponseWithoutData<T>>; | ||
export declare class RequestService<T> { | ||
private readonly config; | ||
constructor(config: ClientOptions); | ||
delete<U>(url: string, optionsOrInjector?: AxiosRequestConfig | InjectorFn): Promise<U>; | ||
get<U>(url: string, optionsOrInjector?: AxiosRequestConfig): Promise<U>; | ||
head<U>(url: string, optionsOrInjector?: AxiosRequestConfig): Promise<U>; | ||
options<U>(url: string, optionsOrInjector?: AxiosRequestConfig): Promise<U>; | ||
patch<U>(url: string, data: T, optionsOrInjector?: AxiosRequestConfig): Promise<U>; | ||
post<U>(url: string, data: T, optionsOrInjector?: AxiosRequestConfig): Promise<U>; | ||
put<U>(url: string, data: T, optionsOrInjector?: AxiosRequestConfig): Promise<U>; | ||
constructor(config: ClientOptions<T>); | ||
delete<U>(url: string, options?: AxiosConfigWithData<T>): Promise<U>; | ||
get<U>(url: string, options?: AxiosConfigWithData<T>): Promise<U>; | ||
head<U>(url: string, options?: AxiosConfigWithData<T>): Promise<U>; | ||
options<U>(url: string, options?: AxiosConfigWithData<T>): Promise<U>; | ||
patch<U>(url: string, options?: AxiosConfigWithData<T>): Promise<U>; | ||
post<U>(url: string, options?: AxiosConfigWithData<T>): Promise<U>; | ||
put<U>(url: string, options?: AxiosConfigWithData<T>): Promise<U>; | ||
setApiUrl(apiUrl: string): void; | ||
@@ -15,0 +21,0 @@ private injectConfig; |
@@ -66,50 +66,113 @@ "use strict"; | ||
} | ||
RequestService.prototype.delete = function (url, optionsOrInjector) { | ||
var config = this.injectConfig({ | ||
method: HttpMethod.DELETE, | ||
url: this.config.apiUrl + url, | ||
}, optionsOrInjector); | ||
return this.request(config); | ||
RequestService.prototype.delete = function (url, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var config; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.injectConfig({ | ||
method: HttpMethod.DELETE, | ||
url: "" + this.config.apiUrl + url, | ||
}, options)]; | ||
case 1: | ||
config = _a.sent(); | ||
return [2 /*return*/, this.request(config)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestService.prototype.get = function (url, optionsOrInjector) { | ||
var config = this.injectConfig({ | ||
method: HttpMethod.GET, | ||
url: this.config.apiUrl + url, | ||
}, optionsOrInjector); | ||
return this.request(config); | ||
RequestService.prototype.get = function (url, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var config; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.injectConfig({ | ||
method: HttpMethod.GET, | ||
url: "" + this.config.apiUrl + url, | ||
}, options)]; | ||
case 1: | ||
config = _a.sent(); | ||
return [2 /*return*/, this.request(config)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestService.prototype.head = function (url, optionsOrInjector) { | ||
var config = this.injectConfig({ | ||
method: HttpMethod.HEAD, | ||
url: this.config.apiUrl + url, | ||
}, optionsOrInjector); | ||
return this.request(config); | ||
RequestService.prototype.head = function (url, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var config; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.injectConfig({ | ||
method: HttpMethod.HEAD, | ||
url: "" + this.config.apiUrl + url, | ||
}, options)]; | ||
case 1: | ||
config = _a.sent(); | ||
return [2 /*return*/, this.request(config)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestService.prototype.options = function (url, optionsOrInjector) { | ||
var config = this.injectConfig({ | ||
method: HttpMethod.OPTIONS, | ||
url: this.config.apiUrl + url, | ||
}, optionsOrInjector); | ||
return this.request(config); | ||
RequestService.prototype.options = function (url, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var config; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.injectConfig({ | ||
method: HttpMethod.OPTIONS, | ||
url: "" + this.config.apiUrl + url, | ||
}, options)]; | ||
case 1: | ||
config = _a.sent(); | ||
return [2 /*return*/, this.request(config)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestService.prototype.patch = function (url, data, optionsOrInjector) { | ||
var config = this.injectConfig({ | ||
method: HttpMethod.PATCH, | ||
url: this.config.apiUrl + url, | ||
}, optionsOrInjector); | ||
return this.request(config); | ||
RequestService.prototype.patch = function (url, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var config; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.injectConfig({ | ||
method: HttpMethod.PATCH, | ||
url: "" + this.config.apiUrl + url, | ||
}, options)]; | ||
case 1: | ||
config = _a.sent(); | ||
return [2 /*return*/, this.request(config)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestService.prototype.post = function (url, data, optionsOrInjector) { | ||
var config = this.injectConfig({ | ||
method: HttpMethod.POST, | ||
url: this.config.apiUrl + url, | ||
}, optionsOrInjector); | ||
return this.request(config); | ||
RequestService.prototype.post = function (url, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var config; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.injectConfig({ | ||
method: HttpMethod.POST, | ||
url: "" + this.config.apiUrl + url, | ||
}, options)]; | ||
case 1: | ||
config = _a.sent(); | ||
return [2 /*return*/, this.request(config)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RequestService.prototype.put = function (url, data, optionsOrInjector) { | ||
var config = this.injectConfig({ | ||
method: HttpMethod.PUT, | ||
url: this.config.apiUrl + url, | ||
}, optionsOrInjector); | ||
return this.request(config); | ||
RequestService.prototype.put = function (url, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var config; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.injectConfig({ | ||
method: HttpMethod.PUT, | ||
url: "" + this.config.apiUrl + url, | ||
}, options)]; | ||
case 1: | ||
config = _a.sent(); | ||
return [2 /*return*/, this.request(config)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
@@ -119,13 +182,20 @@ RequestService.prototype.setApiUrl = function (apiUrl) { | ||
}; | ||
RequestService.prototype.injectConfig = function (baseConfig, optionsOrInjector) { | ||
if (typeof optionsOrInjector === 'function') { | ||
return optionsOrInjector(baseConfig); | ||
} | ||
if (typeof this.config.requestInjector === 'function') { | ||
baseConfig = this.config.requestInjector(baseConfig); | ||
} | ||
if (typeof optionsOrInjector !== 'undefined') { | ||
return __assign({}, baseConfig, (!!optionsOrInjector && optionsOrInjector)); | ||
} | ||
return baseConfig; | ||
RequestService.prototype.injectConfig = function (baseConfig, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!(typeof this.config.requestInjector === 'function')) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.config.requestInjector(baseConfig)]; | ||
case 1: | ||
baseConfig = _a.sent(); | ||
_a.label = 2; | ||
case 2: | ||
if (typeof options !== 'undefined') { | ||
return [2 /*return*/, __assign({}, baseConfig, (!!options && options))]; | ||
} | ||
return [2 /*return*/, baseConfig]; | ||
} | ||
}); | ||
}); | ||
}; | ||
@@ -132,0 +202,0 @@ RequestService.prototype.request = function (config) { |
@@ -29,4 +29,4 @@ { | ||
}, | ||
"version": "0.3.1", | ||
"gitHead": "daf56610d1d438d0f4f9a0f5a213ccbf55d6ac03" | ||
"version": "0.3.2", | ||
"gitHead": "139620af5229fde0d29b811e07f120df41955d97" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29917
425