This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP refactor: generate openapi directly instead of legacy format
- Loading branch information
Derek
committed
Aug 5, 2019
1 parent
64f9e63
commit 8f49c51
Showing
14 changed files
with
395 additions
and
226 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module.exports = addCodeSamples | ||
|
||
const urlTemplate = require('url-template') | ||
const { stringify } = require('javascript-stringify') | ||
|
||
// TODO: find a better place to define parameter examples | ||
const PARAMETER_EXAMPLES = { | ||
owner: 'octocat', | ||
repo: 'hello-world', | ||
issue_number: 1, | ||
title: 'title' | ||
} | ||
|
||
function addCodeSamples (state) { | ||
const { scope, baseUrl } = state | ||
|
||
state.routes.forEach(route => { | ||
const codeSampleParams = { route, scope, baseUrl } | ||
route.operation['x-code-samples'] = route.operation['x-code-samples'].concat( | ||
{ lang: 'Shell', source: toShellExample(codeSampleParams) }, | ||
{ lang: 'JS', source: toJsExample(codeSampleParams) } | ||
) | ||
}) | ||
} | ||
|
||
function toShellExample ({ route, scope, baseUrl }) { | ||
const path = urlTemplate.parse(route.path.replace(/:(\w+)/, '{$1}')).expand(PARAMETER_EXAMPLES) | ||
const params = route.operation.parameters | ||
.filter(param => !param.deprecated) | ||
.filter(param => param.location === 'body') | ||
.filter(param => param.required) | ||
.reduce((params, param) => Object.assign(params, { | ||
[param.name]: PARAMETER_EXAMPLES[param.name] || param.name | ||
}), {}) | ||
|
||
const method = route.method.toUpperCase() | ||
const defaultAcceptHeader = route.operation.parameters[0].schema.default | ||
|
||
const args = [ | ||
method !== 'GET' && `-X${method}`, | ||
`-H"Accept: ${defaultAcceptHeader}"`, | ||
new URL(path, baseUrl).href, | ||
Object.keys(params).length && `-d '${JSON.stringify(params)}'` | ||
].filter(Boolean) | ||
return `curl \\\n ${args.join(' \\\n ')}` | ||
} | ||
|
||
function toJsExample ({ route, scope, baseUrl }) { | ||
const params = route.operation.parameters | ||
.filter(param => !param.deprecated) | ||
.filter(param => param.in !== 'header') | ||
.filter(param => param.required) | ||
.reduce((params, param) => Object.assign(params, { | ||
[param.name]: PARAMETER_EXAMPLES[param.name] || param.name | ||
}), {}) | ||
|
||
return `octokit.${scope}.get(${Object.keys(params).length ? stringify(params, null, 2) : ''})` | ||
} |
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,28 @@ | ||
module.exports = findAcceptHeader | ||
|
||
function findAcceptHeader (state) { | ||
const previewBlocks = state.blocks.filter(block => block.type === 'preview') | ||
|
||
state.routes.forEach(route => { | ||
// TODO: handle optional preview blocks | ||
const requiredPreviewHeaderBlocks = previewBlocks.filter(block => block.required) | ||
const defaultAcceptHeader = requiredPreviewHeaderBlocks.length | ||
? requiredPreviewHeaderBlocks.map(block => `application/vnd.github.${block.preview}-preview+json`).join(',') | ||
: 'application/vnd.github.v3+json' | ||
const acceptHeaderParam = { | ||
name: 'accept', | ||
description: requiredPreviewHeaderBlocks.length ? 'This API is under preview and subject to change.' : 'Setting to `application/vnd.github.v3+json` is recommended', | ||
in: 'header', | ||
schema: { | ||
type: 'string', | ||
default: defaultAcceptHeader | ||
} | ||
} | ||
|
||
if (requiredPreviewHeaderBlocks.length) { | ||
acceptHeaderParam.required = true | ||
} | ||
|
||
route.operation.parameters.unshift(acceptHeaderParam) | ||
}) | ||
} |
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
Oops, something went wrong.