Skip to content

Commit

Permalink
feat: Dynamic OpenAPI schema
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Oct 12, 2020
1 parent c7d1ef3 commit f12236b
Show file tree
Hide file tree
Showing 37 changed files with 506 additions and 3,469 deletions.
41 changes: 40 additions & 1 deletion __tests__/graphqlAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IKeyManager,
IDataStore,
IMessageHandler,
IAgentPluginSchema
} from '../packages/daf-core/src'
import { MessageHandler } from '../packages/daf-message-handler/src'
import { KeyManager } from '../packages/daf-key-manager/src'
Expand Down Expand Up @@ -39,6 +40,43 @@ import { Server } from 'http'
import { createSchema, AgentGraphQLClient, supportedMethods } from '../packages/daf-graphql/src'
import fs from 'fs'

import IMessageHandlerSchema from '../packages/daf-core/build/schemas/IMessageHandler'
import IDataStoreSchema from '../packages/daf-core/build/schemas/IDataStore'
import IKeyManagerSchema from '../packages/daf-core/build/schemas/IKeyManager'
import IResolverSchema from '../packages/daf-core/build/schemas/IResolver'
import IIdentityManagerSchema from '../packages/daf-core/build/schemas/IIdentityManager'
import ISelectiveDisclosureSchema from '../packages/daf-selective-disclosure/build/schemas/ISelectiveDisclosure'
import ICredentialIssuerSchema from '../packages/daf-w3c/build/schemas/ICredentialIssuer'
import IDIDCommSchema from '../packages/daf-did-comm/build/schemas/IDIDComm'
import IDataStoreORMSchema from '../packages/daf-typeorm/build/schemas/IDataStoreORM'

const schema: IAgentPluginSchema = {
components: {
schemas: {
...IMessageHandlerSchema.components.schemas,
...IDataStoreSchema.components.schemas,
...IKeyManagerSchema.components.schemas,
...IResolverSchema.components.schemas,
...IIdentityManagerSchema.components.schemas,
...ISelectiveDisclosureSchema.components.schemas,
...ICredentialIssuerSchema.components.schemas,
...IDIDCommSchema.components.schemas,
...IDataStoreORMSchema.components.schemas,
},
methods: {
...IMessageHandlerSchema.components.methods,
...IDataStoreSchema.components.methods,
...IKeyManagerSchema.components.methods,
...IResolverSchema.components.methods,
...IIdentityManagerSchema.components.methods,
...ISelectiveDisclosureSchema.components.methods,
...ICredentialIssuerSchema.components.methods,
...IDIDCommSchema.components.methods,
...IDataStoreORMSchema.components.methods,
},
}
}

// Shared tests
import verifiableData from './shared/verifiableData'
import handleSdrMessage from './shared/handleSdrMessage'
Expand Down Expand Up @@ -68,7 +106,8 @@ const agent = createAgent<
plugins: [
new AgentGraphQLClient({
url: 'http://localhost:' + port + '/graphql',
enabledMethods: Object.keys(supportedMethods),
enabledMethods: Object.keys(schema.components.methods),
schema
}),
],
})
Expand Down
51 changes: 47 additions & 4 deletions __tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
IKeyManager,
IDataStore,
IMessageHandler,
IAgentPluginSchema,
} from '../packages/daf-core/src'
import { MessageHandler } from '../packages/daf-message-handler/src'
import { KeyManager } from '../packages/daf-key-manager/src'
Expand All @@ -32,12 +33,50 @@ import {
DataStore,
DataStoreORM,
} from '../packages/daf-typeorm/src'
import { AgentRestClient, supportedMethods } from '../packages/daf-rest/src'
import { AgentRestClient } from '../packages/daf-rest/src'
import express from 'express'
import { Server } from 'http'
import { AgentRouter } from '../packages/daf-express/src'
import fs from 'fs'

import IMessageHandlerSchema from '../packages/daf-core/build/schemas/IMessageHandler'
import IDataStoreSchema from '../packages/daf-core/build/schemas/IDataStore'
import IKeyManagerSchema from '../packages/daf-core/build/schemas/IKeyManager'
import IResolverSchema from '../packages/daf-core/build/schemas/IResolver'
import IIdentityManagerSchema from '../packages/daf-core/build/schemas/IIdentityManager'
import ISelectiveDisclosureSchema from '../packages/daf-selective-disclosure/build/schemas/ISelectiveDisclosure'
import ICredentialIssuerSchema from '../packages/daf-w3c/build/schemas/ICredentialIssuer'
import IDIDCommSchema from '../packages/daf-did-comm/build/schemas/IDIDComm'
import IDataStoreORMSchema from '../packages/daf-typeorm/build/schemas/IDataStoreORM'

const schema: IAgentPluginSchema = {
components: {
schemas: {
...IMessageHandlerSchema.components.schemas,
...IDataStoreSchema.components.schemas,
...IKeyManagerSchema.components.schemas,
...IResolverSchema.components.schemas,
...IIdentityManagerSchema.components.schemas,
...ISelectiveDisclosureSchema.components.schemas,
...ICredentialIssuerSchema.components.schemas,
...IDIDCommSchema.components.schemas,
...IDataStoreORMSchema.components.schemas,
},
methods: {
...IMessageHandlerSchema.components.methods,
...IDataStoreSchema.components.methods,
...IKeyManagerSchema.components.methods,
...IResolverSchema.components.methods,
...IIdentityManagerSchema.components.methods,
...ISelectiveDisclosureSchema.components.methods,
...ICredentialIssuerSchema.components.methods,
...IDIDCommSchema.components.methods,
...IDataStoreORMSchema.components.methods,
},
}
}


// Shared tests
import verifiableData from './shared/verifiableData'
import handleSdrMessage from './shared/handleSdrMessage'
Expand Down Expand Up @@ -68,7 +107,8 @@ const agent = createAgent<
plugins: [
new AgentRestClient({
url: 'http://localhost:' + port + '/agent',
enabledMethods: supportedMethods,
enabledMethods: Object.keys(schema.components.methods),
schema
}),
],
})
Expand Down Expand Up @@ -133,14 +173,17 @@ const setup = async (): Promise<boolean> => {
],
})

const basePath = '/agent'

const agentRouter = AgentRouter({
getAgentForRequest: async (req) => serverAgent,
exposedMethods: supportedMethods,
exposedMethods: serverAgent.availableMethods(),
basePath
})

