Coder Social home page Coder Social logo

zackify / use-upload Goto Github PK

View Code? Open in Web Editor NEW
76.0 4.0 4.0 503 KB

Framework agnostic library handling file uploads with progress, direct to AWS / GCP, and other locations

TypeScript 91.68% JavaScript 3.47% Svelte 4.85%
file-upload react react-hook xhr hooks svelte

use-upload's People

Contributors

atomdmac avatar zackify avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

use-upload's Issues

UploadProvider Doesn't Accept GraphQLClient Instance

I've copy-pasted the following from the examples in the README (with some small formatting tweaks). However, tsc throws an error complaining about the client prop. I've done my best to follow the instructions from the README as closely as possible but I can't seem to get around this compilation error.

Code

import React, { useState } from 'react';
import { createGraphQLClient, UploadProvider, useUpload} from 'react-use-upload';
import gql from 'graphql-tag';

const client = createGraphQLClient({
  baseUrl: 'http://localhost:3000/graphql',
  modifyRequest: (request) => ({
    ...request,
    headers: {
      ...request.headers,
      authorization: `Bearer ${localStorage.getItem('token')}`,
    },
  }),
});

const uploadMutation = gql`
  mutation UploadFile($input: UploadFileInput!) {
    uploadFile(input: $input) {
      name
      randomField
    }
  }
`;

export const GraphQLUpload = () => {
  let [files, setFiles] = useState();

  let { loading, progress, error } = useUpload(files, {
    mutation: uploadMutation,
    variables: { input: { files, name: 'test' } },
  });

  return (
    <UploadProvider client={client}>
      <div style={{ marginBottom: 50 }}>
        <div>GraphQL Test</div>
        {loading ? (
          <div>Progress: {progress}</div>
        ) : error ? (
          <div>{error}</div>
        ) : (
          <input type="file" onChange={(e) => setFiles(e.target.files)} multiple />
        )}
      </div>
    </UploadProvider>
  );
};

Compilation Error:

Type '({ onProgress, options, }: GraphQLRequestOptions) => Promise<XHRResponse>' is not assignable to type 'XHRClient'.
  Types of parameters '__0' and 'args' are incompatible.
    Type 'XHRClientProps' is not assignable to type 'GraphQLRequestOptions'.
      Types of property 'options' are incompatible.
        Type 'XHROptions' is missing the following properties from type 'GraphQLOptions': mutation, variables, onProgress  TS2322

remove es6-imports from lib

The version currently published to npm still uses es6-import syntax.
Since most of the time a module bundler or test framework will not transpile node_modules. This leads to errors in environments that do not understand es6 imports.

In my case I would need to add custom jest transform just for this module.

I would propose to publish this module using commonJS + interop.

Allow Async Function for modifyRequest

I have a use case where I'd like to attach an Authorization header but will need to invoke an async function to retrieve it. I can probably work around this by retrieving the value in advance but that is obviously not ideal. What I'd like is to be able to provide an async function for modifyRequest.

With that said, from looking at the code, it seems that modifyRequest actually modifies the options that are passed to the request "factory" function so it's possible that I'm just misunderstanding the intended use for modifyRequest.

onSuccess Callback

Is there a callback for when the upload completes (irrespective of state). It seems weird to be monitoring progress and checking state to figure out if and when a file has been uploaded successfully if that's a point in a sequence of required actions.

useUpload should accept FileOrFileList | null | undefined

Hey, was using your library today with TypeScript and the first argument to useUpload requires a FileOrFileList type, but does not accept null / undefined. I don't have the file yet when the component first mounts. I noticed that you check for files being null / undefined, so I think the type is wrong and needs to be updated to include null / undefined.

https://github.com/zackify/react-use-upload/blob/2d57e7da7dae7eb88789f400119180eda39cae8b/src/use-upload.tsx#L43-L52

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.