mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
Merge pull request #1442 from edwinveldhuizen/feature/extendable-custom-request
[request] export used functions for reusability
This commit is contained in:
commit
30bc235ac7
@ -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),
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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)) {
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const getRequestBody = (options: ApiRequestOptions): any => {
|
||||
export const getRequestBody = (options: ApiRequestOptions): any => {
|
||||
if (options.body) {
|
||||
return options.body;
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const getResponseBody = (response: AxiosResponse<any>): any => {
|
||||
export const getResponseBody = (response: AxiosResponse<any>): any => {
|
||||
if (response.status !== 204) {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
@ -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)) {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const sendRequest = async <T>(
|
||||
export const sendRequest = async <T>(
|
||||
config: OpenAPIConfig,
|
||||
options: ApiRequestOptions,
|
||||
url: string,
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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');
|
||||
|
||||
@ -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)) {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const base64 = (str: string): string => {
|
||||
export const base64 = (str: string): string => {
|
||||
try {
|
||||
return btoa(str);
|
||||
} catch (err) {
|
||||
|
||||
@ -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',
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const getFormData = (options: ApiRequestOptions): FormData | undefined => {
|
||||
export const getFormData = (options: ApiRequestOptions): FormData | undefined => {
|
||||
if (options.formData) {
|
||||
const formData = new FormData();
|
||||
|
||||
|
||||
@ -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) => {
|
||||
|
||||
@ -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' &&
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
const isFormData = (value: any): value is FormData => {
|
||||
export const isFormData = (value: any): value is FormData => {
|
||||
return value instanceof FormData;
|
||||
};
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
const isString = (value: any): value is string => {
|
||||
export const isString = (value: any): value is string => {
|
||||
return typeof value === 'string';
|
||||
};
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
const isStringWithValue = (value: any): value is string => {
|
||||
export const isStringWithValue = (value: any): value is string => {
|
||||
return isString(value) && value !== '';
|
||||
};
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
const isSuccess = (status: number): boolean => {
|
||||
export const isSuccess = (status: number): boolean => {
|
||||
return status >= 200 && status < 300;
|
||||
};
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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');
|
||||
|
||||
@ -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)) {
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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');
|
||||
|
||||
@ -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)) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user