@encore.dev/sdk 2.0.0-beta.15 → 2.0.0-beta.16

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.
Files changed (235) hide show
  1. package/README.md +111 -101
  2. package/app_meta.ts +56 -54
  3. package/config/config.ts +60 -57
  4. package/config/secrets.ts +45 -52
  5. package/config/value.ts +23 -26
  6. package/cron/mod.ts +28 -29
  7. package/dist/app_meta.d.ts +52 -50
  8. package/dist/app_meta.d.ts.map +1 -1
  9. package/dist/app_meta.js +2 -2
  10. package/dist/app_meta.js.map +1 -1
  11. package/dist/config/config.d.ts +58 -55
  12. package/dist/config/config.d.ts.map +1 -1
  13. package/dist/config/config.js +8 -9
  14. package/dist/config/config.js.map +1 -1
  15. package/dist/config/secrets.d.ts +45 -52
  16. package/dist/config/secrets.d.ts.map +1 -1
  17. package/dist/config/secrets.js +8 -9
  18. package/dist/config/secrets.js.map +1 -1
  19. package/dist/config/value.d.ts +20 -23
  20. package/dist/config/value.d.ts.map +1 -1
  21. package/dist/config/value.js +20 -23
  22. package/dist/config/value.js.map +1 -1
  23. package/dist/cron/mod.d.ts +26 -27
  24. package/dist/cron/mod.d.ts.map +1 -1
  25. package/dist/cron/mod.js +13 -14
  26. package/dist/cron/mod.js.map +1 -1
  27. package/dist/frontends/mod.d.ts +20 -19
  28. package/dist/frontends/mod.d.ts.map +1 -1
  29. package/dist/frontends/mod.js +20 -19
  30. package/dist/frontends/mod.js.map +1 -1
  31. package/dist/gateway/errors.d.ts +28 -30
  32. package/dist/gateway/errors.d.ts.map +1 -1
  33. package/dist/gateway/errors.js +29 -31
  34. package/dist/gateway/errors.js.map +1 -1
  35. package/dist/gateway/match.d.ts +54 -54
  36. package/dist/gateway/match.d.ts.map +1 -1
  37. package/dist/gateway/match.js +56 -56
  38. package/dist/gateway/match.js.map +1 -1
  39. package/dist/gateway/middleware.d.ts +62 -64
  40. package/dist/gateway/middleware.d.ts.map +1 -1
  41. package/dist/gateway/middleware.js +1 -1
  42. package/dist/gateway/middleware.js.map +1 -1
  43. package/dist/gateway/mod.d.ts +64 -67
  44. package/dist/gateway/mod.d.ts.map +1 -1
  45. package/dist/gateway/mod.js +72 -76
  46. package/dist/gateway/mod.js.map +1 -1
  47. package/dist/gateway/request.d.ts +138 -134
  48. package/dist/gateway/request.d.ts.map +1 -1
  49. package/dist/gateway/request.js +117 -108
  50. package/dist/gateway/request.js.map +1 -1
  51. package/dist/gateway/serve.d.ts +29 -34
  52. package/dist/gateway/serve.d.ts.map +1 -1
  53. package/dist/gateway/serve.js +51 -56
  54. package/dist/gateway/serve.js.map +1 -1
  55. package/dist/gateways/mod.d.ts +23 -21
  56. package/dist/gateways/mod.d.ts.map +1 -1
  57. package/dist/gateways/mod.js +25 -23
  58. package/dist/gateways/mod.js.map +1 -1
  59. package/dist/internal/runtime/napi/napi.cjs +1 -1
  60. package/dist/internal/runtime/napi/napi.d.cts +10 -3
  61. package/dist/internal/runtime/shutdown.d.ts +75 -57
  62. package/dist/internal/runtime/shutdown.d.ts.map +1 -1
  63. package/dist/internal/runtime/shutdown.js +88 -84
  64. package/dist/internal/runtime/shutdown.js.map +1 -1
  65. package/dist/log/logger.d.ts +43 -37
  66. package/dist/log/logger.d.ts.map +1 -1
  67. package/dist/log/logger.js +26 -25
  68. package/dist/log/logger.js.map +1 -1
  69. package/dist/metrics/mod.d.ts +133 -114
  70. package/dist/metrics/mod.d.ts.map +1 -1
  71. package/dist/metrics/mod.js +109 -101
  72. package/dist/metrics/mod.js.map +1 -1
  73. package/dist/otel.d.ts +6 -3
  74. package/dist/otel.d.ts.map +1 -1
  75. package/dist/otel.js +6 -3
  76. package/dist/otel.js.map +1 -1
  77. package/dist/pubsub/subscription.d.ts +88 -116
  78. package/dist/pubsub/subscription.d.ts.map +1 -1
  79. package/dist/pubsub/subscription.js +14 -17
  80. package/dist/pubsub/subscription.js.map +1 -1
  81. package/dist/pubsub/topic.d.ts +86 -113
  82. package/dist/pubsub/topic.d.ts.map +1 -1
  83. package/dist/pubsub/topic.js +68 -82
  84. package/dist/pubsub/topic.js.map +1 -1
  85. package/dist/reqctx/mod.d.ts +83 -86
  86. package/dist/reqctx/mod.d.ts.map +1 -1
  87. package/dist/reqctx/mod.js +39 -41
  88. package/dist/reqctx/mod.js.map +1 -1
  89. package/dist/services/index.d.ts +76 -75
  90. package/dist/services/index.d.ts.map +1 -1
  91. package/dist/services/index.js +73 -71
  92. package/dist/services/index.js.map +1 -1
  93. package/dist/shutdown/mod.d.ts +26 -25
  94. package/dist/shutdown/mod.d.ts.map +1 -1
  95. package/dist/shutdown/mod.js +26 -25
  96. package/dist/shutdown/mod.js.map +1 -1
  97. package/dist/storage/clickhouse/database.d.ts +113 -115
  98. package/dist/storage/clickhouse/database.d.ts.map +1 -1
  99. package/dist/storage/clickhouse/database.js +65 -79
  100. package/dist/storage/clickhouse/database.js.map +1 -1
  101. package/dist/storage/clickhouse/errors.d.ts +25 -26
  102. package/dist/storage/clickhouse/errors.d.ts.map +1 -1
  103. package/dist/storage/clickhouse/errors.js +15 -17
  104. package/dist/storage/clickhouse/errors.js.map +1 -1
  105. package/dist/storage/clickhouse/mod.d.ts +10 -10
  106. package/dist/storage/clickhouse/mod.js +10 -10
  107. package/dist/storage/objects/bucket.d.ts +196 -225
  108. package/dist/storage/objects/bucket.d.ts.map +1 -1
  109. package/dist/storage/objects/bucket.js +71 -84
  110. package/dist/storage/objects/bucket.js.map +1 -1
  111. package/dist/storage/objects/error.d.ts +4 -4
  112. package/dist/storage/objects/error.js +4 -4
  113. package/dist/storage/postgres/database.d.ts +124 -149
  114. package/dist/storage/postgres/database.d.ts.map +1 -1
  115. package/dist/storage/postgres/database.js +98 -135
  116. package/dist/storage/postgres/database.js.map +1 -1
  117. package/dist/storage/postgres/errors.d.ts +42 -48
  118. package/dist/storage/postgres/errors.d.ts.map +1 -1
  119. package/dist/storage/postgres/errors.js +44 -51
  120. package/dist/storage/postgres/errors.js.map +1 -1
  121. package/dist/storage/postgres/interval.d.ts +19 -19
  122. package/dist/storage/postgres/interval.d.ts.map +1 -1
  123. package/dist/storage/postgres/interval.js +21 -21
  124. package/dist/storage/postgres/interval.js.map +1 -1
  125. package/dist/storage/redis/bitfield.d.ts +48 -23
  126. package/dist/storage/redis/bitfield.d.ts.map +1 -1
  127. package/dist/storage/redis/bitfield.js +38 -19
  128. package/dist/storage/redis/bitfield.js.map +1 -1
  129. package/dist/storage/redis/command.d.ts +16 -12
  130. package/dist/storage/redis/command.d.ts.map +1 -1
  131. package/dist/storage/redis/command.js +16 -12
  132. package/dist/storage/redis/command.js.map +1 -1
  133. package/dist/storage/redis/commands.d.ts +99 -100
  134. package/dist/storage/redis/commands.d.ts.map +1 -1
  135. package/dist/storage/redis/commands.js +84 -85
  136. package/dist/storage/redis/commands.js.map +1 -1
  137. package/dist/storage/redis/database.d.ts +138 -122
  138. package/dist/storage/redis/database.d.ts.map +1 -1
  139. package/dist/storage/redis/database.js +120 -101
  140. package/dist/storage/redis/database.js.map +1 -1
  141. package/dist/storage/redis/mod.d.ts +52 -48
  142. package/dist/storage/redis/mod.d.ts.map +1 -1
  143. package/dist/storage/redis/mod.js +52 -48
  144. package/dist/storage/redis/mod.js.map +1 -1
  145. package/dist/storage/redis/options.d.ts +26 -24
  146. package/dist/storage/redis/options.d.ts.map +1 -1
  147. package/dist/storage/redis/options.js.map +1 -1
  148. package/dist/storage/redis/pubsub.d.ts +16 -16
  149. package/dist/storage/redis/pubsub.d.ts.map +1 -1
  150. package/dist/storage/redis/pubsub.js +14 -14
  151. package/dist/storage/redis/pubsub.js.map +1 -1
  152. package/dist/storage/redis/reply.d.ts +80 -72
  153. package/dist/storage/redis/reply.d.ts.map +1 -1
  154. package/dist/storage/redis/reply.js +53 -46
  155. package/dist/storage/redis/reply.js.map +1 -1
  156. package/dist/storage/redis/scan.d.ts +4 -4
  157. package/dist/storage/redis/scan.d.ts.map +1 -1
  158. package/dist/storage/redis/scan.js +6 -6
  159. package/dist/storage/redis/scan.js.map +1 -1
  160. package/dist/storage/redis/script.d.ts +28 -21
  161. package/dist/storage/redis/script.d.ts.map +1 -1
  162. package/dist/storage/redis/script.js +29 -22
  163. package/dist/storage/redis/script.js.map +1 -1
  164. package/dist/storage/redis/target.d.ts +17 -1
  165. package/dist/storage/redis/target.d.ts.map +1 -1
  166. package/dist/storage/redis/target.js +30 -1
  167. package/dist/storage/redis/target.js.map +1 -1
  168. package/dist/storage/redis/wire.d.ts +6 -6
  169. package/dist/storage/redis/wire.d.ts.map +1 -1
  170. package/dist/storage/redis/wire.js +6 -6
  171. package/dist/storage/redis/wire.js.map +1 -1
  172. package/dist/testing/mod.d.ts +83 -88
  173. package/dist/testing/mod.d.ts.map +1 -1
  174. package/dist/testing/mod.js +75 -81
  175. package/dist/testing/mod.js.map +1 -1
  176. package/dist/testing/playwright.d.ts +28 -32
  177. package/dist/testing/playwright.d.ts.map +1 -1
  178. package/dist/testing/playwright.js +22 -27
  179. package/dist/testing/playwright.js.map +1 -1
  180. package/dist/testing/puppeteer.d.ts +11 -12
  181. package/dist/testing/puppeteer.d.ts.map +1 -1
  182. package/dist/testing/puppeteer.js +17 -18
  183. package/dist/testing/puppeteer.js.map +1 -1
  184. package/dist/testing/system.d.ts +72 -90
  185. package/dist/testing/system.d.ts.map +1 -1
  186. package/dist/testing/system.js +90 -107
  187. package/dist/testing/system.js.map +1 -1
  188. package/dist/tsconfig.build.tsbuildinfo +1 -1
  189. package/frontends/mod.test.ts +6 -2
  190. package/frontends/mod.ts +20 -19
  191. package/gateway/errors.ts +30 -32
  192. package/gateway/match.ts +61 -57
  193. package/gateway/middleware.ts +62 -64
  194. package/gateway/mod.ts +73 -76
  195. package/gateway/request.ts +143 -140
  196. package/gateway/serve.ts +58 -63
  197. package/gateways/mod.ts +25 -23
  198. package/internal/runtime/napi/napi.cjs +1 -1
  199. package/internal/runtime/napi/napi.d.cts +10 -3
  200. package/internal/runtime/shutdown.ts +113 -101
  201. package/log/logger.test.ts +8 -0
  202. package/log/logger.ts +48 -45
  203. package/metrics/mod.ts +138 -122
  204. package/otel.ts +6 -3
  205. package/package.json +1 -1
  206. package/pubsub/subscription.ts +93 -123
  207. package/pubsub/topic.ts +96 -130
  208. package/reqctx/mod.ts +83 -86
  209. package/services/index.ts +78 -78
  210. package/shutdown/mod.ts +26 -25
  211. package/storage/clickhouse/database.ts +115 -129
  212. package/storage/clickhouse/errors.ts +27 -29
  213. package/storage/clickhouse/mod.ts +10 -10
  214. package/storage/objects/bucket.ts +209 -239
  215. package/storage/objects/error.ts +4 -4
  216. package/storage/postgres/database.ts +131 -164
  217. package/storage/postgres/errors.ts +45 -52
  218. package/storage/postgres/interval.ts +21 -21
  219. package/storage/redis/bitfield.ts +51 -26
  220. package/storage/redis/command.ts +16 -12
  221. package/storage/redis/commands.ts +99 -100
  222. package/storage/redis/database.ts +145 -123
  223. package/storage/redis/mod.ts +52 -48
  224. package/storage/redis/options.ts +26 -24
  225. package/storage/redis/pubsub.ts +14 -14
  226. package/storage/redis/redis.test.ts +17 -2
  227. package/storage/redis/reply.ts +85 -77
  228. package/storage/redis/scan.ts +6 -6
  229. package/storage/redis/script.ts +29 -22
  230. package/storage/redis/target.ts +34 -1
  231. package/storage/redis/wire.ts +8 -8
  232. package/testing/mod.ts +99 -105
  233. package/testing/playwright.ts +32 -37
  234. package/testing/puppeteer.ts +22 -23
  235. package/testing/system.ts +117 -133
