Skip to content

Commit

Permalink
Let modify options be async
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Sep 15, 2020
1 parent 1eb44b1 commit c0bfb40
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-use-upload",
"version": "0.5.11",
"version": "0.5.13-beta",
"description": "",
"main": "cjs/index.js",
"module": "lib/index.js",
Expand Down
20 changes: 11 additions & 9 deletions src/clients/graphql/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import request from './request';
import { extractFiles } from 'extract-files';
import { XHRResponse } from '../xhr/listeners';
import request from "./request";
import { extractFiles } from "extract-files";
import { XHRResponse } from "../xhr/listeners";
/*
Extract files is the official package used by the developer who helped
create the graphql upload spec in apollo client / server
Expand All @@ -9,7 +9,9 @@ import { XHRResponse } from '../xhr/listeners';

type GraphQLSetupOptions = {
baseUrl: string;
modifyRequest?: (request: GraphQLOptions) => GraphQLOptions;
modifyRequest?: (
request: GraphQLOptions
) => Promise<GraphQLOptions> | GraphQLOptions;
};

export type GraphQLClientProps = {
Expand Down Expand Up @@ -39,26 +41,26 @@ type FileMap = {
export const createGraphQLClient = ({
baseUrl,
modifyRequest,
}: GraphQLSetupOptions) => ({
}: GraphQLSetupOptions) => async ({
onProgress,
options,
}: GraphQLClientProps): Promise<XHRResponse> => {
let modifiedOptions = modifyRequest ? modifyRequest(options) : options;
let modifiedOptions = modifyRequest ? await modifyRequest(options) : options;

const { clone, files } = extractFiles({
query: options.mutation.loc.source.body,
variables: options.variables,
});

var body = new FormData();
body.append('operations', JSON.stringify(clone));
body.append("operations", JSON.stringify(clone));

const map: FileMap = {};
let i = 0;
files.forEach((paths: string) => {
map[++i] = paths;
});
body.append('map', JSON.stringify(map));
body.append("map", JSON.stringify(map));

i = 0;
files.forEach((paths: string, file: File) => {
Expand All @@ -69,6 +71,6 @@ export const createGraphQLClient = ({
body,
onProgress,
options: modifiedOptions,
url: `${baseUrl}${options.path || ''}`,
url: `${baseUrl}${options.path || ""}`,
});
};
4 changes: 2 additions & 2 deletions src/clients/xhr/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type XHRClient = (args: XHRClientProps) => Promise<XHRResponse>;

export type XHRSetupOptions = {
baseUrl?: string;
modifyRequest?: (request: XHROptions) => XHROptions;
modifyRequest?: (request: XHROptions) => Promise<XHROptions> | XHROptions;
};

export type GetUrlResponse =
Expand Down Expand Up @@ -48,7 +48,7 @@ export const createXhrClient = ({
files,
options,
}: XHRClientProps): Promise<XHRResponse> => {
let modifiedOptions = modifyRequest ? modifyRequest(options) : options;
let modifiedOptions = modifyRequest ? await modifyRequest(options) : options;
let url = `${baseUrl || ""}${options.path || ""}`;

//Get the url using a promise, for signed uploads
Expand Down

0 comments on commit c0bfb40

Please sign in to comment.