@velajs/cloudflare 1.3.0 → 1.5.0

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.
@@ -34,8 +34,8 @@ export type MountOpenApiOptions = Parameters<VelaApplication['mountOpenApi']>[0]
34
34
  * const app = await createCloudflareApp(AppModule);
35
35
  * const document = createOpenApiDocument(AppModule);
36
36
  * app.mountOpenApi({ document, ui: 'scalar' });
37
- * // GET /docs.json -> JSON document
38
- * // GET /docs -> Scalar UI (loads from CDN)
37
+ * // GET /openapi.json -> JSON document
38
+ * // GET /scalar -> Scalar UI (loads from CDN)
39
39
  * ```
40
40
  */
41
41
  export declare class CloudflareApplication {
@@ -45,11 +45,24 @@ export declare class CloudflareApplication {
45
45
  constructor(app: VelaApplication);
46
46
  get fetch(): Hono['fetch'];
47
47
  getHonoApp(): Hono;
48
+ /**
49
+ * Resolve a provider from the application's DI container (delegates to
50
+ * `VelaApplication.get`). Handy for grabbing a service — e.g. an auth service —
51
+ * to use inside `createCloudflareApp({ middleware: [...] })` request middleware,
52
+ * which runs outside the DI request pipeline.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const app = await createCloudflareApp(AppModule);
57
+ * const auth = app.get<BetterAuthService>(BetterAuthService);
58
+ * ```
59
+ */
60
+ get<T>(token: Parameters<VelaApplication['get']>[0]): T;
48
61
  /**
49
62
  * Serve a pre-built OpenAPI document (and optionally a Scalar UI) on the
50
63
  * underlying Hono app. Delegates verbatim to `VelaApplication.mountOpenApi`,
51
- * so the JSON endpoint defaults to `/docs.json` and the Scalar UI (when
52
- * opted in) defaults to `/docs`. Edge-safe — the UI HTML loads Scalar from
64
+ * so the JSON endpoint defaults to `/openapi.json` and the Scalar UI (when
65
+ * opted in) defaults to `/scalar`. Edge-safe — the UI HTML loads Scalar from
53
66
  * a CDN at runtime, nothing is bundled server-side.
54
67
  *
55
68
  * @example
@@ -61,8 +74,8 @@ export declare class CloudflareApplication {
61
74
  * info: { title: 'My API', version: '1.0.0' },
62
75
  * });
63
76
  * app.mountOpenApi({ document, ui: 'scalar' });
64
- * // GET /docs.json -> { openapi: '3.1.0', ... }
65
- * // GET /docs -> Scalar UI HTML
77
+ * // GET /openapi.json -> { openapi: '3.1.0', ... }
78
+ * // GET /scalar -> Scalar UI HTML
66
79
  * ```
67
80
  */
68
81
  mountOpenApi(options: MountOpenApiOptions): this;
