This repository has been archived by the owner on Dec 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: path parameters enhancement #43
Closes #43
- Loading branch information
Showing
9 changed files
with
231 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict' | ||
|
||
const marked = require('marked') | ||
const supHeaders = ['object', 'name', 'description', 'example', 'type', 'required'] | ||
|
||
function parseMdTable (md) { | ||
const parsed = marked.lexer(md) | ||
const table = parsed.find(el => el.type === 'table') | ||
if (table == null) return {} | ||
const { header, cells } = table | ||
if (!header.includes('object') || !header.includes('name')) return {} | ||
const headers = header.map(h => supHeaders.includes(h) ? h : false) | ||
const tableObj = cells.reduce((accTable, cell, i) => { | ||
const cellObj = cell.reduce((accCell, field, index) => { | ||
if (headers[index]) { | ||
accCell[headers[index]] = field | ||
} | ||
return accCell | ||
}, {}) | ||
accTable[cellObj.name] = cellObj | ||
return accTable | ||
}, {}) | ||
return tableObj | ||
} | ||
|
||
module.exports = { parseMdTable } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,7 @@ | |
"check-coverage": true | ||
}, | ||
"dependencies": { | ||
"js-yaml": "^3.14.0" | ||
"js-yaml": "^3.14.0", | ||
"marked": "^1.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict' | ||
|
||
const { describe, it } = require('mocha') | ||
const { parseMdTable } = require('../lib/md-utils') | ||
const { deepEqual } = require('assert').strict | ||
|
||
describe('MD table to Json specs', function () { | ||
const MD_WITH_ADDITIONS = '| object | name | description | required | type | example |\n' + | ||
'|--------------|------|----------|-----|-------|-----|\n' + | ||
'| path | user_id | This is just a user identifier | true | string | 476587598 |\n' + | ||
'| path | group_id | Group of the user | true | string | RETAIL |\n' + | ||
'# hola' | ||
const MD_ADDITIONAL_HEADER = '| object | name | description | required | type | additional | example |\n' + | ||
'|--------------|------|----------|-----|-------|-----|-----|\n' + | ||
'| path | user_id | This is just a user identifier | true | string | add field 1 | 476587598 |\n' + | ||
'| path | group_id | Group of the user | true | string | add field 2 | RETAIL |\n' + | ||
'# hola' | ||
const MD_NO_OBJECT = '| name | description | required | type | additional | example |\n' + | ||
'|------|----------|-----|-------|-----|-----|\n' + | ||
'| user_id | This is just a user identifier | true | string | add field 1 | 476587598 |\n' | ||
const MD_NO_NAME = '| object | description | required | type | additional | example |\n' + | ||
'|------|----------|-----|-------|-----|-----|\n' + | ||
'| user_id | This is just a user identifier | true | string | add field 1 | 476587598 |\n' | ||
const MD_MISS_COLUMN = '| object | name | description | type | additional | example |\n' + | ||
'|--------------|------|----------|-----|-------|-----|\n' + | ||
'| path | user_id | This is just a user identifier | string | add field 1 | 476587598 |\n' + | ||
'# hola' | ||
const MD_INCORRECT = '| object | name | description | required | type | example |\n' + | ||
'|--------------|------|----------|-----|-----|\n' + | ||
'| path | user_id | This is just a user identifier | true | string | 476587598 |\n' + | ||
'| path | group_id | Group of the user | true | string | RETAIL | RETAIL |\n' + | ||
'# hola' | ||
const PARSED_TABLE = { | ||
user_id: { | ||
object: 'path', | ||
name: 'user_id', | ||
description: 'This is just a user identifier', | ||
required: 'true', | ||
type: 'string', | ||
example: '476587598' | ||
}, | ||
group_id: { | ||
object: 'path', | ||
name: 'group_id', | ||
description: 'Group of the user', | ||
required: 'true', | ||
type: 'string', | ||
example: 'RETAIL' | ||
} | ||
} | ||
const PARSED_NO_REQUIRED_TABLE = { | ||
user_id: { | ||
object: 'path', | ||
name: 'user_id', | ||
description: 'This is just a user identifier', | ||
type: 'string', | ||
example: '476587598' | ||
} | ||
} | ||
|
||
it('should return a json object with parsed md table', function () { | ||
const parsed = parseMdTable(MD_WITH_ADDITIONS) | ||
deepEqual(parsed, PARSED_TABLE) | ||
}) | ||
|
||
it('should return empty if not a markdown string', function () { | ||
deepEqual(parseMdTable(''), {}) | ||
}) | ||
|
||
it('should return empty if not a table in the markdown', function () { | ||
deepEqual(parseMdTable('# headers'), {}) | ||
}) | ||
|
||
it('should not parse not allowed headers', function () { | ||
deepEqual(parseMdTable(MD_ADDITIONAL_HEADER), PARSED_TABLE) | ||
}) | ||
|
||
it('should return empty if no "name" column', function () { | ||
deepEqual(parseMdTable(MD_NO_NAME), {}) | ||
}) | ||
|
||
it('should return empty if no "object" column', function () { | ||
deepEqual(parseMdTable(MD_NO_OBJECT), {}) | ||
}) | ||
|
||
it('should not fail if column as "required" not exist in md table', function () { | ||
const parsed = parseMdTable(MD_MISS_COLUMN) | ||
deepEqual(parsed, PARSED_NO_REQUIRED_TABLE) | ||
}) | ||
|
||
it('should return empty if incorrect md table', function () { | ||
deepEqual(parseMdTable(MD_INCORRECT), {}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters