Skip to content

Commit

Permalink
feat: action input parameter support (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmichalina authored Jan 29, 2019
1 parent 0b77949 commit c4a1654
Show file tree
Hide file tree
Showing 29 changed files with 445 additions and 1,763 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"minify": "uglifyjs dist/onvif-rx-umd.js -o dist/onvif-rx-umd.min.js --source-map -c -m",
"rollup": "rollup -c",
"watch": "rollup -cw",
"gen.api": "ts-node scripts/api/ast.ts"
"gen.api": "rm -rf src/api && ts-node scripts/api/ast.ts"
},
"dependencies": {
"js-sha1": "^0.6.0",
Expand Down
131 changes: 12 additions & 119 deletions scripts/api/ast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Project, Scope, FunctionLikeDeclaration } from 'ts-simple-ast'
import { Project, Scope, FunctionLikeDeclaration, ParameterDeclarationStructure } from 'ts-simple-ast'
import { generateActions, generateTypes } from './exec'

// initialize
Expand Down Expand Up @@ -84,19 +84,10 @@ generateTypes()
})

actionTree.forEach(group => {
// index file for each group
// project.createSourceFile(`src/api/${group.type}/index.ts`, {
// exports: group.actions.map(a => {
// return {
// moduleSpecifier: `./${a.actionName}`
// }
// })
// })

project.createSourceFile(`src/api/${group.type.toLowerCase()}.ts`, {
imports: [{
moduleSpecifier: '../soap/request',
namedImports: ['createStandardRequestBodyFromString', 'mapResponseXmlToJson', 'mapResponseObsToProperty'].map(name => ({ name }))
namedImports: ['createStandardRequestBodyFromString', 'mapResponseXmlToJson', 'generateRequestElements', 'mapResponseObsToProperty'].map(name => ({ name }))
},
{
moduleSpecifier: '../config',
Expand Down Expand Up @@ -125,15 +116,17 @@ generateTypes()
isStatic: true,
docs: [{ description: action.documentation.replace(/\*/g, '') }],
name: action.actionName,
bodyText: `return createStandardRequestBodyFromString('<${action.soapRequestNode} />')
bodyText: `return createStandardRequestBodyFromString(generateRequestElements('${action.soapRequestNode}')([${action.input.parameters.map(a => `'${a.name}'`).join(',')}])(${action.input.parameters.map(a => a.name).join(',')}))
.map(mapResponseXmlToJson<any>('${action.output.ref}')())
`,
parameters: action.input.parameters.map(p => {
return {
name: p.name,
type: p.propertyType
}
})
parameters: action.input.parameters
.map<ParameterDeclarationStructure>(p => {
return {
name: p.name,
type: p.propertyType,
hasQuestionToken: p.propertyMinOccurs === '0'
}
})
}
}),
...group.actions.map(action => {
Expand All @@ -148,109 +141,9 @@ generateTypes()
}
})
}
})


]
// namespaces: [{
// isExported: true,
// name: group.type,
// functions: group.actions.map(action => {
// return {
// isExported: true,
// docs: [{ description: action.documentation.replace(/\*/g, '') }],
// name: action.actionName,
// bodyText: `return createStandardRequestBodyFromString('<${action.soapRequestNode} />')
// .map(mapResponseXmlToJson<any>('${action.output.ref}')())
// `,
// parameters: action.input.parameters.map(p => {
// return {
// name: p.name,
// type: p.propertyType
// }
// })
// }
// })
// {
// isExported: true,
// name: 'ManagedApi',
// parameters: [{
// name: 'config',
// type: 'IDeviceConfig'
// }],
// functions: [{
// name: 'inner',
// }]
// }
// classes: [
// {
// name: 'Api',
// isExported: true,
// ctors: [{
// parameters: [{
// name: 'config',
// type: 'IDeviceConfig'
// }]
// }],
// // properties: [{
// // isReadonly: true,
// // name: 'test1',
// // initializer: '() => console.log()',
// // }],
// methods: [{
// name: 'test2',
// parameters: [{
// name: 'param1',
// type: 'any'
// }],
// bodyText: ``
// }]
// }
// ]
})]
}]
})
})
return project.save()
})



// const myClassFile = project.createSourceFile('src/MyClass.ts', {
// classes: [{

// }]
// })
// const myEnumFile = project.createSourceFile('src/MyEnum.ts', {
// enums: [{
// name: 'MyEnum',
// isExported: true,
// members: [{ name: 'member' }]
// }]
// })

// // get information from ast
// const myClass = myClassFile.getClassOrThrow('MyClass')
// myClass.getName() // returns: "MyClass"
// myClass.hasExportKeyword() // returns: true
// myClass.isDefaultExport() // returns: false

// // manipulate ast
// const myInterface = myClassFile.addInterface({
// name: 'IMyInterface',
// isExported: true,
// properties: [{
// name: 'myProp',
// type: 'number'
// }]
// })

// myClass.rename('NewName')
// myClass.addImplements(myInterface.getName())
// myClass.addProperty({
// name: 'myProp',
// initializer: '5'
// })

// project.getSourceFileOrThrow('src/ExistingFile.ts').delete()
// get underlying compiler node from the typescript AST from any node
// const compilerNode = myClassFile.compilerNode
5 changes: 3 additions & 2 deletions scripts/api/parse-onvif-wsdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,18 @@ export const parseOnvifWsdlDocument = (xmlDoc: Document) => {
.map(paramNode => {
const name = paramNode.getAttribute('name') as string
const propertyType = maybe(paramNode.getAttribute('type')).flatMapAuto(a => a.split(':').pop()).map(typeConvert).valueOr('')
const propertyMinOccurs = paramNode.getAttribute('minOccurs') as string
const propertyMinOccurs = paramNode.getAttribute('minOccurs') || '1' as string
const propertyMaxOccurs = paramNode.getAttribute('maxOccurs') as string
const documentation = maybe(paramNode.getElementsByTagNameNS(xmlSchemaNs, 'documentation').item(0)).map(a => a.textContent).valueOr('') as string

return {
name,
propertyType,
propertyMinOccurs,
propertyMaxOccurs,
documentation
}
})
}).sort((a, b) => parseInt(b.propertyMinOccurs) - parseInt(a.propertyMinOccurs))
}).valueOr([])

const input = typeRef('input')
Expand Down
Loading

0 comments on commit c4a1654

Please sign in to comment.