@arteneo/forge-uppy
v3.0.2
Published
Forge Uppy
Downloads
97
Readme
Future plans
When uploading an image and could not refresh token anymore via RefreshToken we get 401
and uppy error, but it is not shown in UppyError
component.
When using UppySingleDragDropImage
and you cancel an upload, image should revert to previous one (or empty if it will be very hard to do). Probably the same case with UppySingleInputFile
.
help
flag is not tested with UppySingleDragDropImage
.
Some kind of onChange prop for BaseUpload
(which means UppySingleDragDropImage
and UppySingleFile
will also have it).
Branches
You can use following branches:
main
(default) - branch for current version3.x
v2
- branch for version2.x
Restrictions
Uppy documentation: https://uppy.io/docs/uppy/#restrictions
Max file size
Maximum file size in bytes for each individual file.
const options = {
restrictions: {
// 500 kb
maxFileSize: 500 * 1024,
},
};
Allowed file types
Array of wildcards image/*
, exact mime types image/jpeg
, or file extensions .jpg
.
const options = {
restrictions: {
allowedFileTypes: ["image/*", ".jpg", ".jpeg", ".png", ".gif"],
},
};
Note! Allowed file types will also be used in hidden file input (in accept
HTML attribute). This means selecting a file from your PC will also be restricted (i.e. Chrome is actually hiding not accepted files).
Access and refresh tokens (JWT)
We can pass additional options in uppyTusOptions
to add access token to headers and use refresh token when needed.
Example implementation in utilities/uppy.tsx
as follows.
import { TusOptions } from "@uppy/tus";
import { DetailedError } from "tus-js-client";
import { getAccessToken, refreshAccessToken, updateLastAlive } from "~app/utilities/authenticationTokens";
export const uppyTusOptions: TusOptions = {
async onBeforeRequest(req) {
const accessToken = await getAccessToken();
req.setHeader("Authorization", "Bearer " + accessToken);
},
onShouldRetry(err, retryAttempt, options, next) {
if ((err as DetailedError)?.originalResponse?.getStatus() === 401) {
return true;
}
return next(err);
},
async onAfterResponse(req, res) {
if (res.getStatus() === 401) {
await refreshAccessToken();
updateLastAlive();
}
},
};
Explanation for internal functions:
getAccessToken
- Returns access token (as a string)refreshAccessToken
- Refreshes access token using refresh tokenupdateLastAlive
- Application specific, can be used to keep alive session
Example usage:
import React from "react";
import { getFields } from "@arteneo/forge";
import { UploadSingleDragDropImage } from "@arteneo/forge-uppy";
import { uppyTusOptions } from "~app/utilities/uppy";
type FieldName = "image";
const fields = {
image: <UploadSingleDragDropImage {...{ uppyTusOptions }} />,
};
export default (names?: FieldName[]) => getFields<FieldName>(names, fields);
export { FieldName };
Translations
Did not find a good solution to handle translations within Uppy. In our case it applies only to errors emitted by Restrictor (restriction of number of files, file sizes or file types). It seems that we need to prepare translation files specifically for Uppy if we want different translations. Uppy uses internal Translator with has different variable syntax and probably different pluralization.
More on translations:
https://uppy.io/docs/locales/
Development
- Install dependencies using
npm install
. - Build package using
npm run build
. - Update
version
inpackage.json
. - Commit and push changes.
- Publish package using
npm publish
.