Skip to content

Commit

Permalink
663/mk/standardize op client requests (#971)
Browse files Browse the repository at this point in the history
* chore(open-payments): standardize incoming payment requests

* chore(open-payments): standardize outgoing payment requests

* chore(open-payments): update tests for ilp-stream-connection & payment pointers

* chore(open-payments): add test mock functions

* chore(open-payments): add remaining tests for request functions

* chore(open-payments): address comments

* chore(open-payments): use types suggestion

* chore(open-payments): remove extraneous tests

* chore(open-payments): address object copy suggestion
  • Loading branch information
mkurapov authored Jan 13, 2023
1 parent 421d89a commit b3ce7af
Show file tree
Hide file tree
Showing 15 changed files with 748 additions and 414 deletions.
23 changes: 11 additions & 12 deletions packages/open-payments/src/client/grant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { HttpMethod } from 'openapi'
import { RouteDeps } from '.'
import {
ResourceRequestArgs,
RouteDeps,
UnauthenticatedResourceRequestArgs
} from '.'
import {
getASPath,
InteractiveGrant,
Expand All @@ -13,21 +17,16 @@ export interface GrantRouteDeps extends RouteDeps {
client: string
}

interface PostArgs {
url: string
accessToken: string
}

export interface GrantRoutes {
request(
postArgs: Omit<PostArgs, 'accessToken'>,
postArgs: UnauthenticatedResourceRequestArgs,
args: Omit<GrantRequest, 'client'>
): Promise<InteractiveGrant | NonInteractiveGrant>
continue(
postArgs: PostArgs,
postArgs: ResourceRequestArgs,
args: GrantContinuationRequest
): Promise<NonInteractiveGrant>
cancel(postArgs: PostArgs): Promise<void>
cancel(postArgs: ResourceRequestArgs): Promise<void>
}

export const createGrantRoutes = (deps: GrantRouteDeps): GrantRoutes => {
Expand All @@ -49,7 +48,7 @@ export const createGrantRoutes = (deps: GrantRouteDeps): GrantRoutes => {

return {
request: (
{ url }: Omit<PostArgs, 'accessToken'>,
{ url }: UnauthenticatedResourceRequestArgs,
args: Omit<GrantRequest, 'client'>
) =>
post(
Expand All @@ -64,7 +63,7 @@ export const createGrantRoutes = (deps: GrantRouteDeps): GrantRoutes => {
requestGrantValidator
),
continue: (
{ url, accessToken }: PostArgs,
{ url, accessToken }: ResourceRequestArgs,
args: GrantContinuationRequest
) =>
post(
Expand All @@ -76,7 +75,7 @@ export const createGrantRoutes = (deps: GrantRouteDeps): GrantRoutes => {
},
continueGrantValidator
),
cancel: ({ url, accessToken }: PostArgs) =>
cancel: ({ url, accessToken }: ResourceRequestArgs) =>
deleteRequest(
deps,
{
Expand Down
49 changes: 41 additions & 8 deletions packages/open-payments/src/client/ilp-stream-connection.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { createILPStreamConnectionRoutes } from './ilp-stream-connection'
import { OpenAPI, HttpMethod, createOpenAPI } from 'openapi'
import path from 'path'
import { defaultAxiosInstance, silentLogger } from '../test/helpers'
import {
defaultAxiosInstance,
mockILPStreamConnection,
silentLogger
} from '../test/helpers'
import * as requestors from './requests'

jest.mock('./requests', () => {
return {
// https://jestjs.io/docs/jest-object#jestmockmodulename-factory-options
__esModule: true,
...jest.requireActual('./requests')
}
})

describe('ilp-stream-connection', (): void => {
let openApi: OpenAPI
Expand All @@ -15,14 +28,34 @@ describe('ilp-stream-connection', (): void => {
const axiosInstance = defaultAxiosInstance
const logger = silentLogger

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

describe('get', (): void => {
test('calls get method with correct validator', async (): Promise<void> => {
const mockResponseValidator = ({ path, method }) =>
path === '/connections/{id}' && method === HttpMethod.GET

jest
.spyOn(openApi, 'createResponseValidator')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.mockImplementation(mockResponseValidator as any)

const getSpy = jest
.spyOn(requestors, 'get')
.mockResolvedValueOnce(ilpStreamConnection)

await createILPStreamConnectionRoutes({
openApi,
axiosInstance,
logger
}).get({ url: ilpStreamConnection.id })

createILPStreamConnectionRoutes({ axiosInstance, openApi, logger })
expect(openApi.createResponseValidator).toHaveBeenCalledWith({
path: '/connections/{id}',
method: HttpMethod.GET
expect(getSpy).toHaveBeenCalledWith(
{ axiosInstance, logger },
{ url: ilpStreamConnection.id },
true
)
})
})
})
Expand Down
10 changes: 3 additions & 7 deletions packages/open-payments/src/client/ilp-stream-connection.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { HttpMethod } from 'openapi'
import { RouteDeps } from '.'
import { RouteDeps, UnauthenticatedResourceRequestArgs } from '.'
import { getRSPath, ILPStreamConnection } from '../types'
import { get } from './requests'

interface GetArgs {
url: string
}

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

export const createILPStreamConnectionRoutes = (
Expand All @@ -23,7 +19,7 @@ export const createILPStreamConnectionRoutes = (
})

return {
get: (args: GetArgs) =>
get: (args: UnauthenticatedResourceRequestArgs) =>
get({ axiosInstance, logger }, args, getILPStreamConnectionValidator)
}
}
Loading

0 comments on commit b3ce7af

Please sign in to comment.