graphql-upload-ts
Version:
TypeScript-first middleware and Upload scalar for GraphQL multipart requests (file uploads) with support for Apollo Server, Express, Koa, and more.
110 lines (106 loc) • 4.41 kB
JavaScript
;
var graphql = require('graphql');
exports.UploadErrorCode = void 0;
(function (UploadErrorCode) {
UploadErrorCode["FILE_TOO_LARGE"] = "FILE_TOO_LARGE";
UploadErrorCode["TOO_MANY_FILES"] = "TOO_MANY_FILES";
UploadErrorCode["FIELD_TOO_LARGE"] = "FIELD_TOO_LARGE";
UploadErrorCode["INVALID_MULTIPART"] = "INVALID_MULTIPART";
UploadErrorCode["MISSING_OPERATIONS"] = "MISSING_OPERATIONS";
UploadErrorCode["MISSING_MAP"] = "MISSING_MAP";
UploadErrorCode["INVALID_JSON"] = "INVALID_JSON";
UploadErrorCode["INVALID_MAP"] = "INVALID_MAP";
UploadErrorCode["FILE_MISSING"] = "FILE_MISSING";
UploadErrorCode["STREAM_ERROR"] = "STREAM_ERROR";
UploadErrorCode["REQUEST_DISCONNECTED"] = "REQUEST_DISCONNECTED";
UploadErrorCode["INVALID_UPLOAD_VALUE"] = "INVALID_UPLOAD_VALUE";
UploadErrorCode["UPLOAD_LITERAL_UNSUPPORTED"] = "UPLOAD_LITERAL_UNSUPPORTED";
UploadErrorCode["UPLOAD_SERIALIZATION_UNSUPPORTED"] = "UPLOAD_SERIALIZATION_UNSUPPORTED";
})(exports.UploadErrorCode || (exports.UploadErrorCode = {}));
class UploadError extends Error {
constructor(message, code, status = 400, expose = true) {
super(message);
this.name = 'UploadError';
this.code = code;
this.status = status;
this.expose = expose;
Object.setPrototypeOf(this, UploadError.prototype);
}
toGraphQLError() {
return new graphql.GraphQLError(this.message, {
extensions: {
code: this.code,
status: this.status,
},
});
}
}
class FileTooLargeError extends UploadError {
constructor(maxSize) {
super(`File truncated as it exceeds the ${maxSize} byte size limit.`, exports.UploadErrorCode.FILE_TOO_LARGE, 413);
}
}
class TooManyFilesError extends UploadError {
constructor(maxFiles) {
super(`${maxFiles} max file uploads exceeded.`, exports.UploadErrorCode.TOO_MANY_FILES, 413);
}
}
class FieldTooLargeError extends UploadError {
constructor(fieldName, maxSize) {
super(`The '${fieldName}' multipart field value exceeds the ${maxSize} byte size limit.`, exports.UploadErrorCode.FIELD_TOO_LARGE, 413);
}
}
class InvalidMultipartError extends UploadError {
constructor(message) {
super(message, exports.UploadErrorCode.INVALID_MULTIPART);
}
}
class MissingOperationsError extends UploadError {
constructor() {
super(`Missing multipart field 'operations' (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`, exports.UploadErrorCode.MISSING_OPERATIONS);
}
}
class MissingMapError extends UploadError {
constructor() {
super(`Missing multipart field 'map' (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`, exports.UploadErrorCode.MISSING_MAP);
}
}
class InvalidJSONError extends UploadError {
constructor(fieldName) {
super(`Invalid JSON in the '${fieldName}' multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`, exports.UploadErrorCode.INVALID_JSON);
}
}
class InvalidMapError extends UploadError {
constructor(message) {
super(`${message} (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`, exports.UploadErrorCode.INVALID_MAP);
}
}
class FileMissingError extends UploadError {
constructor() {
super('File missing in the request.', exports.UploadErrorCode.FILE_MISSING);
}
}
class StreamError extends UploadError {
constructor(message) {
super(message, exports.UploadErrorCode.STREAM_ERROR, 500);
}
}
class RequestDisconnectedError extends UploadError {
constructor() {
super('Request disconnected during file upload stream parsing.', exports.UploadErrorCode.REQUEST_DISCONNECTED, 499);
}
}
const GRAPHQL_MULTIPART_REQUEST_SPEC_URL = 'https://github.com/jaydenseric/graphql-multipart-request-spec';
exports.FieldTooLargeError = FieldTooLargeError;
exports.FileMissingError = FileMissingError;
exports.FileTooLargeError = FileTooLargeError;
exports.InvalidJSONError = InvalidJSONError;
exports.InvalidMapError = InvalidMapError;
exports.InvalidMultipartError = InvalidMultipartError;
exports.MissingMapError = MissingMapError;
exports.MissingOperationsError = MissingOperationsError;
exports.RequestDisconnectedError = RequestDisconnectedError;
exports.StreamError = StreamError;
exports.TooManyFilesError = TooManyFilesError;
exports.UploadError = UploadError;
//# sourceMappingURL=upload-errors.js.map