From d09e2a4e847c9c3dc6a25ff88f3017ad57bd9a6e Mon Sep 17 00:00:00 2001 From: Andrey Ponomarenko Date: Wed, 25 Nov 2020 10:54:42 +0300 Subject: [PATCH] feat: added possibility to change endpoint --- src/core/fetchJobStatus/index.ts | 7 +++---- src/core/rawUpload/index.ts | 3 ++- src/core/upload/index.ts | 11 +++++++---- src/types/index.ts | 5 +++++ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/core/fetchJobStatus/index.ts b/src/core/fetchJobStatus/index.ts index 2e215af..7f18488 100644 --- a/src/core/fetchJobStatus/index.ts +++ b/src/core/fetchJobStatus/index.ts @@ -1,9 +1,8 @@ -import { API_STATUS } from '@app/constants'; -import { ApiStatusParams, ApiStatusResponse } from '@app/types'; +import { ApiStatusParams, ApiStatusResponse, StatusOptions } from '@app/types'; import fetch from 'node-fetch'; -export const fetchJobStatus = async ({ token, job }: ApiStatusParams): Promise => { - const url = `${API_STATUS}/?token=${token}&job=${job}`; +export const fetchJobStatus = async ({ token, job }: ApiStatusParams, { apiStatusEndpoint }: StatusOptions): Promise => { + const url = `${apiStatusEndpoint}/?token=${token}&job=${job}`; const res = await fetch(url); return res.json(); diff --git a/src/core/rawUpload/index.ts b/src/core/rawUpload/index.ts index cb316f0..42d1b2a 100644 --- a/src/core/rawUpload/index.ts +++ b/src/core/rawUpload/index.ts @@ -14,6 +14,7 @@ export const rawUpload = async (params: ApiUploadProps, options: UploadOptions = const { onUploadProgress = noop, + apiUploadEndpoint = API_UPLOAD, } = options; const filePath = path.resolve(file); @@ -35,7 +36,7 @@ export const rawUpload = async (params: ApiUploadProps, options: UploadOptions = const data = await request({ method: 'post', - host: new URL(API_UPLOAD).host, + host: new URL(apiUploadEndpoint).host, headers: form.getHeaders(), formData: form, }); diff --git a/src/core/upload/index.ts b/src/core/upload/index.ts index c981621..a960e83 100644 --- a/src/core/upload/index.ts +++ b/src/core/upload/index.ts @@ -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 { 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'; -interface Options extends UploadOptions{ +interface Options extends UploadOptions, StatusOptions { maxApiStatusCalls?: number; onStatusProgress?: (status: JOB_STATUS) => any; } @@ -17,10 +17,13 @@ export const upload = async (props: ApiUploadProps, options: Options = {}) => { maxApiStatusCalls = DEFAULT_MAX_API_STATUS_CALLS, onUploadProgress = noop, onStatusProgress = noop, + apiUploadEndpoint = API_UPLOAD, + apiStatusEndpoint = API_STATUS, } = options; const { job } = await rawUpload(props, { onUploadProgress, + apiUploadEndpoint, }); let statusCallsCount = 0; @@ -29,7 +32,7 @@ export const upload = async (props: ApiUploadProps, options: Options = {}) => { const checkStatus = async (): Promise => { 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; statusCallsCount += 1; diff --git a/src/types/index.ts b/src/types/index.ts index 1b8e654..f235226 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -14,6 +14,11 @@ export interface ApiUploadProps { export interface UploadOptions { onUploadProgress?: (progressPercent: number, args: {bytesWritten: number, fileSize: number }) => any; + apiUploadEndpoint?: string; +} + +export interface StatusOptions { + apiStatusEndpoint?: string; } export interface ApiUploadResponse {