@@ -33,8 +33,8 @@ function invoke(instance, methodName, args) {
33
33
  * const app = await createCloudflareApp(AppModule);
34
34
  * const document = createOpenApiDocument(AppModule);
35
35
  * app.mountOpenApi({ document, ui: 'scalar' });
36
- * // GET /docs.json -> JSON document
37
- * // GET /docs -> Scalar UI (loads from CDN)
36
+ * // GET /openapi.json -> JSON document
37
+ * // GET /scalar -> Scalar UI (loads from CDN)
38
38
  * ```
39
39
  */ export class CloudflareApplication {
40
40
  app;
@@ -50,10 +50,24 @@ function invoke(instance, methodName, args) {
50
50
  return this.app.getHonoApp();
51
51
  }
52
52
  /**
53
+ * Resolve a provider from the application's DI container (delegates to
54
+ * `VelaApplication.get`). Handy for grabbing a service — e.g. an auth service —
55
+ * to use inside `createCloudflareApp({ middleware: [...] })` request middleware,
56
+ * which runs outside the DI request pipeline.
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * const app = await createCloudflareApp(AppModule);
61
+ * const auth = app.get<BetterAuthService>(BetterAuthService);
62
+ * ```
63
+ */ get(token) {
64
+ return this.app.get(token);
65
+ }
66
+ /**
53
67
  * Serve a pre-built OpenAPI document (and optionally a Scalar UI) on the
54
68
  * underlying Hono app. Delegates verbatim to `VelaApplication.mountOpenApi`,
55
- * so the JSON endpoint defaults to `/docs.json` and the Scalar UI (when
56
- * opted in) defaults to `/docs`. Edge-safe — the UI HTML loads Scalar from
69
+ * so the JSON endpoint defaults to `/openapi.json` and the Scalar UI (when
70
+ * opted in) defaults to `/scalar`. Edge-safe — the UI HTML loads Scalar from
57
71
  * a CDN at runtime, nothing is bundled server-side.
58
72
  *
59
73
  * @example
@@ -65,8 +79,8 @@ function invoke(instance, methodName, args) {
65
79
  * info: { title: 'My API', version: '1.0.0' },
66
80
  * });
67
81
  * app.mountOpenApi({ document, ui: 'scalar' });
68
- * // GET /docs.json -> { openapi: '3.1.0', ... }
69
- * // GET /docs -> Scalar UI HTML
82
+ * // GET /openapi.json -> { openapi: '3.1.0', ... }
83
+ * // GET /scalar -> Scalar UI HTML
70
84
  * ```
71
85
  */ mountOpenApi(options) {
72
86
  this.app.mountOpenApi(options);
@@ -1,6 +1,7 @@
1
1
  import { VelaFactory } from "@velajs/vela";
2
2
  import { BindingRef } from "./binding-ref.js";
3
3
  import { CloudflareApplication } from "./cloudflare-application.js";
4
+ import { EnvRef } from "./env-ref.js";
4
5
  // Walks every provider registered in the container and pulls out the
5
6
  // BindingRef instances. Replaces the old module-level `bindingsRegistry`
6
7
  // global so two CloudflareApplication instances in one process don't
@@ -56,7 +57,9 @@ function collectBindingRefs(container) {
56
57
  initialized = true;
57
58
  const env = c.env ?? {};
58
59
  for (const ref of refs){
59
- ref._initialize(env[ref.bindingName]);
60
+ // EnvRef holds the whole env; every other ref holds one binding.
61
+ if (ref instanceof EnvRef) ref._initialize(env);
62
+ else ref._initialize(env[ref.bindingName]);
60
63
  }
61
64
  }
62
65
  await next();
@@ -0,0 +1,10 @@
1
+ import { BindingRef } from './binding-ref';
2
+ /**
3
+ * Holds the entire Cloudflare Worker `env` record (bindings + vars/secrets),
4
+ * not a single binding. Subclasses {@link BindingRef} so createCloudflareApp's
5
+ * binding-init middleware collects and initializes it through the same path;
6
+ * the factory special-cases it to pass the full `env` instead of one binding.
7
+ */
8
+ export declare class EnvRef extends BindingRef<Record<string, unknown>> {
9
+ constructor();
10
+ }
@@ -0,0 +1,13 @@
1
+ import { BindingRef } from "./binding-ref.js";
2
+ // Sentinel binding name for the whole-env holder — never used as an env key.
3
+ const ENV_SENTINEL = '__cf_env__';
4
+ /**
5
+ * Holds the entire Cloudflare Worker `env` record (bindings + vars/secrets),
6
+ * not a single binding. Subclasses {@link BindingRef} so createCloudflareApp's
7
+ * binding-init middleware collects and initializes it through the same path;
8
+ * the factory special-cases it to pass the full `env` instead of one binding.
9
+ */ export class EnvRef extends BindingRef {
10
+ constructor(){
11
+ super(ENV_SENTINEL);
12
+ }
13
+ }
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export { DurableObjectModule } from './modules/durable-object.module';
10
10
  export { AIModule } from './modules/ai.module';
11
11
  export { VectorizeModule } from './modules/vectorize.module';
12
12
  export { HyperdriveModule } from './modules/hyperdrive.module';
13
+ export { EnvModule } from './modules/env.module';
13
14
  export { KVService } from './services/kv.service';
14
15
  export { D1Service } from './services/d1.service';
15
16
  export { R2Service } from './services/r2.service';
@@ -18,6 +19,7 @@ export { DurableObjectService } from './services/durable-object.service';
18
19
  export { AIService } from './services/ai.service';
19
20
  export { VectorizeService } from './services/vectorize.service';
20
21
  export { HyperdriveService } from './services/hyperdrive.service';
22
+ export { EnvService } from './services/env.service';
21
23
  export { Env } from './decorators/env';
22
24
  export { Scheduled } from './decorators/scheduled';
23
25
  export { QueueConsumer } from './decorators/queue-consumer';
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ export { DurableObjectModule } from "./modules/durable-object.module.js";
10
10
  export { AIModule } from "./modules/ai.module.js";
11
11
  export { VectorizeModule } from "./modules/vectorize.module.js";
12
12
  export { HyperdriveModule } from "./modules/hyperdrive.module.js";
13
+ export { EnvModule } from "./modules/env.module.js";
13
14
  // Services
14
15
  export { KVService } from "./services/kv.service.js";
15
16
  export { D1Service } from "./services/d1.service.js";
@@ -19,6 +20,7 @@ export { DurableObjectService } from "./services/durable-object.service.js";
19
20
  export { AIService } from "./services/ai.service.js";
20
21
  export { VectorizeService } from "./services/vectorize.service.js";
21
22
  export { HyperdriveService } from "./services/hyperdrive.service.js";
23
+ export { EnvService } from "./services/env.service.js";
22
24
  // Decorators
23
25
  export { Env } from "./decorators/env.js";
24
26
  export { Scheduled } from "./decorators/scheduled.js";
@@ -0,0 +1,17 @@
1
+ import type { DynamicModule } from '@velajs/vela';
2
+ /**
3
+ * Provides {@link EnvService} GLOBALLY so any module's provider factories can
4
+ * `inject: [EnvService]`. Register once on the root module:
5
+ *
6
+ * ```ts
7
+ * @Module({ imports: [EnvModule.forRoot(), AuthModule.forRootAsync({ ... })] })
8
+ * class AppModule {}
9
+ * ```
10
+ *
11
+ * The {@link EnvRef} it provides is collected and initialized by
12
+ * createCloudflareApp's binding-init middleware on the first request (the
13
+ * factory passes it the full `env`, not a single binding).
14
+ */
15
+ export declare class EnvModule {
16
+ static forRoot(): DynamicModule;
17
+ }
@@ -0,0 +1,35 @@
1
+ import { EnvRef } from "../env-ref.js";
2
+ import { EnvService } from "../services/env.service.js";
3
+ import { ENV_REF } from "../tokens.js";
4
+ /**
5
+ * Provides {@link EnvService} GLOBALLY so any module's provider factories can
6
+ * `inject: [EnvService]`. Register once on the root module:
7
+ *
8
+ * ```ts
9
+ * @Module({ imports: [EnvModule.forRoot(), AuthModule.forRootAsync({ ... })] })
10
+ * class AppModule {}
11
+ * ```
12
+ *
13
+ * The {@link EnvRef} it provides is collected and initialized by
14
+ * createCloudflareApp's binding-init middleware on the first request (the
15
+ * factory passes it the full `env`, not a single binding).
16
+ */ export class EnvModule {
17
+ static forRoot() {
18
+ const ref = new EnvRef();
19
+ return {
20
+ module: EnvModule,
21
+ global: true,
22
+ providers: [
23
+ {
24
+ provide: ENV_REF,
25
+ useValue: ref
26
+ },
27
+ EnvService
28
+ ],
29
+ exports: [
30
+ EnvService,
31
+ ENV_REF
32
+ ]
33
+ };
34
+ }
35
+ }
@@ -0,0 +1,31 @@
1
+ import type { EnvRef } from '../env-ref';
2
+ /**
3
+ * Injectable access to the full Cloudflare Worker `env` (bindings + vars +
4
+ * secrets).
5
+ *
6
+ * The per-binding services (`D1Service`, `KVService`, …) each expose a single
7
+ * binding, and the `@Env()` param decorator only works inside a request-scoped
8
+ * controller handler. `EnvService` fills the gap: it can be injected into
9
+ * PROVIDER FACTORIES — e.g. `SomeModule.forRootAsync({ inject: [EnvService] })`
10
+ * — so a factory can read secrets/origins without reaching for the request.
11
+ *
12
+ * Reads are lazy. `env` only exists per request, so a factory (which runs at
13
+ * bootstrap) must capture the `EnvService` and read inside its callback:
14
+ *
15
+ * ```ts
16
+ * AuthModule.forRootAsync({
17
+ * inject: [EnvService],
18
+ * useFactory: (env: EnvService) => buildAuth(() => env.get<string>('AUTH_SECRET')),
19
+ * });
20
+ * ```
21
+ *
22
+ * Reading eagerly at bootstrap (`env.get(...)` before any request) throws.
23
+ */
24
+ export declare class EnvService {
25
+ private ref;
26
+ constructor(ref: EnvRef);
27
+ /** The full env record. Throws if read before the first request. */
28
+ get env(): Record<string, unknown>;
29
+ /** Read a single env entry (binding, var, or secret) by name. */
30
+ get<T = unknown>(key: string): T | undefined;
31
+ }
@@ -0,0 +1,36 @@
1
+ function _ts_decorate(decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ }
7
+ function _ts_metadata(k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ }
10
+ function _ts_param(paramIndex, decorator) {
11
+ return function(target, key) {
12
+ decorator(target, key, paramIndex);
13
+ };
14
+ }
15
+ import { Inject, Injectable } from "@velajs/vela";
16
+ import { ENV_REF } from "../tokens.js";
17
+ export class EnvService {
18
+ ref;
19
+ constructor(ref){
20
+ this.ref = ref;
21
+ }
22
+ /** The full env record. Throws if read before the first request. */ get env() {
23
+ return this.ref.value;
24
+ }
25
+ /** Read a single env entry (binding, var, or secret) by name. */ get(key) {
26
+ return this.ref.value[key];
27
+ }
28
+ }
29
+ EnvService = _ts_decorate([
30
+ Injectable(),
31
+ _ts_param(0, Inject(ENV_REF)),
32
+ _ts_metadata("design:type", Function),
33
+ _ts_metadata("design:paramtypes", [
34
+ typeof EnvRef === "undefined" ? Object : EnvRef
35
+ ])
36
+ ], EnvService);
package/dist/tokens.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { InjectionToken } from '@velajs/vela';
2
2
  import type { BindingRef } from './binding-ref';
3
+ import type { EnvRef } from './env-ref';
3
4
  export declare const KV_BINDING_REF: InjectionToken<BindingRef<unknown>>;
4
5
  export declare const D1_BINDING_REF: InjectionToken<BindingRef<unknown>>;
5
6
  export declare const R2_BINDING_REF: InjectionToken<BindingRef<unknown>>;
@@ -8,3 +9,4 @@ export declare const DO_BINDING_REF: InjectionToken<BindingRef<unknown>>;
8
9
  export declare const AI_BINDING_REF: InjectionToken<BindingRef<unknown>>;
9
10
  export declare const VECTORIZE_BINDING_REF: InjectionToken<BindingRef<unknown>>;
10
11
  export declare const HYPERDRIVE_BINDING_REF: InjectionToken<BindingRef<unknown>>;
12
+ export declare const ENV_REF: InjectionToken<EnvRef>;
package/dist/tokens.js CHANGED
@@ -8,3 +8,5 @@ export const DO_BINDING_REF = new InjectionToken('CF_DO_BINDING_REF');
8
8
  export const AI_BINDING_REF = new InjectionToken('CF_AI_BINDING_REF');
9
9
  export const VECTORIZE_BINDING_REF = new InjectionToken('CF_VECTORIZE_BINDING_REF');
10
10
  export const HYPERDRIVE_BINDING_REF = new InjectionToken('CF_HYPERDRIVE_BINDING_REF');
11
+ // Token for the whole-env holder (full c.env), backing EnvService.
12
+ export const ENV_REF = new InjectionToken('CF_ENV_REF');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velajs/cloudflare",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "Cloudflare Workers integration for Vela framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -17,6 +17,13 @@
17
17
  "LICENSE",
18
18
  "CHANGELOG.md"
19
19
  ],
20
+ "scripts": {
21
+ "build": "rm -rf dist && swc src -d dist --strip-leading-paths && tsc --emitDeclarationOnly",
22
+ "test": "vitest run",
23
+ "typecheck": "tsc --noEmit",
24
+ "prepare": "swc src -d dist --strip-leading-paths && tsc --emitDeclarationOnly",
25
+ "prepublishOnly": "pnpm run typecheck && pnpm test"
26
+ },
20
27
  "sideEffects": false,
21
28
  "keywords": [
22
29
  "vela",
@@ -45,22 +52,24 @@
45
52
  },
46
53
  "peerDependencies": {
47
54
  "@cloudflare/workers-types": ">=4",
48
- "@velajs/vela": ">=1.5.0",
55
+ "@velajs/vela": ">=1.8.5",
49
56
  "hono": ">=4"
50
57
  },
51
58
  "devDependencies": {
52
- "@cloudflare/workers-types": "^4",
53
- "@swc/cli": "^0.8.0",
54
- "@swc/core": "^1.15.11",
55
- "@velajs/vela": "^1.5.0",
56
- "hono": "^4",
57
- "typescript": "^5",
59
+ "@cloudflare/workers-types": "^4.20260624.1",
60
+ "@swc/cli": "^0.8.1",
61
+ "@swc/core": "^1.15.43",
62
+ "@velajs/vela": "^1.8.5",
63
+ "hono": "^4.12.27",
64
+ "typescript": "^6.0.3",
58
65
  "unplugin-swc": "^1.5.9",
59
- "vitest": "^4.0.18"
66
+ "vitest": "^4.1.9"
60
67
  },
61
- "scripts": {
62
- "build": "rm -rf dist && swc src -d dist --strip-leading-paths && tsc --emitDeclarationOnly",
63
- "test": "vitest run",
64
- "typecheck": "tsc --noEmit"
68
+ "packageManager": "pnpm@10.20.0",
69
+ "pnpm": {
70
+ "onlyBuiltDependencies": [
71
+ "@swc/core",
72
+ "esbuild"
73
+ ]
65
74
  }
66
- }
75
+ }