@funcd-dev/shim 0.22.0 → 0.24.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/dist/build.d.ts +1 -3
- package/dist/build.js +9 -39
- package/dist/types.d.ts +3 -10
- package/package.json +1 -3
package/dist/build.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { type Schema } from 'ts-json-schema-generator';
|
|
2
2
|
export interface ContractBuild {
|
|
3
|
-
/** an ESM module source defining the precompiled __funcdValidate* (null ⇒ no contract). */
|
|
4
|
-
validatorSource: string | null;
|
|
5
3
|
inputSchema: Schema | null;
|
|
6
4
|
outputSchema: Schema | null;
|
|
7
5
|
}
|
|
8
|
-
/**
|
|
6
|
+
/** Build the FuncInput/FuncOutput contract of the TS module at `tsPath`; a side it does not declare is null. */
|
|
9
7
|
export declare function buildContract(tsPath: string): ContractBuild;
|
package/dist/build.js
CHANGED
|
@@ -1,47 +1,17 @@
|
|
|
1
|
-
// funcd Node contract
|
|
2
|
-
//
|
|
3
|
-
// (ts-json-schema-generator — closed records, no $ref)
|
|
4
|
-
//
|
|
5
|
-
// (`(data) => Error[]`, [] ⇒ valid) — exactly what shim.ts's resolveValidators reads.
|
|
1
|
+
// funcd Node contract build (BUILD-TIME, ADR-0058/0060). The TS analog of funcd_shim.build: for an
|
|
2
|
+
// author module declaring `FuncInput`/`FuncOutput` types it generates the JSON Schema of each side
|
|
3
|
+
// (ts-json-schema-generator — closed records, no $ref). It bakes no validator: funcd checks each call
|
|
4
|
+
// against the contract itself (funcd ADR-0303).
|
|
6
5
|
//
|
|
7
|
-
// Runs on the push box; NOT bundled into shim.mjs.
|
|
8
|
-
// erases types), so unlike Python there is no source-stripping — only the validator is injected.
|
|
6
|
+
// Runs on the push box; NOT bundled into shim.mjs.
|
|
9
7
|
//
|
|
10
8
|
// (ADR-0060 names typia; typia needs a TS *transformer* (ts-patch) that does not compose with the
|
|
11
9
|
// esbuild bundle. ts-json-schema-generator is the no-transformer equivalent — same "TS type ->
|
|
12
|
-
// JSON Schema", same MIT licence
|
|
13
|
-
import Ajv from 'ajv';
|
|
14
|
-
import standaloneCode from 'ajv/dist/standalone/index.js';
|
|
10
|
+
// JSON Schema", same MIT licence.)
|
|
15
11
|
import { createGenerator } from 'ts-json-schema-generator';
|
|
16
|
-
/**
|
|
12
|
+
/** Build the FuncInput/FuncOutput contract of the TS module at `tsPath`; a side it does not declare is null. */
|
|
17
13
|
export function buildContract(tsPath) {
|
|
18
|
-
|
|
19
|
-
const outputSchema = schemaFor(tsPath, 'FuncOutput');
|
|
20
|
-
if (!inputSchema && !outputSchema) {
|
|
21
|
-
return { validatorSource: null, inputSchema: null, outputSchema: null };
|
|
22
|
-
}
|
|
23
|
-
// Register each schema under an $id, then standalone-code by id (the multi-export API takes
|
|
24
|
-
// schema ids, not compiled fns). standaloneCode emits `export const _funcdInput = …` — eval-free
|
|
25
|
-
// (compiled here, at build).
|
|
26
|
-
// strictSchema:false ⇒ AJV ignores the `discriminator` keyword we add for the gate (it validates
|
|
27
|
-
// a tagged union via `oneOf` alone; `discriminator` is metadata the Go profile gate requires).
|
|
28
|
-
const ajv = new Ajv({ code: { source: true, esm: true }, allErrors: true, strictSchema: false });
|
|
29
|
-
const refs = {};
|
|
30
|
-
if (inputSchema) {
|
|
31
|
-
ajv.addSchema({ ...stripMeta(inputSchema), $id: 'funcdInput' });
|
|
32
|
-
refs._funcdInput = 'funcdInput';
|
|
33
|
-
}
|
|
34
|
-
if (outputSchema) {
|
|
35
|
-
ajv.addSchema({ ...stripMeta(outputSchema), $id: 'funcdOutput' });
|
|
36
|
-
refs._funcdOutput = 'funcdOutput';
|
|
37
|
-
}
|
|
38
|
-
const base = standaloneCode(ajv, refs);
|
|
39
|
-
const wrap = (name, fn) => `export function ${name}(d) { return ${fn}(d) ? [] : (${fn}.errors ?? []); }\n`;
|
|
40
|
-
const validatorSource = base +
|
|
41
|
-
'\n' +
|
|
42
|
-
(inputSchema ? wrap('__funcdValidateInput', '_funcdInput') : '') +
|
|
43
|
-
(outputSchema ? wrap('__funcdValidateOutput', '_funcdOutput') : '');
|
|
44
|
-
return { validatorSource, inputSchema, outputSchema };
|
|
14
|
+
return { inputSchema: schemaFor(tsPath, 'FuncInput'), outputSchema: schemaFor(tsPath, 'FuncOutput') };
|
|
45
15
|
}
|
|
46
16
|
/** Generate the JSON Schema for `type` from `tsPath`, inlined (no $ref) + closed records — the
|
|
47
17
|
* funcd profile shape; returns null when the type is not declared. */
|
|
@@ -113,7 +83,7 @@ function discriminatorTag(branches) {
|
|
|
113
83
|
}
|
|
114
84
|
return null;
|
|
115
85
|
}
|
|
116
|
-
/** Drop JSON-Schema meta keys
|
|
86
|
+
/** Drop JSON-Schema meta keys the gate doesn't need (the contract is the type shape). */
|
|
117
87
|
function stripMeta(schema) {
|
|
118
88
|
const { $schema, $ref, definitions, ...rest } = schema;
|
|
119
89
|
void $schema;
|
package/dist/types.d.ts
CHANGED
|
@@ -27,9 +27,9 @@ export interface CloudEvent<T = unknown> {
|
|
|
27
27
|
export interface FunctionContext {
|
|
28
28
|
/** Structured log line → stdout (collected by the platform, ADR-0010). */
|
|
29
29
|
log(...args: unknown[]): void;
|
|
30
|
-
/** Synchronously invoke a linked function by its spec.bindings.links alias (ADR-0064).
|
|
31
|
-
*
|
|
32
|
-
*
|
|
30
|
+
/** Synchronously invoke a linked function by its spec.bindings.links alias (ADR-0064). funcd checks the
|
|
31
|
+
* input against the target's contract (funcd ADR-0303); its result is returned. Fails closed (rejects) if
|
|
32
|
+
* the caller declares no such link.
|
|
33
33
|
*
|
|
34
34
|
* The target receives `input` as its `event.data`, except that an object with a top-level `data` or
|
|
35
35
|
* `specversion` key is taken as a full CloudEvent envelope (ADR-0134): the target receives only its
|
|
@@ -81,10 +81,3 @@ export declare function http(app: HttpApp): HttpHandler;
|
|
|
81
81
|
* generated contract is the empty schema `{}` (accepts any JSON). Typed as `unknown` so the
|
|
82
82
|
* handler must narrow before use — deliberately NOT the unsafe `any`. */
|
|
83
83
|
export type Json = unknown;
|
|
84
|
-
/** A single error from a precompiled validator. Shape kept loose (AJV vs pydantic differ); the
|
|
85
|
-
* shim only inspects the array length and echoes the errors as 422/500 details. */
|
|
86
|
-
export type ValidationError = unknown;
|
|
87
|
-
/** A precompiled, eval-free validator the push build inlines into the bundle from the author's
|
|
88
|
-
* FuncInput/FuncOutput type (ADR-0058). Returns [] when `data` is valid. The shim runs it; it
|
|
89
|
-
* never compiles a schema at runtime. */
|
|
90
|
-
export type Validator = (data: unknown) => ValidationError[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@funcd-dev/shim",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Types and the contract build for writing funcd functions in TypeScript",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -38,8 +38,6 @@
|
|
|
38
38
|
"build:lib": "tsc -p tsconfig.lib.json"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"ajv": "^8.20.0",
|
|
42
|
-
"ajv-formats": "^3.0.1",
|
|
43
41
|
"ts-json-schema-generator": "^2.9.0"
|
|
44
42
|
},
|
|
45
43
|
"devDependencies": {
|