Skip to content

Commit

Permalink
feat: use better regex for route parameters (#1393)
Browse files Browse the repository at this point in the history
  • Loading branch information
TommoLeedsy authored May 21, 2024
1 parent c1adaf7 commit ba24407
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
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

0 comments on commit ba24407

Please sign in to comment.