feat: added possibility to change endpoint

This commit is contained in:
Andrey Ponomarenko 2020-11-25 10:54:42 +03:00
parent 4ac38898fc
commit d09e2a4e84
4 changed files with 17 additions and 9 deletions

View File

@ -1,9 +1,8 @@
import { API_STATUS } from '@app/constants'; import { ApiStatusParams, ApiStatusResponse, StatusOptions } from '@app/types';
import { ApiStatusParams, ApiStatusResponse } from '@app/types';
import fetch from 'node-fetch'; import fetch from 'node-fetch';
export const fetchJobStatus = async ({ token, job }: ApiStatusParams): Promise<ApiStatusResponse> => { export const fetchJobStatus = async ({ token, job }: ApiStatusParams, { apiStatusEndpoint }: StatusOptions): Promise<ApiStatusResponse> => {
const url = `${API_STATUS}/?token=${token}&job=${job}`; const url = `${apiStatusEndpoint}/?token=${token}&job=${job}`;
const res = await fetch(url); const res = await fetch(url);
return res.json(); return res.json();

View File

@ -14,6 +14,7 @@ export const rawUpload = async (params: ApiUploadProps, options: UploadOptions =
const { const {
onUploadProgress = noop, onUploadProgress = noop,
apiUploadEndpoint = API_UPLOAD,
} = options; } = options;
const filePath = path.resolve(file); const filePath = path.resolve(file);
@ -35,7 +36,7 @@ export const rawUpload = async (params: ApiUploadProps, options: UploadOptions =
const data = await request({ const data = await request({
method: 'post', method: 'post',
host: new URL(API_UPLOAD).host, host: new URL(apiUploadEndpoint).host,
headers: form.getHeaders(), headers: form.getHeaders(),
formData: form, formData: form,
}); });

View File

@ -1,9 +1,9 @@
import { ApiStatusResponse, ApiUploadProps, UploadOptions } from '@app/types'; import { ApiStatusResponse, ApiUploadProps, StatusOptions, UploadOptions } from '@app/types';
import { rawUpload, fetchJobStatus } from '@app/core'; import { rawUpload, fetchJobStatus } from '@app/core';
import { JOB_STATUS, DEFAULT_MAX_API_STATUS_CALLS } from '@app/constants'; import { JOB_STATUS, DEFAULT_MAX_API_STATUS_CALLS, API_UPLOAD, API_STATUS } from '@app/constants';
import { noop, sleep } from '@app/utils'; import { noop, sleep } from '@app/utils';
interface Options extends UploadOptions{ interface Options extends UploadOptions, StatusOptions {
maxApiStatusCalls?: number; maxApiStatusCalls?: number;
onStatusProgress?: (status: JOB_STATUS) => any; onStatusProgress?: (status: JOB_STATUS) => any;
} }
@ -17,10 +17,13 @@ export const upload = async (props: ApiUploadProps, options: Options = {}) => {
maxApiStatusCalls = DEFAULT_MAX_API_STATUS_CALLS, maxApiStatusCalls = DEFAULT_MAX_API_STATUS_CALLS,
onUploadProgress = noop, onUploadProgress = noop,
onStatusProgress = noop, onStatusProgress = noop,
apiUploadEndpoint = API_UPLOAD,
apiStatusEndpoint = API_STATUS,
} = options; } = options;
const { job } = await rawUpload(props, { const { job } = await rawUpload(props, {
onUploadProgress, onUploadProgress,
apiUploadEndpoint,
}); });
let statusCallsCount = 0; let statusCallsCount = 0;
@ -29,7 +32,7 @@ export const upload = async (props: ApiUploadProps, options: Options = {}) => {
const checkStatus = async (): Promise<ApiStatusResponse> => { const checkStatus = async (): Promise<ApiStatusResponse> => {
if (statusCallsCount > maxApiStatusCalls) throw new Error('max api calls exceeded'); if (statusCallsCount > maxApiStatusCalls) throw new Error('max api calls exceeded');
const jobStatus = await fetchJobStatus({ token, job }); const jobStatus = await fetchJobStatus({ token, job }, { apiStatusEndpoint });
const { status, message } = jobStatus; const { status, message } = jobStatus;
statusCallsCount += 1; statusCallsCount += 1;

View File

@ -14,6 +14,11 @@ export interface ApiUploadProps {
export interface UploadOptions { export interface UploadOptions {
onUploadProgress?: (progressPercent: number, args: {bytesWritten: number, fileSize: number }) => any; onUploadProgress?: (progressPercent: number, args: {bytesWritten: number, fileSize: number }) => any;
apiUploadEndpoint?: string;
}
export interface StatusOptions {
apiStatusEndpoint?: string;
} }
export interface ApiUploadResponse { export interface ApiUploadResponse {