diff --git a/packages/core-utils/src/query-gen.ts b/packages/core-utils/src/query-gen.ts index b342bc1cc..dcfc4fdb1 100644 --- a/packages/core-utils/src/query-gen.ts +++ b/packages/core-utils/src/query-gen.ts @@ -3,6 +3,7 @@ import { print } from "graphql"; import { ModeSetting, ModeSettingValues, + PlanModesInput, TransportMode } from "@opentripplanner/types"; @@ -20,7 +21,7 @@ type OTPQueryParams = { date?: string; departArrive?: string; from: LonLatOutput & { name?: string }; - modes: TransportMode[]; + modes: PlanModesInput; modeSettings: ModeSetting[]; numItineraries?: number; omitCanceled?: boolean; @@ -30,6 +31,10 @@ type OTPQueryParams = { via?: OTPViaLocationInput[] }; +type GenerateCombinationsParams = Omit & { + modes: TransportMode[]; +}; + type GraphQLQuery = { query: string; variables: Record; @@ -78,24 +83,34 @@ export function extractAdditionalModes( /** * Generates a list of queries for OTP based on planConnection config - * @param params OTP Query Params + * @param params OTP query params with mode definitions to expand * @returns Set of parameters to generate queries */ -export function generateCombinations(params: OTPQueryParams): OTPQueryParams[] { +export function generateCombinations( + params: GenerateCombinationsParams +): OTPQueryParams[] { const completeModeList = [ ...extractAdditionalModes(params.modeSettings, params.modes), ...params.modes ]; // List of the transit *submodes* that are included in the input params - const transitModes = completeModeList - .filter(mode => isTransit(mode.mode) && mode.mode !== "TRANSIT") + const transitModes = completeModeList.filter( + mode => isTransit(mode.mode) && mode.mode !== "TRANSIT" + ); - // @ts-expect-error types packagge fail return completeModeList .filter(mode => !!mode.input) - // @ts-expect-error types packagge fail - .map(mode => ({ ...params, modes: { ...mode.input, transit: { ...mode?.input?.transit, ...transitModes.length > 0 && { transit: transitModes } } } })); + .map(mode => ({ + ...params, + modes: { + ...mode.input, + transit: { + ...mode?.input?.transit, + ...(transitModes.length > 0 && { transit: transitModes }) + } + } + })); } /** diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 9f39a6bc8..4bc5ee84e 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -832,22 +832,30 @@ export type ModeSetting = ( ModeSettingBase; export type ModeSettingValues = Record; +export type PlanModesInput = { + direct?: string[]; + directOnly?: boolean; + transit?: PlanTransitModesInput; + transitOnly?: boolean; +}; + +export type PlanTransitModesInput = { + access?: string[]; + egress?: string[]; + transfer?: string[]; + transit?: PlanTransitModePreferenceInput[]; +}; + +export type PlanTransitModePreferenceInput = { + mode: string; + cost?: { reluctance: number }; + replacement?: { requirement?: string }; +}; + /** - * TransportModes correspond with the OTP GraphQL TransportMode. - * Could be anything from walk, bike to transit, tram, or bus. + * A mode selected by the UI, with an optional PlanConnection input for a query. */ -export type TransportMode = { mode: string; input?: ModeInput }; - -export type ModeInput = - | { - access: string[]; - egress: string[]; - transfer?: string[]; // Not actually, but for our config yes - transit: { mode: string }[]; - } - | { - direct: string[]; - }; +export type TransportMode = { mode: string; input?: PlanModesInput }; /** * This is a combination of transportation modes,