package/config/config.ts CHANGED
@@ -6,18 +6,18 @@ import {build} from './value';
6
6
  export {Duration} from './value';
7
7
 
8
8
  /**
9
- * A config: a typed value a service reads, written in files beside the code
10
- * and changed while the app runs. Calling it reads the value now.
9
+ * A typed configuration value defined in config files alongside your code and
10
+ * hot-reloaded while the app runs. Call the handle to read the current value.
11
11
  *
12
- * Declare one with {@link config}, giving the value's type as the type
13
- * argument. Encore reads that type to know what the config's files may hold,
14
- * and checks them against it before the value reaches the code.
12
+ * Declare a config with {@link config}, passing its TypeScript type as the
13
+ * type argument. Encore derives a schema from that type and validates config
14
+ * files against it before delivering values to your code.
15
15
  *
16
16
  * ```ts
17
17
  * import { config, type Min, type Max, type Duration } from "@encore.dev/sdk/config";
18
18
  *
19
19
  * interface Checkout {
20
- * // Most items one order may hold.
20
+ * // Maximum number of items in an order.
21
21
  * maxItems: number & Int & Min<1> & Max<500>;
22
22
  * currencies?: string[];
23
23
  * paymentTimeout: Duration;
@@ -28,72 +28,76 @@ export {Duration} from './value';
28
28
  * if (items.length > checkout().maxItems) { ... }
29
29
  * ```
30
30
  *
31
- * # Values
31
+ * # Config files
32
32
  *
33
- * A config's value is written in a file named after it — `checkout.config.toml`,
34
- * or `.json` — beside the declaring service's `encore.service.json`, or beside
35
- * `encore.app.json` for a shared or global config. Files over it change it for some
36
- * environments only: `checkout.production.config.toml` for environments of a
37
- * type, `checkout.@prod-eu.config.toml` for one environment, and
38
- * `checkout.user.config.toml` — gitignored, the user override — for a
39
- * developer's own `encore run`, never in tests.
33
+ * Values live in a file named after the config — `checkout.config.toml` or
34
+ * `.json` — next to the declaring service's `encore.service.json` (or next to
35
+ * `encore.app.json` for a shared or global config). Environment-specific
36
+ * overlay files override the base file:
37
+ * - `checkout.production.config.toml` for all environments of a given type
38
+ * - `checkout.@prod-eu.config.toml` for a single named environment
39
+ * - `checkout.user.config.toml` (gitignored) for local overrides during
40
+ * `encore run` (ignored in tests)
40
41
  *
41
- * Calling the config returns a snapshot: the same object until the value
42
- * changes, which it may while the app runs, without a restart. Read it where
43
- * it is used rather than keeping it, or use {@link Config.onChange}.
42
+ * Calling the config returns a snapshot that remains the same object until the
43
+ * value changes. Because values can update at runtime without a restart, call
44
+ * the config where you use it rather than caching the result, or subscribe
45
+ * with {@link Config.onChange}.
44
46
  */
