Skip to content

Commit

Permalink
Merge branch 'main' into bc/upgrade-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
BlairCurrey committed Jan 16, 2024
2 parents 3a4e342 + ccad2cc commit 4043c3c
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 119 deletions.
16 changes: 7 additions & 9 deletions packages/auth/src/graphql/resolvers/page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,17 @@ export const getPageTests = <T extends Model, M extends BaseModel>({
const toConnection = (query: ApolloQueryResult<T>): Connection<T> => {
if (query.data) {
if (parent) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (query.data[parent.query]) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return query.data[parent.query][pagedQuery]
const parentData = query.data[parent.query as keyof typeof query.data]

if (parentData) {
return (parentData as Record<string, Connection<T>>)[pagedQuery]
} else {
throw new Error(`Parent ${parent.query} was empty`)
}
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return query.data[pagedQuery]
return query.data[
pagedQuery as keyof typeof query.data
] as Connection<T>
}
} else {
throw new Error('Data was empty')
Expand Down
8 changes: 2 additions & 6 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ export const start = async (
logger.info('completed graceful shutdown.')
process.exit(0)
} catch (err) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const errInfo = err instanceof Error && error.stack ? err.stack : err
const errInfo = err instanceof Error && err.stack ? err.stack : err
logger.error({ err: errInfo }, 'error while shutting down')
process.exit(1)
}
Expand All @@ -255,9 +253,7 @@ export const start = async (
logger.info('completed graceful shutdown.')
process.exit(0)
} catch (err) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const errInfo = err instanceof Error && error.stack ? err.stack : err
const errInfo = err instanceof Error && err.stack ? err.stack : err
logger.error({ err: errInfo }, 'error while shutting down')
process.exit(1)
}
Expand Down
4 changes: 0 additions & 4 deletions packages/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,6 @@ export class App {
quoteRoutes.get
)

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
router.get(
WALLET_ADDRESS_PATH + '/jwks.json',
createWalletAddressMiddleware(),
Expand All @@ -568,8 +566,6 @@ export class App {

// Add the wallet address query route last.
// Otherwise it will be matched instead of other Open Payments endpoints.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
router.get(
WALLET_ADDRESS_PATH,
createWalletAddressMiddleware(),
Expand Down
16 changes: 7 additions & 9 deletions packages/backend/src/graphql/resolvers/page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,17 @@ export const getPageTests = <T extends Model, M extends BaseModel>({
const toConnection = (query: ApolloQueryResult<T>): Connection<T> => {
if (query.data) {
if (parent) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (query.data[parent.query]) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return query.data[parent.query][pagedQuery]
const parentData = query.data[parent.query as keyof typeof query.data]

if (parentData) {
return (parentData as Record<string, Connection<T>>)[pagedQuery]
} else {
throw new Error(`Parent ${parent.query} was empty`)
}
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return query.data[pagedQuery]
return query.data[
pagedQuery as keyof typeof query.data
] as Connection<T>
}
} else {
throw new Error('Data was empty')
Expand Down
7 changes: 4 additions & 3 deletions packages/backend/src/open_payments/auth/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ export function createTokenIntrospectionMiddleware({
) {
ctx.grant = {
id: tokenInfo.grant,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
limits: access['limits'] ? parseLimits(access['limits']) : undefined
limits:
'limits' in access && access.limits
? parseLimits(access.limits)
: undefined
}
}
await next()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MockServerResponse
} from '.'
import { ILPContext } from '..'
import { ServerResponse, IncomingMessage } from 'http'

export type Options<StateT, CustomT> = {
app?: Koa<StateT, CustomT>
Expand All @@ -20,9 +21,10 @@ export function createContext<StateT = unknown, CustomT = unknown>(
): Koa.ParameterizedContext<StateT, CustomT> {
const app = options.app || new Koa<StateT, CustomT>()
const req = new MockIncomingMessage(options.req || {})
const res = new MockServerResponse(req, options.res)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const res = new MockServerResponse(
req,
options.res
) as unknown as ServerResponse<IncomingMessage>
const context = app.createContext<StateT>(req, res)

Object.keys(options).forEach((key) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion, @typescript-eslint/ban-ts-comment */
import {
IncomingMessage,
IncomingHttpHeaders,
Expand All @@ -9,8 +8,10 @@ import {
import { Transform } from 'stream'
import { Socket } from 'net'

type GenericOptionValues = string | undefined | string[] | IncomingHttpHeaders

export interface MockIncomingMessageOptions {
[key: string]: string | undefined | string[] | IncomingHttpHeaders
[key: string]: GenericOptionValues
method?: string
url?: string
headers?: IncomingHttpHeaders
Expand All @@ -21,12 +22,10 @@ export class MockIncomingMessage extends Transform {
readonly httpVersion = '1.1'
readonly httpVersionMajor = 1
readonly httpVersionMinor = 1
// @ts-ignore: Property has no initializer and is not definitely assigned in the constructor.
id: string | number | Record<string, unknown>
id!: string | number | Record<string, unknown>
aborted = false
complete = false
// @ts-ignore: Property has no initializer and is not definitely assigned in the constructor.
connection: Socket
connection!: Socket
headers: IncomingHttpHeaders
rawHeaders: string[]
trailers: { [key: string]: string | undefined } = {}
Expand All @@ -41,8 +40,7 @@ export class MockIncomingMessage extends Transform {
url?: string | undefined
statusCode?: number | undefined
statusMessage?: string | undefined
// @ts-ignore: Property has no initializer and is not definitely assigned in the constructor.
socket: Socket
socket!: Socket

private _failError?: Error

Expand All @@ -66,8 +64,8 @@ export class MockIncomingMessage extends Transform {
const reservedOptions = ['method', 'url', 'headers', 'rawHeaders']
Object.keys(options).forEach((key) => {
if (reservedOptions.indexOf(key) === -1) {
// @ts-ignore
this[key] = options[key]
;(this as unknown as Record<string, GenericOptionValues>)[key] =
options[key]
}
})

Expand Down Expand Up @@ -116,12 +114,10 @@ export class MockServerResponse extends Transform {
sendDate = true
finished = false
headersSent = false
// @ts-ignore: Property has no initializer and is not definitely assigned in the constructor.
connection: Socket
connection!: Socket
socket: Socket | null = null

// @ts-ignore: Property has no initializer and is not definitely assigned in the constructor.
setTimeout: (msecs: number, callback?: () => void) => this
setTimeout!: (msecs: number, callback?: () => void) => this
setHeader = (name: string, value: number | string | string[]): void => {
this._headers[name.toLowerCase()] = value
}
Expand All @@ -146,14 +142,10 @@ export class MockServerResponse extends Transform {
delete this._headers[name.toLowerCase()]
}

// @ts-ignore: Property has no initializer and is not definitely assigned in the constructor.
addTrailers: (headers: OutgoingHttpHeaders | Array<[string, string]>) => void
// @ts-ignore: Property has no initializer and is not definitely assigned in the constructor.
flushHeaders: () => void
// @ts-ignore: Property has no initializer and is not definitely assigned in the constructor.
assignSocket: (socket: Socket) => void
// @ts-ignore: Property has no initializer and is not definitely assigned in the constructor.
detachSocket: (socket: Socket) => void
addTrailers!: (headers: OutgoingHttpHeaders | Array<[string, string]>) => void
flushHeaders!: () => void
assignSocket!: (socket: Socket) => void
detachSocket!: (socket: Socket) => void

writeContinue = (callback?: () => void): void => {
if (callback) callback()
Expand All @@ -169,7 +161,6 @@ export class MockServerResponse extends Transform {
reasonPhrase = undefined
}
this.statusCode = statusCode
// @ts-ignore
this.statusMessage = reasonPhrase || STATUS_CODES[statusCode] || 'unknown'
if (headers) {
for (const [name, header] of Object.entries(headers)) {
Expand Down
54 changes: 1 addition & 53 deletions packages/backend/src/payment-method/ilp/spsp/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import {
spspMiddleware,
SPSPConnectionContext,
SPSPWalletAddressContext
} from './middleware'
import { spspMiddleware, SPSPWalletAddressContext } from './middleware'
import { setup } from '../../../open_payments/wallet_address/model.test'
import { Config } from '../../../config/app'
import { IocContract } from '@adonisjs/fold'
import { initIocContainer } from '../../..'
import { AppServices } from '../../../app'
import { createTestApp, TestContainer } from '../../../tests/app'
import { createAsset } from '../../../tests/asset'
import { createContext } from '../../../tests/context'
import { createIncomingPayment } from '../../../tests/incomingPayment'
import { createWalletAddress } from '../../../tests/walletAddress'
import { truncateTables } from '../../../tests/tableManager'

Expand Down Expand Up @@ -78,50 +72,4 @@ describe('SPSP Middleware', (): void => {
})
})
})

describe('Incoming Payment Connection', (): void => {
let ctx: SPSPConnectionContext

beforeEach(async (): Promise<void> => {
ctx = createContext<SPSPConnectionContext>(
{
headers: {
Accept: 'application/json'
}
},
{}
)
const asset = await createAsset(deps)
const { id: walletAddressId } = await createWalletAddress(deps, {
assetId: asset.id
})
const incomingPayment = await createIncomingPayment(deps, {
walletAddressId
})
ctx.incomingPayment = incomingPayment
ctx.container = deps
})

test('calls next for non-SPSP request', async (): Promise<void> => {
await expect(spspMiddleware(ctx, next)).resolves.toBeUndefined()
expect(next).toHaveBeenCalled()
})

test('calls SPSP route for SPSP query', async (): Promise<void> => {
const spspRoutes = await ctx.container.use('spspRoutes')
const spspSpy = jest
.spyOn(spspRoutes, 'get')
.mockResolvedValueOnce(undefined)

ctx.headers['accept'] = 'application/spsp4+json'
await expect(spspMiddleware(ctx, next)).resolves.toBeUndefined()
expect(spspSpy).toHaveBeenCalledTimes(1)
expect(next).not.toHaveBeenCalled()
expect(ctx.paymentTag).toEqual(ctx.incomingPayment.id)
expect(ctx.asset).toEqual({
code: ctx.incomingPayment.asset.code,
scale: ctx.incomingPayment.asset.scale
})
})
})
})
11 changes: 2 additions & 9 deletions packages/backend/src/payment-method/ilp/spsp/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { AppContext, WalletAddressContext, SPSPContext } from '../../../app'
import { IncomingPayment } from '../../../open_payments/payment/incoming/model'

export type SPSPConnectionContext = AppContext &
SPSPContext & {
walletAddress?: never
incomingPayment: IncomingPayment
}
import { WalletAddressContext, SPSPContext } from '../../../app'

export type SPSPWalletAddressContext = WalletAddressContext &
SPSPContext & {
incomingPayment?: never
}

export const spspMiddleware = async (
ctx: SPSPConnectionContext | SPSPWalletAddressContext,
ctx: SPSPWalletAddressContext,
next: () => Promise<unknown>
): Promise<void> => {
// Fall back to legacy protocols if client doesn't support Open Payments.
Expand Down

0 comments on commit 4043c3c

Please sign in to comment.