Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate plugin schema #277

Merged
merged 5 commits into from
Nov 24, 2020
Merged
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
6 changes: 2 additions & 4 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ examples
build/**/*
docs
report
packages/daf-rest/src/openApiSchema.ts
.vscode/settings.json
lerna.json
CHANGELOG.md
*.json
*.md
11 changes: 8 additions & 3 deletions docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ expect(doc).toEqual({
id: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190',
publicKey: [
{
id: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190#owner',
id: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190#controller',
type: 'Secp256k1VerificationKey2018',
owner: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190',
controller: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190',
ethereumAddress: '0xb09b66026ba5909a7cfe99b76875431d2b8d5190'
}
],
authentication: [
{
type: 'Secp256k1SignatureAuthentication2018',
publicKey: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190#owner'
publicKey: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190#controller'
}
]
})
Expand Down Expand Up @@ -227,6 +227,11 @@ Creates a Verifiable Presentation. The payload, signer and format are chosen bas
## [ISelectiveDisclosure](./api/daf-selective-disclosure.iselectivedisclosure.md)


### [createProfilePresentation](./api/daf-selective-disclosure.iselectivedisclosure.createprofilepresentation.md)




### [createSelectiveDisclosureRequest](./api/daf-selective-disclosure.iselectivedisclosure.createselectivedisclosurerequest.md)


Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "daf",
"private": true,
"scripts": {
"build": "yarn lerna run build && yarn extract-api && yarn generate-schema && yarn docs:api && yarn docs:reference && yarn docs:clean-temp && yarn prettier:examples",
"build": "yarn lerna run build && yarn extract-api && yarn generate-plugin-schema && yarn docs:api && yarn docs:examples && yarn docs:clean-temp && yarn prettier:examples",
"publish": "lerna publish",
"bootstrap": "lerna bootstrap",
"extract-api": "lerna run extract-api",
"generate-schema": "lerna run generate-schema",
"docs:examples": "ts-node --project packages/tsconfig.settings.json ./scripts/generate-schemas.ts",
"generate-plugin-schema": "lerna run generate-plugin-schema",
"docs:examples": "ts-node --project packages/tsconfig.settings.json ./scripts/extract-examples.ts",
"docs:api": "ts-node ./scripts/generate-docs.ts",
"docs:reference": "api-documenter markdown -i ./temp -o ./docs/api",
"docs:clean-temp": "rm -rf ./temp",
Expand Down
7 changes: 6 additions & 1 deletion packages/daf-cli/src/lib/objectCreator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { set, get } from 'jsonpointer'
import parse from 'url-parse'
const { resolve } = require('path')

export function createObjects(config: object, pointers: Record<string, string>): any {
const objects = {}
Expand Down Expand Up @@ -39,12 +40,16 @@ export function createObjects(config: object, pointers: Record<string, string>):
let object
// console.log('Requiring', objectConfig['$require'])
const parsed = parse(objectConfig['$require'], {}, true)
const module = parsed.pathname
let module = parsed.pathname
const member = parsed.hash.length > 1 ? parsed.hash.slice(1) : 'default'
const type = parsed.query['t'] || 'class'
const args = objectConfig['$args']
// console.log({module, member, type, query: parsed.query})

if (module.slice(0, 2) === './') {
module = resolve(module)
}

const resolvedArgs = args !== undefined ? resolveRefs(args) : []
let required = require(module)[member]
if (type === 'class') {
Expand Down
75 changes: 41 additions & 34 deletions packages/daf-cli/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getAgent } from './setup'
import program from 'commander'
const fs = require('fs')
import { resolve, dirname } from "path"
import { resolve, dirname } from 'path'
import { writeFileSync, readFileSync } from 'fs'
import * as TJS from "ts-json-schema-generator"
import { JSONSchema7 } from "json-schema"
import * as TJS from 'ts-json-schema-generator'
import { JSONSchema7 } from 'json-schema'
import { OpenAPIV3 } from 'openapi-types'
import { Extractor, ExtractorConfig, ExtractorResult } from '@microsoft/api-extractor'
import {
Expand All @@ -15,6 +15,7 @@ import {
ApiReturnTypeMixin,
ApiMethodSignature,
} from '@microsoft/api-extractor-model'
import { IIdentity } from 'daf-core'

interface Method {
packageName: string
Expand Down Expand Up @@ -83,19 +84,36 @@ function getReference(response: string): OpenAPIV3.ReferenceObject | OpenAPIV3.S
}

program
.command('generatePluginSchema')
.description('Generate DAF plugin schema')
.option('-p, --packageConfig <string>', 'package.json file containing DAF plugin interface config', './package.json')
.option('-o, --outputFolder <string>', 'Generated schema typescript file', './src/schemas')
.command('generate-plugin-schema')
.description('Generate plugin schema')
.option('-c, --extractorConfig <string>', 'API Extractor config file', './api-extractor.json')
.option(
'-p, --packageConfig <string>',
'package.json file containing DAF plugin interface config',
'./package.json',
)

.action(async (options) => {
const apiExtractorJsonPath: string = resolve(options.extractorConfig)
const extractorConfig: ExtractorConfig = ExtractorConfig.loadFileAndPrepare(apiExtractorJsonPath)

const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, {
localBuild: true,
showVerboseMessages: true,
})

if (!extractorResult.succeeded) {
console.error(
`API Extractor completed with ${extractorResult.errorCount} errors` +
` and ${extractorResult.warningCount} warnings`,
)
process.exitCode = 1
}

const packageConfig = require(resolve(options.packageConfig))
const interfaces: any = {}

for (const pluginInterfaceName in packageConfig.daf.pluginInterfaces) {
const outputFile = resolve(options.outputFolder, pluginInterfaceName + '.ts')
console.log('Writing', outputFile)

const entryFile = packageConfig.daf.pluginInterfaces[pluginInterfaceName]
const api = {
components: {
Expand All @@ -107,12 +125,12 @@ program
const generator = TJS.createGenerator({
path: resolve(entryFile),
encodeRefs: false,
additionalProperties: true
additionalProperties: true,
})

const apiModel: ApiModel = new ApiModel()
const apiPackage = apiModel.loadPackage(resolve('./api', packageConfig.name + '.api.json'))
const apiPackage = apiModel.loadPackage(extractorConfig.apiJsonFilePath)

const entry = apiPackage.entryPoints[0]

const pluginInterface = entry.findMembersByName(pluginInterfaceName)[0]
Expand All @@ -133,7 +151,6 @@ program
//@ts-ignore
?.getChildNodes()[0]?.text


method.description = method.description || ''

if (method.parameters) {
Expand All @@ -144,44 +161,36 @@ program
...createSchema(generator, method.parameters).components.schemas,
}
}

//@ts-ignore
api.components.schemas = {
//@ts-ignore
...api.components.schemas,
...createSchema(generator, method.response).components.schemas,
}

//@ts-ignore
api.components.methods[method.operationId] = {
description: method.description,
arguments: getReference(method.parameters),
returnType: getReference(method.response)
returnType: getReference(method.response),
}

}

if (!fs.existsSync(resolve(options.outputFolder))) {
fs.mkdirSync(resolve(options.outputFolder))
}
writeFileSync(outputFile, 'export default ' + JSON.stringify(api, null, 2))

interfaces[pluginInterfaceName] = api
}



writeFileSync(resolve('./plugin.schema.json'), JSON.stringify(interfaces, null, 2))
})


program
.command('extractPluginApi')
.description('Extract DAF plugin api ')
.option('-c, --extractorConfig <string>', 'API Extractor config file')

program
.command('extract-api')
.description('Extract API')
.option('-c, --extractorConfig <string>', 'API Extractor config file', './api-extractor.json')
.action(async (options) => {

const apiExtractorJsonPath: string = resolve(options.extractorConfig)
const extractorConfig: ExtractorConfig = ExtractorConfig.loadFileAndPrepare(apiExtractorJsonPath)

const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, {
localBuild: true,
showVerboseMessages: true,
Expand All @@ -194,6 +203,4 @@ program
)
process.exitCode = 1
}


})
21 changes: 21 additions & 0 deletions packages/daf-core/api/daf-core.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -5993,6 +5993,27 @@
],
"extendsTokenRanges": []
},
{
"kind": "Variable",
"canonicalReference": "daf-core!schema:var",
"docComment": "",
"excerptTokens": [
{
"kind": "Content",
"text": "schema: "
},
{
"kind": "Content",
"text": "any"
}
],
"releaseTag": "Public",
"name": "schema",
"variableTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
}
},
{
"kind": "TypeAlias",
"canonicalReference": "daf-core!TAgent:type",
Expand Down
Loading