45
47
  export interface Config<T> {
46
- /** The config's value now. */
48
+ /** Returns the current config value. */
47
49
  (): T;
48
50
 
49
51
  /** The name the config was declared with. */
50
52
  readonly name: string;
51
53
 
52
54
  /**
53
- * Calls `listener` with each new value the config is given from now on,
54
- * while the app runs, until the returned function is called, whether or
55
- * not it is kept. Values that come faster than `listener` is called are
56
- * coalesced: it gets the latest, once. Does not keep the process alive.
55
+ * Registers a `listener` invoked whenever the config value changes while the
56
+ * app is running. Returns an unsubscribe function (the subscription stays
57
+ * active even if you discard the returned function). Rapid updates are
58
+ * coalesced so the listener receives the latest value once. Does not keep
59
+ * the process alive.
57
60
  */
58
61
  onChange(listener: (value: T) => void): () => void;
59
62
  }
60
63
 
61
- /** The options a config is declared with. */
64
+ /** Options for declaring a config with {@link config}. */
62
65
  export interface ConfigOptions {
63
66
  /**
64
- * Who the config belongs to, written as a string literal for Encore to
65
- * read:
67
+ * Controls which services own and can read the config. Must be a string
68
+ * literal so Encore can resolve it statically:
66
69
  *
67
- * - `"service"`, the default: the service that declares it, which holds
68
- * its own value for it. Two services may each declare a config of the
69
- * same name. The declaration must live inside a service.
70
- * - `"shared"`: the whole app, with one value. A service that reads it is
71
- * recorded as using it. It may be declared outside a service.
72
- * - `"global"`: the whole app, with one value available to every service
73
- * without being recorded as used. It may be declared outside a service.
70
+ * - `"service"` (default): scoped to the declaring service, which has its
71
+ * own value. Two services may declare configs with the same name. Must be
72
+ * declared inside a service.
73
+ * - `"shared"`: app-wide with a single value; each service that reads it is
74
+ * recorded as a consumer. May be declared outside a service.
75
+ * - `"global"`: app-wide with a single value available to every service
76
+ * without tracking per-service usage. May be declared outside a service.
74
77
  *
75
- * A config name has one scope across the app, as a secret's has: a name
76
- * declared shared or global can't also be a service's own.
78
+ * Each config name has a single scope across the app, just like secrets: a
79
+ * name declared as `shared` or `global` cannot also be declared as a
80
+ * service-scoped config.
77
81
  */
78
82
  scope?: 'service' | 'shared' | 'global';
79
83
 
80
84
  /**
81
- * Tags that classify the config, for policies to read: written out as
82
- * string literals, as in `{data: "pci"}`. Keys and values are lowercase
83
- * letters, digits, `_` and `-`, at most 63 characters; a key starts with a
84
- * letter, and not with `encore`, which is reserved. At most 32 tags.
85
+ * Static string-literal tags that classify the config for policy checks
86
+ * (e.g. `{data: "pci"}`). Keys and values may contain lowercase letters,
87
+ * digits, `_`, and `-`, up to 63 characters. Keys must start with a letter
88
+ * and cannot start with the reserved prefix `encore`. At most 32 tags.
85
89
  */
86
90
  tags?: Record<string, string>;
87
91
  }
