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

feat: use better regex for route parameters #1393

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ export const VERBS_WITH_BODY = [

export const URL_REGEX =
/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;

export const TEMPLATE_TAG_REGEX = /\${(.+?)}/g; // For replace of 'thing' ${thing}
2 changes: 1 addition & 1 deletion packages/core/src/getters/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { camel, sanitize } from '../utils';
import { TEMPLATE_TAG_REGEX } from '../constants';

const TEMPLATE_TAG_REGEX = /\${(\w+)}/g; // For replace of 'thing' ${thing}
const TEMPLATE_TAG_IN_PATH_REGEX = /\/([\w]+)(?:\$\{)/g; // all dynamic parts of path

const hasParam = (path: string): boolean => /[^{]*{[\w*_-]*}.*/.test(path);
Expand Down
3 changes: 2 additions & 1 deletion packages/query/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
upath,
GetterProps,
GetterPropType,
TEMPLATE_TAG_REGEX,
} from '@orval/core';
import chalk from 'chalk';

Expand Down Expand Up @@ -122,7 +123,7 @@ export const wrapRouteParameters = (
route: string,
prepend: string,
append: string,
): string => route.replaceAll(/\${(.+?)}/g, `\${${prepend}$1${append}}`);
): string => route.replaceAll(TEMPLATE_TAG_REGEX, `\${${prepend}$1${append}}`);

export const makeRouteSafe = (route: string): string =>
wrapRouteParameters(route, 'encodeURIComponent(String(', '))');
Expand Down