Skip to content

Commit 6b83e6d

Browse files
Disable includes.json files affects for endpoint:crypto/ crypto_lwba aliases (#368)
* remove includes.json requestTransform from crypto endpoint * crypto-lwba endpoint added * include supported endpoints to includes.json * fix lint error * ran prettier * backward compatibility added * prettier * remove extra case * test cases * prettier
1 parent 5beb69b commit 6b83e6d

2 files changed

Lines changed: 126 additions & 8 deletions

File tree

src/adapter/price.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type IncludeDetails = {
5959
from: string
6060
to: string
6161
inverse: boolean
62+
endpoints?: string[]
6263
}
6364
type IncludePair = {
6465
from: string
@@ -140,14 +141,23 @@ export class PriceAdapter<
140141
}
141142
const includesDetails = this.includesMap?.[requestData.base]?.[requestData.quote]
142143

143-
if (includesDetails) {
144-
requestData.base = includesDetails.from || requestData.base
145-
requestData.quote = includesDetails.to || requestData.quote
146-
}
147-
148-
const inverse = includesDetails?.inverse || false
149-
priceRequest.requestContext.priceMeta = {
150-
inverse,
144+
if (
145+
includesDetails?.endpoints === undefined ||
146+
includesDetails?.endpoints?.includes(req.requestContext.endpointName)
147+
) {
148+
if (includesDetails) {
149+
requestData.base = includesDetails.from || requestData.base
150+
requestData.quote = includesDetails.to || requestData.quote
151+
}
152+
153+
const inverse = includesDetails?.inverse || false
154+
priceRequest.requestContext.priceMeta = {
155+
inverse,
156+
}
157+
} else {
158+
throw new Error(
159+
`Endpoint ${req.requestContext.endpointName} not supported for ${requestData.base}/${requestData.quote} in includes.json`,
160+
)
151161
}
152162
}
153163

test/price.test.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,3 +668,111 @@ test('can create a crypto price endpoint with non-required base and quote and ex
668668

669669
t.truthy(adapter)
670670
})
671+
672+
test('does not process request if endpoint not supported in includes', async (t) => {
673+
const includes = [
674+
{
675+
from: 'ETH',
676+
to: 'USD',
677+
includes: [
678+
{
679+
from: 'USD',
680+
to: 'ETH',
681+
inverse: true,
682+
endpoints: ['forex'],
683+
},
684+
],
685+
},
686+
]
687+
688+
const mockResponse: AdapterResponse<PriceTestTypes['Response']> = {
689+
result: 1234,
690+
data: {
691+
result: 1234,
692+
},
693+
statusCode: 200,
694+
timestamps: {
695+
providerDataRequestedUnixMs: 0,
696+
providerDataReceivedUnixMs: 0,
697+
providerIndicatedTimeUnixMs: undefined,
698+
},
699+
}
700+
701+
const data = {
702+
base: 'ETH',
703+
quote: 'USD',
704+
}
705+
706+
const testAdapter = await buildAdapter(
707+
t.context,
708+
(req) => {
709+
t.deepEqual(req.requestContext.data, data)
710+
711+
return mockResponse
712+
},
713+
includes,
714+
)
715+
716+
const response = await testAdapter.request({
717+
...data,
718+
endpoint: 'test',
719+
})
720+
t.is(response.statusCode, 500)
721+
t.is(response.body, 'Endpoint test not supported for ETH/USD in includes.json')
722+
})
723+
724+
test('processes request if endpoint supported in includes', async (t) => {
725+
const includes = [
726+
{
727+
from: 'ETH',
728+
to: 'BTC',
729+
includes: [
730+
{
731+
from: 'BTC',
732+
to: 'ETH',
733+
inverse: true,
734+
endpoints: ['test'],
735+
},
736+
],
737+
},
738+
]
739+
740+
const mockResponse: AdapterResponse<PriceTestTypes['Response']> = {
741+
result: 1 / 1234,
742+
data: {
743+
result: 1 / 1234,
744+
},
745+
statusCode: 200,
746+
timestamps: {
747+
providerDataRequestedUnixMs: 0,
748+
providerDataReceivedUnixMs: 0,
749+
providerIndicatedTimeUnixMs: undefined,
750+
},
751+
}
752+
753+
const data = {
754+
base: 'ETH',
755+
quote: 'BTC',
756+
}
757+
758+
const testAdapter = await buildAdapter(
759+
t.context,
760+
(req) => {
761+
t.deepEqual(req.requestContext.data, {
762+
base: 'BTC',
763+
quote: 'ETH',
764+
})
765+
766+
return mockResponse
767+
},
768+
includes,
769+
)
770+
771+
const response = await testAdapter.request({
772+
...data,
773+
endpoint: 'test',
774+
})
775+
t.is(response.statusCode, 200)
776+
t.is(response.json().result, 1234)
777+
t.is(response.json().data.result, 1234)
778+
})

0 commit comments

Comments
 (0)