88
92
 
89
93
  /**
90
- * config declares a {@link Config}, by default belonging to the service that
91
- * declares it (see {@link ConfigOptions.scope}). The declaration is a
92
- * module-level `export const`, with the name a string literal.
94
+ * Declares a {@link Config}, scoped by default to the declaring service (see
95
+ * {@link ConfigOptions.scope}). Must be a module-level `export const` with a
96
+ * string-literal name.
93
97
  *
94
98
  * @example
95
99
  * ```ts
96
- * // A config several services read.
100
+ * // A config shared by several services.
97
101
  * export const flags = config<Flags>("flags", {scope: "shared"});
98
102
  * ```
99
103
  */
@@ -113,20 +117,19 @@ export function config<T>(
113
117
  const names = new WeakMap<Config<any>, string>();
114
118
 
115
119
  /**
116
- * Gives `cfg` the value `value` for the test `scope`, once the core has
117
- * checked it against the config's schema: reading the config in the test
118
- * returns it from then on. For `@encore.dev/sdk/testing`.
120
+ * Sets `cfg` to `value` for the given test `scope` after validating it against
121
+ * the config's schema. For `@encore.dev/sdk/testing`.
119
122
  *
120
123
  * @internal
121
124
  */
122
125
  export function setTestValue<T>(scope: Scope, cfg: Config<T>, value: T): void {
123
126
  const name = names.get(cfg);
124
127
  if (name === undefined) {
125
- throw new TypeError('setConfig takes a config declared with `config()`');
128
+ throw new TypeError('setConfig requires a config declared with `config()`');
126
129
  }
127
130
  const json = JSON.stringify(value);
128
131
  if (json === undefined) {
129
- throw new TypeError(`config ${name}: the value has no JSON form`);
132
+ throw new TypeError(`config ${name}: value cannot be serialized to JSON`);
130
133
  }
131
134
  const prepared = JSON.parse(scope.native.setConfig(name, json));
132
135
  const impl = runtime.RT.config(name);
@@ -208,29 +211,29 @@ function makeConfig<T>(name: string, impl: Impl | null): Config<T> {
208
211
  }
209
212
 
210
213
  /**
211
- * The value is at least `N`. A constraint on a `number` field of a config's
212
- * type, written as an intersection: `number & Min<1>`. Encore checks the
213
- * config's files against it; at runtime the field is a plain `number`.
214
+ * Requires a `number` config field to be at least `N`, written as an
215
+ * intersection (`number & Min<1>`). Validated by Encore against config files;
216
+ * at runtime the field is a plain `number`.
214
217
  */
215
218
  export type Min<N extends number> = {readonly '~encore.min'?: N};
216
219
 
217
- /** The value is at most `N`: `number & Max<500>`. See {@link Min}. */
220
+ /** Requires a `number` config field to be at most `N` (`number & Max<500>`). See {@link Min}. */
218
221
  export type Max<N extends number> = {readonly '~encore.max'?: N};
219
222
 
220
- /** The value is a whole number: `number & Int`. See {@link Min}. */
223
+ /** Requires a `number` config field to be an integer (`number & Int`). See {@link Min}. */
221
224
  export type Int = {readonly '~encore.int'?: true};
222
225
 
223
- /** The string is at least `N` characters long: `string & MinLen<3>`. */
226
+ /** Requires a `string` config field to be at least `N` characters long (`string & MinLen<3>`). */
224
227
  export type MinLen<N extends number> = {readonly '~encore.min_len'?: N};
225
228
 
226
- /** The string is at most `N` characters long: `string & MaxLen<64>`. */
229
+ /** Requires a `string` config field to be at most `N` characters long (`string & MaxLen<64>`). */
227
230
  export type MaxLen<N extends number> = {readonly '~encore.max_len'?: N};
228
231
 
229
- /** The string matches the regular expression `S`: `string & Pattern<"^[A-Z]+$">`. */
232
+ /** Requires a `string` config field to match the regular expression `S` (`string & Pattern<"^[A-Z]+$">`). */
230
233
  export type Pattern<S extends string> = {readonly '~encore.pattern'?: S};
231
234
 
232
- /** The array has at least `N` items: `string[] & MinItems<1>`. */
235
+ /** Requires an array config field to have at least `N` items (`string[] & MinItems<1>`). */
233
236
  export type MinItems<N extends number> = {readonly '~encore.min_items'?: N};
234
237
 
235
- /** The array has at most `N` items: `string[] & MaxItems<10>`. */
238
+ /** Requires an array config field to have at most `N` items (`string[] & MaxItems<10>`). */
236
239
  export type MaxItems<N extends number> = {readonly '~encore.max_items'?: N};
package/config/secrets.ts CHANGED
@@ -3,14 +3,12 @@ import {lookupScope, testMode} from '../internal/testing/scope';
3
3
  import {StringLiteral} from '../internal/utils/constraints';
4
4
 
5
5
  /**
6
- * Secret represents a single secret value that is loaded
7
- * into the application. It is strongly typed for that secret,
8
- * so that you can write functions which expect a specific one.
6
+ * Represents a single secret loaded into the application. It is strongly typed
7
+ * to its secret name so functions can require a specific secret.
9
8
  *
10
- * You can use {@link AnySecret} to represent any secret without knowing
11
- * it's name.
9
+ * Use {@link AnySecret} to accept any secret regardless of its name.
12
10
  *
13
- * `T` is the type of the secret's value. Only `string` is supported.
11
+ * `T` is the secret's value type (only `string` is supported).
14
12
  *
15
13
  * @example
16
14
  *
@@ -22,13 +20,12 @@ export interface Secret<Name extends string, T extends string = string> {
22
20
  /**
23
21
  * Returns the value of the secret.
24
22
  *
25
- * The value is read once per process and does not change while it runs:
26
- * a new value reaches a service when it restarts.
23
+ * The value is loaded once per process and does not change while it is
24
+ * running; updated values take effect when the service restarts.
27
25
  *
28
- * Locally (`encore run`, `encore test`), a secret with no value reads as
29
- * `""`, like one set empty: a secret lacks a value there until it is set.
30
- * So `if (!apiKey())` skips an integration that isn't configured yet.
31
- * Anywhere else it throws an `Error` naming the secret.
26
+ * Locally (`encore run`, `encore test`), an unset secret returns `""` so
27
+ * checks like `if (!apiKey())` can skip unconfigured integrations. In all
28
+ * other environments, reading an unset secret throws an `Error` naming it.
32
29
  */
33
30
  (): T;
34
31
 
@@ -38,69 +35,66 @@ export interface Secret<Name extends string, T extends string = string> {
38
35
  readonly name: Name;
39
36
 
40
37
  /**
41
- * Marks the type as an Encore secret, for Encore's parser, which reads a
42
- * config's type to know which of its fields read a secret. Never set.
38
+ * Type brand used by Encore's parser to detect secret fields inside a config
39
+ * type. Never set at runtime.
43
40
  *
44
- * As a field of a config's type, `apiKey: Secret<"StripeKey">` reads the
45
- * secret of that name in the config's own scope: the declaring service's
46
- * own for a service config, the app's for a shared or global one. The
47
- * secret is declared as any other is, with {@link secret}.
41
+ * When used in a config interface, `apiKey: Secret<"StripeKey">` references
42
+ * the secret with that name in the config's scope (the declaring service for
43
+ * a service config, or the app for a shared or global config). The secret
44
+ * itself must still be declared with {@link secret}.
48
45
  */
49
46
  readonly '~encore.secret'?: true;
50
47
  }
51
48
 
52
49
  /**
53
- * AnySecret is a type which can be used to represent any {@link Secret}
54
- * without knowing its name.
50
+ * Represents any {@link Secret} without requiring a specific name.
55
51
  */
56
52
  export type AnySecret = Secret<string>;
57
53
 
58
54
  /**
59
- * The options a secret is declared with. Read by Encore's parser, so the
60
- * values must be literals.
55
+ * Options for declaring a secret with {@link secret}. Values must be literals
56
+ * so Encore's parser can inspect them statically.
61
57
  */
62
58
  export interface SecretOptions {
63
59
  /**
64
- * Who the secret belongs to, written as a string literal for Encore to
65
- * read:
60
+ * Controls which services own and can read the secret. Must be a string
61
+ * literal so Encore can resolve it statically:
66
62
  *
67
- * - `"service"`, the default: the service that declares it. Two services
68
- * may each declare a secret of the same name and hold different values
69
- * in them, so adding a secret to one service can never collide with, or
70
- * silently reuse, a name another service already took. The declaration
71
- * must therefore live inside a service; Encore reports an error
72
- * otherwise.
73
- * - `"shared"`: the whole app, readable from any service, each that reads
74
- * it recorded as using it. It may be declared outside a service. Use it
75
- * only for a value several services genuinely share: a shared secret
76
- * couples every service that reads it, and widening a secret's scope
77
- * later is easier than narrowing it.
78
- * - `"global"`: the whole app, with one value available to every service
79
- * without being granted. It may be declared outside a service.
63
+ * - `"service"` (default): scoped to the declaring service. Two services may
64
+ * declare secrets with the same name and hold independent values, avoiding
65
+ * accidental collisions or silent reuse. Must be declared inside a
66
+ * service; Encore reports an error otherwise.
67
+ * - `"shared"`: app-wide and readable from any service, with each reading
68
+ * service recorded as a consumer. May be declared outside a service. Use
69
+ * this only when multiple services genuinely share a value — widening a
70
+ * secret's scope later is easier than narrowing it.
71
+ * - `"global"`: app-wide with a single value available to every service
72
+ * without requiring per-service grants. May be declared outside a service.
80
73
  */
81
74
  scope?: 'service' | 'shared' | 'global';
82
75
 
83
76
  /**
84
- * What the secret is, who issues it and how to rotate it, shown wherever its
85
- * values are managed. The declaration's doc comment wins when there is one.
77
+ * Human-readable description of the secret (what it is, who issues it, and
78
+ * how to rotate it), displayed wherever secret values are managed. A doc
79
+ * comment on the declaration takes precedence if both are present.
86
80
  */
87
81
  description?: string;
88
82
 
89
83
  /**
90
- * Tags that classify the secret, for policies to read: written out as
91
- * string literals, as in `{data: "pci"}`. Keys and values are lowercase
92
- * letters, digits, `_` and `-`, at most 63 characters; a key starts with a
93
- * letter, and not with `encore`, which is reserved. At most 32 tags.
84
+ * Static string-literal tags that classify the secret for policy checks
85
+ * (e.g. `{data: "pci"}`). Keys and values may contain lowercase letters,
86
+ * digits, `_`, and `-`, up to 63 characters. Keys must start with a letter
87
+ * and cannot start with the reserved prefix `encore`. At most 32 tags.
94
88
  */
95
89
  tags?: Record<string, string>;
96
90
  }
97
91
 
98
92
  /**
99
- * secret declares a {@link Secret}, by default belonging to the service that
100
- * declares it. See {@link SecretOptions.scope} for a secret of the whole app.
93
+ * Declares a {@link Secret}, scoped by default to the declaring service. See
94
+ * {@link SecretOptions.scope} for app-wide secrets.
101
95
  *
102
- * A doc comment on the declaration, or a `description` option, is shown
103
- * wherever the secret's values are managed.
96
+ * A doc comment on the declaration (or the `description` option) is shown
97
+ * wherever secret values are managed.
104
98
  *
105
99
  * @example
106
100
  * ```ts
@@ -111,14 +105,14 @@ export interface SecretOptions {
111
105
  *
112
106
  * @example
113
107
  * ```ts
114
- * // A secret several services share.
108
+ * // A secret shared by several services.
115
109
  * import {secret} from "@encore.dev/sdk/config";
116
110
  * const signingKey = secret("SigningKey", {scope: "shared"});
117
111
  * ```
118
112
  *
119
113
  * @example
120
114
  * ```ts
121
- * // Documenting it.
115
+ * // Documenting a secret.
122
116
  * const apiKey = secret("ApiKey", {
123
117
  * description: "Issued by Acme Pay. Rotate it in their portal under Settings → API.",
124
118
  * });
@@ -135,9 +129,8 @@ export function secret<Name extends string>(
135
129
  }
136
130
 
137
131
  /**
138
- * The secret a config's secret field reads: what the runtime's placeholder
139
- * for the field stands for. A secret name has one scope across the app, so it
140
- * is read by name whatever the config's scope. Internal to the config module.
132
+ * Returns the secret handle referenced by a config's secret field placeholder.
133
+ * Internal to the config module.
141
134
  *
142
135
  * @internal
143
136
  */
package/config/value.ts CHANGED
@@ -1,12 +1,11 @@
1
1
  /**
2
- * What a config's value is built from: the JSON the runtime hands over,
3
- * turned into what the declared type says, guided by the config's schema.
4
- * Durations become {@link Duration}s, and the runtime's placeholder for each
5
- * secret field becomes the secret itself. Kept apart from the runtime, so it
6
- * can be tested without one.
2
+ * Converts raw config JSON from the runtime into typed values using the
3
+ * config's JSON Schema: duration strings become {@link Duration} instances and
4
+ * secret placeholders become secret handles. Kept separate from the runtime so
5
+ * it can be unit-tested standalone.
7
6
  */
8
7
 
9
- /** What a duration string looks like: one or more numbers, each with a unit. */
8
+ /** Matches a non-negative duration string (one or more number+unit segments). */
10
9
  const DURATION = /^([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$/;
11
10
  const UNIT_MS: Record<string, number> = {
12
11
  ns: 1e-6,
@@ -19,9 +18,10 @@ const UNIT_MS: Record<string, number> = {
19
18
  };
20
19
 
21
20
  /**
22
- * A length of time, written in config files as a number with a unit, or
23
- * several: `"30s"`, `"250ms"`, `"1h30m"` (units `ns`, `us`, `ms`, `s`, `m`,
24
- * `h`, as Go's `time.ParseDuration` reads them, without a sign).
21
+ * A non-negative length of time, written in config files as one or more
22
+ * number-and-unit pairs such as `"30s"`, `"250ms"`, or `"1h30m"`. Supported
23
+ * units are `ns`, `us`, `ms`, `s`, `m`, and `h` (matching Go's
24
+ * `time.ParseDuration`, without a leading sign).
25
25
  *
26
26
  * @example
27
27
  * interface Checkout { paymentTimeout: Duration }
@@ -29,8 +29,8 @@ const UNIT_MS: Record<string, number> = {
29
29
  */
30
30
  export class Duration {
31
31
  /**
32
- * Marks the type as Encore's duration, for Encore's parser, which reads a
33
- * config's type to know what its files may hold. Never set.
32
+ * Type brand used by Encore's parser to identify duration fields in a config
33
+ * type. Never set at runtime.
34
34
  */
35
35
  declare readonly '~encore.duration'?: true;
36
36
 
@@ -40,11 +40,11 @@ export class Duration {
40
40
  this.ms = ms;
41
41
  }
42
42
 
43
- /** Reads a duration string; throws on anything else. */
43
+ /** Parses a duration string and throws if the format is invalid. */
44
44
  static parse(text: string): Duration {
45
45
  if (!DURATION.test(text)) {
46
46
  throw new Error(
47
- `${JSON.stringify(text)} is not a duration, such as "30s", "250ms" or "1h30m"`,
47
+ `${JSON.stringify(text)} is not a valid duration (expected e.g. "30s", "250ms", or "1h30m")`,
48
48
  );
49
49
  }
50
50
  let ms = 0;
@@ -57,25 +57,25 @@ export class Duration {
57
57
  return new Duration(Math.round(ms * 1e6) / 1e6);
58
58
  }
59
59
 
60
- /** A duration of `ms` milliseconds. */
60
+ /** Creates a duration of `ms` milliseconds. */
61
61
  static ofMilliseconds(ms: number): Duration {
62
62
  if (!(ms >= 0)) {
63
- throw new Error('a duration is not negative');
63
+ throw new Error('duration must not be negative');
64
64
  }
65
65
  return new Duration(ms);
66
66
  }
67
67
 
68
- /** The duration in milliseconds: what `setTimeout` takes. */
68
+ /** Returns the duration in milliseconds (suitable for `setTimeout`). */
69
69
  get milliseconds(): number {
70
70
  return this.ms;
71
71
  }
72
72
 
73
- /** The duration in seconds. */
73
+ /** Returns the duration in seconds. */
74
74
  get seconds(): number {
75
75
  return this.ms / 1000;
76
76
  }
77
77
 
78
- /** As a config file writes it: `"1h30m"`, `"250ms"`, `"0s"`. */
78
+ /** Formats the duration in config-file syntax (e.g. `"1h30m"`, `"250ms"`, `"0s"`). */
79
79
  toString(): string {
80
80
  let ms = this.ms;
81
81
  if (ms === 0) return '0s';
@@ -104,22 +104,19 @@ export class Duration {
104
104
  }
105
105
 
106
106
  /**
107
- * The key of the runtime's placeholder for a secret field:
108
- * `{"$encore_secret": "<name>"}`. The name alone says which secret it is: a
109
- * secret's name has one scope across the app.
107
+ * Property key used in runtime secret placeholders (`{"$encore_secret": "<name>"}`).
110
108
  */
111
109
  export const PLACEHOLDER_KEY = '$encore_secret';
112
110
 
113
- /** Builds the secret a placeholder stands for, by its name. */
111
+ /** Resolves a secret placeholder's name into a secret handle. */
114
112
  export type SecretOf = (name: string) => unknown;
115
113
 
116
114
  type Schema = Record<string, any>;
117
115
 
118
116
  /**
119
- * `value`, as the declared type has it: the JSON the runtime hands over, with
120
- * each duration string `schema` marks made a {@link Duration}, and the
121
- * runtime's placeholder at each secret field `schema` marks made the secret
122
- * `secretOf` builds.
117
+ * Converts a raw JSON config `value` according to `schema`, turning duration
118
+ * strings into {@link Duration} instances and secret placeholders into handles
119
+ * via `secretOf`.
123
120
  */
124
121
  export function build(
125
122
  schema: Schema,
package/cron/mod.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  import {DurationString} from '../types/mod';
2
2
 
3
3
  /**
4
- * The header with a run's idempotency key: an id that is the same each time
5
- * Encore sends the run, as when it sends it again after a failure. A job
6
- * that must not do its work twice can check whether it has seen the key
7
- * before:
4
+ * Header containing a cron run's idempotency key. The value stays the same
5
+ * across retries of the same execution, so a job that must not run its side
6
+ * effects twice can deduplicate using this key:
8
7
  *
9
8
  * ```ts
10
9
  * app.post('/internal/cleanup', (req, res) => {
@@ -13,66 +12,66 @@ import {DurationString} from '../types/mod';
13
12
  * });
14
13
  * ```
15
14
  *
16
- * Only Encore sends it: a gateway removes it from the requests it takes from
17
- * clients. `encore cron trigger <name>` runs a job locally now, as Encore runs
18
- * it when it is due; one declared with `runIn: {local: true}` also runs on
19
- * schedule while the app has a session open.
15
+ * Only Encore sets this header; gateways strip it from external client
16
+ * requests. Run `encore cron trigger <name>` to execute a job immediately
17
+ * during local development. Jobs declared with `runIn: {local: true}` also run
18
+ * on schedule while a local session is active.
20
19
  */
21
20
  export const IDEMPOTENCY_KEY_HEADER = 'x-encore-cron-execution';
22
21
 
23
22
  /**
24
- * The header that says why a job runs: `scheduled`, when it was due, or
25
- * `manual`, when someone asked for it to run now.
23
+ * Header indicating what triggered a cron run: `"scheduled"` when the job ran
24
+ * on its schedule, or `"manual"` when triggered manually.
26
25
  */
27
26
  export const TRIGGER_HEADER = 'x-encore-cron-trigger';
28
27
 
29
28
  export class CronJob {
30
29
  readonly name: string;
31
30
  /**
32
- * What the job was declared with. Read-only: Encore schedules the job from
33
- * the declaration in the source, so a changed path would not be requested.
31
+ * The cron job configuration as declared in source code. Read-only because
32
+ * Encore reads the schedule and target path statically from the declaration.
34
33
  */
35
34
  readonly cfg: Readonly<CronJobConfig>;
36
35
  constructor(name: string, cfg: CronJobConfig) {
37
36
  this.name = name;
38
- // Frozen as well as typed `readonly`, so a write throws in an app that
39
- // runs without `tsc` checking it.
37
+ // Frozen at runtime as well as typed `readonly`, so mutations throw even
38
+ // when running without TypeScript type checking.
40
39
  this.cfg = Object.freeze({...cfg});
41
40
  Object.freeze(this);
42
41
  }
43
42
  }
44
43
 
45
44
  /**
46
- * The configuration of a cron job: the path it requests and when.
45
+ * Configuration for a cron job: the endpoint path to call and its schedule.
47
46
  *
48
- * When the job is due, Encore sends a `POST` request for `path` to the service
49
- * that declares the job. The fields must be literals: Encore reads them from
50
- * the source.
47
+ * When the job is due, Encore sends a `POST` request to `path` on the service
48
+ * that declares the job. All configuration fields must be string/boolean
49
+ * literals because Encore parses them statically from source code.
51
50
  */
52
51
  export type CronJobConfig = {
53
- /** The path the job requests of its service, starting with `/`. */
52
+ /** The endpoint path on the declaring service to `POST` to, starting with `/`. */
54
53
  path: string;
55
54
  title?: string;
56
55
  /**
57
- * Tags that classify the cron job, for policies to read: written out as
58
- * string literals, as in `{data: "pci"}`. Keys and values are lowercase
59
- * letters, digits, `_` and `-`, at most 63 characters; a key starts with a
60
- * letter, and not with `encore`, which is reserved. At most 32 tags.
56
+ * Classification tags for policies, written as string literals (for example,
57
+ * `{data: "pci"}`). Keys and values may contain lowercase letters, digits,
58
+ * `_`, and `-`, up to 63 characters. Keys must start with a letter and cannot
59
+ * start with `encore` (which is reserved). At most 32 tags are allowed.
61
60
  */
62
61
  tags?: Record<string, string>;
63
- /** The kinds of environment the job is scheduled in. */
62
+ /** Controls which environment types run this job on schedule. */
64
63
  runIn?: CronJobRunIn;
65
64
  } & ({every: DurationString} | {schedule: string});
66
65
 
67
66
  /**
68
- * The kinds of environment a cron job is scheduled in. Wherever the app is
69
- * deployed, it is; locally only when `local` says so.
67
+ * Controls which environment types run a cron job on schedule. Cron jobs always
68
+ * run in deployed environments; locally they only run when `local` is `true`.
70
69
  */
71
70
  export type CronJobRunIn = {
72
71
  /**
73
- * Whether the job is scheduled on a developer's machine too, while the
74
- * app has a session open there (as `encore run` keeps one). False unless
75
- * set; a developer can override it for their checkout either way.
72
+ * Whether the job also runs on schedule during local development (for example
73
+ * while `encore run` is active). Defaults to `false`, and developers can
74
+ * override it per local checkout.
76
75
  */
77
76
  local?: boolean;
78
77
  };