api-client/helpers/utils/EffectiveURL.ts

44 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-07-12 01:33:11 +00:00
import { combineLatest, Observable } from "rxjs"
import { map } from "rxjs/operators"
import { HoppRESTRequest } from "../types/HoppRESTRequest"
import { Environment } from "~/newstore/environments"
2021-07-13 05:37:29 +00:00
export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
2021-07-12 01:33:11 +00:00
/**
* The effective final URL.
*
* This contains path, params and environment variables all applied to it
*/
effectiveFinalURL: string
2021-07-14 19:36:15 +00:00
effectiveFinalHeaders: { key: string; value: string }[]
2021-07-12 01:33:11 +00:00
}
/**
* Creates an Observable Stream that emits HoppRESTRequests whenever
* the input streams emit a value
*
* @param request$ The request stream containing request data
* @param environment$ The environment stream containing environment data to apply
*
* @returns Observable Stream for the Effective Request Object
*/
export function getEffectiveRESTRequestStream(
request$: Observable<HoppRESTRequest>,
environment$: Observable<Environment>
): Observable<EffectiveHoppRESTRequest> {
return combineLatest([request$, environment$]).pipe(
map(([request, _env]) => {
// TODO: Change this
return {
...request,
effectiveFinalURL: request.endpoint,
2021-07-14 19:36:15 +00:00
effectiveFinalHeaders: request.headers.filter(
(x) =>
x.key !== "" && // Remove empty keys
x.active // Only active
),
2021-07-12 01:33:11 +00:00
}
})
)
}