Skip to content

Commit b040266

Browse files
committed
Address comments
1 parent 11b1e59 commit b040266

6 files changed

Lines changed: 77 additions & 94 deletions

File tree

src/adapter/basic.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,20 +468,16 @@ export class Adapter<
468468
fallbackReq,
469469
replySent,
470470
endpoint.transportRoutes.get(fallback.transportName),
471-
).then(
472-
(response) => ({ response }),
473-
(error) => ({ error }),
474471
)
475472

476473
try {
477474
return await this.handleSingleTransportRequest(req, replySent, transport)
478475
} catch (primaryError) {
479-
const fallbackResult = await fallbackResponsePromise
480-
if ('response' in fallbackResult) {
481-
return fallbackResult.response
476+
try {
477+
return await fallbackResponsePromise
478+
} catch (_fallbackError) {
479+
throw primaryError
482480
}
483-
484-
throw primaryError
485481
}
486482
}
487483

src/adapter/endpoint.ts

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,7 @@ export class AdapterEndpoint<T extends EndpointGenerics> implements AdapterEndpo
7979
this.overrides = params.overrides
8080
this.requestTransforms = [this.symbolOverrider.bind(this), ...(params.requestTransforms || [])]
8181

82-
// Validate fallback structure if it's defined
83-
Object.entries(this.fallbackTransport || {}).forEach(([primary, fallback]) => {
84-
if (primary === fallback) {
85-
throw new AdapterError({
86-
statusCode: 400,
87-
message: `Fallback transport "${fallback}" cannot be the same as primary transport.`,
88-
})
89-
}
90-
91-
if (!this.transportRoutes.get(fallback)) {
92-
throw new AdapterError({
93-
statusCode: 400,
94-
message: `No fallback transport found for key "${fallback}", must be one of ${JSON.stringify(
95-
this.transportRoutes.routeNames(),
96-
)}`,
97-
})
98-
}
99-
})
82+
this.validateFallbackTransport()
10083
}
10184

10285
/**
@@ -249,4 +232,40 @@ export class AdapterEndpoint<T extends EndpointGenerics> implements AdapterEndpo
249232
}
250233
return rawRequestBody.data?.transport
251234
}
235+
236+
private validateFallbackTransport() {
237+
if (this.cacheKeyGenerator && this.fallbackTransport) {
238+
throw new AdapterError({
239+
message: 'fallbackTransport not allowed for endpoints with cacheKeyGenerator',
240+
statusCode: 404,
241+
})
242+
}
243+
244+
Object.entries(this.fallbackTransport || {}).forEach(([primary, fallback]) => {
245+
if (primary === fallback) {
246+
throw new AdapterError({
247+
statusCode: 400,
248+
message: `Fallback transport "${fallback}" cannot be the same as primary transport.`,
249+
})
250+
}
251+
252+
if (!this.transportRoutes.get(primary)) {
253+
throw new AdapterError({
254+
statusCode: 400,
255+
message: `No primary transport found for key "${primary}", must be one of ${JSON.stringify(
256+
this.transportRoutes.routeNames(),
257+
)}`,
258+
})
259+
}
260+
261+
if (!this.transportRoutes.get(fallback)) {
262+
throw new AdapterError({
263+
statusCode: 400,
264+
message: `No fallback transport found for key "${fallback}", must be one of ${JSON.stringify(
265+
this.transportRoutes.routeNames(),
266+
)}`,
267+
})
268+
}
269+
})
270+
}
252271
}

src/util/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export type AdapterRequestContext<T> = {
3939
cacheKey: string
4040

4141
/** Fallback transport context to use if the primary transport is unable to return data */
42-
// TODO: Implement using this
4342
fallback?: {
4443
transportName: string
4544
cacheKey: string

src/validation/index.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,8 @@ export const validatorMiddleware: AdapterMiddlewareBuilder =
106106
req.requestContext = { ...req.requestContext, meta: { metrics } }
107107
}
108108

109-
const fallbackTransportName = endpoint.getFallbackTransportNameForRequest(
110-
req.requestContext.transportName,
111-
adapter.config.settings,
112-
)
113-
114109
// Now that all the transformations have been applied, all that's left is calculating the cache key
115110
if (endpoint.cacheKeyGenerator) {
116-
if (fallbackTransportName) {
117-
throw new AdapterInputError({
118-
message: 'fallbackTransport not supported for endpoints with cacheKeyGenerator',
119-
statusCode: 404,
120-
})
121-
}
122-
123111
let cacheKey
124112
cacheKey = endpoint.cacheKeyGenerator(req.requestContext.data)
125113
if (cacheKey.length > adapter.config.settings.MAX_COMMON_KEY_SIZE) {
@@ -149,6 +137,10 @@ export const validatorMiddleware: AdapterMiddlewareBuilder =
149137
transportName: req.requestContext.transportName,
150138
})
151139

