Skip to content

Commit

Permalink
feat(rest)!: update to AFJ 0.2.0 (#148)
Browse files Browse the repository at this point in the history
Co-authored-by: Akiff Manji <akiff.manji@gmail.com>

Signed-off-by: Jan <60812202+janrtvld@users.noreply.github.com>
  • Loading branch information
janrtvld committed Sep 2, 2022
1 parent c0351c5 commit 8ec4dc4
Show file tree
Hide file tree
Showing 54 changed files with 8,503 additions and 1,807 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,17 @@ jobs:
- name: Install dependencies
run: yarn install

- name: Run tests
run: TEST_AGENT_PUBLIC_DID_SEED=${TEST_AGENT_PUBLIC_DID_SEED} GENESIS_TXN_PATH=${GENESIS_TXN_PATH} yarn test --coverage
- name: Run tests for Push notifications
run: TEST_AGENT_PUBLIC_DID_SEED=${TEST_AGENT_PUBLIC_DID_SEED} GENESIS_TXN_PATH=${GENESIS_TXN_PATH} yarn test push-notifications --coverage

- name: Run tests for React hooks
run: TEST_AGENT_PUBLIC_DID_SEED=${TEST_AGENT_PUBLIC_DID_SEED} GENESIS_TXN_PATH=${GENESIS_TXN_PATH} yarn test react-hooks --coverage

- name: Run tests for Redux store
run: TEST_AGENT_PUBLIC_DID_SEED=${TEST_AGENT_PUBLIC_DID_SEED} GENESIS_TXN_PATH=${GENESIS_TXN_PATH} yarn test redux-store --coverage

- name: Run tests for Rest
run: TEST_AGENT_PUBLIC_DID_SEED=${TEST_AGENT_PUBLIC_DID_SEED} GENESIS_TXN_PATH=${GENESIS_TXN_PATH} yarn test rest --coverage

- uses: codecov/codecov-action@v1
if: always()
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ build
.vscode
.idea
coverage
CHANGELOG.md
CHANGELOG.md
routes
22 changes: 12 additions & 10 deletions packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,36 @@
"afj-rest": "bin/afj-rest.js"
},
"scripts": {
"dev": "tsnd --respawn samples/sampleWithApp.ts",
"tsoa": "tsoa spec-and-routes",
"dev": "tsoa spec-and-routes && tsnd --respawn samples/sampleWithApp.ts",
"build": "yarn run clean && yarn run compile",
"clean": "rimraf -rf ./build",
"compile": "tsc -p tsconfig.build.json",
"prepublishOnly": "yarn run build",
"test": "jest"
"test": "jest",
"postinstall": "tsoa spec-and-routes"
},
"dependencies": {
"@aries-framework/core": "^0.1.0",
"@aries-framework/node": "^0.1.0",
"class-transformer": "0.5.1",
"class-validator": "0.13.1",
"class-validator-jsonschema": "^3.1.1",
"@aries-framework/core": "^0.2.3",
"@aries-framework/node": "^0.2.3",
"body-parser": "^1.20.0",
"cors": "^2.8.5",
"express": "^4.18.1",
"node-fetch": "^2.6.7",
"reflect-metadata": "^0.1.13",
"routing-controllers": "^0.9.0",
"routing-controllers-openapi": "^3.1.0",
"swagger-ui-express": "^4.4.0",
"tslog": "^3.3.3",
"tsoa": "^4.1.2",
"tsyringe": "^4.7.0",
"yargs": "^17.3.1"
},
"devDependencies": {
"@types/body-parser": "^1.19.2",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jest": "^27.0.3",
"@types/node": "^17.0.41",
"@types/multer": "^1.4.7",
"@types/node": "^16.7.10",
"@types/supertest": "^2.0.12",
"@types/swagger-ui-express": "^4.1.3",
"@types/uuid": "^8.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/samples/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ServerConfig } from '../src/utils/ServerConfig'
import { connect } from 'ngrok'

import { startServer } from '../src/index'
import { setupAgent } from '../tests/utils/agent'
import { setupAgent } from '../src/utils/agent'

const run = async () => {
const endpoint = await connect(3001)
Expand Down
20 changes: 12 additions & 8 deletions packages/rest/samples/sampleWithApp.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { ServerConfig } from '../src/utils/ServerConfig'
import type { Express } from 'express'

import { AgentConfig } from '@aries-framework/core'
import bodyParser from 'body-parser'
import express from 'express'
import { connect } from 'ngrok'
import { createExpressServer } from 'routing-controllers'

import { startServer } from '../src/index'
import { setupAgent } from '../tests/utils/agent'

import { GreetingController } from './utils/GreetingController'
import { setupAgent } from '../src/utils/agent'

const run = async () => {
const endpoint = await connect(3001)
Expand All @@ -19,14 +18,19 @@ const run = async () => {
name: 'Aries Test Agent',
})

const app: Express = createExpressServer({
controllers: [GreetingController],
const app = express()
const jsonParser = bodyParser.json()

app.post('/greeting', jsonParser, (req, res) => {
const config = agent.injectionContainer.resolve(AgentConfig)

res.send(`Hello, ${config.label}!`)
})

const conf: ServerConfig = {
port: 3000,
app: app,
webhookUrl: 'http://localhost:5000/agent-events',
app: app,
}

await startServer(agent, conf)
Expand Down
23 changes: 0 additions & 23 deletions packages/rest/samples/utils/GreetingController.ts

This file was deleted.

12 changes: 8 additions & 4 deletions packages/rest/src/controllers/agent/AgentController.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import type { AgentInfo } from '../types'

import { Agent } from '@aries-framework/core'
import { Get, JsonController } from 'routing-controllers'
import { Controller, Get, Route, Tags } from 'tsoa'
import { injectable } from 'tsyringe'

@JsonController('/agent')
@Tags('Agent')
@Route('/agent')
@injectable()
export class AgentController {
export class AgentController extends Controller {
private agent: Agent

public constructor(agent: Agent) {
super()
this.agent = agent
}

/**
* Retrieve basic agent information
*/
@Get('/')
public async getAgentInfo() {
public async getAgentInfo(): Promise<AgentInfo> {
return {
label: this.agent.config.label,
endpoints: this.agent.config.endpoints,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
import type { BasicMessageRecord, BasicMessageStorageProps } from '@aries-framework/core'

import { Agent, RecordNotFoundError } from '@aries-framework/core'
import {
Body,
Get,
InternalServerError,
JsonController,
NotFoundError,
OnUndefined,
Param,
Post,
} from 'routing-controllers'
import { Body, Controller, Example, Get, Path, Post, Res, Route, Tags, TsoaResponse } from 'tsoa'
import { injectable } from 'tsyringe'

import { BasicMessageRequest } from '../../schemas/BasicMessageRequest'
import { BasicMessageRecordExample, RecordId } from '../examples'

@JsonController('/basic-messages')
@Tags('Basic Messages')
@Route('/basic-messages')
@injectable()
export class BasicMessageController {
export class BasicMessageController extends Controller {
private agent: Agent

public constructor(agent: Agent) {
super()
this.agent = agent
}

/**
* Retrieve basic messages by connectionId
* Retrieve basic messages by connection id
*
* @param connectionId Connection identifier
* @returns BasicMessageRecord[]
*/
@Example<BasicMessageStorageProps[]>([BasicMessageRecordExample])
@Get('/:connectionId')
public async getBasicMessages(@Param('connectionId') connectionId: string) {
const basicMessages = await this.agent.basicMessages.findAllByQuery({ connectionId: connectionId })
return basicMessages.map((m) => m.toJSON())
public async getBasicMessages(@Path('connectionId') connectionId: RecordId): Promise<BasicMessageRecord[]> {
return await this.agent.basicMessages.findAllByQuery({ connectionId })
}

/**
* Send a basic message to a connection
*
* @param connectionId Connection identifier
* @param content The content of the message
*/
@Post('/:connectionId')
@OnUndefined(204)
public async sendMessage(
@Param('connectionId') connectionId: string,
@Body()
basicMessage: BasicMessageRequest
@Path('connectionId') connectionId: RecordId,
@Body() request: Record<'content', string>,
@Res() notFoundError: TsoaResponse<404, { reason: string }>,
@Res() internalServerError: TsoaResponse<500, { message: string }>
) {
try {
await this.agent.basicMessages.sendMessage(connectionId, basicMessage.content)
this.setStatus(204)
await this.agent.basicMessages.sendMessage(connectionId, request.content)
} catch (error) {
if (error instanceof RecordNotFoundError) {
throw new NotFoundError(`connection with connectionId "${connectionId}" not found.`)
return notFoundError(404, { reason: `connection with connection id "${connectionId}" not found.` })
}
throw new InternalServerError(`something went wrong: ${error}`)
return internalServerError(500, { message: `something went wrong: ${error}` })
}
}
}
Loading

0 comments on commit 8ec4dc4

Please sign in to comment.