Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate URL for oneToMany using comma separated style for many-array #170

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Company, Vehicle } from './types';
import { Coordinates, Location } from './location';
import { MAX_MATCHING_DISTANCE, MAX_TRAVEL_SECONDS, MOTIS_BASE_URL } from './constants';
import { coordinatesToPlace, coordinatesToStr } from './motisUtils';
import { coordinatesToPlace, coordinatesToStr, customQuerySerializer } from './motisUtils';
import { type Duration, type PlanResponse } from './motis/types.gen';
import { oneToMany as oneToManyMotis, plan as planMotis } from './motis/services.gen';
import { secondsToMs } from './time_utils';
Expand Down Expand Up @@ -122,6 +122,7 @@ export const oneToMany = async (
): Promise<number[]> => {
return await oneToManyMotis({
baseUrl: MOTIS_BASE_URL,
querySerializer: customQuerySerializer,
query: {
one: coordinatesToStr(one),
many: many.map(coordinatesToStr),
Expand Down
28 changes: 28 additions & 0 deletions src/lib/motisUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,31 @@ export const coordinatesToStr = (c: Coordinates) => {
export const coordinatesToPlace = (c: Coordinates) => {
return `${c.lat},${c.lng},0`;
};

export const customQuerySerializer = (params: Record<string, unknown>): string => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ich hatte die Option nicht gesehen, stimmt das ist deutlich einfacher und erfüllt den selben Zweck

const qs: string[] = [];

const append = (key: string, value: unknown) => {
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
};

const encodePair = (key: string, value: unknown) => {
if (value === undefined || value === null) {
return;
}

if (value instanceof Date) {
append(key, value.toISOString());
} else if (Array.isArray(value)) {
append(key, value.join(','));
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => encodePair(`${key}[${k}]`, v));
} else {
append(key, value);
}
};

Object.entries(params).forEach(([key, value]) => encodePair(key, value));

return qs.length ? `?${qs.join('&')}` : '';
};