Skip to content

Commit

Permalink
feat(params): allow [_-] in url params (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
CPatchane authored Feb 20, 2022
1 parent e0ccc13 commit 7cb7112
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
run: yarn
- name: build
run: yarn build
- name: unit tests
run: yarn test --run
- name: tests install
working-directory: ./tests
run: yarn
Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
. "$(dirname "$0")/_/husky.sh"

yarn lint
yarn test --run
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"build": "tsup ./src/bin/orval.ts ./src/index.ts --minify --clean --dts --splitting",
"dev": "tsup ./src/bin/orval.ts ./src/index.ts --clean --watch src --onSuccess 'yarn generate-api'",
"lint": "eslint src/**/*.ts",
"test": "vitest --global test.ts",
"format": "prettier --write 'src/**/*.{js,ts}'",
"prerelease": "yarn build && cd ./tests && yarn generate && yarn build",
"release": "dotenv release-it",
Expand Down Expand Up @@ -88,6 +89,7 @@
"rimraf": "^3.0.2",
"tsup": "^5.11.13",
"typescript": "^4.5.5",
"vitest": "^0.3.6",
"zx": "^4.2.0"
},
"dependencies": {
Expand Down
7 changes: 5 additions & 2 deletions src/core/getters/params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ContextSpecs } from '../../types';
import { GetterParameters, GetterParams } from '../../types/getters';
import { camel } from '../../utils/case';
import { sanitize, stringify } from '../../utils/string';
import { resolveValue } from '../resolvers/value';

Expand Down Expand Up @@ -40,7 +41,9 @@ export const getParams = ({
return Promise.all(
params.map(async (p) => {
const pathParam = pathParams.find(
({ parameter }) => sanitize(parameter.name) === p,
({ parameter }) =>
camel(sanitize(parameter.name, { underscore: true, dash: true })) ===
p,
);

if (!pathParam) {
Expand All @@ -55,7 +58,7 @@ export const getParams = ({
schema,
} = pathParam.parameter;

const name = sanitize(nameWithoutSanitize);
const name = sanitize(camel(nameWithoutSanitize));

if (!schema) {
return {
Expand Down
16 changes: 16 additions & 0 deletions src/core/getters/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getRoute } from './route';

describe('getRoute getter', () => {
[
['/api/test/{id}', '/api/test/${id}'],
['/api/test/{user_id}', '/api/test/${userId}'],
['/api/test/{locale}.js', '/api/test/${locale}.js'],
['/api/test/i18n-{locale}.js', '/api/test/i18n-${locale}.js'],
['/api/test/{param1}-{param2}.js', '/api/test/${param1}-${param2}.js'],
['/api/test/user{param1}-{param2}.html', '/api/test/user${param1}-${param2}.html']
].forEach(([input, expected]) => {
it(`should process ${input} to ${expected}`, () => {
expect(getRoute(input)).toBe(expected);
});
});
});
24 changes: 13 additions & 11 deletions src/core/getters/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { camel } from '../../utils/case';
import { sanitize } from '../../utils/string';

const getRoutePath = (path: string) => {
return path.split('').reduce((acc, letter) => {
if (letter === '{') {
return acc + '${';
}

if (letter === '}') {
return acc + '}';
}
const hasParam = (path: string) => /[^{]*{[\w_-]*}.*/.test(path)

return acc + sanitize(letter, { dot: true });
}, '');
const getRoutePath = (path: string) => {
const matches = path.match(/([^{]*){?([\w_-]*)}?(.*)/)
if (!matches?.length) return path // impossible due to regexp grouping here, but for TS
const prev = matches[1]
const param = camel(sanitize(matches[2], { underscore: true, dash: true, dot: true }))
const next: string = hasParam(matches[3]) ? getRoutePath(matches[3]) : matches[3]
if (hasParam(path)) {
return `${prev}\${${param}}${next}`
} else {
return `${prev}${param}${next}`
}
};

export const getRoute = (route: string) => {
Expand Down
Loading

1 comment on commit 7cb7112

@vercel
Copy link

@vercel vercel bot commented on 7cb7112 Feb 20, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.