Skip to content

Commit 8a4d44d

Browse files
authored
[JET-2527] feature: Adapt breeze.js to zephyr (#9)
1 parent 1359a37 commit 8a4d44d

3 files changed

Lines changed: 48 additions & 132 deletions

File tree

lib/runtime.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const isDeno = typeof Deno !== "undefined";
22
export const isBreezeRuntime = typeof BreezeRuntime !== "undefined";
33

44
export function getEnv(key: string): string | undefined {
5-
return _internals.doGetEnv(key);
5+
return Deno.env.get(key);
66
}
77

88
export function getEnvOrThrow(key: string): string {
@@ -13,18 +13,14 @@ export function getEnvOrThrow(key: string): string {
1313
return value;
1414
}
1515

16-
export function serveHttp(
16+
export function serve(
1717
handler: (req: Request) => Response | Promise<Response>,
1818
) {
19-
if (isBreezeRuntime) return BreezeRuntime.serveHttp(handler);
20-
if (isDeno) return Deno.serve(handler);
21-
throw new Error("Unknown runtime for serveHttp");
19+
Deno.serve({ port: 0 }, handler);
2220
}
2321

2422
function doGetEnv(key: string): string | undefined {
25-
if (isBreezeRuntime) return BreezeRuntime.env.get(key);
26-
if (isDeno) return Deno.env.get(key);
27-
throw new Error("Unknown runtime for getEnv");
23+
return Deno.env.get(key);
2824
}
2925

3026
export const _internals = { doGetEnv };

mod.d.ts

Lines changed: 24 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,35 @@
11
declare namespace BreezeRuntime {
2-
export interface Env {
3-
/**
4-
* Retrieves the value of an environment variable.
5-
* @param key The name of the environment variable.
6-
* @returns The value of the environment variable, or undefined if it is not set.
7-
*
8-
* @example
9-
* ```ts
10-
* BreezeRuntime.env.get("foo");
11-
* // Returns "bar" if the environment variable "foo" is set to "bar".
12-
* ```
13-
*/
14-
get(key: string): string | undefined;
15-
16-
/**
17-
* Retrieves all environment variables.
18-
* @returns A map of all environment variables.
19-
*
20-
* @example
21-
* ```ts
22-
* BreezeRuntime.env.toObject();
23-
* // Returns { foo: "bar" } if the environment variable "foo" is set to "bar".
24-
* ```
25-
*/
26-
toObject(): Record<string, string | undefined>;
27-
28-
/**
29-
* Checks whether an environment variable is set.
30-
* @param key The name of the environment variable.
31-
* @returns Whether the environment variable is set.
32-
*
33-
* @example
34-
* ```ts
35-
* BreezeRuntime.env.has("foo");
36-
* // Returns true if the environment variable "foo" is set.
37-
* ```
38-
*/
39-
has(key: string): boolean;
2+
export interface DeploymentInfo {
3+
projectId: string;
4+
environmentId: string;
5+
functionId: string;
406
}
417

428
export interface Plugin {
43-
/**
44-
* Retrieves the plugin's endpoint.
45-
*
46-
* @example
47-
*
48-
* To retrieve the endpoint of the kv plugin:
49-
* ```ts
50-
* BreezeRuntime.plugins.kv.getEndpoint();
51-
* // Returns new URL("breeze://kv.plugins")
52-
* ```
53-
*
54-
* To retrieve the endpoint of the kv plugin under a subpath:
55-
* ```ts
56-
* BreezeRuntime.plugins.kv.getEndpoint("/put");
57-
* // Returns new URL("breeze://kv.plugins/put")
58-
* ```
59-
*/
60-
getEndpoint(subpath?: string): Promise<URL>;
9+
id: string;
10+
endpoint: string;
6111
}
6212

6313
/**
64-
* The environment variables.
14+
* An JWT token generated by Breeze to authorize plugin or workflow requests.
6515
*/
66-
export const env: Env;
16+
export type Token = string;
6717

68-
/**
69-
* A map of all plugins.
70-
*/
71-
export const plugins: Record<string, Plugin>;
18+
export interface TokenOptions {
19+
/**
20+
* The instance name of plugin.
21+
*/
22+
plugin?: string;
23+
}
7224

73-
/**
74-
* Provides an interface to handle HTTP request and responses over TCP connections.
75-
* See detail at https://deno.land/api?s=Deno.serveHttp
76-
* @example
77-
* ```ts
78-
* import { Hono } from "https://deno.land/x/hono@v3.4.1/mod.ts";
79-
* const app = new Hono();
80-
*
81-
* app.get("/", (c) => c.text("Hello world!"));
82-
* app.get("/greet/:name", (c) => {
83-
* const name = c.req.param("name");
84-
* return c.text(`Hi, ${name}`);
85-
* });
86-
* app.notFound((c) => {
87-
* return c.text("Not found", 404);
88-
* });
89-
*
90-
* BreezeRuntime.serveHttp(app.fetch);
91-
* ```
92-
*/
93-
export function serveHttp(
94-
handler: (req: Request) => Response | Promise<Response>,
95-
): void;
25+
export function getDeploymentInfo(): DeploymentInfo;
26+
27+
export function getPlugin(plugin: string): Plugin | null;
28+
29+
export function generateToken(opts?: TokenOptions): Token;
30+
31+
export function pluginFetch(
32+
plugin: string,
33+
opts: RequestInit,
34+
): Promise<Response>;
9635
}

types_test.ts

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,36 @@ import {
55
IsNullable,
66
} from "https://deno.land/std@0.212.0/testing/types.ts";
77

8-
// env
98
{
10-
// get
11-
{
12-
const value = BreezeRuntime.env.get("foo");
9+
type Result = ReturnType<typeof BreezeRuntime.getDeploymentInfo>;
1310

14-
assertType<IsNullable<typeof value>>(true);
15-
assertType<Has<typeof value, string>>(true);
16-
}
17-
18-
// toObject
19-
{
20-
const values = BreezeRuntime.env.toObject();
21-
22-
assertType<IsExact<typeof values, Record<string, string | undefined>>>(
23-
true,
24-
);
25-
}
26-
27-
// has
28-
{
29-
const value = BreezeRuntime.env.has("foo");
30-
31-
assertType<IsExact<typeof value, boolean>>(true);
32-
}
11+
assertType<IsExact<Result, BreezeRuntime.DeploymentInfo>>(true);
3312
}
3413

35-
// plugins
3614
{
37-
// get a plugin
38-
{
39-
const plugin = BreezeRuntime.plugins.kv;
15+
type Name = Parameters<typeof BreezeRuntime.getPlugin>[0];
16+
type Result = ReturnType<typeof BreezeRuntime.getPlugin>;
4017

41-
assertType<IsExact<typeof plugin, BreezeRuntime.Plugin>>(true);
42-
}
18+
assertType<IsExact<Name, string>>(true);
19+
assertType<Has<Result, BreezeRuntime.Plugin>>(true);
20+
assertType<IsNullable<Result>>(true);
21+
}
4322

44-
// getEndpoint
45-
{
46-
const result = BreezeRuntime.plugins.kv.getEndpoint();
23+
{
24+
type Options = Parameters<typeof BreezeRuntime.generateToken>[0];
25+
type Result = ReturnType<typeof BreezeRuntime.generateToken>;
4726

48-
assertType<IsExact<typeof result, Promise<URL>>>(true);
49-
}
27+
assertType<Has<Options, BreezeRuntime.TokenOptions>>(true);
28+
assertType<IsNullable<Options>>(true);
29+
assertType<IsExact<Result, string>>(true);
5030
}
5131

52-
// serveHttp
5332
{
54-
type Params = Parameters<typeof BreezeRuntime.serveHttp>[0];
33+
type Name = Parameters<typeof BreezeRuntime.pluginFetch>[0];
34+
type Options = Parameters<typeof BreezeRuntime.pluginFetch>[1];
35+
type Result = ReturnType<typeof BreezeRuntime.pluginFetch>;
5536

56-
assertType<
57-
IsExact<Params, (req: Request) => Response | Promise<Response>>
58-
>(true);
37+
assertType<IsExact<Name, string>>(true);
38+
assertType<Has<Options, RequestInit>>(true);
39+
assertType<IsExact<Result, Promise<Response>>>(true);
5940
}

0 commit comments

Comments
 (0)