Skip to content

Commit

Permalink
feat: adding support for cookies in snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion committed Mar 22, 2022
1 parent 44a6f11 commit 6ebb613
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/httpsnippet-client-api/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.nyc_output/
node_modules/
test/__datasets__/
test/__datasets__/**/output.js
2 changes: 1 addition & 1 deletion packages/httpsnippet-client-api/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.nyc_output/
test/__datasets__/
test/__datasets__/**/output.js
3 changes: 2 additions & 1 deletion packages/httpsnippet-client-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"lint": "eslint .",
"pretest": "npm run lint",
"prettier": "prettier --list-different --write \"./**/**.js\"",
"test": "nyc mocha \"test/**/*.test.js\""
"test": "nyc mocha \"test/**/*.test.js\"",
"test:watch": "nyc mocha \"test/**/*.test.js\" --watch"
},
"repository": {
"type": "git",
Expand Down
32 changes: 22 additions & 10 deletions packages/httpsnippet-client-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,31 @@ module.exports = function (source, options) {
}

let metadata = {};
if (Object.keys(source.queryObj).length) {
const queryParams = source.queryObj;
Object.keys(source.queryObj).forEach(param => {
if (authSources.query.includes(param)) {
authData.push(buildAuthSnippet(source.queryObj[param]));

Object.keys(queryParams).forEach(param => {
if (authSources.query.includes(param)) {
authData.push(buildAuthSnippet(queryParams[param]));
// If this query param is part of an auth source then we don't want it doubled up in the
// snippet.
return;
}

delete queryParams[param];
}
});
metadata[param] = source.queryObj[param];
});

metadata = Object.assign(metadata, queryParams);
}
Object.keys(source.cookiesObj).forEach(cookie => {
if (authSources.cookie.includes(cookie)) {
authData.push(buildAuthSnippet(source.cookiesObj[cookie]));

// If this cookie is part of an auth source then we don't want it doubled up.
return;
}

// Note that we may have the potential to overlap any cookie that also shares the name as
// another metadata parameter. This problem is currently inherent to `api` and not this
// snippet generator.
metadata[cookie] = source.cookiesObj[cookie];
});

// If we have path parameters present, we should only add them in if we have an `operationId` as
// we don't want metadata to duplicate what we'll be setting the path in the snippet to.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"log": {
"entries": [
{
"request": {
"cookies": [
{ "name": "api_key", "value": "buster" }
],
"httpVersion": "HTTP/1.1",
"method": "POST",
"url": "https://httpbin.org/apiKey"
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@readme/oas-examples/3.0/json/security.json');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const sdk = require('api')('https://example.com/openapi.json');

sdk.auth('buster');
sdk.post('/apiKey')
.then(res => console.log(res))
.catch(err => console.error(err));
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const sdk = require('api')('https://example.com/openapi.json');

sdk.post('/anything')
sdk.post('/anything', {bar: 'baz', foo: 'bar'})
.then(res => console.log(res))
.catch(err => console.error(err));
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
],
"cookies": [
{
"name": "foo",
"name": "foo-cookie",
"value": "bar"
},
{
"name": "bar",
"name": "bar-cookie",
"value": "baz"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ sdk.post('/anything', {
foo: ['bar', 'baz'],
baz: 'abc',
key: 'value',
'bar-cookie': 'baz',
'foo-cookie': 'bar',
accept: 'application/json'
})
.then(res => console.log(res))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
],
"cookies": [
{
"name": "foo",
"name": "foo-cookie",
"value": "bar"
},
{
"name": "bar",
"name": "bar-cookie",
"value": "baz"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ sdk.post('/anything', {foo: 'bar'}, {
foo: ['bar', 'baz'],
baz: 'abc',
key: 'value',
'bar-cookie': 'baz',
'foo-cookie': 'bar',
accept: 'application/json'
})
.then(res => console.log(res))
Expand Down
5 changes: 0 additions & 5 deletions packages/httpsnippet-client-api/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ describe('httpsnippet-client-api', function () {
describe('snippets', function () {
SNIPPETS.forEach(snippet => {
it(`should generate \`${snippet}\` snippet`, async function () {
// Cookies test needs to get built out.
if (snippet === 'cookies') {
this.skip();
}

const [har, definition] = await getSnippetDataset(snippet);
const expected = await fs.readFile(path.join(DATASETS_DIR, snippet, 'output.js'), 'utf-8');

Expand Down

0 comments on commit 6ebb613

Please sign in to comment.