Skip to content

Commit

Permalink
663/mk/open payments validate (#679)
Browse files Browse the repository at this point in the history
* feat(open-payments-client): initial POC of open-payments-client

* feat(open-payments-client): cleanup

* feat(open-payments): remove open-payments-client folder

* feat(open-payments): make open-payments folder and use script for type generation

* feat(open-payments): fix rootDir

* feat(open-payments): downgrading generation lib, updating how script is ran

* feat(open-payments): update default config usage

* feat(open-payments): update pnpm-lock.yaml

* feat(open-payments): update pnpm-lock.yaml

* feat(open-payments): adding tests

* feat(open-payments): update pnpm-lock.yaml

* feat(open-payments): simplify test

* feat(open-payments): updating workflows

* feat(open-payments): pin the open api spec to the most recent commit

* feat(open-payments): adding openapi validation

* feat(open-payments): adding openapi validation

* feat(open-payments): adding tests

* feat(open-payments): correcting test

* feat(open-payments): use updated RS spec

* feat(open-payments): build open api package on build step

* feat(open-payments): building open-api package during workflow

* Revert "feat(open-payments): building open-api package during workflow"

This reverts commit e11ec65.

* feat(open-payments): building open-api package during workflow

* feat(open-payments): update naming

* feat(open-payments): instantiate validator functions once

* chore(openapi): add docs for usage

* chore(openapi): update docs

* chore(openapi): prettify docs

* chore(openapi): update docs

* feat(open-payments): adding logger as dependency

* feat(open-payments): updating logging & error logic

* feat(open-payments): remove unnecessary imports

* feat(open-payments): update error handling & test
  • Loading branch information
mkurapov authored and omertoast committed Oct 28, 2022
1 parent d095f7b commit e1a7f60
Show file tree
Hide file tree
Showing 18 changed files with 377 additions and 140 deletions.
1 change: 1 addition & 0 deletions .github/workflows/lint_test_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: ./.github/workflows/rafiki/env-setup
- run: pnpm --filter openapi build
- run: pnpm --filter open-payments test

build:
Expand Down
7 changes: 5 additions & 2 deletions packages/open-payments/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dist/**/*"
],
"scripts": {
"build": "pnpm clean && tsc --build tsconfig.json",
"build:deps": "pnpm --filter openapi build",
"build": "pnpm build:deps && pnpm clean && tsc --build tsconfig.json",
"clean": "rm -fr dist/",
"generate:types": "npx ts-node scripts/generate-types.ts",
"prepack": "pnpm build",
Expand All @@ -21,6 +22,8 @@
"typescript": "^4.3.0"
},
"dependencies": {
"axios": "^1.1.2"
"axios": "^1.1.2",
"openapi": "workspace:../openapi",
"pino": "^8.4.2"
}
}
67 changes: 0 additions & 67 deletions packages/open-payments/src/client.test.ts

This file was deleted.

67 changes: 0 additions & 67 deletions packages/open-payments/src/client.ts

This file was deleted.

28 changes: 28 additions & 0 deletions packages/open-payments/src/client/ilp-stream-connection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { createILPStreamConnectionRoutes } from './ilp-stream-connection'
import { OpenAPI, HttpMethod, createOpenAPI } from 'openapi'
import config from '../config'
import { defaultAxiosInstance, silentLogger } from '../test/helpers'

describe('ilp-stream-connection', (): void => {
let openApi: OpenAPI

beforeAll(async () => {
openApi = await createOpenAPI(config.OPEN_PAYMENTS_OPEN_API_URL)
})

const axiosInstance = defaultAxiosInstance
const logger = silentLogger

describe('createILPStreamConnectionRoutes', (): void => {
test('calls createResponseValidator properly', async (): Promise<void> => {
jest.spyOn(openApi, 'createResponseValidator')

createILPStreamConnectionRoutes({ axiosInstance, openApi, logger })
expect(openApi.createResponseValidator).toHaveBeenCalledWith({
path: '/connections/{id}',
method: HttpMethod.GET
})
})
})
})
25 changes: 25 additions & 0 deletions packages/open-payments/src/client/ilp-stream-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { HttpMethod } from 'openapi'
import { ClientDeps } from '.'
import { getPath, ILPStreamConnection } from '../types'
import { GetArgs, get } from './requests'

