@apicrafthq/script-sdk 0.1.0-beta.5 → 0.1.0-beta.6
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 +19 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @apicrafthq/script-sdk
|
|
2
2
|
|
|
3
|
-
TypeScript types for [API Craft](https://github.com/fvarrin/api-craft)
|
|
3
|
+
TypeScript types for [API Craft](https://github.com/fvarrin/api-craft) scripts. Use it to get autocomplete and type-checking for the `ctx` argument in middleware, custom-auth handlers, and workflow script blocks.
|
|
4
4
|
|
|
5
5
|
> **Beta.** API Craft is pre-1.0. Breaking changes may happen before the stable release. Pin to a specific `beta` version if you need stability.
|
|
6
6
|
|
|
@@ -14,18 +14,22 @@ pnpm add -D @apicrafthq/script-sdk
|
|
|
14
14
|
npm i -D @apicrafthq/script-sdk
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
The package is types-only: it has no runtime code and no dependencies.
|
|
18
|
+
|
|
19
|
+
## Middleware and custom auth
|
|
20
|
+
|
|
21
|
+
Request middleware and custom-auth handlers receive a `BeforeRequestContext`. Mutate `ctx.request` in place:
|
|
18
22
|
|
|
19
23
|
```ts
|
|
20
24
|
import type { BeforeRequestContext } from '@apicrafthq/script-sdk';
|
|
21
25
|
|
|
22
26
|
export const addAuthHeader = async (ctx: BeforeRequestContext) => {
|
|
23
|
-
const token =
|
|
27
|
+
const token = ctx.var('AUTH_TOKEN');
|
|
24
28
|
ctx.request.headers['Authorization'] = `Bearer ${token}`;
|
|
25
29
|
};
|
|
26
30
|
```
|
|
27
31
|
|
|
28
|
-
Response middleware
|
|
32
|
+
Response middleware receives an `AfterResponseContext`. `ctx.request` is read-only; mutate `ctx.response`:
|
|
29
33
|
|
|
30
34
|
```ts
|
|
31
35
|
import type { AfterResponseContext } from '@apicrafthq/script-sdk';
|
|
@@ -35,16 +39,23 @@ export const logStatus = async (ctx: AfterResponseContext) => {
|
|
|
35
39
|
};
|
|
36
40
|
```
|
|
37
41
|
|
|
38
|
-
|
|
42
|
+
Every context also provides `ctx.http` (an HTTP client for auxiliary requests such as login or token refresh) and `ctx.cache` (a persistent key/value store shared across the project's request scripts).
|
|
43
|
+
|
|
44
|
+
## Workflow scripts
|
|
39
45
|
|
|
40
|
-
|
|
46
|
+
Workflow blocks have their own contexts: `AssertContext`, `ConditionContext`, `TransformContext`, and `ScriptContext`. They expose `ctx.blockResult(blockId)` to read previous block outputs and `ctx.env(apiSlug)` for the resolved environment of a bound API:
|
|
41
47
|
|
|
42
48
|
```ts
|
|
43
|
-
import {
|
|
49
|
+
import type { TransformContext } from '@apicrafthq/script-sdk';
|
|
44
50
|
|
|
45
|
-
|
|
51
|
+
export const extractUserId = async (ctx: TransformContext) => {
|
|
52
|
+
const { response } = ctx.blockResult('create-user');
|
|
53
|
+
return { userId: response.body.id };
|
|
54
|
+
};
|
|
46
55
|
```
|
|
47
56
|
|
|
57
|
+
An assert script fails by throwing; a condition script returns a boolean; a transform script returns an object whose keys become the block's outputs. Free-form `ScriptContext` blocks additionally get `ctx.sleep(milliseconds)` for polling.
|
|
58
|
+
|
|
48
59
|
## License
|
|
49
60
|
|
|
50
61
|
MIT
|