@clevercloud/client
Advanced tools
Comparing version 1.0.0 to 1.0.1
# Clever Client changelog | ||
## 1.0.1 (2019-07-25) | ||
- Fix JSON handling for node (request.request.js) | ||
## 1.0.0 (2019-07-25) | ||
@@ -4,0 +8,0 @@ |
@@ -730,3 +730,3 @@ "use strict"; | ||
users: { | ||
post: prepareRequest(unknown.todo_createUserFromForm), | ||
post: prepareRequest(unknown.todo_createUser), | ||
_: { | ||
@@ -733,0 +733,0 @@ get: prepareRequest(unknown.todo_getUser, ['id']), |
@@ -45,3 +45,3 @@ "use strict"; | ||
exports.todo_getSignupForm_1 = todo_getSignupForm_1; | ||
exports.todo_createUserFromForm = todo_createUserFromForm; | ||
exports.todo_createUser = todo_createUser; | ||
exports.todo_getUser = todo_getUser; | ||
@@ -849,2 +849,4 @@ exports.todo_getApplications = todo_getApplications; | ||
* @param {Object} params | ||
* @param {String} params.invitationKey | ||
* @param {String} params.addonBetaInvitationKey | ||
* @param {Object} body | ||
@@ -854,3 +856,3 @@ */ | ||
function todo_createUserFromForm(params, body) { | ||
function todo_createUser(params, body) { | ||
// no multipath for /self or /organisations/{id} | ||
@@ -862,5 +864,5 @@ return Promise.resolve({ | ||
Accept: 'application/json', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
'Content-Type': 'application/json' | ||
}, | ||
// no query params | ||
queryParams: (0, _pickNonNull.pickNonNull)(params, ['invitationKey', 'addonBetaInvitationKey']), | ||
body | ||
@@ -867,0 +869,0 @@ }); |
@@ -12,7 +12,36 @@ "use strict"; | ||
const JSON_TYPE = 'application/json'; | ||
const FORM_TYPE = 'application/x-www-form-urlencoded'; | ||
function formatBody(requestParams) { | ||
// for now we support the fact that users sometimes already stringified the body | ||
if (requestParams.headers['Content-Type'] === JSON_TYPE && typeof requestParams.body !== 'string') { | ||
return JSON.stringify(requestParams.body); | ||
} | ||
if (requestParams.headers['Content-Type'] === FORM_TYPE && typeof requestParams.body !== 'string') { | ||
const qs = new URLSearchParams(); | ||
Object.entries(requestParams.body).forEach(([name, value]) => qs.set(name, value)); | ||
return qs.toString(); | ||
} | ||
return requestParams.body; | ||
} | ||
function formatResponse(requestParams, response, rawBody) { | ||
if (requestParams.headers['Accept'] === JSON_TYPE && response.headers['content-type'] === JSON_TYPE) { | ||
return JSON.parse(rawBody); | ||
} | ||
if (requestParams.headers['Accept'] === FORM_TYPE && response.headers['content-type'] === FORM_TYPE) { | ||
const objectResponse = {}; | ||
Array.from(new URLSearchParams(rawBody).entries()).forEach(([name, value]) => objectResponse[name] = value); | ||
return objectResponse; | ||
} | ||
return rawBody; | ||
} | ||
async function request(requestParams) { | ||
const jsonRequest = requestParams.headers['Content-Type'] === 'application/json'; | ||
const jsonResponse = requestParams.headers['Accept'] === 'application/json'; | ||
const bodyIsString = typeof requestParams.body === 'string'; | ||
const json = !bodyIsString && (jsonRequest || jsonResponse); | ||
const body = formatBody(requestParams); | ||
const options = { | ||
@@ -23,14 +52,20 @@ url: requestParams.url, | ||
qs: requestParams.queryParams, | ||
body: requestParams.body, | ||
json | ||
body, | ||
json: false | ||
}; | ||
return new Promise((resolve, reject) => { | ||
(0, _request.default)(options, (error, response, resBody) => { | ||
(0, _request.default)(options, (error, response, rawBody) => { | ||
if (error != null) { | ||
return reject(error); | ||
} | ||
const responseBody = formatResponse(requestParams, response, rawBody); | ||
if (response.statusCode >= 400) { | ||
return reject(Error(resBody.message)); | ||
return reject(Error(responseBody.message)); | ||
} | ||
return error != null ? reject(error) : resolve(resBody); | ||
return resolve(responseBody); | ||
}); | ||
}); | ||
} |
@@ -713,3 +713,3 @@ import * as addon from './addon.js'; | ||
users: { | ||
post: prepareRequest(unknown.todo_createUserFromForm), | ||
post: prepareRequest(unknown.todo_createUser), | ||
_: { | ||
@@ -716,0 +716,0 @@ get: prepareRequest(unknown.todo_getUser, ['id']), |
@@ -689,5 +689,7 @@ import { pickNonNull } from '../pick-non-null.js'; | ||
* @param {Object} params | ||
* @param {String} params.invitationKey | ||
* @param {String} params.addonBetaInvitationKey | ||
* @param {Object} body | ||
*/ | ||
export function todo_createUserFromForm(params, body) { | ||
export function todo_createUser(params, body) { | ||
// no multipath for /self or /organisations/{id} | ||
@@ -697,4 +699,4 @@ return Promise.resolve({ | ||
url: `/users`, | ||
headers: { Accept: 'application/json', 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
// no query params | ||
headers: { Accept: 'application/json', 'Content-Type': 'application/json' }, | ||
queryParams: pickNonNull(params, ['invitationKey', 'addonBetaInvitationKey']), | ||
body, | ||
@@ -701,0 +703,0 @@ }); |
import req from 'request'; | ||
const JSON_TYPE = 'application/json'; | ||
const FORM_TYPE = 'application/x-www-form-urlencoded'; | ||
function formatBody (requestParams) { | ||
// for now we support the fact that users sometimes already stringified the body | ||
if (requestParams.headers['Content-Type'] === JSON_TYPE && typeof requestParams.body !== 'string') { | ||
return JSON.stringify(requestParams.body); | ||
} | ||
if (requestParams.headers['Content-Type'] === FORM_TYPE && typeof requestParams.body !== 'string') { | ||
const qs = new URLSearchParams(); | ||
Object | ||
.entries(requestParams.body) | ||
.forEach(([name, value]) => qs.set(name, value)); | ||
return qs.toString(); | ||
} | ||
return requestParams.body; | ||
} | ||
function formatResponse (requestParams, response, rawBody) { | ||
if (requestParams.headers['Accept'] === JSON_TYPE && response.headers['content-type'] === JSON_TYPE) { | ||
return JSON.parse(rawBody); | ||
} | ||
if (requestParams.headers['Accept'] === FORM_TYPE && response.headers['content-type'] === FORM_TYPE) { | ||
const objectResponse = {}; | ||
Array | ||
.from(new URLSearchParams(rawBody).entries()) | ||
.forEach(([name, value]) => (objectResponse[name] = value)); | ||
return objectResponse; | ||
} | ||
return rawBody; | ||
} | ||
export async function request (requestParams) { | ||
const jsonRequest = (requestParams.headers['Content-Type'] === 'application/json'); | ||
const jsonResponse = (requestParams.headers['Accept'] === 'application/json'); | ||
const bodyIsString = (typeof requestParams.body === 'string'); | ||
const body = formatBody(requestParams); | ||
const json = !bodyIsString && (jsonRequest || jsonResponse); | ||
const options = { | ||
@@ -16,14 +50,18 @@ url: requestParams.url, | ||
qs: requestParams.queryParams, | ||
body: requestParams.body, | ||
json, | ||
body, | ||
json: false, | ||
}; | ||
return new Promise((resolve, reject) => { | ||
req(options, (error, response, resBody) => { | ||
req(options, (error, response, rawBody) => { | ||
if (error != null) { | ||
return reject(error); | ||
} | ||
const responseBody = formatResponse(requestParams, response, rawBody); | ||
if (response.statusCode >= 400) { | ||
return reject(Error(resBody.message)); | ||
return reject(Error(responseBody.message)); | ||
} | ||
return (error != null) ? reject(error) : resolve(resBody); | ||
return resolve(responseBody); | ||
}); | ||
}); | ||
} |
{ | ||
"name": "@clevercloud/client", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "JavaScript REST client and utils for Clever Cloud's API", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/CleverCloud/clever-client.js", |
315588
10359