kalamata icon indicating copy to clipboard operation
kalamata copied to clipboard

Bug: Hooks only work when model name is the same as endpoint name

Open nabilfreeman opened this issue 7 years ago • 6 comments

This commit:

https://github.com/mikec/kalamata/commit/bdd45d6a6c92492afed5e3b3d69685fd05e65a20

appears to have broken hooks in situations where the lowercase modelName would not be the same as the endpoint name.

I think this is because on this line, the hooks for all the routes are being retrieved with the key modelNameLower:

https://github.com/mikec/kalamata/blob/bdd45d6a6c92492afed5e3b3d69685fd05e65a20/src/index.js#L52

var beforeHooks = hooks[modelNameLower].before;
var afterHooks = hooks[modelNameLower].after;

Also, it looks like beforeHooks and afterHooks are actually being re-declared and overwriting a different value a few lines up.

but in hookFn, all the hooks are being attached with this key: https://github.com/mikec/kalamata/blob/bdd45d6a6c92492afed5e3b3d69685fd05e65a20/src/index.js#L392

hooks[opts.endpointName][prefix][type].push(fn);

So it seems that the code either needs to use modelNameLower in both places, or use the endpointName in both places. I think I managed to fix it simply by deleting the re-declaration of beforeHooks and afterHooks but this might have consequences I haven't recognised.

nabilfreeman avatar Feb 24 '17 16:02 nabilfreeman