export interface ILPStreamConnectionRoutes {
get(args: GetArgs): Promise<ILPStreamConnection>
}

export const createILPStreamConnectionRoutes = (
clientDeps: ClientDeps
): ILPStreamConnectionRoutes => {
const { axiosInstance, openApi, logger } = clientDeps

const getILPStreamConnectionValidator =
openApi.createResponseValidator<ILPStreamConnection>({
path: getPath('/connections/{id}'),
method: HttpMethod.GET
})

return {
get: (args: GetArgs) =>
get({ axiosInstance, logger }, args, getILPStreamConnectionValidator)
}
}
32 changes: 32 additions & 0 deletions packages/open-payments/src/client/incoming-payment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { createIncomingPaymentRoutes } from './incoming-payment'
import { OpenAPI, HttpMethod, createOpenAPI } from 'openapi'
import config from '../config'
import { defaultAxiosInstance, silentLogger } from '../test/helpers'

describe('incoming-payment', (): void => {
let openApi: OpenAPI

beforeAll(async () => {
openApi = await createOpenAPI(config.OPEN_PAYMENTS_OPEN_API_URL)
})

const axiosInstance = defaultAxiosInstance
const logger = silentLogger

describe('createIncomingPaymentRoutes', (): void => {
test('calls createResponseValidator properly', async (): Promise<void> => {
jest.spyOn(openApi, 'createResponseValidator')

createIncomingPaymentRoutes({
axiosInstance,
openApi,
logger
})
expect(openApi.createResponseValidator).toHaveBeenCalledWith({
path: '/incoming-payments/{id}',
method: HttpMethod.GET
})
})
})
})
25 changes: 25 additions & 0 deletions packages/open-payments/src/client/incoming-payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { HttpMethod } from 'openapi'
import { ClientDeps } from '.'
import { IncomingPayment, getPath } from '../types'
import { GetArgs, get } from './requests'

export interface IncomingPaymentRoutes {
get(args: GetArgs): Promise<IncomingPayment>
}

export const createIncomingPaymentRoutes = (
clientDeps: ClientDeps
): IncomingPaymentRoutes => {
const { axiosInstance, openApi, logger } = clientDeps

const getIncomingPaymentValidator =
openApi.createResponseValidator<IncomingPayment>({
path: getPath('/incoming-payments/{id}'),
method: HttpMethod.GET
})

return {
get: (args: GetArgs) =>
get({ axiosInstance, logger }, args, getIncomingPaymentValidator)
}
}
46 changes: 46 additions & 0 deletions packages/open-payments/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createOpenAPI, OpenAPI } from 'openapi'
import createLogger, { Logger } from 'pino'
import config from '../config'
import {
createIncomingPaymentRoutes,
IncomingPaymentRoutes
} from './incoming-payment'
import {
createILPStreamConnectionRoutes,
ILPStreamConnectionRoutes
} from './ilp-stream-connection'
import { createAxiosInstance } from './requests'
import { AxiosInstance } from 'axios'

export interface CreateOpenPaymentClientArgs {
requestTimeoutMs?: number
logger?: Logger
}

export interface ClientDeps {
axiosInstance: AxiosInstance
openApi: OpenAPI
logger: Logger
}

export interface OpenPaymentsClient {
incomingPayment: IncomingPaymentRoutes
ilpStreamConnection: ILPStreamConnectionRoutes
}

export const createClient = async (
args?: CreateOpenPaymentClientArgs
): Promise<OpenPaymentsClient> => {
const axiosInstance = createAxiosInstance({
requestTimeoutMs:
args?.requestTimeoutMs ?? config.DEFAULT_REQUEST_TIMEOUT_MS
})
const openApi = await createOpenAPI(config.OPEN_PAYMENTS_OPEN_API_URL)
const logger = args?.logger ?? createLogger()
const deps = { axiosInstance, openApi, logger }

return {
incomingPayment: createIncomingPaymentRoutes(deps),
ilpStreamConnection: createILPStreamConnectionRoutes(deps)
}
}
Loading

0 comments on commit e1a7f60

Please sign in to comment.