@hulla/api-cloudflare 0.0.0-stage → 2.0.1
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.ts +34 -0
- package/dist/index.js +37 -0
- package/dist/pages.d.ts +34 -0
- package/dist/pages.js +31 -0
- package/package.json +43 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { FetchServerErrorInput } from "@hulla/api/fetch";
|
|
2
|
+
import { Awaitable, ServerAdapter, ServerContextInput, ServerExecutableFor } from "@hulla/api/server";
|
|
3
|
+
import { Contract } from "@hulla/api";
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
/** The portable part of Cloudflare's generated `ExecutionContext` type used by an HTTP Worker. */
|
|
6
|
+
type CloudflareExecutionContext = {
|
|
7
|
+
readonly passThroughOnException: () => void;
|
|
8
|
+
readonly waitUntil: (promise: Promise<unknown>) => void;
|
|
9
|
+
};
|
|
10
|
+
type CloudflareWorkerHandler<Env = unknown, ExecutionContext extends CloudflareExecutionContext = CloudflareExecutionContext> = (request: Request, env: Env, ctx: ExecutionContext) => Promise<Response>;
|
|
11
|
+
type CloudflareAdapterContext<Env, ExecutionContext extends CloudflareExecutionContext> = {
|
|
12
|
+
readonly request: Request;
|
|
13
|
+
readonly env: Env;
|
|
14
|
+
readonly ctx: ExecutionContext;
|
|
15
|
+
};
|
|
16
|
+
type CloudflareHandlerContext<Env, ExecutionContext extends CloudflareExecutionContext> = {
|
|
17
|
+
readonly env: Env;
|
|
18
|
+
readonly ctx: ExecutionContext;
|
|
19
|
+
};
|
|
20
|
+
type CloudflareServerErrorInput<Env = unknown, ExecutionContext extends CloudflareExecutionContext = CloudflareExecutionContext> = Omit<FetchServerErrorInput<CloudflareHandlerContext<Env, ExecutionContext>>, 'handlerContext'> & {
|
|
21
|
+
readonly env: Env;
|
|
22
|
+
readonly ctx: ExecutionContext;
|
|
23
|
+
};
|
|
24
|
+
type CloudflareServerOptions<Env = unknown, ExecutionContext extends CloudflareExecutionContext = CloudflareExecutionContext> = {
|
|
25
|
+
readonly onError?: ((input: CloudflareServerErrorInput<Env, ExecutionContext>) => Awaitable<Response | undefined | void>) | undefined;
|
|
26
|
+
};
|
|
27
|
+
type CloudflareContextInput<ContractType extends Contract = Contract, Env = unknown, ExecutionContext extends CloudflareExecutionContext = CloudflareExecutionContext> = ServerContextInput<ContractType> & CloudflareAdapterContext<Env, ExecutionContext>;
|
|
28
|
+
type CloudflareAdapter<Env = unknown, ExecutionContext extends CloudflareExecutionContext = CloudflareExecutionContext> = ServerAdapter<'cloudflare-workers', CloudflareAdapterContext<Env, ExecutionContext>> & {
|
|
29
|
+
readonly mount: <const ContractType extends Contract, const Context extends object>(implementation: ServerExecutableFor<ContractType, Context, 'cloudflare-workers', CloudflareAdapterContext<Env, ExecutionContext>>, options?: CloudflareServerOptions<Env, ExecutionContext>) => CloudflareWorkerHandler<Env, ExecutionContext>;
|
|
30
|
+
};
|
|
31
|
+
/** Creates a Cloudflare Workers adapter with native context and mounting operations. */
|
|
32
|
+
declare function cloudflareAdapter<Env = unknown, ExecutionContext extends CloudflareExecutionContext = CloudflareExecutionContext>(options?: CloudflareServerOptions<Env, ExecutionContext>): CloudflareAdapter<Env, ExecutionContext>;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { CloudflareAdapter, CloudflareContextInput, CloudflareExecutionContext, CloudflareServerErrorInput, CloudflareServerOptions, CloudflareWorkerHandler, cloudflareAdapter };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createFetchHandler } from "@hulla/api/fetch";
|
|
2
|
+
import { createServerAdapter, serverContextAdapterId } from "@hulla/api/server";
|
|
3
|
+
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
/** Creates the `fetch` handler for a Cloudflare Module Worker. */
|
|
6
|
+
function createWorkerHandler(implementation, options = {}) {
|
|
7
|
+
const usesNativeContext = serverContextAdapterId(implementation.context) !== void 0;
|
|
8
|
+
const handler = createFetchHandler(implementation, {
|
|
9
|
+
contextAdapter: "cloudflare-workers",
|
|
10
|
+
...usesNativeContext ? { contextInput: (_request, input) => input } : {},
|
|
11
|
+
...options.onError === void 0 ? {} : { onError: ({ handlerContext, ...input }) => options.onError?.({
|
|
12
|
+
...input,
|
|
13
|
+
...handlerContext
|
|
14
|
+
}) }
|
|
15
|
+
});
|
|
16
|
+
if (!usesNativeContext && options.onError === void 0) return (request) => handler(request, void 0);
|
|
17
|
+
return (request, env, ctx) => handler(request, {
|
|
18
|
+
env,
|
|
19
|
+
ctx
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
let cloudflareAdapterValue;
|
|
23
|
+
function createCloudflareAdapter(defaults) {
|
|
24
|
+
return createServerAdapter("cloudflare-workers", { mount: (implementation, options) => createWorkerHandler(implementation, options === void 0 ? defaults : {
|
|
25
|
+
...defaults,
|
|
26
|
+
...options
|
|
27
|
+
}) });
|
|
28
|
+
}
|
|
29
|
+
/** Creates a Cloudflare Workers adapter with native context and mounting operations. */
|
|
30
|
+
function cloudflareAdapter(options) {
|
|
31
|
+
if (options !== void 0) return createCloudflareAdapter({ ...options });
|
|
32
|
+
cloudflareAdapterValue ??= createCloudflareAdapter({});
|
|
33
|
+
return cloudflareAdapterValue;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
export { cloudflareAdapter };
|
package/dist/pages.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { FetchServerErrorInput } from "@hulla/api/fetch";
|
|
2
|
+
import { Awaitable, ServerAdapter, ServerContextInput, ServerExecutableFor } from "@hulla/api/server";
|
|
3
|
+
import { Contract } from "@hulla/api";
|
|
4
|
+
//#region src/pages.d.ts
|
|
5
|
+
type CloudflarePagesFetcher = {
|
|
6
|
+
readonly fetch: typeof fetch;
|
|
7
|
+
};
|
|
8
|
+
type CloudflarePagesParams<Param extends string = string> = Record<Param, string | string[]>;
|
|
9
|
+
/** The native context supplied to a Cloudflare Pages Function. */
|
|
10
|
+
type CloudflarePagesEventContext<Env = unknown, Param extends string = string, Data extends Record<string, unknown> = Record<string, unknown>> = {
|
|
11
|
+
readonly request: Request;
|
|
12
|
+
readonly functionPath: string;
|
|
13
|
+
readonly waitUntil: (promise: Promise<unknown>) => void;
|
|
14
|
+
readonly passThroughOnException: () => void;
|
|
15
|
+
readonly next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
|
|
16
|
+
readonly env: Env & {
|
|
17
|
+
readonly ASSETS: CloudflarePagesFetcher;
|
|
18
|
+
};
|
|
19
|
+
readonly params: CloudflarePagesParams<Param>;
|
|
20
|
+
readonly data: Data;
|
|
21
|
+
};
|
|
22
|
+
type CloudflarePagesFunction<Env = unknown, Param extends string = string, Data extends Record<string, unknown> = Record<string, unknown>> = (context: CloudflarePagesEventContext<Env, Param, Data>) => Promise<Response>;
|
|
23
|
+
type CloudflarePagesContextInput<ContractType extends Contract = Contract, Env = unknown, Param extends string = string, Data extends Record<string, unknown> = Record<string, unknown>> = ServerContextInput<ContractType> & CloudflarePagesEventContext<Env, Param, Data>;
|
|
24
|
+
type CloudflarePagesServerErrorInput<Env = unknown, Param extends string = string, Data extends Record<string, unknown> = Record<string, unknown>> = Omit<FetchServerErrorInput<CloudflarePagesEventContext<Env, Param, Data>>, 'handlerContext'> & CloudflarePagesEventContext<Env, Param, Data>;
|
|
25
|
+
type CloudflarePagesServerOptions<Env = unknown, Param extends string = string, Data extends Record<string, unknown> = Record<string, unknown>> = {
|
|
26
|
+
readonly onError?: ((input: CloudflarePagesServerErrorInput<Env, Param, Data>) => Awaitable<Response | undefined | void>) | undefined;
|
|
27
|
+
};
|
|
28
|
+
type CloudflarePagesAdapter<Env = unknown, Param extends string = string, Data extends Record<string, unknown> = Record<string, unknown>> = ServerAdapter<'cloudflare-pages', CloudflarePagesEventContext<Env, Param, Data>> & {
|
|
29
|
+
readonly mount: <const ContractType extends Contract, const Context extends object>(implementation: ServerExecutableFor<ContractType, Context, 'cloudflare-pages', CloudflarePagesEventContext<Env, Param, Data>>, options?: CloudflarePagesServerOptions<Env, Param, Data>) => CloudflarePagesFunction<Env, Param, Data>;
|
|
30
|
+
};
|
|
31
|
+
/** Creates a Cloudflare Pages Functions adapter with native event context and mounting operations. */
|
|
32
|
+
declare function cloudflarePagesAdapter<Env = unknown, Param extends string = string, Data extends Record<string, unknown> = Record<string, unknown>>(options?: CloudflarePagesServerOptions<Env, Param, Data>): CloudflarePagesAdapter<Env, Param, Data>;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { CloudflarePagesAdapter, CloudflarePagesContextInput, CloudflarePagesEventContext, CloudflarePagesFetcher, CloudflarePagesFunction, CloudflarePagesParams, CloudflarePagesServerErrorInput, CloudflarePagesServerOptions, cloudflarePagesAdapter };
|
package/dist/pages.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createFetchHandler } from "@hulla/api/fetch";
|
|
2
|
+
import { createServerAdapter } from "@hulla/api/server";
|
|
3
|
+
|
|
4
|
+
//#region src/pages.ts
|
|
5
|
+
function createPagesHandler(implementation, options) {
|
|
6
|
+
const handler = createFetchHandler(implementation, {
|
|
7
|
+
contextAdapter: "cloudflare-pages",
|
|
8
|
+
contextInput: (_request, eventContext) => eventContext,
|
|
9
|
+
...options.onError === void 0 ? {} : { onError: ({ handlerContext, ...input }) => options.onError?.({
|
|
10
|
+
...handlerContext,
|
|
11
|
+
...input
|
|
12
|
+
}) }
|
|
13
|
+
});
|
|
14
|
+
return (eventContext) => handler(eventContext.request, eventContext);
|
|
15
|
+
}
|
|
16
|
+
let cloudflarePagesAdapterValue;
|
|
17
|
+
function createCloudflarePagesAdapter(defaults) {
|
|
18
|
+
return createServerAdapter("cloudflare-pages", { mount: (implementation, options) => createPagesHandler(implementation, options === void 0 ? defaults : {
|
|
19
|
+
...defaults,
|
|
20
|
+
...options
|
|
21
|
+
}) });
|
|
22
|
+
}
|
|
23
|
+
/** Creates a Cloudflare Pages Functions adapter with native event context and mounting operations. */
|
|
24
|
+
function cloudflarePagesAdapter(options) {
|
|
25
|
+
if (options !== void 0) return createCloudflarePagesAdapter({ ...options });
|
|
26
|
+
cloudflarePagesAdapterValue ??= createCloudflarePagesAdapter({});
|
|
27
|
+
return cloudflarePagesAdapterValue;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
export { cloudflarePagesAdapter };
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hulla/api-cloudflare",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "Cloudflare Workers and Pages Functions server adapters for @hulla/api",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/hulladev/api.git",
|
|
9
|
+
"directory": "packages/cloudflare"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./pages": {
|
|
22
|
+
"types": "./dist/pages.d.ts",
|
|
23
|
+
"import": "./dist/pages.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public",
|
|
28
|
+
"registry": "https://registry.npmjs.org/"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsdown",
|
|
32
|
+
"clean": "rm -rf dist coverage",
|
|
33
|
+
"test": "bun run build && vitest run",
|
|
34
|
+
"test:coverage": "bun run build && vitest run --coverage",
|
|
35
|
+
"test:watch": "vitest",
|
|
36
|
+
"typecheck": "tsc --noEmit"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@hulla/api": "2.0.1",
|
|
40
|
+
"zod": "^4.4.3"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@hulla/api": "^2.0.1"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/README.md
DELETED