Skip to content

Commit 248910c

Browse files
feat: suppot YAML in body data (if enabled) (#929)
1 parent d43261c commit 248910c

5 files changed

Lines changed: 81 additions & 1 deletion

File tree

package-lock.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/helix-shared-body-data/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@
3636
},
3737
"optionalDependencies": {
3838
"@adobe/helix-universal": "^4.0.0"
39+
},
40+
"devDependencies": {
41+
"yaml": "2.4.1"
3942
}
4043
}

packages/helix-shared-body-data/src/body-data-wrapper.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export declare interface BodyDataOptions {
3131
* Flag that coerces number strings to numbers.
3232
*/
3333
coerceNumber?:boolean;
34+
35+
/**
36+
* Support YAML in the POST body.
37+
*/
38+
supportYAML?:boolean;
3439
}
3540

3641
/**

packages/helix-shared-body-data/src/body-data-wrapper.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const BODY_METHODS = ['POST', 'PUT', 'PATCH'];
2222
* available.
2323
*
2424
* @param {Request} request The universal request
25-
* @param {BodyDataOptions} [opts] Options
25+
* @param {import('./body-data-wrapper').BodyDataOptions} [opts] Options
2626
* @returns {Promise<object>} the parsed data object.
2727
*/
2828
async function getData(request, opts) {
@@ -31,6 +31,11 @@ async function getData(request, opts) {
3131
return request.json();
3232
}
3333

34+
const { supportYAML } = opts;
35+
if (supportYAML && /\/yaml/.test(contentType) && BODY_METHODS.includes(request.method)) {
36+
return request.text();
37+
}
38+
3439
let data;
3540
if (/^application\/x-www-form-urlencoded/.test(contentType) && BODY_METHODS.includes(request.method)) {
3641
data = new URLSearchParams(await request.text());

packages/helix-shared-body-data/test/body-data-wrapper.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
/* eslint-env mocha */
1616
import assert from 'assert';
17+
import YAML from 'yaml';
1718
import { Request, Response } from '@adobe/fetch';
1819

1920
import wrap from '@adobe/helix-shared-wrap';
@@ -267,3 +268,66 @@ describe('Body Data Wrapper Unit Tests (Form Data)', () => {
267268
assert.strictEqual(response.status, 200, 'universal function should be executed');
268269
});
269270
});
271+
272+
describe('Body Data Wrapper Unit Tests (YAML Body)', () => {
273+
const contents = { indices: [] };
274+
275+
['POST', 'post', 'PUT', 'PATCH'].forEach((method) => {
276+
it(`Loads YAML (${method})`, async () => {
277+
const universalfunct = async (request, context) => {
278+
const yaml = YAML.parse(context.data);
279+
assert.deepStrictEqual(yaml, contents);
280+
return new Response('ok');
281+
};
282+
283+
const actualfunct = wrap(universalfunct).with(bodyData, { supportYAML: true });
284+
const response = await actualfunct(new Request('http://localhost', {
285+
body: YAML.stringify(contents),
286+
method,
287+
headers: {
288+
'content-type': 'text/yaml',
289+
},
290+
}), {
291+
log,
292+
});
293+
assert.strictEqual(response.status, 200, 'universal function should be executed');
294+
});
295+
});
296+
297+
it('Ignores body for GET requests.', async () => {
298+
const universalfunct = async (request, context) => {
299+
assert.deepEqual(context.data, { });
300+
return new Response('ok');
301+
};
302+
303+
const actualfunct = wrap(universalfunct).with(bodyData, { supportYAML: true });
304+
const response = await actualfunct(new Request('http://localhost', {
305+
method: 'GET',
306+
headers: {
307+
'content-type': 'text/yaml',
308+
},
309+
}), {
310+
log,
311+
});
312+
assert.strictEqual(response.status, 200);
313+
});
314+
315+
it('Ignores body when support YAML is not enabled.', async () => {
316+
const universalfunct = async (request, context) => {
317+
assert.deepEqual(context.data, { });
318+
return new Response('ok');
319+
};
320+
321+
const actualfunct = wrap(universalfunct).with(bodyData);
322+
const response = await actualfunct(new Request('http://localhost', {
323+
body: YAML.stringify(contents),
324+
method: 'POST',
325+
headers: {
326+
'content-type': 'text/yaml',
327+
},
328+
}), {
329+
log,
330+
});
331+
assert.strictEqual(response.status, 200);
332+
});
333+
});

0 commit comments

Comments
 (0)