140+
const fallbackTransportName = endpoint.getFallbackTransportNameForRequest(
141+
req.requestContext.transportName,
142+
adapter.config.settings,
143+
)
152144
if (fallbackTransportName) {
153145
req.requestContext.fallback = {
154146
transportName: fallbackTransportName,

test/cache/cache-key.test.ts

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -225,61 +225,6 @@ test.serial('builds fallback cache key from fallback transport name', async (t)
225225
)
226226
})
227227

228-
test.serial('custom cache key generator throws on fallback transport', async (t) => {
229-
const config = new AdapterConfig(
230-
{},
231-
{
232-
envDefaultOverrides: {
233-
TRANSPORT_FALLBACK_ENABLED: true,
234-
},
235-
},
236-
)
237-
const adapter = new Adapter({
238-
name: 'TEST',
239-
defaultEndpoint: 'test-custom-cache-key-fallback',
240-
config,
241-
endpoints: [
242-
new AdapterEndpoint<NopTransportTypes>({
243-
name: 'test-custom-cache-key-fallback',
244-
cacheKeyGenerator: (_) => `test:custom_cache_key`,
245-
transportRoutes: new TransportRoutes<NopTransportTypes>()
246-
.register(
247-
'primary',
248-
new (class extends NopTransport {
249-
override async foregroundExecute(
250-
req: AdapterRequest<NopTransportTypes['Parameters']>,
251-
) {
252-
return {
253-
data: null,
254-
statusCode: 200,
255-
result: (req.requestContext.fallback?.cacheKey ||
256-
req.requestContext.cacheKey) as unknown as null,
257-
timestamps: {
258-
providerDataRequestedUnixMs: 0,
259-
providerDataReceivedUnixMs: 0,
260-
providerIndicatedTimeUnixMs: undefined,
261-
},
262-
}
263-
}
264-
})(),
265-
)
266-
.register('fallback', new NopTransport()),
267-
defaultTransport: 'primary',
268-
fallbackTransport: { primary: 'fallback' },
269-
}),
270-
],
271-
})
272-
const testAdapter = await TestAdapter.start(adapter, t.context)
273-
274-
const response = await testAdapter.request({})
275-
276-
t.is(response.statusCode, 404)
277-
t.is(
278-
response.json().error.message,
279-
'fallbackTransport not supported for endpoints with cacheKeyGenerator',
280-
)
281-
})
282-
283228
test.serial('custom cache key is truncated if over max size', async (t) => {
284229
const response = await t.context.testAdapter.request({
285230
endpoint: 'test-custom-cache-key-long',

test/transports/routing.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,22 @@ test.serial('fallback transport must be registered', async (t) => {
738738
)
739739
})
740740

741+
test.serial("fallback's primary transport must be registered", async (t) => {
742+
t.throws(
743+
() =>
744+
new AdapterEndpoint<BaseEndpointTypes>({
745+
inputParameters,
746+
name: 'price',
747+
transportRoutes: transports,
748+
fallbackTransport: { invalid: 'WEBSOCKET' },
749+
}),
750+
{
751+
message:
752+
'No primary transport found for key "invalid", must be one of ["websocket","batch","sse"]',
753+
},
754+
)
755+
})
756+
741757
test.serial('fallback transport cannot match primary transport', async (t) => {
742758
t.throws(
743759
() =>
@@ -753,6 +769,22 @@ test.serial('fallback transport cannot match primary transport', async (t) => {
753769
)
754770
})
755771

772+
test.serial('fallback transport not allowed with cacheKeyGenerator', async (t) => {
773+
t.throws(
774+
() =>
775+
new AdapterEndpoint<BaseEndpointTypes>({
776+
inputParameters,
777+
name: 'price',
778+
transportRoutes: transports,
779+
fallbackTransport: { batch: 'WEBSOCKET' },
780+
cacheKeyGenerator: () => 'cache',
781+
}),
782+
{
783+
message: 'fallbackTransport not allowed for endpoints with cacheKeyGenerator',
784+
},
785+
)
786+
})
787+
756788
test.serial('transport override routes to correct Transport', async (t) => {
757789
axiosMock
758790
.onPost(`${restUrl}/price`, {

0 commit comments

Comments
 (0)