feat: added possibility to change endpoint
This commit is contained in:
parent
4ac38898fc
commit
d09e2a4e84
|
@ -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<ApiStatusResponse> => {
|
||||
const url = `${API_STATUS}/?token=${token}&job=${job}`;
|
||||
export const fetchJobStatus = async ({ token, job }: ApiStatusParams, { apiStatusEndpoint }: StatusOptions): Promise<ApiStatusResponse> => {
|
||||
const url = `${apiStatusEndpoint}/?token=${token}&job=${job}`;
|
||||
const res = await fetch(url);
|
||||
|
||||
return res.json();
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
|
|
|
@ -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<ApiStatusResponse> => {
|
||||
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;
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue