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

Additional handlers config #8

Merged
merged 6 commits into from
Nov 11, 2020

Conversation

nick-lvov-dev
Copy link
Contributor

@nick-lvov-dev nick-lvov-dev commented Nov 8, 2020

Hey man! Just gotta say, your library is amazing, we're actively using it in our project, thank you for creating it!

I had this little idea to add custom handlers to the path besides $path, $raw etc.
Basically I've added a second argument to the typedPath function that can serve as a handler config. Then you can add as many custom handlers as you'd like.

Short example of how it could be used:

const tpHandlers = {
  $abs: (path: string[]) => () => {
    return `/${path.join('/')}`;
  },
  $last: (path: string[]) => (arg?: number) => {
    return path.slice(-(arg ?? 1)).join('/');
  },
  $url: (path: string[]) => () => {
    return path.join('/');
  },
};

export const tp = typedPath<RoutesConfig, typeof tpHandlers>([], tpHandlers);

tp.clients.list.$abs(); //    /clients/list
tp.clients.list.$last(); //   list
tp.clients.list.$url(); //    clients/list

I've tested it locally, all the base functions work.

Please take a look, tell me what you think :)

@nick-lvov-dev nick-lvov-dev force-pushed the additional-handlers-config branch from 96a3ce8 to e03349b Compare November 8, 2020 16:54
@bsalex
Copy link
Owner

bsalex commented Nov 8, 2020

Hi Nick,
I'm glad that you find the module helpful in your project.

Thank you for the contribution.
Your idea is great, I'll take a closer look at the PR ASAP.

@bsalex
Copy link
Owner

bsalex commented Nov 10, 2020

Could you add tests for the new methods to index.spec.ts ?

@nick-lvov-dev
Copy link
Contributor Author

Sure, will do today

@nick-lvov-dev
Copy link
Contributor Author

Done, would this be enough?

index.ts Outdated Show resolved Hide resolved
index.ts Outdated Show resolved Hide resolved
@bsalex
Copy link
Owner

bsalex commented Nov 10, 2020

Done, would this be enough?

Regarding tests - yes, that's perfect

@nick-lvov-dev nick-lvov-dev requested a review from bsalex November 10, 2020 14:43
@nick-lvov-dev
Copy link
Contributor Author

Updated it, had to use as any because of this issue microsoft/TypeScript#1863

…itional handlers documentation. Version bump.
@bsalex
Copy link
Owner

bsalex commented Nov 10, 2020

@nick-lvov-dev
I've created a PR to your PR, please take a look.
nick-lvov-dev#1

Please make sure that my changes don't break functionality that you need

PR changes. Additional handlers - optimization for applying default handlers. Additional handlers documentation. Version bump.
@nick-lvov-dev
Copy link
Contributor Author

Looks great, merged

@bsalex bsalex merged commit 210620f into bsalex:master Nov 11, 2020
@bsalex
Copy link
Owner

bsalex commented Nov 11, 2020

Thanks for the contribution,
typed-path@2.1.0 has been published
https://www.npmjs.com/package/typed-path

@nick-lvov-dev
Copy link
Contributor Author

Thank you for publishing it, and again for making this library :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants