From 0190115fdd19d0d19036184ca32316596a91be6e Mon Sep 17 00:00:00 2001 From: chientrm Date: Tue, 18 Jul 2023 19:21:04 +0000 Subject: [PATCH] service binding .fetch --- README.md | 19 +++++++++++++++ src/factory.ts | 3 +++ src/proxies/fetcher/fetch/proxy.ts | 36 +++++++++++++++++++++++++++++ src/proxies/fetcher/proxy_holder.ts | 34 +++++++++++++++++++++++++++ src/worker.ts | 20 ++++++++++------ 5 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 src/proxies/fetcher/fetch/proxy.ts create mode 100644 src/proxies/fetcher/proxy_holder.ts diff --git a/README.md b/README.md index 92e67ab..3261664 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,10 @@ Enable Cloudflare Workers runtime for local development. ### D1Database +```ts +import { createD1 } from 'cf-workers-proxy'; +``` + | Function | Status | | ----------- | ------ | | `prepare()` | ✅ | @@ -38,6 +42,10 @@ Enable Cloudflare Workers runtime for local development. ### Email Routing +```ts +import { sendEmail } from 'cf-workers-proxy'; +``` + ```ts sendEmail = async (data : { seb?: SendEmail; // Platform binding object @@ -52,6 +60,17 @@ sendEmail = async (data : { }) ``` +### Service Bindings + +```ts +import { createServiceBinding } from 'cf-workers-proxy'; +``` + +| Function | Status | +| ----------- | ------ | +| `fetch()` | ✅ | +| `connect()` | ❌ | + ## Contributing Just pull request 😐 diff --git a/src/factory.ts b/src/factory.ts index 858ce70..59ca6cd 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -3,6 +3,7 @@ import { D1DatabaseExecProxy } from './proxies/d1_database/exec/proxy'; import { D1DatabasePreparedStatementAllProxy } from './proxies/d1_database/prepared_statement/all/proxy'; import { D1DatabasePreparedStatementFirstProxy } from './proxies/d1_database/prepared_statement/first/proxy'; import { D1DatabasePreparedStatementRunProxy } from './proxies/d1_database/prepared_statement/run/proxy'; +import { FetcherFetchProxy } from './proxies/fetcher/fetch/proxy'; import { SendEmailProxy } from './proxies/send_email/proxy'; class ProxyFactory { @@ -19,6 +20,8 @@ class ProxyFactory { return new D1DatabaseExecProxy({ name, payload }); case SendEmailProxy.proxyType: return new SendEmailProxy({ name, payload }); + case FetcherFetchProxy.proxyType: + return new FetcherFetchProxy({ name, payload }); default: throw new Error('Unknown proxy type.'); } diff --git a/src/proxies/fetcher/fetch/proxy.ts b/src/proxies/fetcher/fetch/proxy.ts new file mode 100644 index 0000000..62e71e8 --- /dev/null +++ b/src/proxies/fetcher/fetch/proxy.ts @@ -0,0 +1,36 @@ +import { Proxy } from '../../proxy'; + +interface Payload { + path: string; + method?: string; + body: string; +} + +class FetcherFetchProxy extends Proxy { + static readonly proxyType = 'FetcherFetchProxy'; + constructor({ + host, + name, + payload, + }: { + host?: string; + name: string; + payload: Payload; + }) { + const proxyType = FetcherFetchProxy.proxyType; + super({ proxyType, host, name, payload }); + } + async execute(env: any): Promise { + const { name } = this, + { path, body, method } = this.payload, + fetcher = env[name] as Fetcher, + result = await fetcher.fetch(path, { + body, + method, + headers: { 'content-type': 'application/json' }, + }); + return result; + } +} + +export { FetcherFetchProxy }; diff --git a/src/proxies/fetcher/proxy_holder.ts b/src/proxies/fetcher/proxy_holder.ts new file mode 100644 index 0000000..c08c86f --- /dev/null +++ b/src/proxies/fetcher/proxy_holder.ts @@ -0,0 +1,34 @@ +import { ProxyHolder } from '../proxy_holder'; +import { FetcherFetchProxy } from './fetch/proxy'; + +interface Payload {} + +class FetcherProxyHolder extends ProxyHolder implements Fetcher { + async fetch( + input: RequestInfo>, + init?: RequestInit> | undefined + ): Promise { + const { host, name } = this, + path = input.toString(), + body = init?.body, + method = init?.method; + if (typeof body == 'string') { + const proxy = new FetcherFetchProxy({ + host, + name, + payload: { path, body, method }, + }); + const responseBody = await proxy.post(); + return new Response(responseBody); + } + throw new Error('Method not implemented.'); + } + connect( + address: string | SocketAddress, + options?: SocketOptions | undefined + ): Socket { + throw new Error('Method not implemented.'); + } +} + +export { FetcherProxyHolder }; diff --git a/src/worker.ts b/src/worker.ts index 2e149b2..b3b8ffe 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -2,6 +2,7 @@ import { createMimeMessage } from 'mimetext'; import type { ErrorResult, PostData, SuccessResult } from './data'; import { ProxyFactory } from './factory'; import { D1DatabaseProxyHolder } from './proxies/d1_database/proxy_holder'; +import { FetcherProxyHolder } from './proxies/fetcher/proxy_holder'; import { SendEmailProxy } from './proxies/send_email/proxy'; const json = (data: T) => { @@ -78,11 +79,7 @@ const json = (data: T) => { msg.setSubject(subject); msg.addMessage({ contentType, data }); const { EmailMessage } = await import('cloudflare:email'), - message = new EmailMessage( - 'admin@chientrm.com', - 'admin@chientrm.com', - msg.asRaw() - ); + message = new EmailMessage(addr, recipent, msg.asRaw()); await seb.send(message); } else { const proxy = new SendEmailProxy({ @@ -92,6 +89,15 @@ const json = (data: T) => { }); await proxy.post(); } - }; + }, + createServiceBinding = ( + name: string, + options?: { hostname?: string } + ): Fetcher => + new FetcherProxyHolder({ + host: options?.hostname ?? 'http://localhost:8787', + name, + payload: {}, + }); -export { createD1, createWorker, sendEmail }; +export { createD1, createServiceBinding, createWorker, sendEmail };