Skip to content

Commit 8bb246b

Browse files
jfet97patroza
andauthored
Boyscout/andrea doing (#273)
* nix * chore: Refactor middleware and routing utilities for improved clarity and consistency * refactor: Update middleware and routing types for improved type safety and clarity * refactor: Simplify makeMiddleware function and enhance context handling in tests * refactor: Update middleware implementation and adjust type parameters for improved clarity * refactor: Enhance contextMaker documentation and rename DefaultContextMaker to EmptyContextMaker for clarity * take messy types out of impl * refactor: Improve comments for clarity in DynamicMiddleware * refactor: Update type parameter naming in makeRpcEffect for clarity * refactor: Rename type parameter 'R' to 'MiddlewareR' for clarity in RPCHandlerFactory and Middleware * refactor: Improve comments for clarity in routing and dynamic middleware * refactor: Handle potential undefined layers in makeRouter by using nullish coalescing * refactor: Use nullish coalescing for layers and middleware dependencies in makeRouter * hmmmm * refactor: Update middleware creation to use makeMiddlewareContextual for improved type safety * nvm * one as any less * add changeset * example * refactor: Clarify comments in Middleware interface for better understanding * rpx example * THIS * Revert "THIS" This reverts commit e19e123. * improve names * improve names * crazy * improve descriptions * cleanup * improve naming * fix * fixes * refactor: only provide middleware dependencies to actual middleware (#274) * workaround broken makeMiddleware * fix: layer elimination bs * improve: Middleware Dependencies should not be provided to anything else. * cleanup deps * fool around * fix make layer once! * fix makeMiddlewareContextual * cleanup * replace makeMiddleware * add changeset --------- Co-authored-by: Patrick Roza <contact@patrickroza.com>
1 parent 1eea091 commit 8bb246b

12 files changed

Lines changed: 576 additions & 361 deletions

File tree

.changeset/huge-turkeys-occur.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"effect-app": minor
3+
"@effect-app/infra": minor
4+
---
5+
6+
cleanup and document router/middleware

.changeset/shaky-taxes-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect-app/infra": patch
3+
---
4+
5+
better middlewares creation

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ fabric.properties
4747

4848
# vitest?
4949
*.timestamp-*.mjs
50+
51+
# nix
52+
.direnv

flake.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
description = "Node 22 + pnpm 10 dev shell";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, flake-utils }:
10+
flake-utils.lib.eachDefaultSystem (system:
11+
let
12+
pkgs = import nixpkgs { inherit system; };
13+
nodejs = pkgs.nodejs_22;
14+
pnpm = pkgs.pnpm.override { inherit nodejs; };
15+
tools = with pkgs; [
16+
git
17+
nixfmt-classic
18+
nodejs
19+
pnpm
20+
typescript
21+
];
22+
in {
23+
devShells.default = pkgs.mkShellNoCC {
24+
packages = tools;
25+
};
26+
});
27+
}

packages/effect-app/src/client/req.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,45 +43,45 @@ export declare namespace RPCContextMap {
4343
}
4444
}
4545

46-
export type GetEffectContext<CTXMap extends Record<string, RPCContextMap.Any>, T> = Values<
46+
export type GetEffectContext<RequestContextMap extends Record<string, RPCContextMap.Any>, T> = Values<
4747
// inverted: contextActivation is false => remove if explicitly set to true (like allowAnonymous: true disables auth and auth service and related errors)
4848
& {
4949
[
50-
key in keyof CTXMap as CTXMap[key]["contextActivation"] extends true ? never
51-
: key extends keyof T ? T[key] extends true ? never : CTXMap[key]["key"]
52-
: CTXMap[key]["key"]
50+
key in keyof RequestContextMap as RequestContextMap[key]["contextActivation"] extends true ? never
51+
: key extends keyof T ? T[key] extends true ? never : RequestContextMap[key]["key"]
52+
: RequestContextMap[key]["key"]
5353
]: // TODO: or as an Optional available?
54-
CTXMap[key]["service"]
54+
RequestContextMap[key]["service"]
5555
}
5656
// normal: contextActivation is true => add if explicitly set to true
5757
& {
5858
[
59-
key in keyof CTXMap as CTXMap[key]["contextActivation"] extends false ? never
60-
: key extends keyof T ? T[key] extends true ? CTXMap[key]["key"] : never
59+
key in keyof RequestContextMap as RequestContextMap[key]["contextActivation"] extends false ? never
60+
: key extends keyof T ? T[key] extends true ? RequestContextMap[key]["key"] : never
6161
: never
6262
]: // TODO: or as an Optional available?
63-
CTXMap[key]["service"]
63+
RequestContextMap[key]["service"]
6464
}
6565
>
6666

