Skip to content

Commit

Permalink
feat: suppot YAML in body data (if enabled) (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominique-pfister authored Apr 17, 2024
1 parent d43261c commit 248910c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/helix-shared-body-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@
},
"optionalDependencies": {
"@adobe/helix-universal": "^4.0.0"
},
"devDependencies": {
"yaml": "2.4.1"
}
}
5 changes: 5 additions & 0 deletions packages/helix-shared-body-data/src/body-data-wrapper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export declare interface BodyDataOptions {
* Flag that coerces number strings to numbers.
*/
coerceNumber?:boolean;

/**
* Support YAML in the POST body.
*/
supportYAML?:boolean;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/helix-shared-body-data/src/body-data-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const BODY_METHODS = ['POST', 'PUT', 'PATCH'];
* available.
*
* @param {Request} request The universal request
* @param {BodyDataOptions} [opts] Options
* @param {import('./body-data-wrapper').BodyDataOptions} [opts] Options
* @returns {Promise<object>} the parsed data object.
*/
async function getData(request, opts) {
Expand All @@ -31,6 +31,11 @@ async function getData(request, opts) {
return request.json();
}

const { supportYAML } = opts;
if (supportYAML && /\/yaml/.test(contentType) && BODY_METHODS.includes(request.method)) {
return request.text();
}

let data;
if (/^application\/x-www-form-urlencoded/.test(contentType) && BODY_METHODS.includes(request.method)) {
data = new URLSearchParams(await request.text());
Expand Down
64 changes: 64 additions & 0 deletions packages/helix-shared-body-data/test/body-data-wrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

/* eslint-env mocha */
import assert from 'assert';
import YAML from 'yaml';
import { Request, Response } from '@adobe/fetch';

import wrap from '@adobe/helix-shared-wrap';
Expand Down Expand Up @@ -267,3 +268,66 @@ describe('Body Data Wrapper Unit Tests (Form Data)', () => {
assert.strictEqual(response.status, 200, 'universal function should be executed');
});
});

describe('Body Data Wrapper Unit Tests (YAML Body)', () => {
const contents = { indices: [] };

['POST', 'post', 'PUT', 'PATCH'].forEach((method) => {
it(`Loads YAML (${method})`, async () => {
const universalfunct = async (request, context) => {
const yaml = YAML.parse(context.data);
assert.deepStrictEqual(yaml, contents);
return new Response('ok');
};

const actualfunct = wrap(universalfunct).with(bodyData, { supportYAML: true });
const response = await actualfunct(new Request('http://localhost', {
body: YAML.stringify(contents),
method,
headers: {
'content-type': 'text/yaml',
},
}), {
log,
});
assert.strictEqual(response.status, 200, 'universal function should be executed');
});
});

it('Ignores body for GET requests.', async () => {
const universalfunct = async (request, context) => {
assert.deepEqual(context.data, { });
return new Response('ok');
};

const actualfunct = wrap(universalfunct).with(bodyData, { supportYAML: true });
const response = await actualfunct(new Request('http://localhost', {
method: 'GET',
headers: {
'content-type': 'text/yaml',
},
}), {
log,
});
assert.strictEqual(response.status, 200);
});

it('Ignores body when support YAML is not enabled.', async () => {
const universalfunct = async (request, context) => {
assert.deepEqual(context.data, { });
return new Response('ok');
};

const actualfunct = wrap(universalfunct).with(bodyData);
const response = await actualfunct(new Request('http://localhost', {
body: YAML.stringify(contents),
method: 'POST',
headers: {
'content-type': 'text/yaml',
},
}), {
log,
});
assert.strictEqual(response.status, 200);
});
});

0 comments on commit 248910c

Please sign in to comment.