Initial commit for the js version
This commit is contained in:
8
dist/core/ApiError.d.ts
vendored
Normal file
8
dist/core/ApiError.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
import type { ApiResult } from './ApiResult';
|
||||
export declare class ApiError extends Error {
|
||||
readonly url: string;
|
||||
readonly status: number;
|
||||
readonly statusText: string;
|
||||
readonly body: any;
|
||||
constructor(response: ApiResult, message: string);
|
||||
}
|
||||
13
dist/core/ApiError.js
vendored
Normal file
13
dist/core/ApiError.js
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ApiError = void 0;
|
||||
class ApiError extends Error {
|
||||
constructor(response, message) {
|
||||
super(message);
|
||||
this.url = response.url;
|
||||
this.status = response.status;
|
||||
this.statusText = response.statusText;
|
||||
this.body = response.body;
|
||||
}
|
||||
}
|
||||
exports.ApiError = ApiError;
|
||||
11
dist/core/ApiRequestOptions.d.ts
vendored
Normal file
11
dist/core/ApiRequestOptions.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
export declare type ApiRequestOptions = {
|
||||
readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
|
||||
readonly path: string;
|
||||
readonly cookies?: Record<string, any>;
|
||||
readonly headers?: Record<string, any>;
|
||||
readonly query?: Record<string, any>;
|
||||
readonly formData?: Record<string, any>;
|
||||
readonly body?: any;
|
||||
readonly responseHeader?: string;
|
||||
readonly errors?: Record<number, string>;
|
||||
};
|
||||
2
dist/core/ApiRequestOptions.js
vendored
Normal file
2
dist/core/ApiRequestOptions.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
7
dist/core/ApiResult.d.ts
vendored
Normal file
7
dist/core/ApiResult.d.ts
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
export declare type ApiResult = {
|
||||
readonly url: string;
|
||||
readonly ok: boolean;
|
||||
readonly status: number;
|
||||
readonly statusText: string;
|
||||
readonly body: any;
|
||||
};
|
||||
2
dist/core/ApiResult.js
vendored
Normal file
2
dist/core/ApiResult.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
13
dist/core/OpenAPI.d.ts
vendored
Normal file
13
dist/core/OpenAPI.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
declare type Resolver<T> = () => Promise<T>;
|
||||
declare type Headers = Record<string, string>;
|
||||
declare type Config = {
|
||||
BASE: string;
|
||||
VERSION: string;
|
||||
WITH_CREDENTIALS: boolean;
|
||||
TOKEN?: string | Resolver<string>;
|
||||
USERNAME?: string | Resolver<string>;
|
||||
PASSWORD?: string | Resolver<string>;
|
||||
HEADERS?: Headers | Resolver<Headers>;
|
||||
};
|
||||
export declare const OpenAPI: Config;
|
||||
export {};
|
||||
12
dist/core/OpenAPI.js
vendored
Normal file
12
dist/core/OpenAPI.js
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.OpenAPI = void 0;
|
||||
exports.OpenAPI = {
|
||||
BASE: '',
|
||||
VERSION: '1.0.0',
|
||||
WITH_CREDENTIALS: false,
|
||||
TOKEN: undefined,
|
||||
USERNAME: undefined,
|
||||
PASSWORD: undefined,
|
||||
HEADERS: undefined,
|
||||
};
|
||||
9
dist/core/request.d.ts
vendored
Normal file
9
dist/core/request.d.ts
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { ApiRequestOptions } from './ApiRequestOptions';
|
||||
import type { ApiResult } from './ApiResult';
|
||||
/**
|
||||
* Request using fetch client
|
||||
* @param options The request options from the the service
|
||||
* @result ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
export declare function request(options: ApiRequestOptions): Promise<ApiResult>;
|
||||
183
dist/core/request.js
vendored
Normal file
183
dist/core/request.js
vendored
Normal file
@@ -0,0 +1,183 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.request = void 0;
|
||||
/* istanbul ignore file */
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
const ApiError_1 = require("./ApiError");
|
||||
const OpenAPI_1 = require("./OpenAPI");
|
||||
function isDefined(value) {
|
||||
return value !== undefined && value !== null;
|
||||
}
|
||||
function isString(value) {
|
||||
return typeof value === 'string';
|
||||
}
|
||||
function isStringWithValue(value) {
|
||||
return isString(value) && value !== '';
|
||||
}
|
||||
function isBlob(value) {
|
||||
return value instanceof Blob;
|
||||
}
|
||||
function getQueryString(params) {
|
||||
const qs = [];
|
||||
Object.keys(params).forEach(key => {
|
||||
const value = params[key];
|
||||
if (isDefined(value)) {
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach(value => {
|
||||
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
||||
});
|
||||
}
|
||||
else {
|
||||
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (qs.length > 0) {
|
||||
return `?${qs.join('&')}`;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
function getUrl(options) {
|
||||
const path = options.path.replace(/[:]/g, '_');
|
||||
const url = `${OpenAPI_1.OpenAPI.BASE}${path}`;
|
||||
if (options.query) {
|
||||
return `${url}${getQueryString(options.query)}`;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
function getFormData(params) {
|
||||
const formData = new FormData();
|
||||
Object.keys(params).forEach(key => {
|
||||
const value = params[key];
|
||||
if (isDefined(value)) {
|
||||
formData.append(key, value);
|
||||
}
|
||||
});
|
||||
return formData;
|
||||
}
|
||||
async function resolve(resolver) {
|
||||
if (typeof resolver === 'function') {
|
||||
return resolver();
|
||||
}
|
||||
return resolver;
|
||||
}
|
||||
async function getHeaders(options) {
|
||||
const headers = new Headers({
|
||||
Accept: 'application/json',
|
||||
...OpenAPI_1.OpenAPI.HEADERS,
|
||||
...options.headers,
|
||||
});
|
||||
const token = await resolve(OpenAPI_1.OpenAPI.TOKEN);
|
||||
const username = await resolve(OpenAPI_1.OpenAPI.USERNAME);
|
||||
const password = await resolve(OpenAPI_1.OpenAPI.PASSWORD);
|
||||
if (isStringWithValue(token)) {
|
||||
headers.append('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
if (isStringWithValue(username) && isStringWithValue(password)) {
|
||||
const credentials = btoa(`${username}:${password}`);
|
||||
headers.append('Authorization', `Basic ${credentials}`);
|
||||
}
|
||||
if (options.body) {
|
||||
if (isBlob(options.body)) {
|
||||
headers.append('Content-Type', options.body.type || 'application/octet-stream');
|
||||
}
|
||||
else if (isString(options.body)) {
|
||||
headers.append('Content-Type', 'text/plain');
|
||||
}
|
||||
else {
|
||||
headers.append('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
function getRequestBody(options) {
|
||||
if (options.formData) {
|
||||
return getFormData(options.formData);
|
||||
}
|
||||
if (options.body) {
|
||||
if (isString(options.body) || isBlob(options.body)) {
|
||||
return options.body;
|
||||
}
|
||||
else {
|
||||
return JSON.stringify(options.body);
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
async function sendRequest(options, url) {
|
||||
const request = {
|
||||
method: options.method,
|
||||
headers: await getHeaders(options),
|
||||
body: getRequestBody(options),
|
||||
};
|
||||
return await fetch(url, request);
|
||||
}
|
||||
function getResponseHeader(response, responseHeader) {
|
||||
if (responseHeader) {
|
||||
const content = response.headers.get(responseHeader);
|
||||
if (isString(content)) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
async function getResponseBody(response) {
|
||||
try {
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
if (contentType) {
|
||||
const isJSON = contentType.toLowerCase().startsWith('application/json');
|
||||
if (isJSON) {
|
||||
return await response.json();
|
||||
}
|
||||
else {
|
||||
return await response.text();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function catchErrors(options, result) {
|
||||
const errors = {
|
||||
400: 'Bad Request',
|
||||
401: 'Unauthorized',
|
||||
403: 'Forbidden',
|
||||
404: 'Not Found',
|
||||
500: 'Internal Server Error',
|
||||
502: 'Bad Gateway',
|
||||
503: 'Service Unavailable',
|
||||
...options.errors,
|
||||
};
|
||||
const error = errors[result.status];
|
||||
if (error) {
|
||||
throw new ApiError_1.ApiError(result, error);
|
||||
}
|
||||
if (!result.ok) {
|
||||
throw new ApiError_1.ApiError(result, 'Generic Error');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Request using fetch client
|
||||
* @param options The request options from the the service
|
||||
* @result ApiResult
|
||||
* @throws ApiError
|
||||
*/
|
||||
async function request(options) {
|
||||
const url = getUrl(options);
|
||||
const response = await sendRequest(options, url);
|
||||
const responseBody = await getResponseBody(response);
|
||||
const responseHeader = getResponseHeader(response, options.responseHeader);
|
||||
const result = {
|
||||
url,
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
body: responseHeader || responseBody,
|
||||
};
|
||||
catchErrors(options, result);
|
||||
return result;
|
||||
}
|
||||
exports.request = request;
|
||||
Reference in New Issue
Block a user