67-
export type GetEffectError<CTXMap extends Record<string, RPCContextMap.Any>, T> = Values<
67+
export type GetEffectError<RequestContextMap extends Record<string, RPCContextMap.Any>, T> = Values<
6868
// inverted: contextActivation is false => remove if explicitly set to true (like allowAnonymous: true disables auth and auth service and related errors)
6969
& {
7070
[
71-
key in keyof CTXMap as CTXMap[key]["contextActivation"] extends true ? never
72-
: key extends keyof T ? T[key] extends true ? never : CTXMap[key]["key"]
73-
: CTXMap[key]["key"]
71+
key in keyof RequestContextMap as RequestContextMap[key]["contextActivation"] extends true ? never
72+
: key extends keyof T ? T[key] extends true ? never : RequestContextMap[key]["key"]
73+
: RequestContextMap[key]["key"]
7474
]: // TODO: or as an Optional available?
75-
CTXMap[key]["error"]
75+
RequestContextMap[key]["error"]
7676
}
7777
// normal: contextActivation is true => add if explicitly set to true
7878
& {
7979
[
80-
key in keyof CTXMap as CTXMap[key]["contextActivation"] extends false ? never
81-
: key extends keyof T ? T[key] extends true ? CTXMap[key]["key"] : never
80+
key in keyof RequestContextMap as RequestContextMap[key]["contextActivation"] extends false ? never
81+
: key extends keyof T ? T[key] extends true ? RequestContextMap[key]["key"] : never
8282
: never
8383
]: // TODO: or as an Optional available?
84-
CTXMap[key]["error"]
84+
RequestContextMap[key]["error"]
8585
}
8686
>
8787

@@ -114,10 +114,10 @@ const ForceVoid: S.Schema<void> = S.transform(S.Any, S.Void, { decode: () => voi
114114

115115
export const makeRpcClient = <
116116
RequestConfig extends object,
117-
CTXMap extends Record<string, RPCContextMap.Any>,
117+
RequestContextMap extends Record<string, RPCContextMap.Any>,
118118
GeneralErrors extends S.Schema.All = never
119119
>(
120-
errors: { [K in keyof CTXMap]: CTXMap[K]["error"] },
120+
errors: { [K in keyof RequestContextMap]: RequestContextMap[K]["error"] },
121121
generalErrors?: GeneralErrors
122122
) => {
123123
// Long way around Context/C extends etc to support actual jsdoc from passed in RequestConfig etc... (??)
@@ -138,7 +138,7 @@ export const makeRpcClient = <
138138
{ readonly _tag: S.tag<Tag> } & Payload,
139139
SchemaOrFields<typeof config["success"]>,
140140
JoinSchema<
141-
[SchemaOrFields<typeof config["failure"]> | GetEffectError<CTXMap, C> | GeneralErrors]
141+
[SchemaOrFields<typeof config["failure"]> | GetEffectError<RequestContextMap, C> | GeneralErrors]
142142
>
143143
>
144144
& { config: Omit<C, "success" | "failure"> }
@@ -152,7 +152,7 @@ export const makeRpcClient = <
152152
Tag,
153153
{ readonly _tag: S.tag<Tag> } & Payload,
154154
SchemaOrFields<typeof config["success"]>,
155-
JoinSchema<[GetEffectError<CTXMap, C> | GeneralErrors]>
155+
JoinSchema<[GetEffectError<RequestContextMap, C> | GeneralErrors]>
156156
>
157157
& { config: Omit<C, "success" | "failure"> }
158158
<Tag extends string, Payload extends S.Struct.Fields, C extends Pick<Context, "failure">>(
@@ -166,7 +166,7 @@ export const makeRpcClient = <
166166
{ readonly _tag: S.tag<Tag> } & Payload,
167167
typeof S.Void,
168168
JoinSchema<
169-
[SchemaOrFields<typeof config["failure"]> | GetEffectError<CTXMap, C> | GeneralErrors]
169+
[SchemaOrFields<typeof config["failure"]> | GetEffectError<RequestContextMap, C> | GeneralErrors]
170170
>
171171
>
172172
& { config: Omit<C, "success" | "failure"> }
@@ -180,7 +180,7 @@ export const makeRpcClient = <
180180
Tag,
181181
{ readonly _tag: S.tag<Tag> } & Payload,
182182
typeof S.Void,
183-
JoinSchema<[GetEffectError<CTXMap, C> | GeneralErrors]>
183+
JoinSchema<[GetEffectError<RequestContextMap, C> | GeneralErrors]>
184184
>
185185
& { config: Omit<C, "success" | "failure"> }
186186
<Tag extends string, Payload extends S.Struct.Fields>(

packages/infra/src/api/internal/middlewares.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export const cors = (_options?: Partial<Middlewares.CorsOptions>) => {
211211
})()
212212

213213
const allowHeaders = (accessControlRequestHeaders: string | undefined) => {
214-
if(!options.allowedOrigins) return undefined
214+
if (!options.allowedOrigins) return undefined
215215

216216
if (options.allowedHeaders.length === 0 && accessControlRequestHeaders) {
217217
return {

0 commit comments

Comments
 (0)