forked from ravi0lii/node-sharex-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.js
83 lines (72 loc) · 2.48 KB
/
response.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const basicResponse = function (res, status, json) {
res.status(status).json(json)
}
// Responds with 400 bad request and the supplied error message/fix
const responseBadRequest = function (res, errorMessage, errorFix) {
basicResponse(res, 400, {
success: false,
error: {
message: errorMessage,
fix: errorFix
}
});
}
// Responds with 401 unauthorized and the supplied error message/fix
const responseUnauthorized = function (res, errorMessage, errorFix) {
basicResponse(res, 401, {
success: false,
error: {
message: errorMessage,
fix: errorFix
}
});
}
// Responds with empty key error
const responseEmptyKey = function (res) {
responseBadRequest(res, 'Key is empty.', 'Submit a key.');
}
// Responds with invalid key error
const responseInvalidKey = function (res) {
responseUnauthorized(res, 'Key is invalid.', 'Submit a valid key.');
}
// Responds with no uploaded file error
const responseNoFileUploaded = function (res) {
responseBadRequest(res, 'No file was uploaded.', 'Upload a file.');
}
// Responds with invalid extension error
const responseInvalidFileExtension = function (res) {
responseBadRequest(res, 'Invalid file extension.', 'Upload a file with a valid extension.');
}
// Responds with a uploaded response
const responseUploaded = function (res, url, deleteUrl) {
basicResponse(res, 200, {
success: true,
file: {
url: url,
delete_url: deleteUrl
}
});
}
// Responds with deleted response
const responseDeleted = function (res, fileName) {
basicResponse(res, 200, {
success: true,
message: "Deleted file " + fileName
})
}
// Responds with a file does not exists error
const responseFileDoesntExists = function (res) {
responseBadRequest(res, 'The file does not exists.', 'Submit a existing file name.')
}
// Responds with a file name is empty error
const responseFileNameIsEmpty = function (res) {
responseBadRequest(res, 'File name is empty.', 'Provide a file name.');
}
module.exports.emptyKey = responseEmptyKey;
module.exports.invalidKey = responseInvalidKey;
module.exports.noFileUploaded = responseNoFileUploaded;
module.exports.invalidFileExtension = responseInvalidFileExtension;
module.exports.uploaded = responseUploaded;
module.exports.deleted = responseDeleted;
module.exports.fileDoesNotExists = responseFileDoesntExists;
module.exports.fileNameIsEmpty = responseFileNameIsEmpty;