Merge pull request #1442 from edwinveldhuizen/feature/extendable-custom-request

[request] export used functions for reusability
This commit is contained in:
Ferdi Koomen 2023-04-11 08:37:09 +02:00 committed by GitHub
commit 30bc235ac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 32 additions and 32 deletions

View File

@ -1,4 +1,4 @@
const getHeaders = (config: OpenAPIConfig, options: ApiRequestOptions): Observable<HttpHeaders> => {
export const getHeaders = (config: OpenAPIConfig, options: ApiRequestOptions): Observable<HttpHeaders> => {
return forkJoin({
token: resolve(options, config.TOKEN),
username: resolve(options, config.USERNAME),

View File

@ -1,4 +1,4 @@
const getRequestBody = (options: ApiRequestOptions): any => {
export const getRequestBody = (options: ApiRequestOptions): any => {
if (options.body) {
if (options.mediaType?.includes('/json')) {
return JSON.stringify(options.body)

View File

@ -1,4 +1,4 @@
const getResponseBody = <T>(response: HttpResponse<T>): T | undefined => {
export const getResponseBody = <T>(response: HttpResponse<T>): T | undefined => {
if (response.status !== 204 && response.body !== null) {
return response.body;
}

View File

@ -1,4 +1,4 @@
const getResponseHeader = <T>(response: HttpResponse<T>, responseHeader?: string): string | undefined => {
export const getResponseHeader = <T>(response: HttpResponse<T>, responseHeader?: string): string | undefined => {
if (responseHeader) {
const value = response.headers.get(responseHeader);
if (isString(value)) {

View File

@ -1,4 +1,4 @@
const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions, formData?: FormData): Promise<Record<string, string>> => {
export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions, formData?: FormData): Promise<Record<string, string>> => {
const token = await resolve(options, config.TOKEN);
const username = await resolve(options, config.USERNAME);
const password = await resolve(options, config.PASSWORD);

View File

@ -1,4 +1,4 @@
const getRequestBody = (options: ApiRequestOptions): any => {
export const getRequestBody = (options: ApiRequestOptions): any => {
if (options.body) {
return options.body;
}

View File

@ -1,4 +1,4 @@
const getResponseBody = (response: AxiosResponse<any>): any => {
export const getResponseBody = (response: AxiosResponse<any>): any => {
if (response.status !== 204) {
return response.data;
}

View File

@ -1,4 +1,4 @@
const getResponseHeader = (response: AxiosResponse<any>, responseHeader?: string): string | undefined => {
export const getResponseHeader = (response: AxiosResponse<any>, responseHeader?: string): string | undefined => {
if (responseHeader) {
const content = response.headers[responseHeader];
if (isString(content)) {

View File

@ -1,4 +1,4 @@
const sendRequest = async <T>(
export const sendRequest = async <T>(
config: OpenAPIConfig,
options: ApiRequestOptions,
url: string,

View File

@ -1,4 +1,4 @@
const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => {
export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => {
const token = await resolve(options, config.TOKEN);
const username = await resolve(options, config.USERNAME);
const password = await resolve(options, config.PASSWORD);

View File

@ -1,4 +1,4 @@
const getRequestBody = (options: ApiRequestOptions): any => {
export const getRequestBody = (options: ApiRequestOptions): any => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
return JSON.stringify(options.body)

View File

@ -1,4 +1,4 @@
const getResponseBody = async (response: Response): Promise<any> => {
export const getResponseBody = async (response: Response): Promise<any> => {
if (response.status !== 204) {
try {
const contentType = response.headers.get('Content-Type');

View File

@ -1,4 +1,4 @@
const getResponseHeader = (response: Response, responseHeader?: string): string | undefined => {
export const getResponseHeader = (response: Response, responseHeader?: string): string | undefined => {
if (responseHeader) {
const content = response.headers.get(responseHeader);
if (isString(content)) {

View File

@ -1,4 +1,4 @@
const base64 = (str: string): string => {
export const base64 = (str: string): string => {
try {
return btoa(str);
} catch (err) {

View File

@ -1,4 +1,4 @@
const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => {
export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => {
const errors: Record<number, string> = {
400: 'Bad Request',
401: 'Unauthorized',

View File

@ -1,4 +1,4 @@
const getFormData = (options: ApiRequestOptions): FormData | undefined => {
export const getFormData = (options: ApiRequestOptions): FormData | undefined => {
if (options.formData) {
const formData = new FormData();

View File

@ -1,4 +1,4 @@
const getQueryString = (params: Record<string, any>): string => {
export const getQueryString = (params: Record<string, any>): string => {
const qs: string[] = [];
const append = (key: string, value: any) => {

View File

@ -1,4 +1,4 @@
const isBlob = (value: any): value is Blob => {
export const isBlob = (value: any): value is Blob => {
return (
typeof value === 'object' &&
typeof value.type === 'string' &&

View File

@ -1,3 +1,3 @@
const isDefined = <T>(value: T | null | undefined): value is Exclude<T, null | undefined> => {
export const isDefined = <T>(value: T | null | undefined): value is Exclude<T, null | undefined> => {
return value !== undefined && value !== null;
};

View File

@ -1,3 +1,3 @@
const isFormData = (value: any): value is FormData => {
export const isFormData = (value: any): value is FormData => {
return value instanceof FormData;
};

View File

@ -1,3 +1,3 @@
const isString = (value: any): value is string => {
export const isString = (value: any): value is string => {
return typeof value === 'string';
};

View File

@ -1,3 +1,3 @@
const isStringWithValue = (value: any): value is string => {
export const isStringWithValue = (value: any): value is string => {
return isString(value) && value !== '';
};

View File

@ -1,3 +1,3 @@
const isSuccess = (status: number): boolean => {
export const isSuccess = (status: number): boolean => {
return status >= 200 && status < 300;
};

View File

@ -1,6 +1,6 @@
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
const resolve = async <T>(options: ApiRequestOptions, resolver?: T | Resolver<T>): Promise<T | undefined> => {
export const resolve = async <T>(options: ApiRequestOptions, resolver?: T | Resolver<T>): Promise<T | undefined> => {
if (typeof resolver === 'function') {
return (resolver as Resolver<T>)(options);
}

View File

@ -1,4 +1,4 @@
const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => {
export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => {
const token = await resolve(options, config.TOKEN);
const username = await resolve(options, config.USERNAME);
const password = await resolve(options, config.PASSWORD);

View File

@ -1,4 +1,4 @@
const getRequestBody = (options: ApiRequestOptions): any => {
export const getRequestBody = (options: ApiRequestOptions): any => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
return JSON.stringify(options.body)

View File

@ -1,4 +1,4 @@
const getResponseBody = async (response: Response): Promise<any> => {
export const getResponseBody = async (response: Response): Promise<any> => {
if (response.status !== 204) {
try {
const contentType = response.headers.get('Content-Type');

View File

@ -1,4 +1,4 @@
const getResponseHeader = (response: Response, responseHeader?: string): string | undefined => {
export const getResponseHeader = (response: Response, responseHeader?: string): string | undefined => {
if (responseHeader) {
const content = response.headers.get(responseHeader);
if (isString(content)) {

View File

@ -1,4 +1,4 @@
const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => {
export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => {
const token = await resolve(options, config.TOKEN);
const username = await resolve(options, config.USERNAME);
const password = await resolve(options, config.PASSWORD);

View File

@ -1,4 +1,4 @@
const getRequestBody = (options: ApiRequestOptions): any => {
export const getRequestBody = (options: ApiRequestOptions): any => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
return JSON.stringify(options.body)

View File

@ -1,4 +1,4 @@
const getResponseBody = (xhr: XMLHttpRequest): any => {
export const getResponseBody = (xhr: XMLHttpRequest): any => {
if (xhr.status !== 204) {
try {
const contentType = xhr.getResponseHeader('Content-Type');

View File

@ -1,4 +1,4 @@
const getResponseHeader = (xhr: XMLHttpRequest, responseHeader?: string): string | undefined => {
export const getResponseHeader = (xhr: XMLHttpRequest, responseHeader?: string): string | undefined => {
if (responseHeader) {
const content = xhr.getResponseHeader(responseHeader);
if (isString(content)) {