primitive-functions 0.3.0-alpha.9 → 0.3.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.
package/README.md CHANGED
@@ -15,6 +15,44 @@ builds your function's bundle with esbuild, inlines every npm dependency, and
15
15
  leaves `primitive-functions` external — so your function always runs against the SDK
16
16
  the platform is running.
17
17
 
18
- This release is a **name reservation** (issue #3179). Its runtime surface
19
- lands with the server-functions loader; calling anything here outside a
20
- Primitive sandbox throws.
18
+ The `latest` release's types leave out experimental `ctx.api` methods, which
19
+ production does not serve; the `alpha` release (`primitive-functions@alpha`)
20
+ has them.
21
+
22
+ Calling anything in the main entry outside a Primitive sandbox throws.
23
+
24
+ ## Unit-testing a handler
25
+
26
+ `primitive-functions/testing` builds a `ctx` and a `step` for a unit test that
27
+ runs a handler's logic under Node. Install the package as a dev dependency and
28
+ override only the members the handler uses:
29
+
30
+ ```ts
31
+ import { createTestContext, createTestStep } from "primitive-functions/testing";
32
+
33
+ const ctx = createTestContext({
34
+ runId: "run-1",
35
+ configVar: async () => "doc-1",
36
+ });
37
+
38
+ await archiveOrder({ orderId: "o1" }, ctx, createTestStep());
39
+ ```
40
+
41
+ The overrides are typed from `FunctionContext` and `FunctionStep`, so an
42
+ override that no longer matches its member fails the typecheck. A member the
43
+ test did not provide throws an `Error` named `TEST_MEMBER_NOT_PROVIDED`, whose
44
+ `member` is its path; `step.do` runs its body. A namespace override (`users`,
45
+ `api.groups`) is a plain object of the methods the test provides.
46
+
47
+ For an object-form handler, pass `auth` and, when the logic checks the caller
48
+ again, `reauthorize`; the result is typed as the handler's `ctx`, and with
49
+ `auth` given the `reauthorize` fake must resolve the type of `auth`. The
50
+ testing types need TypeScript 5.4 or later.
51
+
52
+ Keep the handler's logic in a module of its own that the function's entry
53
+ imports and passes to `defineFunction`, and import that module in the test.
54
+ Importing the entry runs its `defineFunction` call, which throws outside the
55
+ platform.
56
+
57
+ The module is for tests only: `primitive config push` refuses a function whose
58
+ bundle imports it.