Skip to content

Commit

Permalink
Merge pull request #10 from stape-io/x-www-form-urlencoded-support
Browse files Browse the repository at this point in the history
x-www-form-urlencoded support
  • Loading branch information
Bukashk0zzz authored Jan 30, 2024
2 parents 285aedf + 1fc551b commit f4d40e4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
2 changes: 2 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
homepage: "https://stape.io/"
versions:
- sha: e31d2f39c10285cbc5169e349c473f73e4668692
changeNotes: Added support for x-www-form-urlencoded.
- sha: 4d9f4d4fbded5414cb091efac70224d31c522e5c
changeNotes: Fix user address data.
- sha: 905af50fe8eb5f61e58a6d04c33fd6faf01e7f98
Expand Down
49 changes: 48 additions & 1 deletion template.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const computeEffectiveTldPlusOne = require('computeEffectiveTldPlusOne');
const getRequestQueryParameter = require('getRequestQueryParameter');
const getType = require('getType');
const Promise = require('Promise');
const decodeUriComponent = require('decodeUriComponent');
const createRegex = require('createRegex');
const makeString = require('makeString');

const requestMethod = getRequestMethod();
const path = getRequestPath();
Expand Down Expand Up @@ -522,7 +525,11 @@ function getEventModels(baseEventModel) {
const body = getRequestBody();

if (body) {
let bodyJson = JSON.parse(body);
const contentType = getRequestHeader('content-type');
const isFormUrlEncoded =
!!contentType &&
contentType.indexOf('application/x-www-form-urlencoded') !== -1;
let bodyJson = isFormUrlEncoded ? parseUrlEncoded(body) : JSON.parse(body);
if (bodyJson) {
const bodyType = getType(bodyJson);
const shouldUseOriginalBody =
Expand Down Expand Up @@ -586,3 +593,43 @@ function assign() {
}
return target;
}

function parseUrlEncoded(data) {
const pairs = data.split('&');
const parsedData = {};
const regex = createRegex('\\+', 'g');
for (const pair of pairs) {
const pairValue = pair.split('=');
const key = pairValue[0];
const value = pairValue[1];
const keys = key
.split('.')
.map((k) => decodeUriComponent(k.replace(regex, ' ')));

let currentObject = parsedData;

for (let i = 0; i < keys.length - 1; i++) {
const currentKey = keys[i];

if (!currentObject[currentKey]) {
const nextKey = keys[i + 1];
const nextKeyIsNumber = makeString(makeInteger(nextKey)) === nextKey;
currentObject[currentKey] = nextKeyIsNumber ? [] : {};
}

currentObject = currentObject[currentKey];
}

const lastKey = keys[keys.length - 1];
const decodedValue = decodeUriComponent(value.replace(regex, ' '));
const parsedValue = JSON.parse(decodedValue) || decodedValue;

if (getType(currentObject) === 'array') {
currentObject.push(parsedValue);
} else {
currentObject[lastKey] = parsedValue;
}
}

return parsedData;
}
54 changes: 52 additions & 2 deletions template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ const computeEffectiveTldPlusOne = require('computeEffectiveTldPlusOne');
const getRequestQueryParameter = require('getRequestQueryParameter');
const getType = require('getType');
const Promise = require('Promise');
const decodeUriComponent = require('decodeUriComponent');
const createRegex = require('createRegex');
const makeString = require('makeString');

const requestMethod = getRequestMethod();
const path = getRequestPath();
Expand Down Expand Up @@ -798,7 +801,11 @@ function getEventModels(baseEventModel) {
const body = getRequestBody();
if (body) {
let bodyJson = JSON.parse(body);
const contentType = getRequestHeader('content-type');
const isFormUrlEncoded =
!!contentType &&
contentType.indexOf('application/x-www-form-urlencoded') !== -1;
let bodyJson = isFormUrlEncoded ? parseUrlEncoded(body) : JSON.parse(body);
if (bodyJson) {
const bodyType = getType(bodyJson);
const shouldUseOriginalBody =
Expand Down Expand Up @@ -863,6 +870,46 @@ function assign() {
return target;
}

function parseUrlEncoded(data) {
const pairs = data.split('&');
const parsedData = {};
const regex = createRegex('\\+', 'g');
for (const pair of pairs) {
const pairValue = pair.split('=');
const key = pairValue[0];
const value = pairValue[1];
const keys = key
.split('.')
.map((k) => decodeUriComponent(k.replace(regex, ' ')));
let currentObject = parsedData;
for (let i = 0; i < keys.length - 1; i++) {
const currentKey = keys[i];
if (!currentObject[currentKey]) {
const nextKey = keys[i + 1];
const nextKeyIsNumber = makeString(makeInteger(nextKey)) === nextKey;
currentObject[currentKey] = nextKeyIsNumber ? [] : {};
}

currentObject = currentObject[currentKey];
}

const lastKey = keys[keys.length - 1];
const decodedValue = decodeUriComponent(value.replace(regex, ' '));
const parsedValue = JSON.parse(decodedValue) || decodedValue;

if (getType(currentObject) === 'array') {
currentObject.push(parsedValue);
} else {
currentObject[lastKey] = parsedValue;
}
}

return parsedData;
}


___SERVER_PERMISSIONS___

Expand Down Expand Up @@ -1163,7 +1210,10 @@ ___SERVER_PERMISSIONS___

___TESTS___

scenarios: []
scenarios:
- name: Quick Test
code: runCode();
setup: ''


___NOTES___
Expand Down

0 comments on commit f4d40e4

Please sign in to comment.