From 555995a031a559598cb6fd1d94ebb9f26907dc37 Mon Sep 17 00:00:00 2001 From: Adam Macumber Date: Mon, 14 Sep 2020 15:59:13 -0400 Subject: [PATCH] fix: Allow GQL or XHR client to be provided to UploadProvider. --- src/clients/graphql/index.tsx | 10 +++++++--- src/clients/xhr/index.tsx | 2 +- src/provider.tsx | 5 +++-- src/use-upload.tsx | 8 ++++---- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/clients/graphql/index.tsx b/src/clients/graphql/index.tsx index ac751c9..7b642a0 100644 --- a/src/clients/graphql/index.tsx +++ b/src/clients/graphql/index.tsx @@ -11,10 +11,14 @@ type GraphQLSetupOptions = { baseUrl: string; modifyRequest?: (request: GraphQLOptions) => GraphQLOptions; }; -export type GraphQLRequestOptions = { + +export type GraphQLClientProps = { onProgress: (progress: number) => void; - options: GraphQLOptions; + options: any; }; + +export type GraphQLClient = (args: GraphQLClientProps) => Promise; + type Headers = { [key: string]: any; }; @@ -38,7 +42,7 @@ export const createGraphQLClient = ({ }: GraphQLSetupOptions) => ({ onProgress, options, -}: GraphQLRequestOptions): Promise => { +}: GraphQLClientProps): Promise => { let modifiedOptions = modifyRequest ? modifyRequest(options) : options; const { clone, files } = extractFiles({ diff --git a/src/clients/xhr/index.tsx b/src/clients/xhr/index.tsx index 8d00e2a..ba4d9eb 100644 --- a/src/clients/xhr/index.tsx +++ b/src/clients/xhr/index.tsx @@ -7,7 +7,7 @@ export type XHRClientProps = { dispatch: dispatchType; onProgress: (progress: number) => void; files: FileOrFileList; - options: XHROptions; + options: any; }; export type XHRClient = (args: XHRClientProps) => Promise; diff --git a/src/provider.tsx b/src/provider.tsx index e7622ab..2e9deb8 100644 --- a/src/provider.tsx +++ b/src/provider.tsx @@ -1,12 +1,13 @@ import React, { ReactNode } from 'react'; import { XHRClient } from './clients/xhr'; +import { GraphQLClient } from './clients/graphql'; type Props = { - client: XHRClient | null; + client: XHRClient | GraphQLClient | null; children: ReactNode; }; -export const UploadContext = React.createContext(null); +export const UploadContext = React.createContext(null); export const UploadProvider = ({ client, children }: Props) => ( {children} diff --git a/src/use-upload.tsx b/src/use-upload.tsx index ddecfdc..22f4279 100644 --- a/src/use-upload.tsx +++ b/src/use-upload.tsx @@ -10,11 +10,11 @@ import { } from './upload-reducer'; import { XHRClient, XHROptions, createXhrClient } from './clients/xhr'; import { FileOrFileList } from './'; -import { GraphQLOptions } from 'clients/graphql'; +import { GraphQLClient, GraphQLOptions } from 'clients/graphql'; type HookProps = { files: File | FileList; - client: XHRClient | null; + client: XHRClient | GraphQLClient | null; options: XHROptions | GraphQLOptions; dispatch: dispatchType; }; @@ -34,7 +34,7 @@ const handleUpload = async ({ files, options, dispatch, - onProgress: progress => + onProgress: (progress: number) => dispatch({ type: SET_UPLOAD_PROGRESS, payload: progress }), }); if (response) dispatch({ type: FINISH_UPLOADING, payload: response }); @@ -44,7 +44,7 @@ export const useUpload = ( files: FileOrFileList, options: XHROptions | GraphQLOptions, ): UploadState => { - let client = useContext(UploadContext); + let client = useContext(UploadContext); const [state, dispatch] = useReducer(reducer, {}); useEffect(() => {