Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Node-TS",
"type": "node",
"request": "launch",
"runtimeExecutable": "ts-node",
"runtimeArgs": ["--esm", "--experimentalSpecifierResolution=node"],

"args": ["./test/index.ts"],
"base"
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": ["<node_internals>/**", "node_modules/**"]
}
]
}
103 changes: 103 additions & 0 deletions base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Propertie, UnionYapiType } from "yapi"

const typeMap = {
'string': 'string',
'boolean': 'boolean',
'integer': 'number',
'number': 'number',
'object': 'object',
'array': 'array'
}
// let res = ''
// const defineTypes:string[] = []
const defineTypeNames: string[] = []

export interface BaseLeaf {
name: string,
type: UnionYapiType | 'unknown',
isArray: boolean,
require?: boolean,
description?: string,
children?: BaseLeaf[],
parent?: BaseLeaf,
}
export class ApiTree {
tree:BaseLeaf[] = []
twig:BaseLeaf[] = []
}
interface IToBaseLeaf {
key: string,
propertie: Propertie,
apiTree: ApiTree,
parent?: BaseLeaf
}
export const toBaseLeaf = ({
key,
propertie,
apiTree,
parent
}:IToBaseLeaf) => {
const leaf: BaseLeaf = {
name: key,
type: propertie.type,
description: propertie.description,
isArray: false,
parent
}
if (propertie.type === 'object') {
leaf.children = []
for (const childKey in propertie.properties) {
if (Object.prototype.hasOwnProperty.call(propertie.properties, childKey)) {
leaf.children.push(toBaseLeaf({
key:childKey,
propertie:propertie.properties[childKey],
apiTree,
parent:leaf
}))
}
}
apiTree.twig.push(leaf)
}
if (propertie.type === 'array') {
leaf.isArray = true
if (propertie.items.type === 'object') {
leaf.children = []
for (const childKey in propertie.items.properties) {
if (Object.prototype.hasOwnProperty.call(propertie.items.properties, childKey)) {
leaf.children.push(toBaseLeaf({
key:childKey,
propertie:propertie.items.properties[childKey],
apiTree,
parent:leaf
}))
}
}
apiTree.twig.push(leaf)
} else {
leaf.type = propertie.items.type
}
}
leaf.type = toTypeName(leaf)
apiTree.tree.push(leaf)
return leaf
}
const nameFactory = (value: string, parent?: BaseLeaf, isPrimary?: boolean) => {
const name = value[0].toLocaleUpperCase() + value.slice(1, value.length)
if (defineTypeNames.includes(name) && parent && isPrimary) {
const fitableName = nameFactory(`${parent.name}${name}`, parent.parent)
defineTypeNames.push(fitableName)
return fitableName
} else {
defineTypeNames.push(name)
return name
}
}
const toTypeName = (leaf: BaseLeaf) => {
if (leaf.type === 'array' && leaf.children) {
return nameFactory(leaf.name, leaf.parent, true)
} else if (leaf.type === 'object') {
return nameFactory(leaf.name, leaf.parent, true)
} else {
return typeMap[leaf.type]
}
}
49 changes: 49 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// define
import { ApiTree, BaseLeaf, toBaseLeaf } from './base';
import { Propertie } from './yapi';

const middleWare = {}
const defaultConfig = {
typeMode: false
}

const transformer = (twig: BaseLeaf[],config?:typeof defaultConfig) => {
const _conf = {
...defaultConfig,
...config
}
let res:string[] = []
for (let i = 0; i < twig.length; i++) {
const currentLeaf = twig[i]
let text =
_conf.typeMode?`type ${currentLeaf.type} = {\n`:`interface ${currentLeaf.type} {\n`
currentLeaf.children?.forEach(item=>{
if(item.description){
text += ` /** ${item.description} */\n`
}
text += ` ${item.name}: ${item.type}${item.isArray?'[]':''};\n`
})
text += '}'
res.push(text)
}
return res;
}
const runner = (name: string, propertie: Propertie, conf?: typeof defaultConfig) => {
const apiTree = new ApiTree()
toBaseLeaf({
key: name,
propertie,
apiTree
})
const res = transformer(apiTree.twig, conf)
const dd = res.join('\n')
return dd
}
const use = (fn)=>{

}

export {
runner,
use
}
143 changes: 143 additions & 0 deletions package-lock.json

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

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "yapi-tool",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "rollup -c --bundleConfigAsCjs"
},
"author": "",
"license": "ISC",
"dependencies": {
"clipboardy": "^3.0.0"
},
"type": "module",
"devDependencies": {
"@types/node": "^18.11.0",
"@types/tampermonkey": "^4.0.5",
"rollup": "^3.2.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-typescript": "^1.0.1",
"ts-node": "^10.9.1",
"tslib": "^2.4.0",
"typescript": "^4.8.4"
}
}
24 changes: 24 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import typescript from "rollup-plugin-typescript";
import sourceMaps from "rollup-plugin-sourcemaps";
import commonjs from 'rollup-plugin-commonjs'
export default {
input: "./tools/tampermonkey.ts",
output: [
{
format: "cjs",
file: "dist/bundle.cjs.js",
sourcemap: true
},
{
format: "es",
file: "dist/bundle.esm.js"
}
],
plugins: [
commonjs({
include: /node_modules/
}),
typescript(),
sourceMaps()
],
};
4 changes: 4 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { runner } from '../';
import resource from './resource';

runner('goodDetail', resource)
Loading