Skip to content

Commit

Permalink
Merge pull request #18 from atomdmac/de-type-client-options
Browse files Browse the repository at this point in the history
fix: Allow GQL or XHR client to be provided to UploadProvider.
  • Loading branch information
zackify authored Sep 15, 2020
2 parents 2d57e7d + 555995a commit 6d81f07
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/clients/graphql/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<XHRResponse>;

type Headers = {
[key: string]: any;
};
Expand All @@ -38,7 +42,7 @@ export const createGraphQLClient = ({
}: GraphQLSetupOptions) => ({
onProgress,
options,
}: GraphQLRequestOptions): Promise<XHRResponse> => {
}: GraphQLClientProps): Promise<XHRResponse> => {
let modifiedOptions = modifyRequest ? modifyRequest(options) : options;

const { clone, files } = extractFiles({
Expand Down
2 changes: 1 addition & 1 deletion src/clients/xhr/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<XHRResponse>;
Expand Down
5 changes: 3 additions & 2 deletions src/provider.tsx
Original file line number Diff line number Diff line change
@@ -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<XHRClient | null>(null);
export const UploadContext = React.createContext<XHRClient | GraphQLClient | null>(null);

export const UploadProvider = ({ client, children }: Props) => (
<UploadContext.Provider value={client}>{children}</UploadContext.Provider>
Expand Down
8 changes: 4 additions & 4 deletions src/use-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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 });
Expand All @@ -44,7 +44,7 @@ export const useUpload = (
files: FileOrFileList,
options: XHROptions | GraphQLOptions,
): UploadState => {
let client = useContext<XHRClient | null>(UploadContext);
let client = useContext<XHRClient | GraphQLClient | null>(UploadContext);
const [state, dispatch] = useReducer(reducer, {});

useEffect(() => {
Expand Down

0 comments on commit 6d81f07

Please sign in to comment.