Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

663/mk/standardize op client requests #971

Merged
merged 10 commits into from
Jan 13, 2023
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)
Comment on lines +39 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check that the spy is called?

Copy link
Contributor Author

@mkurapov mkurapov Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already testing this (albeit indirectly): if the createResponseValidator doesn't get the proper path and method when its called, the test will fail based on the expectation of true on L57

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the true! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the easiest to see when first reading it, but that was the best solution I could come up with to test the correct validator was passed in


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