-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(open-payments): adding open-payments package (#669)
* 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): use updated RS spec * feat(open-payments): building open-api package during workflow * Revert "feat(open-payments): building open-api package during workflow" This reverts commit e11ec65.
- Loading branch information
Showing
13 changed files
with
1,214 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict' | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const baseConfig = require('../../jest.config.base.js') | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const packageName = require('./package.json').name | ||
|
||
module.exports = { | ||
...baseConfig, | ||
clearMocks: true, | ||
roots: [`<rootDir>/packages/${packageName}`], | ||
testRegex: `(packages/${packageName}/.*/__tests__/.*|\\.(test|spec))\\.tsx?$`, | ||
moduleDirectories: [`node_modules`, `packages/${packageName}/node_modules`], | ||
modulePaths: [`<rootDir>/packages/${packageName}/src/`], | ||
id: packageName, | ||
displayName: packageName, | ||
rootDir: '../..' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "open-payments", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist/**/*" | ||
], | ||
"scripts": { | ||
"build": "pnpm clean && tsc --build tsconfig.json", | ||
"clean": "rm -fr dist/", | ||
"generate:types": "npx ts-node scripts/generate-types.ts", | ||
"prepack": "pnpm build", | ||
"test": "jest --passWithNoTests" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.7.12", | ||
"nock": "^13.2.9", | ||
"openapi-typescript": "^4.5.0", | ||
"ts-node": "^10.7.0", | ||
"typescript": "^4.3.0" | ||
}, | ||
"dependencies": { | ||
"axios": "^1.1.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import fs from 'fs' | ||
import openapiTS from 'openapi-typescript' | ||
import config from '../src/config' | ||
;(async () => { | ||
try { | ||
const output = await openapiTS(config.OPEN_PAYMENTS_OPEN_API_URL) | ||
const fileName = 'src/generated/types.ts' | ||
|
||
fs.writeFile(fileName, output, (error) => { | ||
if (error) { | ||
console.log(`Error when writing types to ${fileName}`, { error }) | ||
} | ||
}) | ||
} catch (error) { | ||
console.log('Error when generating types', { | ||
error | ||
}) | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { createAxiosInstance, get } from './client' | ||
import nock from 'nock' | ||
|
||
describe('open-payments', (): void => { | ||
describe('createAxiosInstance', (): void => { | ||
test('sets timeout properly', async (): Promise<void> => { | ||
expect(createAxiosInstance({ timeout: 1000 }).defaults.timeout).toBe(1000) | ||
}) | ||
test('sets Content-Type header properly', async (): Promise<void> => { | ||
expect( | ||
createAxiosInstance().defaults.headers.common['Content-Type'] | ||
).toBe('application/json') | ||
}) | ||
}) | ||
|
||
describe('get', (): void => { | ||
const axiosInstance = createAxiosInstance() | ||
const baseUrl = 'http://localhost:1000' | ||
|
||
beforeEach(() => { | ||
jest.spyOn(axiosInstance, 'get') | ||
}) | ||
|
||
test('sets headers properly if accessToken provided', async (): Promise<void> => { | ||
nock(baseUrl) | ||
.get('/incoming-payment') | ||
.reply(200, () => ({ | ||
validReceiver: 0 | ||
})) | ||
|
||
await get(axiosInstance, { | ||
url: `${baseUrl}/incoming-payment`, | ||
accessToken: 'accessToken' | ||
}) | ||
|
||
expect(axiosInstance.get).toHaveBeenCalledWith( | ||
`${baseUrl}/incoming-payment`, | ||
{ | ||
headers: { | ||
Authorization: 'GNAP accessToken', | ||
Signature: 'TODO', | ||
'Signature-Input': 'TODO' | ||
} | ||
} | ||
) | ||
}) | ||
|
||
test('sets headers properly if accessToken is not provided', async (): Promise<void> => { | ||
nock(baseUrl) | ||
.get('/incoming-payment') | ||
.reply(200, () => ({ | ||
validReceiver: 0 | ||
})) | ||
|
||
await get(axiosInstance, { | ||
url: `${baseUrl}/incoming-payment` | ||
}) | ||
|
||
expect(axiosInstance.get).toHaveBeenCalledWith( | ||
`${baseUrl}/incoming-payment`, | ||
{ | ||
headers: {} | ||
} | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ILPStreamConnection, IncomingPayment } from './types' | ||
import axios, { AxiosInstance } from 'axios' | ||
import config from './config' | ||
|
||
interface CreateOpenPaymentClientArgs { | ||
timeout?: number | ||
} | ||
|
||
interface GetArgs { | ||
url: string | ||
accessToken?: string | ||
} | ||
|
||
export interface OpenPaymentsClient { | ||
incomingPayment: { | ||
get(args: GetArgs): Promise<IncomingPayment> | ||
} | ||
ilpStreamConnection: { | ||
get(args: GetArgs): Promise<ILPStreamConnection> | ||
} | ||
} | ||
|
||
export const get = async <T>( | ||
axios: AxiosInstance, | ||
args: GetArgs | ||
): Promise<T> => { | ||
const { url, accessToken } = args | ||
|
||
const { data } = await axios.get(url, { | ||
headers: accessToken | ||
? { | ||
Authorization: `GNAP ${accessToken}`, | ||
Signature: 'TODO', | ||
'Signature-Input': 'TODO' | ||
} | ||
: {} | ||
}) | ||
|
||
return data | ||
} | ||
|
||
export const createAxiosInstance = ( | ||
args?: CreateOpenPaymentClientArgs | ||
): AxiosInstance => { | ||
const axiosInstance = axios.create({ | ||
timeout: args?.timeout ?? config.DEFAULT_REQUEST_TIMEOUT | ||
}) | ||
|
||
axiosInstance.defaults.headers.common['Content-Type'] = 'application/json' | ||
|
||
return axiosInstance | ||
} | ||
|
||
export const createClient = ( | ||
args?: CreateOpenPaymentClientArgs | ||
): OpenPaymentsClient => { | ||
const axios = createAxiosInstance(args) | ||
|
||
return { | ||
incomingPayment: { | ||
get: (args: GetArgs) => get<IncomingPayment>(axios, args) | ||
}, | ||
ilpStreamConnection: { | ||
get: (args: GetArgs) => get<ILPStreamConnection>(axios, args) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default { | ||
OPEN_PAYMENTS_OPEN_API_URL: | ||
'https://raw.githubusercontent.com/interledger/open-payments/7bb2e6a03d7dfe7ecb0553afb6c70741317bb489/openapi/RS/openapi.yaml', | ||
DEFAULT_REQUEST_TIMEOUT: 3_000 | ||
} |
Oops, something went wrong.