src/client.ts
constructor(conf: ShifterConfig)
|
Protected createURL |
createURL(path: string)
|
Create a Request URL
Parameters :
Returns:
void
|
Protected getAPIURL |
getAPIURL(path: string)
|
Get API Request URL
Parameters :
Returns:
string
|
Protected createRequestConfig |
createRequestConfig(config: AxiosRequestConfig)
|
Create axios request config object
Parameters :
Returns:
AxiosRequestConfig
|
Protected get |
get(path: string, config: AxiosRequestConfig)
|
Call GET API with token
Parameters :
Returns:
any
|
Protected post |
post(path: string, body: Request, config: AxiosRequestConfig)
|
Call POST API with token
Parameters :
Returns:
any
|
Protected put |
put(path: string, body: Request, config: AxiosRequestConfig)
|
Call put API with token
Parameters :
Returns:
any
|
Protected delete |
delete(path: string, config: AxiosRequestConfig)
|
Call DELETE API with token
Parameters :
Returns:
any
|
Protected client |
client: |
Default value: axios
|
Request Client |
Protected endpoint |
endpoint: |
Default value: DEFAULT_ENDPOINT
|
Shifter API Endpoint |
Protected resource |
resource: |
API namespace |
Protected token |
token: |
Shifter API token |
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
export const DEFAULT_ENDPOINT = 'https://api.hl.getshifter.net';
/**
* Shifter client config
*/
export type ShifterConfig = {
/**
* API Token
*/
token: string;
/**
* API endpoint
*/
endpoint?: string;
/**
* Target resource
*/
resource?: string;
};
export abstract class ShifterClient {
/**
* Request Client
*/
protected readonly client: AxiosInstance = axios;
/**
* Shifter API token
*/
protected readonly token: string;
/**
* Shifter API Endpoint
*/
protected readonly endpoint: string = DEFAULT_ENDPOINT;
/**
* API namespace
*/
protected readonly resource: string = '';
constructor(conf: ShifterConfig) {
this.token = conf.token;
if (conf.endpoint) this.endpoint = conf.endpoint;
if (conf.resource) this.resource = conf.resource;
}
/**
* Create a Request URL
* @param path
*/
protected createURL(path?: string) {
const apiPath = [this.resource, path]
.filter(item => !!item)
.join('/')
.replace(/\/\//, '/')
.replace(/\/$/, '')
.replace(/^\//, '');
return [this.endpoint, apiPath].join('/');
}
/**
* Get API Request URL
* @param path
*/
protected getAPIURL(path?: string): string {
return this.createURL(path);
}
/**
* Create axios request config object
* @param config
*/
protected createRequestConfig(
config?: AxiosRequestConfig
): AxiosRequestConfig {
const headers = config && config.headers ? config.headers : {};
headers.Authorization = this.token;
return Object.assign({}, config, { headers });
}
/**
* Call GET API with token
* @param path
* @param config
*/
protected async get<T = any>(
path?: string,
config?: AxiosRequestConfig
): Promise<T> {
const url = this.getAPIURL(path);
const conf = this.createRequestConfig(config);
const result = await this.client.get(url, conf);
return result.data;
}
/**
* Call POST API with token
* @param path
* @param body
* @param config
*/
protected async post<Request = any, Response = any>(
path?: string,
body?: Request,
config?: AxiosRequestConfig
): Promise<Response> {
const url = this.getAPIURL(path);
const conf = this.createRequestConfig(config);
const result = await this.client.post<Response>(url, body, conf);
return result.data;
}
/**
* Call put API with token
* @param path
* @param body
* @param config
*/
protected async put<Request = any, Response = any>(
path?: string,
body?: Request,
config?: AxiosRequestConfig
): Promise<Response> {
const url = this.getAPIURL(path);
const conf = this.createRequestConfig(config);
const result = await this.client.put<Response>(url, body, conf);
return result.data;
}
/**
* Call DELETE API with token
* @param path
* @param config
*/
protected async delete<Response = any>(
path?: string,
config?: AxiosRequestConfig
): Promise<Response> {
const url = this.getAPIURL(path);
const conf = this.createRequestConfig(config);
const result = await this.client.delete<Response>(url, conf);
return result.data;
}
}