From 8527ba710f0b4a8134125f6456d3950b11c466c9 Mon Sep 17 00:00:00 2001 From: Dominik Opyd Date: Wed, 28 Jun 2023 22:56:21 +0200 Subject: [PATCH 1/4] docs: add `h3` middleware example --- README.md | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b0fba5c..7156fe2 100644 --- a/README.md +++ b/README.md @@ -19,14 +19,49 @@ The default server directory is the current working directory. ### Programatic Usage -You can use IPX as a Connect/Express middleware or directly use ipx api. +You can use IPX as a middleware or directly use ipx api. + +#### H3 Middleware +[H3](/~https://github.com/unjs/h3) is a minimal h(ttp) framework built for high performance and portability. It is part of the [unjs](http://unjs.io) ecosystem to which `ipx` belongs. ```js +import { createApp, eventHandler, toNodeListener } from "h3"; +import { createIPX, createIPXMiddleware } from "ipx"; +import { listen } from "listhen"; + +const app = createApp(); +const ipx = createIPX({ + domains: ['unjs.io'] +}); + +const middleware = createIPXMiddleware(ipx) + +const handler = eventHandler(async (event) => { + await middleware(event.node.req, event.node.res); +}); + +app.use("/image", handler); + +listen(toNodeListener(app)); +``` + +#### Express Middleware +[Express](https://expressjs.com) is a fast, unopinionated, minimalist web framework for Node.js. + +```js +import express from 'express'; import { createIPX, createIPXMiddleware } from "ipx"; -const ipx = createIPX(/* options */); const app = express(); +const ipx = createIPX({ + domains: ['unjs.io'] +}); + app.use("/image", createIPXMiddleware(ipx)); + +app.listen(3000, () => + console.log('Example app listening on port 3000!'), +); ``` ### Examples From 40c0f1e5e0437df9e7caa4cdae0bf72305c587ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Tue, 25 Jul 2023 16:33:26 +0200 Subject: [PATCH 2/4] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7156fe2..afd5fd7 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ The default server directory is the current working directory. You can use IPX as a middleware or directly use ipx api. #### H3 Middleware + [H3](/~https://github.com/unjs/h3) is a minimal h(ttp) framework built for high performance and portability. It is part of the [unjs](http://unjs.io) ecosystem to which `ipx` belongs. ```js From 3a572b825fdf4591c13f056d7ae0447246c16ce2 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 5 Sep 2023 15:44:33 +0200 Subject: [PATCH 3/4] update --- README.md | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 9e72a30..db51bdc 100644 --- a/README.md +++ b/README.md @@ -19,50 +19,42 @@ The default server directory is the current working directory. ### Programatic Usage -You can use IPX as a middleware or directly use ipx api. +You can use IPX as a middleware or directly use ipx api: -#### H3 Middleware +```ts +import { createIPX, createIPXMiddleware } from "ipx"; + +const ipx = createIPX({ domains: ["unjs.io"] }); +``` -[H3](/~https://github.com/unjs/h3) is a minimal h(ttp) framework built for high performance and portability. It is part of the [unjs](http://unjs.io) ecosystem to which `ipx` belongs. +**Example**: Using with [unjs/h3](/~https://github.com/unjs/h3): ```js -import { createApp, eventHandler, toNodeListener } from "h3"; import { createIPX, createIPXMiddleware } from "ipx"; import { listen } from "listhen"; +import { createApp, fromNodeMiddleware, toNodeListener } from "h3"; -const app = createApp(); -const ipx = createIPX({ - domains: ['unjs.io'] -}); +const ipx = createIPX({ domains: ["unjs.io"] }); +const ipxMiddleware = createIPXMiddleware(ipx); -const middleware = createIPXMiddleware(ipx) - -const handler = eventHandler(async (event) => { - await middleware(event.node.req, event.node.res); -}); - -app.use("/image", handler); +const app = createApp().use("/", fromNodeMiddleware(ipxMiddleware)); listen(toNodeListener(app)); ``` -#### Express Middleware -[Express](https://expressjs.com) is a fast, unopinionated, minimalist web framework for Node.js. +**Example:** Using [express](https://expressjs.com): ```js -import express from 'express'; import { createIPX, createIPXMiddleware } from "ipx"; +import { listen } from "listhen"; +import express from "express"; -const app = express(); -const ipx = createIPX({ - domains: ['unjs.io'] -}); +const ipx = createIPX({ domains: ["unjs.io"] }); +const ipxMiddleware = createIPXMiddleware(ipx); -app.use("/image", createIPXMiddleware(ipx)); +const app = express().use("/", ipxMiddleware); -app.listen(3000, () => - console.log('Example app listening on port 3000!'), -); +listen(app); ``` ### Examples From 881581e64ae0ff0ff1dff0c2a031741fe63c3121 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 5 Sep 2023 15:49:36 +0200 Subject: [PATCH 4/4] update --- README.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index db51bdc..400f3d1 100644 --- a/README.md +++ b/README.md @@ -5,26 +5,27 @@ High performance, secure and easy to use image proxy based on [sharp](/~https://github.com/lovell/sharp) and [libvips](/~https://github.com/libvips/libvips). -## Usage - -### Quick Start +## Using CLI You can use `ipx` command to start server using: ```bash -$ npx ipx +npx ipx@latest ``` The default server directory is the current working directory. -### Programatic Usage +## Programatic API -You can use IPX as a middleware or directly use ipx api: +You can use IPX as a middleware or directly use IPX interface. ```ts import { createIPX, createIPXMiddleware } from "ipx"; const ipx = createIPX({ domains: ["unjs.io"] }); + +// (req, res) => void +const ipxMiddleware = createIPXMiddleware(ipx); ``` **Example**: Using with [unjs/h3](/~https://github.com/unjs/h3): @@ -34,7 +35,7 @@ import { createIPX, createIPXMiddleware } from "ipx"; import { listen } from "listhen"; import { createApp, fromNodeMiddleware, toNodeListener } from "h3"; -const ipx = createIPX({ domains: ["unjs.io"] }); +const ipx = createIPX({}); const ipxMiddleware = createIPXMiddleware(ipx); const app = createApp().use("/", fromNodeMiddleware(ipxMiddleware)); @@ -49,7 +50,7 @@ import { createIPX, createIPXMiddleware } from "ipx"; import { listen } from "listhen"; import express from "express"; -const ipx = createIPX({ domains: ["unjs.io"] }); +const ipx = createIPX({}); const ipxMiddleware = createIPXMiddleware(ipx); const app = express().use("/", ipxMiddleware); @@ -57,7 +58,7 @@ const app = express().use("/", ipxMiddleware); listen(app); ``` -### Examples +## Examples Get original image: @@ -75,7 +76,7 @@ Resize to `200x200px` using `embed` method and change format to `webp`: `/embed,f_webp,s_200x200/static/buffalo.png` -### Modifiers +## Modifiers | Property | Docs | Example | Comments | | -------------- | :-------------------------------------------------------------- | :--------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -106,7 +107,7 @@ Resize to `200x200px` using `embed` method and change format to `webp`: | grayscale | [Docs](https://sharp.pixelplumbing.com/api-colour#grayscale) | `/grayscale/buffalo.png` | | animated | - | `/animated/buffalo.gif` | Experimental | -### Config +## Config Config can be customized using `IPX_*` environment variables.