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

runtimes/js: Added middleware functionality #1631

Merged
merged 9 commits into from
Jan 8, 2025

Conversation

fredr
Copy link
Member

@fredr fredr commented Dec 6, 2024

Adds a way to set custom status codes and an api for passing data from middlewares to the handler.

const mw = middleware(async (req, next) => {
  // add data that can be read from the handler
  req.data.myMiddlewareData = { some: "data" };

  const resp = await next(req);

  // change the status code on the response
  resp.status = 418;

  return resp
});

To access data in the handler

export const get = api(
  { expose: true, method: "GET", path: "/hello/:name" },
  async ({ name }: { name: string }): Promise<Response> => {
    const callMeta = currentRequest() as APICallMeta;
    const myData = callMeta.middlewareData?.myMiddlewareData;

    return { message: "Hello " + name };
  },
);

@fredr fredr self-assigned this Dec 6, 2024
@encore-cla
Copy link

encore-cla bot commented Dec 6, 2024

All committers have signed the CLA.

@fredr fredr force-pushed the fredr/ts-middleware-improvements branch from 2924d37 to 04dd70c Compare December 6, 2024 09:43
runtimes/js/src/request_meta.rs Outdated Show resolved Hide resolved
@fredr fredr force-pushed the fredr/ts-middleware-improvements branch 2 times, most recently from a2c3481 to 7c5d4ca Compare December 18, 2024 12:56
@solarsoft0
Copy link

solarsoft0 commented Jan 6, 2025

any luck on this PR, cc @fredr @eandre, I actually need a way to set context/data from an handler too; after hook could benefit from this to determine behavior, or is there another alternative way to do this aside from raw?

seems blocking my effort on this, and i'm avoiding forklift migration. solarsoft0/encore-better-auth#1

@solarsoft0
Copy link

any luck on this PR, cc @fredr @eandre, I actually need a way to set context/data from an handler too; after hook could benefit from this to determine behavior, or is there another alternative way to do this aside from raw?

seems blocking my effort on this, and i'm avoiding forklift migration. solarsoft0/encore-better-auth#1

without this, i would need to resort to handling middleware manually like its currently being done, which makes it less of encore, if that makes sense.

@fredr
Copy link
Member Author

fredr commented Jan 7, 2025

hey @solarsoft0, this PR stalled over Christmas and new years vacation, but I'm back now and we should soon get this merged. We are exploring some different approaches for the implementation first

runtimes/js/encore.dev/internal/appinit/mod.ts Outdated Show resolved Hide resolved
runtimes/js/encore.dev/req_meta.ts Outdated Show resolved Hide resolved
@fredr fredr force-pushed the fredr/ts-middleware-improvements branch from 7c5d4ca to 3c0ae7c Compare January 7, 2025 15:08
@fredr fredr force-pushed the fredr/ts-middleware-improvements branch from 3c0ae7c to 766e255 Compare January 7, 2025 15:09
@fredr fredr requested a review from eandre January 7, 2025 15:17
@solarsoft0
Copy link

hey @solarsoft0, this PR stalled over Christmas and new years vacation, but I'm back now and we should soon get this merged. We are exploring some different approaches for the implementation first

thank you! I can see it's reaaaaady!

@fredr fredr merged commit f74e1e8 into main Jan 8, 2025
4 checks passed
@fredr fredr deleted the fredr/ts-middleware-improvements branch January 8, 2025 08:13
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.

3 participants