Skip to content

Commit

Permalink
feat(core): add ability to define the agent context type (#350)
Browse files Browse the repository at this point in the history
* feat(core): ability to define agent context type
* test(core): test that context type is optional (compile time)
  • Loading branch information
simonas-notcat authored Jan 26, 2021
1 parent f0fb459 commit 89255b9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 28 additions & 1 deletion packages/core/src/__tests__/agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Agent } from '../agent'
import { Agent, createAgent } from '../agent'
import { IAgentPlugin } from '../types/IAgent'
import { IResolver } from '../types/IResolver'

describe('core agent', () => {
it('should use plugin methods', async () => {
Expand Down Expand Up @@ -97,7 +98,33 @@ describe('core agent', () => {
{ foo: 'bar' },
{ agent, authorizedDid: 'did:example:123' },
)
})

it('should be possible to define context type', () => {
interface IContext {
name: string
authorizedDid: string
}
const agent = createAgent<IResolver, IContext>({
context: {
name: 'Agent name',
authorizedDid: 'did:example:123',
},
})

expect(agent.context?.name).toEqual('Agent name')
expect(agent.context?.authorizedDid).toEqual('did:example:123')
})

it('context type should be optional', () => {
const agent = createAgent<IResolver>({
context: {
name: 'Agent name',
authorizedDid: 'did:example:123',
},
})

expect(agent.context?.name).toEqual('Agent name')
expect(agent.context?.authorizedDid).toEqual('did:example:123')
})

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ export class Agent implements IAgent {
* @returns configured agent
* @public
*/
export function createAgent<T extends IPluginMethodMap>(options: IAgentOptions): TAgent<T> {
export function createAgent<T extends IPluginMethodMap, C = Record<string, any>>(
options: IAgentOptions & { context?: C },
): TAgent<T> & { context?: C } {
//@ts-ignore
return new Agent(options)
}

0 comments on commit 89255b9

Please sign in to comment.