-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Client.js
136 lines (128 loc) · 3.53 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
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
import UserAgent from './UserAgent';
class CognitoError extends Error {
constructor(message, code, name, statusCode) {
super(message);
this.code = code;
this.name = name;
this.statusCode = statusCode;
}
}
/** @class */
export default class Client {
/**
* Constructs a new AWS Cognito Identity Provider client object
* @param {string} region AWS region
* @param {string} endpoint endpoint
* @param {object} fetchOptions options for fetch API (only credentials is supported)
*/
constructor(region, endpoint, fetchOptions) {
this.endpoint = endpoint || `https://cognito-idp.${region}.amazonaws.com/`;
const { credentials } = fetchOptions || {};
this.fetchOptions = credentials ? { credentials } : {};
}
/**
* Makes an unauthenticated request on AWS Cognito Identity Provider API
* using fetch
* @param {string} operation API operation
* @param {object} params Input parameters
* @returns Promise<object>
*/
promisifyRequest(operation, params) {
return new Promise((resolve, reject) => {
this.request(operation, params, (err, data) => {
if (err) {
reject(
new CognitoError(err.message, err.code, err.name, err.statusCode)
);
} else {
resolve(data);
}
});
});
}
/**
* 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': UserAgent.prototype.userAgent,
};
const options = Object.assign({}, this.fetchOptions, {
headers,
method: 'POST',
mode: 'cors',
cache: 'no-cache',
body: JSON.stringify(params),
});
let response;
let responseJsonData;
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 => {
// return parsed body stream
if (response.ok) return callback(null, data);
responseJsonData = 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 => {
// 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-amzn-errortype').split(':')[0];
const error = {
code,
name: code,
statusCode: response.status,
message: response.status ? response.status.toString() : null,
};
return callback(error);
} catch (ex) {
return callback(err);
}
// otherwise check if error is Network error
} else if (err instanceof Error && err.message === 'Network error') {
const error = {
code: 'NetworkError',
name: err.name,
message: err.message,
};
return callback(error);
} else {
return callback(err);
}
});
}
}