-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pipe): add swag-ofetch js and ts gen package
- Loading branch information
Showing
11 changed files
with
329 additions
and
3 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,25 @@ | ||
{ | ||
"name": "@genapi/swag-ofetch-js", | ||
"version": "1.0.0", | ||
"main": "src/index.ts", | ||
"scripts": { | ||
"build": "ptsup -r src --dts --meta --clean", | ||
"lint": "eslint ./" | ||
}, | ||
"publishConfig": { | ||
"directory": "dist", | ||
"main": "index.js", | ||
"types": "./index.d.ts", | ||
"linkDirectory": false | ||
}, | ||
"dependencies": { | ||
"@genapi/config": "workspace:*", | ||
"@genapi/core": "*", | ||
"@genapi/pipeline": "workspace:*", | ||
"@genapi/swag-parser": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"ofetch": "^1.0.1", | ||
"openapi-specification-types": "^0.0.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { ApiPipeline } from '@genapi/config' | ||
import { replaceMainext } from '@genapi/config' | ||
import { readConfig as _readConfig } from '@genapi/pipeline' | ||
|
||
export function readConfig(config: ApiPipeline.Config): ApiPipeline.ConfigRead { | ||
config.import = config.import || {} | ||
config.output = config.output || {} | ||
config.import.http = config.import.http || 'ofetch' | ||
config.output = replaceMainext(config.output) || 'src/api/index.js' | ||
|
||
const configRead = _readConfig(config) | ||
|
||
configRead.graphs.imports.push({ | ||
name: 'ofetch', | ||
value: config.import.http, | ||
}) | ||
|
||
return configRead | ||
} |
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,20 @@ | ||
import { compiler, dest, generate, original } from '@genapi/pipeline' | ||
import type { ApiPipeline } from '@genapi/config' | ||
import { pipeline } from '@genapi/core' | ||
import { readConfig } from './config' | ||
import { parser } from './parser' | ||
|
||
function OpenAPI2Typescript(config: ApiPipeline.Config) { | ||
const process = pipeline( | ||
config => readConfig(config), | ||
configRead => original(configRead), | ||
configRead => parser(configRead), | ||
configRead => compiler(configRead), | ||
configRead => generate(configRead), | ||
configRead => dest(configRead), | ||
) | ||
return process(config) | ||
} | ||
export { readConfig, original, parser, compiler, generate, dest } | ||
|
||
export default OpenAPI2Typescript |
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,97 @@ | ||
import type { ApiPipeline, StatementFunction, StatementInterface } from '@genapi/config' | ||
import type { Paths } from 'openapi-specification-types' | ||
import { | ||
literalFieldsToString, | ||
parseHeaderCommits, | ||
parseMethodMetadata, | ||
parseMethodParameters, | ||
parseOpenAPISpecification32, | ||
transformBaseURL, | ||
transformDefinitions, | ||
transformParameters, | ||
transformQueryParams, | ||
transformUrlSyntax, | ||
traversePaths, | ||
} from '@genapi/swag-parser' | ||
|
||
export interface PathsTransformOptions { | ||
configRead: ApiPipeline.ConfigRead | ||
interfaces: StatementInterface[] | ||
functions: StatementFunction[] | ||
} | ||
|
||
export function parser(configRead: ApiPipeline.ConfigRead) { | ||
const source = parseOpenAPISpecification32(configRead.source) | ||
|
||
const comments = parseHeaderCommits(source) | ||
|
||
const interfaces: StatementInterface[] = [] | ||
const functions: StatementFunction[] = [] | ||
|
||
transformBaseURL(source, { | ||
configRead, | ||
}) | ||
|
||
transformDefinitions(source.definitions, { | ||
interfaces, | ||
}) | ||
|
||
transformPaths(source.paths, { | ||
configRead, | ||
functions, | ||
interfaces, | ||
}) | ||
configRead.graphs.comments = comments | ||
configRead.graphs.functions = functions | ||
configRead.graphs.interfaces = interfaces | ||
|
||
return configRead | ||
} | ||
|
||
export function transformPaths(paths: Paths, { configRead, functions, interfaces }: PathsTransformOptions) { | ||
traversePaths(paths, (config) => { | ||
/** | ||
* function params/function options/function use interfaces | ||
*/ | ||
const { parameters, interfaces: attachInters, options } = parseMethodParameters(config, { | ||
formData: 'body', | ||
query: 'params', | ||
}) | ||
|
||
let { name, description, url, responseType, body } = parseMethodMetadata(config) | ||
|
||
interfaces.push(...attachInters) | ||
parameters.push({ | ||
name: 'options', | ||
type: 'import(\'ofetch\').FetchOptions', | ||
required: false, | ||
}) | ||
options.push(['...', 'options']) | ||
options.unshift(['method', `"${config.method}"`]) | ||
if (configRead.config.baseURL) | ||
options.unshift('baseURL') | ||
|
||
transformParameters(parameters, { | ||
syntax: 'ecmascript', | ||
configRead, | ||
description, | ||
interfaces, | ||
responseType, | ||
}) | ||
|
||
url = transformQueryParams('query', { body, options, url }) | ||
url = transformUrlSyntax(url, { baseURL: configRead.config.baseURL }) | ||
|
||
functions.push({ | ||
export: true, | ||
async: true, | ||
name, | ||
description, | ||
parameters, | ||
body: [ | ||
...body, | ||
`return ofetch(${url}, {${literalFieldsToString(options)}})`, | ||
], | ||
}) | ||
}) | ||
} |
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,24 @@ | ||
{ | ||
"name": "@genapi/swag-ofetch-ts", | ||
"version": "1.0.0", | ||
"main": "src/index.ts", | ||
"scripts": { | ||
"build": "ptsup -r src --dts --meta --clean", | ||
"lint": "eslint ./" | ||
}, | ||
"publishConfig": { | ||
"directory": "dist", | ||
"main": "index.js", | ||
"types": "./index.d.ts", | ||
"linkDirectory": false | ||
}, | ||
"dependencies": { | ||
"@genapi/config": "workspace:*", | ||
"@genapi/core": "*", | ||
"@genapi/pipeline": "workspace:*", | ||
"@genapi/swag-parser": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"openapi-specification-types": "^0.0.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { ApiPipeline } from '@genapi/config' | ||
import { readConfig as _readConfig } from '@genapi/pipeline' | ||
|
||
export function readConfig(config: ApiPipeline.Config): ApiPipeline.ConfigRead { | ||
config.import = config.import || {} | ||
config.import.http = config.import.http || 'ofetch' | ||
|
||
const configRead = _readConfig(config) | ||
|
||
configRead.graphs.imports.push({ | ||
name: 'ofetch', | ||
names: config.import.http === 'ofetch' ? ['FetchOptions'] : undefined, | ||
value: config.import.http, | ||
}) | ||
|
||
if (config.import.http !== 'ofetch') { | ||
configRead.graphs.imports.push({ | ||
names: ['FetchOptions'], | ||
value: 'ofetch', | ||
}) | ||
} | ||
|
||
return configRead | ||
} |
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,20 @@ | ||
import { compiler, dest, generate, original } from '@genapi/pipeline' | ||
import type { ApiPipeline } from '@genapi/config' | ||
import { pipeline } from '@genapi/core' | ||
import { readConfig } from './config' | ||
import { parser } from './parser' | ||
|
||
function OpenAPI2Typescript(config: ApiPipeline.Config) { | ||
const process = pipeline( | ||
config => readConfig(config), | ||
configRead => original(configRead), | ||
configRead => parser(configRead), | ||
configRead => compiler(configRead), | ||
configRead => generate(configRead), | ||
configRead => dest(configRead), | ||
) | ||
return process(config) | ||
} | ||
export { readConfig, original, parser, compiler, generate, dest } | ||
|
||
export default OpenAPI2Typescript |
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,96 @@ | ||
import type { ApiPipeline, StatementFunction, StatementInterface } from '@genapi/config' | ||
import type { Paths } from 'openapi-specification-types' | ||
import { | ||
literalFieldsToString, | ||
parseHeaderCommits, | ||
parseMethodMetadata, | ||
parseMethodParameters, | ||
parseOpenAPISpecification32, | ||
transformBaseURL, | ||
transformDefinitions, | ||
transformParameters, | ||
transformQueryParams, | ||
transformUrlSyntax, | ||
traversePaths, | ||
} from '@genapi/swag-parser' | ||
|
||
export interface PathsTransformOptions { | ||
configRead: ApiPipeline.ConfigRead | ||
interfaces: StatementInterface[] | ||
functions: StatementFunction[] | ||
} | ||
|
||
export function parser(configRead: ApiPipeline.ConfigRead) { | ||
const source = parseOpenAPISpecification32(configRead.source) | ||
|
||
const comments = parseHeaderCommits(source) | ||
|
||
const interfaces: StatementInterface[] = [] | ||
const functions: StatementFunction[] = [] | ||
|
||
transformBaseURL(source, { | ||
configRead, | ||
}) | ||
|
||
transformDefinitions(source.definitions, { | ||
interfaces, | ||
}) | ||
|
||
transformPaths(source.paths, { | ||
configRead, | ||
functions, | ||
interfaces, | ||
}) | ||
|
||
configRead.graphs.comments = comments | ||
configRead.graphs.functions = functions | ||
configRead.graphs.interfaces = interfaces | ||
|
||
return configRead | ||
} | ||
|
||
export function transformPaths(paths: Paths, { configRead, functions, interfaces }: PathsTransformOptions) { | ||
traversePaths(paths, (config) => { | ||
/** | ||
* function params/function options/function use interfaces | ||
*/ | ||
const { parameters, interfaces: attachInters, options } = parseMethodParameters(config, { | ||
formData: 'body', | ||
query: 'params', | ||
}) | ||
|
||
let { name, description, url, responseType, body } = parseMethodMetadata(config) | ||
|
||
interfaces.push(...attachInters) | ||
parameters.push({ | ||
name: 'options', | ||
type: 'FetchOptions', | ||
required: false, | ||
}) | ||
options.push(['...', 'options']) | ||
options.unshift(['method', `"${config.method}"`]) | ||
if (configRead.config.baseURL) | ||
options.unshift('baseURL') | ||
|
||
const { spaceResponseType } = transformParameters(parameters, { | ||
syntax: 'typescript', | ||
configRead, | ||
description, | ||
interfaces, | ||
responseType, | ||
}) | ||
|
||
url = transformQueryParams('query', { body, options, url }) | ||
url = transformUrlSyntax(url) | ||
|
||
functions.push({ | ||
export: true, | ||
name, | ||
description, | ||
parameters, | ||
body: [ | ||
`return ofetch<${spaceResponseType}>(${url}, {${literalFieldsToString(options)}})`, | ||
], | ||
}) | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
require('esbuild-register') | ||
require('@genapi/cli/src/bin/index.ts') |
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