From b6d1e36252b74c9decd95a46bb371df092fa6479 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Fri, 24 Jul 2020 16:30:17 +0300 Subject: [PATCH] fix: IIdentityManager interface --- packages/daf-core/src/identity-manager.ts | 142 +++++++------- packages/daf-core/src/index.ts | 13 +- report/daf-core.api.md | 225 ++++++++++------------ scripts/generate-schemas.ts | 58 ++++-- 4 files changed, 230 insertions(+), 208 deletions(-) diff --git a/packages/daf-core/src/identity-manager.ts b/packages/daf-core/src/identity-manager.ts index 33cb8cccd..2b9bf62b3 100644 --- a/packages/daf-core/src/identity-manager.ts +++ b/packages/daf-core/src/identity-manager.ts @@ -3,44 +3,76 @@ import { IAgentPlugin, IIdentity, IService, IKey, IPluginMethodMap, IAgentContex import { AbstractIdentityStore } from './abstract/abstract-identity-store' import { IKeyManager } from './key-manager' +export interface IIdentityManagerGetIdentityArgs { + did: string +} +export interface IIdentityManagerDeleteIdentityArgs { + did: string +} +export interface IIdentityManagerCreateIdentityArgs { + alias?: string + provider?: string + kms?: string + options?: any +} + +export interface IIdentityManagerGetOrCreateIdentityArgs { + alias: string + provider?: string + kms?: string + options?: any +} +export interface IIdentityManagerAddKeyArgs { + did: string + key: IKey + options?: any +} +export interface IIdentityManagerRemoveKeyArgs { + did: string + kid: string + options?: any +} +export interface IIdentityManagerAddServiceArgs { + did: string + service: IService + options?: any +} +export interface IIdentityManagerRemoveServiceArgs { + did: string + id: string + options?: any +} + export interface IIdentityManager extends IPluginMethodMap { - identityManagerGetProviders: () => Promise - identityManagerGetIdentities: () => Promise - identityManagerGetIdentity: (args: { did: string }) => Promise - identityManagerCreateIdentity: ( - args: { - alias?: string - provider?: string - kms?: string - options?: any - }, - context: IAgentContext, - ) => Promise - identityManagerGetOrCreateIdentity: ( - args: { alias: string; provider?: string; kms?: string; options?: any }, + identityManagerGetProviders(): Promise> + identityManagerGetIdentities(): Promise> + identityManagerGetIdentity(args: IIdentityManagerGetIdentityArgs): Promise + identityManagerCreateIdentity( + args: IIdentityManagerCreateIdentityArgs, context: IAgentContext, - ) => Promise - identityManagerImportIdentity: (args: IIdentity) => Promise - identityManagerDeleteIdentity: ( - args: { did: string }, + ): Promise + identityManagerGetOrCreateIdentity( + args: IIdentityManagerGetOrCreateIdentityArgs, context: IAgentContext, - ) => Promise - identityManagerAddKey: ( - args: { did: string; key: IKey; options?: any }, + ): Promise + identityManagerImportIdentity(args: IIdentity): Promise + identityManagerDeleteIdentity( + args: IIdentityManagerDeleteIdentityArgs, context: IAgentContext, - ) => Promise // txHash? - identityManagerRemoveKey: ( - args: { did: string; kid: string; options?: any }, + ): Promise + identityManagerAddKey(args: IIdentityManagerAddKeyArgs, context: IAgentContext): Promise // txHash? + identityManagerRemoveKey( + args: IIdentityManagerRemoveKeyArgs, context: IAgentContext, - ) => Promise // txHash? - identityManagerAddService: ( - args: { did: string; service: IService; options?: any }, + ): Promise // txHash? + identityManagerAddService( + args: IIdentityManagerAddServiceArgs, context: IAgentContext, - ) => Promise //txHash? - identityManagerRemoveService: ( - args: { did: string; id: string; options?: any }, + ): Promise //txHash? + identityManagerRemoveService( + args: IIdentityManagerRemoveServiceArgs, context: IAgentContext, - ) => Promise //txHash? + ): Promise //txHash? } export class IdentityManager implements IAgentPlugin { @@ -86,12 +118,12 @@ export class IdentityManager implements IAgentPlugin { return this.store.list() } - async identityManagerGetIdentity({ did }: { did: string }): Promise { + async identityManagerGetIdentity({ did }: IIdentityManagerGetIdentityArgs): Promise { return this.store.get({ did }) } async identityManagerCreateIdentity( - { provider, alias, kms, options }: { alias?: string; provider?: string; kms?: string; options?: any }, + { provider, alias, kms, options }: IIdentityManagerCreateIdentityArgs, context: IAgentContext, ): Promise { const providerName = provider || this.defaultProvider @@ -103,7 +135,7 @@ export class IdentityManager implements IAgentPlugin { } async identityManagerGetOrCreateIdentity( - { provider, alias, kms, options }: { alias: string; provider?: string; kms?: string; options?: any }, + { provider, alias, kms, options }: IIdentityManagerGetOrCreateIdentityArgs, context: IAgentContext, ): Promise { try { @@ -120,7 +152,7 @@ export class IdentityManager implements IAgentPlugin { } async identityManagerDeleteIdentity( - { did }: { did: string }, + { did }: IIdentityManagerDeleteIdentityArgs, context: IAgentContext, ): Promise { const identity = await this.store.get({ did }) @@ -130,15 +162,7 @@ export class IdentityManager implements IAgentPlugin { } async identityManagerAddKey( - { - did, - key, - options, - }: { - did: string - key: IKey - options?: any - }, + { did, key, options }: IIdentityManagerAddKeyArgs, context: IAgentContext, ): Promise { const identity = await this.store.get({ did }) @@ -150,15 +174,7 @@ export class IdentityManager implements IAgentPlugin { } async identityManagerRemoveKey( - { - did, - kid, - options, - }: { - did: string - kid: string - options?: any - }, + { did, kid, options }: IIdentityManagerRemoveKeyArgs, context: IAgentContext, ): Promise { const identity = await this.store.get({ did }) @@ -170,15 +186,7 @@ export class IdentityManager implements IAgentPlugin { } async identityManagerAddService( - { - did, - service, - options, - }: { - did: string - service: IService - options?: any - }, + { did, service, options }: IIdentityManagerAddServiceArgs, context: IAgentContext, ): Promise { const identity = await this.store.get({ did }) @@ -190,15 +198,7 @@ export class IdentityManager implements IAgentPlugin { } async identityManagerRemoveService( - { - did, - id, - options, - }: { - did: string - id: string - options?: any - }, + { did, id, options }: IIdentityManagerRemoveServiceArgs, context: IAgentContext, ): Promise { const identity = await this.store.get({ did }) diff --git a/packages/daf-core/src/index.ts b/packages/daf-core/src/index.ts index 5a8c1deac..b0d84fccb 100644 --- a/packages/daf-core/src/index.ts +++ b/packages/daf-core/src/index.ts @@ -5,7 +5,18 @@ */ export { Agent, createAgent, IAgentOptions } from './agent' export * from './types' -export { IdentityManager, IIdentityManager } from './identity-manager' +export { + IdentityManager, + IIdentityManager, + IIdentityManagerAddKeyArgs, + IIdentityManagerAddServiceArgs, + IIdentityManagerCreateIdentityArgs, + IIdentityManagerDeleteIdentityArgs, + IIdentityManagerGetIdentityArgs, + IIdentityManagerGetOrCreateIdentityArgs, + IIdentityManagerRemoveKeyArgs, + IIdentityManagerRemoveServiceArgs, +} from './identity-manager' export { KeyManager, IKeyManager, diff --git a/report/daf-core.api.md b/report/daf-core.api.md index ab92fec63..9bda44e06 100644 --- a/report/daf-core.api.md +++ b/report/daf-core.api.md @@ -199,71 +199,31 @@ export class IdentityManager implements IAgentPlugin { }) // (undocumented) identityManagerAddKey( - { - did, - key, - options, - }: { - did: string - key: IKey - options?: any - }, + { did, key, options }: IIdentityManagerAddKeyArgs, context: IAgentContext, ): Promise // (undocumented) identityManagerAddService( - { - did, - service, - options, - }: { - did: string - service: IService - options?: any - }, + { did, service, options }: IIdentityManagerAddServiceArgs, context: IAgentContext, ): Promise // (undocumented) identityManagerCreateIdentity( - { - provider, - alias, - kms, - options, - }: { - alias?: string - provider?: string - kms?: string - options?: any - }, + { provider, alias, kms, options }: IIdentityManagerCreateIdentityArgs, context: IAgentContext, ): Promise // (undocumented) identityManagerDeleteIdentity( - { - did, - }: { - did: string - }, + { did }: IIdentityManagerDeleteIdentityArgs, context: IAgentContext, ): Promise // (undocumented) identityManagerGetIdentities(): Promise // (undocumented) - identityManagerGetIdentity({ did }: { did: string }): Promise + identityManagerGetIdentity({ did }: IIdentityManagerGetIdentityArgs): Promise // (undocumented) identityManagerGetOrCreateIdentity( - { - provider, - alias, - kms, - options, - }: { - alias: string - provider?: string - kms?: string - options?: any - }, + { provider, alias, kms, options }: IIdentityManagerGetOrCreateIdentityArgs, context: IAgentContext, ): Promise // (undocumented) @@ -272,28 +232,12 @@ export class IdentityManager implements IAgentPlugin { identityManagerImportIdentity(identity: IIdentity): Promise // (undocumented) identityManagerRemoveKey( - { - did, - kid, - options, - }: { - did: string - kid: string - options?: any - }, + { did, kid, options }: IIdentityManagerRemoveKeyArgs, context: IAgentContext, ): Promise // (undocumented) identityManagerRemoveService( - { - did, - id, - options, - }: { - did: string - id: string - options?: any - }, + { did, id, options }: IIdentityManagerRemoveServiceArgs, context: IAgentContext, ): Promise // (undocumented) @@ -332,76 +276,121 @@ export interface IIdentity { // @public (undocumented) export interface IIdentityManager extends IPluginMethodMap { // (undocumented) - identityManagerAddKey: ( - args: { - did: string - key: IKey - options?: any - }, - context: IAgentContext, - ) => Promise + identityManagerAddKey(args: IIdentityManagerAddKeyArgs, context: IAgentContext): Promise // (undocumented) - identityManagerAddService: ( - args: { - did: string - service: IService - options?: any - }, + identityManagerAddService( + args: IIdentityManagerAddServiceArgs, context: IAgentContext, - ) => Promise + ): Promise // (undocumented) - identityManagerCreateIdentity: ( - args: { - alias?: string - provider?: string - kms?: string - options?: any - }, + identityManagerCreateIdentity( + args: IIdentityManagerCreateIdentityArgs, context: IAgentContext, - ) => Promise + ): Promise // (undocumented) - identityManagerDeleteIdentity: ( - args: { - did: string - }, + identityManagerDeleteIdentity( + args: IIdentityManagerDeleteIdentityArgs, context: IAgentContext, - ) => Promise + ): Promise // (undocumented) - identityManagerGetIdentities: () => Promise + identityManagerGetIdentities(): Promise> // (undocumented) - identityManagerGetIdentity: (args: { did: string }) => Promise + identityManagerGetIdentity(args: IIdentityManagerGetIdentityArgs): Promise // (undocumented) - identityManagerGetOrCreateIdentity: ( - args: { - alias: string - provider?: string - kms?: string - options?: any - }, + identityManagerGetOrCreateIdentity( + args: IIdentityManagerGetOrCreateIdentityArgs, context: IAgentContext, - ) => Promise + ): Promise // (undocumented) - identityManagerGetProviders: () => Promise + identityManagerGetProviders(): Promise> // (undocumented) - identityManagerImportIdentity: (args: IIdentity) => Promise + identityManagerImportIdentity(args: IIdentity): Promise // (undocumented) - identityManagerRemoveKey: ( - args: { - did: string - kid: string - options?: any - }, + identityManagerRemoveKey( + args: IIdentityManagerRemoveKeyArgs, context: IAgentContext, - ) => Promise + ): Promise // (undocumented) - identityManagerRemoveService: ( - args: { - did: string - id: string - options?: any - }, + identityManagerRemoveService( + args: IIdentityManagerRemoveServiceArgs, context: IAgentContext, - ) => Promise + ): Promise +} + +// @public (undocumented) +export interface IIdentityManagerAddKeyArgs { + // (undocumented) + did: string + // (undocumented) + key: IKey + // (undocumented) + options?: any +} + +// @public (undocumented) +export interface IIdentityManagerAddServiceArgs { + // (undocumented) + did: string + // (undocumented) + options?: any + // (undocumented) + service: IService +} + +// @public (undocumented) +export interface IIdentityManagerCreateIdentityArgs { + // (undocumented) + alias?: string + // (undocumented) + kms?: string + // (undocumented) + options?: any + // (undocumented) + provider?: string +} + +// @public (undocumented) +export interface IIdentityManagerDeleteIdentityArgs { + // (undocumented) + did: string +} + +// @public (undocumented) +export interface IIdentityManagerGetIdentityArgs { + // (undocumented) + did: string +} + +// @public (undocumented) +export interface IIdentityManagerGetOrCreateIdentityArgs { + // (undocumented) + alias: string + // (undocumented) + kms?: string + // (undocumented) + options?: any + // (undocumented) + provider?: string +} + +// @public (undocumented) +export interface IIdentityManagerRemoveKeyArgs { + // (undocumented) + did: string + // (undocumented) + kid: string + // (undocumented) + options?: any +} + +// @public (undocumented) +export interface IIdentityManagerRemoveServiceArgs { + // (undocumented) + did: string + // (undocumented) + id: string + // (undocumented) + options?: any } // @public (undocumented) diff --git a/scripts/generate-schemas.ts b/scripts/generate-schemas.ts index 9c5369bc1..d391b1277 100644 --- a/scripts/generate-schemas.ts +++ b/scripts/generate-schemas.ts @@ -14,14 +14,14 @@ import { const apiExtractorConfig = require('../api-extractor-base.json') const agentPlugins: Record> = { - 'daf-core': ['IResolveDid', 'IDataStore', 'IKeyManager'], + 'daf-core': ['IResolveDid', 'IDataStore', 'IKeyManager', 'IIdentityManager'], 'daf-w3c': ['IW3c'], } interface RestMethod { operationId: string description?: string - parameters: string + parameters?: string response: string } @@ -33,7 +33,7 @@ const openApi = { paths: {}, } -const genericTypes = ['boolean', 'string', 'number'] +const genericTypes = ['boolean', 'string', 'number', 'any', 'Array'] function createSchema(generator: TJS.JsonSchemaGenerator, symbol: string) { if (genericTypes.includes(symbol)) { @@ -41,10 +41,14 @@ function createSchema(generator: TJS.JsonSchemaGenerator, symbol: string) { } //hack - const fixedSymbol = symbol === 'EcdsaSignature | string' ? 'EcdsaSignature' : symbol + let fixedSymbol = symbol === 'EcdsaSignature | string' ? 'EcdsaSignature' : symbol // TODO fix 'EcdsaSignature | string' in openApi responses + fixedSymbol = fixedSymbol.replace('Array<', '').replace('>', '') const schema = generator.getSchemaForSymbol(fixedSymbol) + if (fixedSymbol === 'TIdentityManagerGetIdentitiesResult') { + console.log(schema) + } const newSchema = { components: { @@ -59,6 +63,28 @@ function createSchema(generator: TJS.JsonSchemaGenerator, symbol: string) { return JSON.parse(schemaStr) } +function getParametersSchema(parameters?: string) { + if (!parameters) { + return [] + } else { + return [{ $ref: '#/components/schemas/' + parameters }] + } +} + +function getResponseSchema(response: string) { + if (response.slice(0, 6) === 'Array<') { + const symbol = response.replace('Array<', '').replace('>', '') + return { + type: 'array', + items: genericTypes.includes(symbol) ? { type: symbol } : { $ref: '#/components/schemas/' + symbol }, + } + } + if (response === 'any') { + return { type: ['array', 'boolean', 'integer', 'number', 'object', 'string'] } + } + return genericTypes.includes(response) ? { type: response } : { $ref: '#/components/schemas/' + response } +} + for (const packageName of Object.keys(agentPlugins)) { const program = TJS.getProgramFromFiles([resolve('packages/' + packageName + '/src/index.ts')]) const generator = TJS.buildGenerator(program, { required: true, topRef: true }) @@ -78,7 +104,7 @@ for (const packageName of Object.keys(agentPlugins)) { const method: Partial = {} method.operationId = member.displayName // console.log(member) - method.parameters = (member as ApiParameterListMixin).parameters[0].parameterTypeExcerpt.text + method.parameters = (member as ApiParameterListMixin).parameters[0]?.parameterTypeExcerpt?.text method.response = (member as ApiReturnTypeMixin).returnTypeExcerpt.text .replace('Promise<', '') .replace('>', '') @@ -88,9 +114,15 @@ for (const packageName of Object.keys(agentPlugins)) { //@ts-ignore ?.getChildNodes()[0]?.text + if (method.parameters) { + openApi.components.schemas = { + ...openApi.components.schemas, + ...createSchema(generator, method.parameters).components.schemas, + } + } + openApi.components.schemas = { ...openApi.components.schemas, - ...createSchema(generator, method.parameters).components.schemas, ...createSchema(generator, method.response).components.schemas, } methods.push(method as RestMethod) @@ -102,23 +134,13 @@ for (const packageName of Object.keys(agentPlugins)) { post: { description: method.description, operationId: method.operationId, - parameters: [ - { - //@ts-ignore - $ref: '#/components/schemas/' + method.parameters, - }, - ], + parameters: getParametersSchema(method.parameters), responses: { 200: { description: method.description, content: { 'application/json': { - schema: genericTypes.includes(method.response) - ? // TODO: is this correct? - { type: method.response } - : { - $ref: '#/components/schemas/' + method.response, - }, + schema: getResponseSchema(method.response), }, }, },