@tatara/http 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +6 -8
- package/dist/index.mjs +3 -6
- package/package.json +1 -1
- package/src/app.ts +20 -33
package/dist/index.d.mts
CHANGED
|
@@ -64,14 +64,12 @@ type Defaults<E extends HttpEnv> = Partial<Omit<E["Variables"], "requestId" | "l
|
|
|
64
64
|
export declare function requestContext<E extends HttpEnv>(logger: CoreLogger, defaults?: Defaults<E>): MiddlewareHandler<E, string, {}, Response>;
|
|
65
65
|
interface CreateAppOptions<E extends HttpEnv> {
|
|
66
66
|
logger: CoreLogger;
|
|
67
|
-
/**
|
|
68
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Deprecated: call Hono's `cors()` in the app instead (with `corsOrigin` for the origin check),
|
|
69
|
+
* so its headers and origins are the app's. Kept so existing apps keep working.
|
|
70
|
+
*/
|
|
71
|
+
cors?: {
|
|
69
72
|
origins: string[];
|
|
70
|
-
/**
|
|
71
|
-
* For origins not in `origins`: true allows it. For origins kept in the database, such as a
|
|
72
|
-
* game's web build address; cache the lookup, since it runs on cross-origin requests.
|
|
73
|
-
*/
|
|
74
|
-
allowOrigin?: (origin: string) => boolean | Promise<boolean>;
|
|
75
73
|
exposeHeaders?: string[];
|
|
76
74
|
allowHeaders?: string[];
|
|
77
75
|
};
|
|
@@ -83,7 +81,7 @@ interface CreateAppOptions<E extends HttpEnv> {
|
|
|
83
81
|
}
|
|
84
82
|
/** Matches exact origins and `*.`-patterns, where `*` is exactly one DNS label (tenant subdomains). */
|
|
85
83
|
export declare function corsOrigin(origins: string[]): (origin: string) => string | null;
|
|
86
|
-
/** A plain OpenAPIHono app with request context, secure headers,
|
|
84
|
+
/** A plain OpenAPIHono app with request context, secure headers, body limit, /health and /ready. CORS is the app's: Hono's `cors()`. */
|
|
87
85
|
export declare function createApp<E extends HttpEnv>(options: CreateAppOptions<E>): OpenAPIHono<E>;
|
|
88
86
|
interface RouteDefinition<A extends string = string, E extends HttpEnv = HttpEnv> {
|
|
89
87
|
/** which HTTP app serves these routes */
|
package/dist/index.mjs
CHANGED
|
@@ -145,16 +145,13 @@ function corsOrigin(origins) {
|
|
|
145
145
|
});
|
|
146
146
|
return (origin) => exact.has(origin) || patterns.some((p) => p.test(origin)) ? origin : null;
|
|
147
147
|
}
|
|
148
|
-
|
|
149
|
-
return async (origin) => listed(origin) ?? (origin && check && await check(origin) ? origin : null);
|
|
150
|
-
}
|
|
151
|
-
/** A plain OpenAPIHono app with request context, secure headers, CORS, body limit, /health and /ready. */
|
|
148
|
+
/** A plain OpenAPIHono app with request context, secure headers, body limit, /health and /ready. CORS is the app's: Hono's `cors()`. */
|
|
152
149
|
function createApp(options) {
|
|
153
150
|
const app = new OpenAPIHono({ defaultHook: validationHook });
|
|
154
151
|
app.use(requestContext(options.logger, options.defaults));
|
|
155
152
|
app.use(secureHeaders());
|
|
156
|
-
app.use(cors({
|
|
157
|
-
origin:
|
|
153
|
+
if (options.cors) app.use(cors({
|
|
154
|
+
origin: corsOrigin(options.cors.origins),
|
|
158
155
|
credentials: true,
|
|
159
156
|
allowHeaders: options.cors.allowHeaders ?? [
|
|
160
157
|
"Content-Type",
|
package/package.json
CHANGED
package/src/app.ts
CHANGED
|
@@ -43,17 +43,11 @@ export function requestContext<E extends HttpEnv>(logger: CoreLogger, defaults:
|
|
|
43
43
|
|
|
44
44
|
export interface CreateAppOptions<E extends HttpEnv> {
|
|
45
45
|
logger: CoreLogger;
|
|
46
|
-
/**
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
* game's web build address; cache the lookup, since it runs on cross-origin requests.
|
|
52
|
-
*/
|
|
53
|
-
allowOrigin?: (origin: string) => boolean | Promise<boolean>;
|
|
54
|
-
exposeHeaders?: string[];
|
|
55
|
-
allowHeaders?: string[];
|
|
56
|
-
};
|
|
46
|
+
/**
|
|
47
|
+
* Deprecated: call Hono's `cors()` in the app instead (with `corsOrigin` for the origin check),
|
|
48
|
+
* so its headers and origins are the app's. Kept so existing apps keep working.
|
|
49
|
+
*/
|
|
50
|
+
cors?: { origins: string[]; exposeHeaders?: string[]; allowHeaders?: string[] };
|
|
57
51
|
bodyLimitBytes: number;
|
|
58
52
|
/** a limit on how long a handler may take, with longer ones for slow paths */
|
|
59
53
|
timeouts?: TimeoutOptions;
|
|
@@ -75,32 +69,25 @@ export function corsOrigin(origins: string[]): (origin: string) => string | null
|
|
|
75
69
|
return (origin) => (exact.has(origin) || patterns.some((p) => p.test(origin)) ? origin : null);
|
|
76
70
|
}
|
|
77
71
|
|
|
78
|
-
|
|
79
|
-
listed: (origin: string) => string | null,
|
|
80
|
-
check?: (origin: string) => boolean | Promise<boolean>,
|
|
81
|
-
): (origin: string) => Promise<string | null> {
|
|
82
|
-
return async (origin) =>
|
|
83
|
-
listed(origin) ?? (origin && check && (await check(origin)) ? origin : null);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** A plain OpenAPIHono app with request context, secure headers, CORS, body limit, /health and /ready. */
|
|
72
|
+
/** A plain OpenAPIHono app with request context, secure headers, body limit, /health and /ready. CORS is the app's: Hono's `cors()`. */
|
|
87
73
|
export function createApp<E extends HttpEnv>(options: CreateAppOptions<E>): OpenAPIHono<E> {
|
|
88
74
|
const app = new OpenAPIHono<E>({ defaultHook: validationHook });
|
|
89
75
|
app.use(requestContext<E>(options.logger, options.defaults) as MiddlewareHandler<E>);
|
|
90
76
|
app.use(secureHeaders());
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
77
|
+
if (options.cors)
|
|
78
|
+
app.use(
|
|
79
|
+
cors({
|
|
80
|
+
origin: corsOrigin(options.cors.origins),
|
|
81
|
+
credentials: true,
|
|
82
|
+
allowHeaders: options.cors.allowHeaders ?? [
|
|
83
|
+
"Content-Type",
|
|
84
|
+
"Authorization",
|
|
85
|
+
"X-Request-Id",
|
|
86
|
+
"Accept-Language",
|
|
87
|
+
],
|
|
88
|
+
exposeHeaders: options.cors.exposeHeaders ?? ["X-Request-Id"],
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
104
91
|
app.use(bodyLimit({ maxSize: options.bodyLimitBytes }));
|
|
105
92
|
if (options.timeouts) app.use(requestTimeout(options.timeouts));
|
|
106
93
|
app.get("/health", (c) => c.json({ status: "ok" }));
|