Skip to content

Commit

Permalink
using-deno
Browse files Browse the repository at this point in the history
- proxy middleware added (for servers)
- readme upd
  • Loading branch information
krutoo committed Apr 4, 2024
1 parent d79ace8 commit 80e864b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ const myFetch = configureFetch(
);
```

### `proxy`

Returns simple proxy middleware. Useful for servers based on Fetch API.

```ts
import { applyMiddleware } from '@krutoo/fetch-tools';
import { proxy } from '@krutoo/fetch-tools/middleware';

const enhance = applyMiddleware(
proxy({
// pathname(s) of incoming request URL which will be proxied
filter: ['/api/v2/', '/api/v3/'],

// define target URL
target: 'https://www.my-site.com/',
}),
);

Deno.serve(
enhance(req => {
return new Response('<h1>Main page</h1>');
}),
);
```

## Server utilities

You can use utils for simply configure your HTTP server.
Expand Down
1 change: 1 addition & 0 deletions src/middleware/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export {
} from './log.ts';
export { retry } from './retry.ts';
export { validateStatus, type ValidateStatusOptions } from './validate-status.ts';
export { proxy, type ProxyOptions, type ProxyRequestFilter } from './proxy.ts';
68 changes: 68 additions & 0 deletions src/middleware/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { Middleware } from '../types.ts';

export interface ProxyRequestFilter {
(url: URL, request: Request): boolean;
}

export interface ProxyOptions {
filter: string | string[] | ProxyRequestFilter;
target: string;
}

/**
* Simple proxy middleware for servers based on Web Fetch API.
* Based on good article: https://blog.r0b.io/post/creating-a-proxy-with-deno/
*/
export function proxy({ filter, target }: ProxyOptions): Middleware {
const matches = createMatches(filter);

const createRequest = (url: URL, request: Request) => {
const targetURL = new URL(`.${url.pathname}`, target);
targetURL.search = url.search;

const headers = new Headers(request.headers);
headers.set('Host', targetURL.hostname);

return new Request(targetURL, {
method: request.method,
headers,
body: request.body,
redirect: 'manual',
});
};

return (request, next) => {
const url = new URL(request.url);

if (!matches(url, request)) {
return next(request);
}

console.log('go proxy!');

return fetch(createRequest(url, request)).then(res => {
console.log({ ...res.headers });
return res;
});
};
}

function createMatches(filter: ProxyOptions['filter']): ProxyRequestFilter {
switch (true) {
case Array.isArray(filter):
return url => filter.some(item => url.pathname.startsWith(item));

case typeof filter === 'string':
return url => url.pathname.startsWith(filter);

case typeof filter === 'function':
return filter;

default:
return stubFalse;
}
}

function stubFalse() {
return false;
}

0 comments on commit 80e864b

Please sign in to comment.