This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Formio.js
141 lines (129 loc) · 3.07 KB
/
Formio.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var _ = require('lodash');
var Q = require('q');
var request = require('request');
var User = require('./src/User');
var Form = require('./src/Form');
var Project = require('./src/Project');
/**
* The main Formio object.
* @param _config
* @constructor
*/
var Formio = function(_config) {
_config = _config || {};
this.config = _.defaults(_config, {
formio: 'https://formio.form.io',
api: 'https://api.form.io',
pageSize: 20,
key: ''
});
this.apiKey = this.config.key ? this.config.key : '';
this.adminKey = this.config.adminKey || '';
this.currentUser = null;
this.User = User(this);
this.Form = Form(this);
this.Project = Project(this);
};
/**
* Authenticate a new user.
*
* @param email
* @param password
* @param form
* @returns {*}
*/
Formio.prototype.authenticate = function (email, password, form) {
this.currentUser = new this.User(email, password);
return this.currentUser.authenticate(form);
};
// Create an alias for login.
Formio.prototype.login = Formio.prototype.authenticate;
/**
* Register a new user
*/
Formio.prototype.register = function(user, form) {
this.currentUser = new this.User();
return this.currentUser.register(user, form);
};
/**
* Sets the user token
*/
Formio.prototype.setToken = function(token) {
this.currentUser = new this.User();
this.currentUser.token = token;
};
/**
* Perform a request against the Form.io server.
*
* @param method
* @param url
* @param data
* @param headers
* @returns {*|(()=>Promise<D>)|(()=>Promise<SendData>)}
*/
Formio.prototype.request = function (method, url, data, headers) {
var deferred = Q.defer();
method = method || 'get';
headers = _.defaults(headers || {}, {
'Accept': 'application/json'
});
if (
!headers.hasOwnProperty('x-jwt-token') &&
!headers.hasOwnProperty('x-token') &&
this.apiKey
) {
headers['x-token'] = this.apiKey;
}
else if (
!headers.hasOwnProperty('x-jwt-token') &&
this.currentUser &&
this.currentUser.token
) {
headers['x-jwt-token'] = this.currentUser.token;
}
else if (
!headers.hasOwnProperty('x-jwt-token') &&
!headers.hasOwnProperty('x-token') &&
this.adminKey
) {
headers['x-admin-key'] = this.adminKey;
}
var options = {
method: method.toUpperCase(),
url: url,
rejectUnauthorized: false,
headers: headers,
json: true
};
if (data) {
options.body = data;
}
// Execute the request.
try {
request(options, function(err, response) {
if (err) {
return deferred.reject(err);
}
// Fail for anything other than 200 status code.
if (parseInt(response.statusCode / 100, 10) !== 2) {
var err = new Error(response.body);
err.response = response;
return deferred.reject(err);
}
deferred.resolve(response);
});
}
catch(err) {
deferred.reject(err);
}
return deferred.promise;
};
/**
* The Form.IO interface.
*
* @param config - The configuration.
*/
module.exports = function (config) {
// Return a new instance of the Formio interface.
return new Formio(config);
};