-
Notifications
You must be signed in to change notification settings - Fork 2
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
Bundler plugins #12
Merged
Bundler plugins #12
Changes from all commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
217adca
feat: rollup plugin
magne4000 8893dd1
tests: rollup
magne4000 ce61d2b
tests: rollup
magne4000 1d9dac6
tests: rollup
magne4000 20cc163
feat: esbuild plugin
magne4000 ace5ca3
fix: rollup
magne4000 15b44b1
fix: rollup
magne4000 97d7201
chore(esbuild): respect outbase
magne4000 873e0ca
feat(esbuild): generate report
magne4000 434b3f2
chore: pnpm-lock
magne4000 ea40a65
refactor: share code between esbuild and rollup
magne4000 09f3ef2
chore(rollup): ensure externals
magne4000 f9f1c32
chore: force build order
magne4000 d7bc7ce
chore: windows tests
magne4000 1812dd8
chore: remove comments
magne4000 29fbcae
chore: windows tests
magne4000 64ac9ce
chore: windows tests
magne4000 de9c817
chore: windows tests
magne4000 3c60201
chore: windows tests
magne4000 f048657
chore: windows tests
magne4000 2264f83
chore: windows tests
magne4000 82b8c24
tests: report
magne4000 15315f2
chore: fix build
magne4000 1bccff9
chore: windows tests
magne4000 e4d3406
chore: eslint ignore
magne4000 8dc6134
chore: windows tests
magne4000 5baefa8
chore: windows tests
magne4000 f14c17b
chore: tests-tool
magne4000 5891cad
chore: edit package.json
magne4000 97e0042
chore: working tests-example
magne4000 fa8e540
tests: e2e tests
magne4000 eb5b7df
tests: types
magne4000 cdf09c3
chore
magne4000 417c71c
feat: Add ability to forwards args to underlying handler or middlewar…
magne4000 04b80f7
Merge branch 'main' into plugin
magne4000 e14a8dc
feat: support for In and Out Context
magne4000 1cb1080
feat: webroute adapter
magne4000 c9af0d4
chore: deno.lock
magne4000 1c923ac
fixes
magne4000 5684fb0
fix: typings
magne4000 c1ac056
feat: initial support for runtime parameter
magne4000 d1c2f86
chore: update package.sjon
magne4000 117733a
chore: update package.sjon
magne4000 819f8a6
chore: dedupe webroute
magne4000 53407b3
chore: return context update
magne4000 043298c
feat: complete webroute support
magne4000 7b8a994
chore: better TS types for webroute generated exports
magne4000 edb8959
refactor: dts generation
magne4000 150d7c6
tests: auto generated exports typings
magne4000 ee3a451
chore: deps
magne4000 c267778
release: @universal-middleware/[email protected]
magne4000 57624e4
chore: prepare files for release
magne4000 43c8836
chore: update deno lock files
magne4000 cfa17a4
chore: deno lock false
magne4000 e008ac4
chore: deno --unstable-byonm
magne4000 c084f99
doc(universal-middleware): readme
magne4000 dbcd8ac
doc: symlink global README to universal-middleware package
magne4000 3d1a75e
doc: intro to universal-middleware usage
magne4000 88143c4
doc: README
magne4000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Build artifacts | ||
dist/** | ||
**/dist/** | ||
|
||
# JS files at the root of the project | ||
*.js | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
package-manager-strict=false | ||
auto-install-peers=false |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
packages/universal-middleware/README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
{ | ||
"name": "@universal-middleware-examples/tool", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
"files": [ | ||
"dist" | ||
], | ||
"exports": { | ||
"./dummy-handler": { | ||
"types": "./dist/dummy.d.ts", | ||
"import": "./dist/dummy.js", | ||
"default": "./dist/dummy.js" | ||
}, | ||
"./middlewares/context-middleware": { | ||
"types": "./dist/middlewares/context.d.ts", | ||
"import": "./dist/middlewares/context.js", | ||
"default": "./dist/middlewares/context.js" | ||
}, | ||
"./middlewares/headers-middleware": { | ||
"types": "./dist/middlewares/headers.d.ts", | ||
"import": "./dist/middlewares/headers.js", | ||
"default": "./dist/middlewares/headers.js" | ||
}, | ||
"./dummy-handler-hono": { | ||
"types": "./dist/handlers/universal-hono-handler-handler.d.ts", | ||
"import": "./dist/handlers/universal-hono-handler-handler.js", | ||
"default": "./dist/handlers/universal-hono-handler-handler.js" | ||
}, | ||
"./dummy-handler-express": { | ||
"types": "./dist/handlers/universal-express-handler-handler.d.ts", | ||
"import": "./dist/handlers/universal-express-handler-handler.js", | ||
"default": "./dist/handlers/universal-express-handler-handler.js" | ||
}, | ||
"./dummy-handler-hattip": { | ||
"types": "./dist/handlers/universal-hattip-handler-handler.d.ts", | ||
"import": "./dist/handlers/universal-hattip-handler-handler.js", | ||
"default": "./dist/handlers/universal-hattip-handler-handler.js" | ||
}, | ||
"./middlewares/context-middleware-hono": { | ||
"types": "./dist/middlewares/universal-hono-middleware-context.middleware.d.ts", | ||
"import": "./dist/middlewares/universal-hono-middleware-context.middleware.js", | ||
"default": "./dist/middlewares/universal-hono-middleware-context.middleware.js" | ||
}, | ||
"./middlewares/context-middleware-express": { | ||
"types": "./dist/middlewares/universal-express-middleware-context.middleware.d.ts", | ||
"import": "./dist/middlewares/universal-express-middleware-context.middleware.js", | ||
"default": "./dist/middlewares/universal-express-middleware-context.middleware.js" | ||
}, | ||
"./middlewares/context-middleware-hattip": { | ||
"types": "./dist/middlewares/universal-hattip-middleware-context.middleware.d.ts", | ||
"import": "./dist/middlewares/universal-hattip-middleware-context.middleware.js", | ||
"default": "./dist/middlewares/universal-hattip-middleware-context.middleware.js" | ||
}, | ||
"./middlewares/headers-middleware-hono": { | ||
"types": "./dist/middlewares/universal-hono-middleware-headers.middleware.d.ts", | ||
"import": "./dist/middlewares/universal-hono-middleware-headers.middleware.js", | ||
"default": "./dist/middlewares/universal-hono-middleware-headers.middleware.js" | ||
}, | ||
"./middlewares/headers-middleware-express": { | ||
"types": "./dist/middlewares/universal-express-middleware-headers.middleware.d.ts", | ||
"import": "./dist/middlewares/universal-express-middleware-headers.middleware.js", | ||
"default": "./dist/middlewares/universal-express-middleware-headers.middleware.js" | ||
}, | ||
"./middlewares/headers-middleware-hattip": { | ||
"types": "./dist/middlewares/universal-hattip-middleware-headers.middleware.d.ts", | ||
"import": "./dist/middlewares/universal-hattip-middleware-headers.middleware.js", | ||
"default": "./dist/middlewares/universal-hattip-middleware-headers.middleware.js" | ||
}, | ||
"./dummy-handler-webroute": { | ||
"types": "./dist/handlers/universal-webroute-handler-handler.d.ts", | ||
"import": "./dist/handlers/universal-webroute-handler-handler.js", | ||
"default": "./dist/handlers/universal-webroute-handler-handler.js" | ||
}, | ||
"./middlewares/context-middleware-webroute": { | ||
"types": "./dist/middlewares/universal-webroute-middleware-context.middleware.d.ts", | ||
"import": "./dist/middlewares/universal-webroute-middleware-context.middleware.js", | ||
"default": "./dist/middlewares/universal-webroute-middleware-context.middleware.js" | ||
}, | ||
"./middlewares/headers-middleware-webroute": { | ||
"types": "./dist/middlewares/universal-webroute-middleware-headers.middleware.d.ts", | ||
"import": "./dist/middlewares/universal-webroute-middleware-headers.middleware.js", | ||
"default": "./dist/middlewares/universal-webroute-middleware-headers.middleware.js" | ||
} | ||
}, | ||
"author": "Joël Charles <[email protected]>", | ||
"repository": "https://github.com/magne4000/universal-handler", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "rimraf dist && tsup", | ||
"prepack": "pnpm build", | ||
"test:typecheck": "tsc -p tsconfig.json --noEmit" | ||
}, | ||
"devDependencies": { | ||
"@hono/node-server": "^1.12.0", | ||
"@swc/core": "^1.7.11", | ||
"@types/node": "^20.14.10", | ||
"rimraf": "^6.0.0", | ||
"tsup": "^8.2.4", | ||
"typescript": "^5.5.4", | ||
"universal-middleware": "*" | ||
}, | ||
"optionalDependencies": { | ||
"@universal-middleware/express": "^0", | ||
"@universal-middleware/hattip": "^0", | ||
"@universal-middleware/hono": "^0", | ||
"@universal-middleware/webroute": "^0" | ||
}, | ||
"sideEffects": false, | ||
"peerDependencies": { | ||
"@universal-middleware/express": "^0", | ||
"@universal-middleware/hattip": "^0", | ||
"@universal-middleware/hono": "^0", | ||
"@universal-middleware/webroute": "^0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { Get, UniversalHandler } from "universal-middleware"; | ||
|
||
const handler: Get<[], UniversalHandler> = () => (_request, ctx) => { | ||
return new Response("context: " + JSON.stringify(ctx), { | ||
status: 200, | ||
}); | ||
}; | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Get, UniversalMiddleware } from "universal-middleware"; | ||
|
||
const contextMiddleware = ((value) => (_request, ctx) => { | ||
return { | ||
...ctx, | ||
something: value, | ||
}; | ||
// Using `satisfies` to not lose return type | ||
}) satisfies Get<[string], UniversalMiddleware>; | ||
|
||
export default contextMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Get, UniversalMiddleware } from "universal-middleware"; | ||
|
||
const headersMiddleware: Get<[], UniversalMiddleware<{ something?: string }>> = | ||
() => (_request, ctx) => { | ||
return (response) => { | ||
response.headers.set("X-Custom-Header", ctx.something ?? "NONE"); | ||
|
||
return response; | ||
}; | ||
}; | ||
|
||
export default headersMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"verbatimModuleSyntax": true, | ||
"resolveJsonModule": true, | ||
"skipLibCheck": true, | ||
"sourceMap": true, | ||
"module": "ESNext", | ||
"noEmit": true, | ||
"moduleResolution": "Bundler", | ||
"target": "ES2022", | ||
"lib": ["DOM", "DOM.Iterable", "ESNext"] | ||
}, | ||
"exclude": ["dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { defineConfig } from "tsup"; | ||
import universalMiddleware from "universal-middleware/esbuild"; | ||
|
||
export default defineConfig([ | ||
{ | ||
entry: { | ||
dummy: "./src/handlers/handler.ts", | ||
"middlewares/context": "./src/middlewares/context.middleware.ts", | ||
"middlewares/headers": "./src/middlewares/headers.middleware.ts", | ||
}, | ||
format: ["esm"], | ||
platform: "neutral", | ||
target: "es2022", | ||
dts: true, | ||
esbuildPlugins: [universalMiddleware()], | ||
esbuildOptions(opts) { | ||
opts.outbase = "src"; | ||
}, | ||
bundle: true, | ||
}, | ||
]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't review everything, but I'll be honest, stuff like this rubs me all the wrong ways - requiring optional fields in the context type, and then using the
??
operator, it just sounds too brittle for my taste. Users will put those middlewares in the wrong order, things will quietly malfunction - because the types aren't really telling the truth, it will be difficult for them to understand why.I really want to see type safety for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type safety can be achieved with a proper
webroute-adapter
for instance.Proper type safety cannot be achieved currently with the current state of javascript servers (without something like
webroute
), which is out of scope here.The scope of this PR is to ease the work of tool authors, and the whole middleware composition stack should not be a part of this solution, as it adds a lot of overhead, maintainability cost, and complexity when explaining what this project purpose is.
It's still a huge step forward in the right direction. And again, complete type safety will be possible with
webroute
and the servers it supports.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could perhaps add the
webroute
adapter in this PR, so that we can review it as wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, no doubt. 🙂
How?
If this universal middleware has inaccurate context types, how would
webroute
solve that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm currently working on some improvements and the
webroute
adapter to make it clearer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example using webroute adapter
cc @mindplay-dk @sinclairnick
The middleware signature has been adapted to correspond to this comment.
@sinclairnick as you can see, declaring middlewares outside
@webroute/router
flow makes it a little bit verbose to actually extract the correct types.I wonder if there would be a solution where
webroute
could automatically extract requiredTState
("input" state) and generatedDataResult
("output" state) for middlewares declared outside of webroute flow.For instance, I could declare a middleware with the following signature
WebrouteMiddleware<InState, OutState = OutState>
and havewebroute
extractInState
andOutState
if the signature matchesWebrouteMiddleware
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies if I've misunderstood here, as there's obviously a lot of context/info in this repo I may have missed.
But if I understand correctly, there is indeed a type that's allows declaring the inputs and outputs, without having to rely on inference.
The MiddlewareFn type enables setting:
So to achieve what you're describing this would look something like:
Obviously a small wrapper type could bend this into the correct order of params etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sinclairnick thanks, that's a good start, but I realize that I should make a reproduction repo. My issue seems to be more related to the interaction between
MiddlewareFn
from@webroute/middleware
then using it inside@webroute/route
route().use(...)
. But I think that we'll hit a Typescript limitation for what I have in mind...