return new Promise((resolve, reject) => {
const app = express()
app.use('/agent', agentRouter)
app.use(basePath, agentRouter)
restServer = app.listen(port, () => {
resolve()
})
Expand Down
1 change: 1 addition & 0 deletions docs/api/daf-core.iagentbase.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface IAgentBase
| Property | Type | Description |
| --- | --- | --- |
| [availableMethods](./daf-core.iagentbase.availablemethods.md) | () =&gt; string\[\] | |
| [schema](./daf-core.iagentbase.schema.md) | [IAgentPluginSchema](./daf-core.iagentpluginschema.md) | |

11 changes: 11 additions & 0 deletions docs/api/daf-core.iagentbase.schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [daf-core](./daf-core.md) &gt; [IAgentBase](./daf-core.iagentbase.md) &gt; [schema](./daf-core.iagentbase.schema.md)

## IAgentBase.schema property

<b>Signature:</b>

```typescript
readonly schema: IAgentPluginSchema;
```
13 changes: 13 additions & 0 deletions docs/api/daf-express.agentrouteroptions.basepath.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [daf-express](./daf-express.md) &gt; [AgentRouterOptions](./daf-express.agentrouteroptions.md) &gt; [basePath](./daf-express.agentrouteroptions.basepath.md)

## AgentRouterOptions.basePath property

Base path

<b>Signature:</b>

```typescript
basePath: string;
```
13 changes: 0 additions & 13 deletions docs/api/daf-express.agentrouteroptions.extramethods.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/api/daf-express.agentrouteroptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export interface AgentRouterOptions

| Property | Type | Description |
| --- | --- | --- |
| [basePath](./daf-express.agentrouteroptions.basepath.md) | string | Base path |
| [exposedMethods](./daf-express.agentrouteroptions.exposedmethods.md) | Array&lt;string&gt; | List of exposed methods |
| [extraMethods](./daf-express.agentrouteroptions.extramethods.md) | Array&lt;string&gt; | List of extra methods |
| [getAgentForRequest](./daf-express.agentrouteroptions.getagentforrequest.md) | (req: Request) =&gt; Promise&lt;[IAgent](./daf-core.iagent.md)<!-- -->&gt; | Function that returns configured agent for specific request |
| [serveSchema](./daf-express.agentrouteroptions.serveschema.md) | boolean | If set to <code>true</code>, router will serve OpenAPI schema JSON on <code>/</code> route |

3 changes: 2 additions & 1 deletion docs/api/daf-graphql.agentgraphqlclient._constructor_.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ constructor(options: {
url: string;
enabledMethods: string[];
headers?: Response['headers'];
schema: IAgentPluginSchema;
overrides?: Record<string, IAgentGraphQLMethod>;
});
```
Expand All @@ -21,5 +22,5 @@ constructor(options: {

| Parameter | Type | Description |
| --- | --- | --- |
| options | { url: string; enabledMethods: string\[\]; headers?: Response\['headers'\]; overrides?: Record&lt;string, [IAgentGraphQLMethod](./daf-graphql.iagentgraphqlmethod.md)<!-- -->&gt;; } | |
| options | { url: string; enabledMethods: string\[\]; headers?: Response\['headers'\]; schema: [IAgentPluginSchema](./daf-core.iagentpluginschema.md)<!-- -->; overrides?: Record&lt;string, [IAgentGraphQLMethod](./daf-graphql.iagentgraphqlmethod.md)<!-- -->&gt;; } | |

2 changes: 1 addition & 1 deletion docs/api/daf-key-manager.keymanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export declare class KeyManager implements IAgentPlugin
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [methods](./daf-key-manager.keymanager.methods.md) | | [IKeyManager](./daf-core.ikeymanager.md) | Plugin methods |
| [schema](./daf-key-manager.keymanager.schema.md) | | { components: { schemas: { IKeyManagerCreateKeyArgs: { type: string; properties: { type: { $ref: string; description: string; }; kms: { type: string; description: string; }; meta: { type: string; description: string; }; }; required: string\[\]; description: string; }; TKeyType: { type: string; enum: string\[\]; description: string; }; IKey: { type: string; properties: { kid: { type: string; description: string; }; kms: { type: string; description: string; }; type: { $ref: string; description: string; }; publicKeyHex: { type: string; description: string; }; privateKeyHex: { type: string; description: string; }; meta: { anyOf: { type: string; }\[\]; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerDecryptJWEArgs: { type: string; properties: { kid: { type: string; description: string; }; data: { type: string; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerDeleteKeyArgs: { type: string; properties: { kid: { type: string; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerEncryptJWEArgs: { type: string; properties: { kid: { type: string; description: string; }; to: { type: string; properties: { kid: { type: string; description: string; }; type: { $ref: string; description: string; }; publicKeyHex: { type: string; description: string; }; privateKeyHex: { type: string; description: string; }; meta: { anyOf: { type: string; }\[\]; description: string; }; }; required: string\[\]; description: string; }; data: { type: string; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerGetKeyArgs: { type: string; properties: { kid: { type: string; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerSignEthTXArgs: { type: string; properties: { kid: { type: string; description: string; }; transaction: { type: string; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerSignJWTArgs: { type: string; properties: { kid: { type: string; description: string; }; data: { type: string; description: string; }; }; required: string\[\]; description: string; }; }; methods: { keyManagerCreateKey: { description: string; arguments: { $ref: string; }; returnType: { $ref: string; }; }; keyManagerDecryptJWE: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; keyManagerDeleteKey: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; keyManagerEncryptJWE: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; keyManagerGetKey: { description: string; arguments: { $ref: string; }; returnType: { $ref: string; }; }; keyManagerGetKeyManagementSystems: { description: string; arguments: { type: string; }; returnType: { type: string; items: { type: string; }; }; }; keyManagerImportKey: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; keyManagerSignEthTX: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; keyManagerSignJWT: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; }; }; } | |
| [schema](./daf-key-manager.keymanager.schema.md) | | { components: { schemas: { IKeyManagerCreateKeyArgs: { type: string; properties: { type: { $ref: string; description: string; }; kms: { type: string; description: string; }; meta: { type: string; description: string; }; }; required: string\[\]; description: string; }; TKeyType: { type: string; enum: string\[\]; description: string; }; IKey: { type: string; properties: { kid: { type: string; description: string; }; kms: { type: string; description: string; }; type: { $ref: string; description: string; }; publicKeyHex: { type: string; description: string; }; privateKeyHex: { type: string; description: string; }; meta: { anyOf: { type: string; }\[\]; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerDecryptJWEArgs: { type: string; properties: { kid: { type: string; description: string; }; data: { type: string; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerDeleteKeyArgs: { type: string; properties: { kid: { type: string; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerEncryptJWEArgs: { type: string; properties: { kid: { type: string; description: string; }; to: { type: string; properties: { kid: { type: string; description: string; }; type: { $ref: string; description: string; }; publicKeyHex: { type: string; description: string; }; privateKeyHex: { type: string; description: string; }; meta: { anyOf: { type: string; }\[\]; description: string; }; }; required: string\[\]; description: string; }; data: { type: string; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerGetKeyArgs: { type: string; properties: { kid: { type: string; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerSignEthTXArgs: { type: string; properties: { kid: { type: string; description: string; }; transaction: { type: string; description: string; }; }; required: string\[\]; description: string; }; IKeyManagerSignJWTArgs: { type: string; properties: { kid: { type: string; description: string; }; data: { type: string; description: string; }; }; required: string\[\]; description: string; }; EcdsaSignature: { type: string; properties: { r: { type: string; }; s: { type: string; }; recoveryParam: { type: string; const: number; }; }; required: string\[\]; }; }; methods: { keyManagerCreateKey: { description: string; arguments: { $ref: string; }; returnType: { $ref: string; }; }; keyManagerDecryptJWE: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; keyManagerDeleteKey: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; keyManagerEncryptJWE: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; keyManagerGetKey: { description: string; arguments: { $ref: string; }; returnType: { $ref: string; }; }; keyManagerGetKeyManagementSystems: { description: string; arguments: { type: string; }; returnType: { type: string; items: { type: string; }; }; }; keyManagerImportKey: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; keyManagerSignEthTX: { description: string; arguments: { $ref: string; }; returnType: { type: string; }; }; keyManagerSignJWT: { description: string; arguments: { $ref: string; }; returnType: { $ref: string; }; }; }; }; } | |
## Methods
Expand Down
18 changes: 17 additions & 1 deletion docs/api/daf-key-manager.keymanager.schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ readonly schema: {
required: string[];
description: string;
};
EcdsaSignature: {
type: string;
properties: {
r: {
type: string;
};
s: {
type: string;
};
recoveryParam: {
type: string;
const: number;
};
};
required: string[];
};
};
methods: {
keyManagerCreateKey: {
Expand Down Expand Up @@ -261,7 +277,7 @@ readonly schema: {
$ref: string;
};
returnType: {
type: string;
$ref: string;
};
};
};
Expand Down
4 changes: 2 additions & 2 deletions docs/api/daf-rest.agentrestclient._constructor_.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ Constructs a new instance of the `AgentRestClient` class
constructor(options: {
url: string;
enabledMethods: string[];
schema: IAgentPluginSchema;
headers?: Record<string, string>;
extraMethods?: Array<string>;
});
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| options | { url: string; enabledMethods: string\[\]; headers?: Record&lt;string, string&gt;; extraMethods?: Array&lt;string&gt;; } | |
| options | { url: string; enabledMethods: string\[\]; schema: [IAgentPluginSchema](./daf-core.iagentpluginschema.md)<!-- -->; headers?: Record&lt;string, string&gt;; } | |

11 changes: 11 additions & 0 deletions docs/api/daf-rest.getopenapischema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [daf-rest](./daf-rest.md) &gt; [getOpenApiSchema](./daf-rest.getopenapischema.md)

## getOpenApiSchema variable

<b>Signature:</b>

```typescript
getOpenApiSchema: (agent: IAgent, basePath: string, exposedMethods: Array<string>) => OpenAPIV3.Document
```
5 changes: 2 additions & 3 deletions docs/api/daf-rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## daf-rest package

Provides a [plugin](./daf-rest.agentrestclient.md) for the [Agent](./daf-core.agent.md) that can proxy method execution over HTTPS using [OpenAPI](./daf-rest.openapischema.md)
Provides a [plugin](./daf-rest.agentrestclient.md) for the [Agent](./daf-core.agent.md) that can proxy method execution over HTTPS using

## Classes

Expand All @@ -16,6 +16,5 @@ Provides a [plugin](./daf-rest.agentrestclient.md) for the [Agent](./daf-core.ag

| Variable | Description |
| --- | --- |
| [openApiSchema](./daf-rest.openapischema.md) | |
| [supportedMethods](./daf-rest.supportedmethods.md) | |
| [getOpenApiSchema](./daf-rest.getopenapischema.md) | |

11 changes: 0 additions & 11 deletions docs/api/daf-rest.openapischema.md

This file was deleted.

11 changes: 0 additions & 11 deletions docs/api/daf-rest.supportedmethods.md

This file was deleted.

Loading

0 comments on commit f12236b

Please sign in to comment.