/
Client.js
93 lines (87 loc) · 2.89 KB
/
Client.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
import UserAgent from './UserAgent';
/** @class */
export default class Client {
/**
* Constructs a new AWS Cognito Identity Provider client object
* @param {string} region AWS region
* @param {string} endpoint endpoint
*/
constructor(region, endpoint) {
this.endpoint = endpoint || `https://cognito-idp.${region}.amazonaws.com/`;
this.userAgent = UserAgent.prototype.userAgent || 'aws-amplify/0.1.x js';
}
/**
* Makes an unauthenticated request on AWS Cognito Identity Provider API
* using fetch
* @param {string} operation API operation
* @param {object} params Input parameters
* @param {function} callback Callback called when a response is returned
* @returns {void}
*/
request(operation, params, callback) {
const headers = {
'Content-Type': 'application/x-amz-json-1.1',
'X-Amz-Target': `AWSCognitoIdentityProviderService.${operation}`,
'X-Amz-User-Agent': this.userAgent
};
const options = {
headers,
method: 'POST',
mode: 'cors',
cache: 'no-cache',
body: JSON.stringify(params)
};
let response;
fetch(this.endpoint, options)
.then(resp => {
response = resp;
return resp;
}, err => {
// If error happens here, the request failed
// if it is TypeError throw network error
if (err instanceof TypeError) {
throw new Error('Network error');
}
throw err;
})
.then(resp => resp.json().catch(() => ({})))
.then(data => {
if (response.ok) return callback(null, data);
// Taken from aws-sdk-js/lib/protocol/json.js
// eslint-disable-next-line no-underscore-dangle
const code = (data.__type || data.code).split('#').pop();
const error = {
code,
name: code,
message: (data.message || data.Message || null)
};
return callback(error);
})
.catch(err => {
let error = { code: 'UnknownError', message: 'Unkown error' };
// first check if we have a service error
if (response && response.headers && response.headers.get('x-amzn-errortype')) {
try {
const code = (response.headers.get('x-amz-errortype')).split(':')[0];
error = {
code,
name: code,
statusCode: response.status,
message: (response.status) ? response.status.toString() : null
};
} catch (ex) {
// pass through so it doesn't get swallowed if we can't parse it
}
// otherwise check if error is Network error
} else if (err instanceof Error && err.message === 'Network error') {
error = {
code: err.name,
name: err.name,
message: err.message
};
// finally case will return 'UnknownError'
}
return callback(error);
});
}
}