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

fix: ensure bundled version of path-to-regexp is used #74

Merged
merged 2 commits into from
Mar 9, 2022
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: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// Allow async-await
"generator-star-spacing": "off",
// Do not allow console.logs etc...
"no-console": 1,
"no-console": ["error", { "allow": ["warn", "error"] }],
// Disallow multiple spaces unless in the EOL comments
"no-multi-spaces": ["error", { "ignoreEOLComments": true }],
// At most one empty line
Expand Down
2 changes: 2 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ export default async function NeoModule(moduleOptions) {
apiConfig: JSON.stringify(moduleOptions)
}
});

this.options.alias['~path-to-regexp'] = require.resolve('path-to-regexp');
};
9 changes: 7 additions & 2 deletions lib/plugins/api.template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from 'vue';
import { compile as compilePath } from 'path-to-regexp';
import { compile as compilePath } from '~path-to-regexp';
import ApiHandler from '<%= options.apiHandlerFile %>';

<% if (options.successHandlerFile) { %>
Expand Down Expand Up @@ -48,7 +48,12 @@ function generateAPI(controllerMapping, ctx) {
if (context && context.path && context.verb) {
const pathCompiler = compilePath(context.path, { encode: encodeURIComponent });
api[key] = function ({params, ...values} = {}) {
const compiledPath = pathCompiler(params)
let compiledPath;
try {
compiledPath = pathCompiler(params);
} catch (error) {
throw new Error(`Error calling the nuxt-neo API (name: ${key}, path: ${context.path}): ${error.message}`);
}
return ApiHandler(
compiledPath,
context.verb,
Expand Down
7 changes: 5 additions & 2 deletions lib/utility/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,11 @@ export async function controllerMappingServerSide(directory, req, options, ctx)
const controllerMiddleware = getControllerMiddleware(ControllerClass);

return function ({ params, body, query } = {}) {
// Throws if params don't validate against the path.
pathCompiler(params);
try {
pathCompiler(params);
} catch (error) {
throw new Error(`Error calling the nuxt-neo API (name: ${actionName}, path: ${action.path}): ${error.message}`);
}

return middlewareHandler(apiMiddleware, req)
.then(function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/api.flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test('Test hybrid api data flow client side', async (t) => {
t.is(errorMessage.textContent, 'Forced error');
t.falsy(errorMessageGetOptional);
t.truthy(errorMessageGetWithoutParam);
t.is(errorMessageGetWithoutParam.textContent, 'Expected "id" to be a string');
t.true(errorMessageGetWithoutParam.textContent.includes('Error calling the nuxt-neo API'));

changePath.dispatchEvent(new window.Event('click'));
await new Promise(resolve => setTimeout(resolve, 2000)); // wait for API request
Expand All @@ -91,7 +91,7 @@ test('Test hybrid api data flow client side', async (t) => {
changePathWithoutParam.dispatchEvent(new window.Event('click'));
await new Promise(resolve => setTimeout(resolve, 2000)); // wait for API request
errorMessage = window.document.querySelector('.index .error-message');
t.is(errorMessage.textContent, 'Expected "id" to be a string'); // error when no required param is given
t.true(errorMessage.textContent.includes('Error calling the nuxt-neo API')); // error when no required param is given

getOptional.dispatchEvent(new window.Event('click'));
await new Promise(resolve => setTimeout(resolve, 2000)); // wait for API request
Expand Down
7 changes: 6 additions & 1 deletion tests/fixtures/layouts/error.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

<script>
export default {
props: ['error']
props: {
error: {
type: Object,
default: null
}
}
};
</script>