@apicrafthq/script-sdk 0.1.0-beta.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Florian Varrin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # @apicrafthq/script-sdk
2
+
3
+ TypeScript types for [API Craft](https://github.com/fvarrin/api-craft) middleware and custom-auth scripts. Use it to get autocomplete and type-checking for the `ctx` argument in your `before` / `after` request hooks and custom auth handlers.
4
+
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
+
7
+ ## Install
8
+
9
+ ```sh
10
+ bun add -D @apicrafthq/script-sdk
11
+ # or
12
+ pnpm add -D @apicrafthq/script-sdk
13
+ # or
14
+ npm i -D @apicrafthq/script-sdk
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import type { BeforeRequestContext } from '@apicrafthq/script-sdk';
21
+
22
+ export const addAuthHeader = async (ctx: BeforeRequestContext) => {
23
+ const token = await ctx.var('AUTH_TOKEN');
24
+ ctx.request.headers['Authorization'] = `Bearer ${token}`;
25
+ };
26
+ ```
27
+
28
+ Response middleware:
29
+
30
+ ```ts
31
+ import type { AfterResponseContext } from '@apicrafthq/script-sdk';
32
+
33
+ export const logStatus = async (ctx: AfterResponseContext) => {
34
+ ctx.logger.info(`Response status: ${ctx.response.status}`);
35
+ };
36
+ ```
37
+
38
+ ## `expect` for assertions
39
+
40
+ The `expect` helper is available via a separate subpath so the main entry stays dependency-free:
41
+
42
+ ```ts
43
+ import { expect } from '@apicrafthq/script-sdk/testing';
44
+
45
+ expect(ctx.response.status).toBe(200);
46
+ ```
47
+
48
+ ## License
49
+
50
+ MIT
@@ -0,0 +1,115 @@
1
+ /** HTTP client for auxiliary requests (login, remote signing, polling). */
2
+ interface HttpClient {
3
+ get(url: string, init?: HttpRequestInit): Promise<HttpResponse>;
4
+ post(url: string, init?: HttpRequestInit): Promise<HttpResponse>;
5
+ put(url: string, init?: HttpRequestInit): Promise<HttpResponse>;
6
+ patch(url: string, init?: HttpRequestInit): Promise<HttpResponse>;
7
+ delete(url: string, init?: HttpRequestInit): Promise<HttpResponse>;
8
+ }
9
+ interface HttpRequestInit {
10
+ headers?: Record<string, string>;
11
+ /** Serialized according to the Content-Type. */
12
+ body?: unknown;
13
+ query?: Record<string, string>;
14
+ }
15
+ interface HttpResponse {
16
+ status: number;
17
+ statusText: string;
18
+ headers: Record<string, string>;
19
+ /** Parsed JSON when possible, otherwise a string. */
20
+ body: unknown;
21
+ }
22
+ /** Persistent key/value store across executions. */
23
+ interface ScriptCache {
24
+ get<T = unknown>(key: string): Promise<T | undefined>;
25
+ set<T = unknown>(key: string, value: T): Promise<void>;
26
+ delete(key: string): Promise<void>;
27
+ }
28
+ /** Logger for scripts. Use this instead of `console`, which is not allowed. */
29
+ interface ScriptLogger {
30
+ debug(message: string, ...args: unknown[]): void;
31
+ info(message: string, ...args: unknown[]): void;
32
+ warn(message: string, ...args: unknown[]): void;
33
+ error(message: string, ...args: unknown[]): void;
34
+ }
35
+
36
+ interface EnvSchema {
37
+ }
38
+ type EnvVarName = keyof EnvSchema extends never ? string : keyof EnvSchema | (string & {});
39
+ type EnvVarReturn<K> = K extends keyof EnvSchema ? EnvSchema[K] : string | undefined;
40
+ /** Primitives available to every script. */
41
+ interface BaseContext {
42
+ /** Reads a variable from the environment. */
43
+ var<K extends EnvVarName>(name: K): EnvVarReturn<K>;
44
+ /** HTTP client for auxiliary requests (login, remote signing, polling). */
45
+ http: HttpClient;
46
+ /** Logger. Use instead of `console`, which is not allowed. */
47
+ logger: ScriptLogger;
48
+ }
49
+
50
+ /** Shared base for auth and middleware scripts. */
51
+ interface RequestScopedContext extends BaseContext {
52
+ /** Persistent key/value store, shared across the project's request scripts. */
53
+ cache: ScriptCache;
54
+ }
55
+ /** Context for a before-middleware. Mutate `request` in place. */
56
+ interface BeforeRequestContext extends RequestScopedContext {
57
+ request: MutableResolvedRequest;
58
+ }
59
+ /** Context for an after-middleware. `request` is read-only; mutate `response`. */
60
+ interface AfterResponseContext extends RequestScopedContext {
61
+ readonly request: Readonly<ResolvedRequest>;
62
+ response: HttpResponse;
63
+ }
64
+ interface MutableResolvedRequest {
65
+ method: string;
66
+ url: string;
67
+ headers: Record<string, string>;
68
+ query: Record<string, string>;
69
+ body?: unknown;
70
+ }
71
+ interface ResolvedRequest {
72
+ method: string;
73
+ url: string;
74
+ headers: Record<string, string>;
75
+ query: Record<string, string>;
76
+ body?: unknown;
77
+ }
78
+
79
+ /** Shared base for workflow scripts (assert, condition, script). */
80
+ interface WorkflowScopedContext extends BaseContext {
81
+ /** Reads the result of a previous block by its id. */
82
+ blockResult(blockId: string): BlockResult;
83
+ /** Writes a variable available to later blocks. */
84
+ setVar(name: string, value: unknown): void;
85
+ /** Runs an existing request instance and returns its response. */
86
+ executeRequest(resource: string, instance: string, overrides?: RequestOverrides): Promise<HttpResponse>;
87
+ /** Non-blocking pause, e.g. for polling. */
88
+ sleep(milliseconds: number): Promise<void>;
89
+ }
90
+ interface BlockResult {
91
+ /** Present for `request` blocks. */
92
+ response?: HttpResponse;
93
+ [key: string]: unknown;
94
+ }
95
+ interface RequestOverrides {
96
+ params?: {
97
+ path?: Record<string, string>;
98
+ query?: Record<string, string>;
99
+ header?: Record<string, string>;
100
+ };
101
+ body?: {
102
+ data?: unknown;
103
+ };
104
+ }
105
+ /** Context for an assert script. Throw to fail the assertion. */
106
+ interface AssertContext extends WorkflowScopedContext {
107
+ }
108
+ /** Context for a condition script. Return a boolean to pick then/else. */
109
+ interface ConditionContext extends WorkflowScopedContext {
110
+ }
111
+ /** Context for a free-form script block. */
112
+ interface ScriptContext extends WorkflowScopedContext {
113
+ }
114
+
115
+ export type { AfterResponseContext, AssertContext, BaseContext, BeforeRequestContext, BlockResult, ConditionContext, EnvSchema, HttpClient, HttpRequestInit, HttpResponse, MutableResolvedRequest, RequestOverrides, RequestScopedContext, ResolvedRequest, ScriptCache, ScriptContext, ScriptLogger, WorkflowScopedContext };
package/dist/index.js ADDED
File without changes
@@ -0,0 +1,5 @@
1
+ import { ExpectStatic } from '@vitest/expect';
2
+
3
+ declare const expect: ExpectStatic;
4
+
5
+ export { expect };
@@ -0,0 +1,10 @@
1
+ // src/expect.ts
2
+ import * as chai from "chai";
3
+ import { JestExtend, JestChaiExpect, JestAsymmetricMatchers } from "@vitest/expect";
4
+ chai.use(JestExtend);
5
+ chai.use(JestChaiExpect);
6
+ chai.use(JestAsymmetricMatchers);
7
+ var expect2 = chai.expect;
8
+ export {
9
+ expect2 as expect
10
+ };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@apicrafthq/script-sdk",
3
+ "version": "0.1.0-beta.0",
4
+ "type": "module",
5
+ "description": "TypeScript types for API Craft middleware and custom-auth scripts.",
6
+ "license": "MIT",
7
+ "author": "Florian Varrin",
8
+ "homepage": "https://github.com/fvarrin/api-craft#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/fvarrin/api-craft.git",
12
+ "directory": "packages/script-sdk"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/fvarrin/api-craft/issues"
16
+ },
17
+ "keywords": [
18
+ "api-craft",
19
+ "sdk",
20
+ "middleware",
21
+ "types",
22
+ "http",
23
+ "typescript"
24
+ ],
25
+ "sideEffects": false,
26
+ "files": [
27
+ "dist",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "publishConfig": {
32
+ "access": "public",
33
+ "tag": "beta"
34
+ },
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.ts",
38
+ "import": "./dist/index.js"
39
+ },
40
+ "./testing": {
41
+ "types": "./dist/testing.d.ts",
42
+ "import": "./dist/testing.js"
43
+ }
44
+ },
45
+ "devDependencies": {
46
+ "@types/chai": "^5.2.0",
47
+ "tsup": "^8",
48
+ "typescript": "^5"
49
+ },
50
+ "dependencies": {
51
+ "@vitest/expect": "^3.2.0",
52
+ "chai": "^5.1.2"
53
+ },
54
+ "scripts": {
55
+ "build": "tsup src/index.ts src/testing.ts --format esm --dts --clean",
56
+ "dev": "tsup src/index.ts src/testing.ts --format esm --dts --watch"
57
+ }
58
+ }