Skip to content

Add middleware support - #87

Merged
KATT merged 15 commits into
mainfrom
feature/preHook
Feb 19, 2021
Merged

KATT merged 15 commits into
mainfrom
feature/preHook

Conversation

@KATT

@KATT KATT commented Feb 18, 2021 •

Copy link
Copy Markdown
Member
  • allows you to create a middleware-esque thing on a router that cascades down on all routes added after
  • I don't love the name preHook - would love some input.
  • (started on a proper "middleware"-thing in wip - proper middlewares #88, but it's a bit complicated as it might require async iterables, so might do that later or never)
  • see the tests for usage, here's an example of an admin "sub-router"
    .merge(
    'admin.',
    trpc
    .router<Context>()
    .preHook(({ ctx }) => {
    if (!ctx.user?.isAdmin) {
    throw httpError.unauthorized();
    }
    })
    .query('secretPlace', {
    resolve() {
    resolverMock();
    return 'a key';
    },
    }),
    ),
    {

@vercel

vercel Bot commented Feb 18, 2021 •

Copy link
Copy Markdown

@KATT KATT changed the title [RFC] Prehook [RFC] preHook Feb 18, 2021
Comment thread packages/server/src/router.ts Resolved
@KATT
KATT marked this pull request as ready for review February 18, 2021 10:26
Comment thread packages/server/test/preHook.test.ts Outdated
Comment on lines +149 to +152
expect(await client.query('child.child.name')).toBe('GrandChild');
expect(preHookInParent).toHaveBeenCalledTimes(1);
expect(preHookInChild).toHaveBeenCalledTimes(1);
expect(preHookInGrandChild).toHaveBeenCalledTimes(1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice. so it will call each of those "middleware" hooks when going to a subrouter. That's perfect.

Comment thread packages/server/test/preHook.test.ts Resolved

@cyrus-za cyrus-za left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is exactly what I requested. Thanks so much

@KATT

KATT commented Feb 18, 2021

Copy link
Copy Markdown
Member Author

@all-contributors add @cyrus-za for idea

@allcontributors

Copy link
Copy Markdown
Contributor

@KATT

I've put up a pull request to add @cyrus-za! 🎉

@codecov-io

codecov-io commented Feb 19, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #87 (835a728) into main (d3c4b31) will decrease coverage by 0.19%.
The diff coverage is 86.95%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #87      +/-   ##
==========================================
- Coverage   85.88%   85.69%   -0.20%     
==========================================
  Files          13       13              
  Lines         588      601      +13     
  Branches      107      109       +2     
==========================================
+ Hits          505      515      +10     
- Misses         82       85       +3     
  Partials        1        1              
Impacted Files Coverage Δ
packages/server/src/adapters/express.ts 100.00% <ø> (ø)
packages/server/src/adapters/next.ts 0.00% <ø> (ø)
packages/server/src/http.ts 86.84% <ø> (ø)
packages/server/src/router.ts 92.85% <86.95%> (-2.92%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d3c4b31...835a728. Read the comment docs.

@KATT KATT changed the title [RFC] preHook Add middleware support Feb 19, 2021

This branch was successfully deployed

3 active deployments
Preview – chat — 835a728f Deployed Feb 19, 2021 by vercel[bot]
Preview – www — 835a728f Deployed Feb 19, 2021 by vercel[bot]
Preview – hello-world — 835a728f Deployed Feb 19, 2021 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants