@apifuse/provider-sdk 2.1.0-beta.1 → 2.1.0-beta.10

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 (212) hide show
  1. package/AUTHORING.md +208 -2
  2. package/CHANGELOG.md +47 -0
  3. package/README.md +114 -10
  4. package/SUBMISSION.md +87 -0
  5. package/bin/apifuse-check.ts +86 -4
  6. package/bin/apifuse-dev.ts +87 -13
  7. package/bin/apifuse-pack-check.ts +80 -0
  8. package/bin/apifuse-pack-smoke.ts +303 -2
  9. package/bin/apifuse-perf.ts +142 -49
  10. package/bin/apifuse-record.ts +182 -104
  11. package/bin/apifuse-submit-check.ts +2538 -0
  12. package/bin/apifuse.ts +1 -1
  13. package/dist/ceremonies/index.d.ts +41 -0
  14. package/dist/ceremonies/index.js +490 -0
  15. package/dist/choice-token.d.ts +24 -0
  16. package/dist/choice-token.js +74 -0
  17. package/dist/cli/commands.d.ts +10 -0
  18. package/dist/cli/commands.js +80 -0
  19. package/dist/cli/create.d.ts +47 -0
  20. package/dist/cli/create.js +762 -0
  21. package/dist/cli/templates/provider/.dockerignore.tpl +22 -0
  22. package/dist/cli/templates/provider/.gitignore.tpl +22 -0
  23. package/dist/cli/templates/provider/Dockerfile.tpl +7 -0
  24. package/dist/cli/templates/provider/README.md.tpl +160 -0
  25. package/dist/cli/templates/provider/dev.ts.tpl +5 -0
  26. package/dist/cli/templates/provider/domain/README.md.tpl +3 -0
  27. package/dist/cli/templates/provider/index.test.ts.tpl +13 -0
  28. package/dist/cli/templates/provider/index.ts.tpl +15 -0
  29. package/dist/cli/templates/provider/mappers/README.md.tpl +3 -0
  30. package/dist/cli/templates/provider/meta.ts.tpl +7 -0
  31. package/dist/cli/templates/provider/operations/index.ts.tpl +5 -0
  32. package/dist/cli/templates/provider/operations/ping.ts.tpl +24 -0
  33. package/dist/cli/templates/provider/schemas/ping.ts.tpl +24 -0
  34. package/dist/cli/templates/provider/start.ts.tpl +5 -0
  35. package/dist/cli/templates/provider/upstream/README.md.tpl +3 -0
  36. package/dist/config/loader.d.ts +107 -0
  37. package/dist/config/loader.js +935 -0
  38. package/dist/contract-json.d.ts +9 -0
  39. package/dist/contract-json.js +51 -0
  40. package/dist/contract-serialization.d.ts +4 -0
  41. package/dist/contract-serialization.js +78 -0
  42. package/dist/contract-types.d.ts +49 -0
  43. package/dist/contract-types.js +1 -0
  44. package/dist/contract.d.ts +6 -0
  45. package/dist/contract.js +155 -0
  46. package/dist/define.d.ts +97 -0
  47. package/dist/define.js +1320 -0
  48. package/dist/dev.d.ts +9 -0
  49. package/dist/dev.js +15 -0
  50. package/dist/errors.d.ts +59 -0
  51. package/dist/errors.js +97 -0
  52. package/dist/i18n/catalog.d.ts +29 -0
  53. package/dist/i18n/catalog.js +159 -0
  54. package/dist/i18n/index.d.ts +2 -0
  55. package/dist/i18n/index.js +2 -0
  56. package/dist/i18n/keys.d.ts +10 -0
  57. package/dist/i18n/keys.js +34 -0
  58. package/dist/index.d.ts +41 -0
  59. package/dist/index.js +37 -0
  60. package/dist/lint.d.ts +73 -0
  61. package/dist/lint.js +702 -0
  62. package/dist/observability.d.ts +5 -0
  63. package/dist/observability.js +39 -0
  64. package/dist/provider.d.ts +9 -0
  65. package/dist/provider.js +8 -0
  66. package/dist/public-schema-field-lint.d.ts +2 -0
  67. package/dist/public-schema-field-lint.js +158 -0
  68. package/dist/recipes/gov-api.d.ts +19 -0
  69. package/dist/recipes/gov-api.js +72 -0
  70. package/dist/recipes/rest-api.d.ts +21 -0
  71. package/dist/recipes/rest-api.js +115 -0
  72. package/dist/runtime/auth-flow.d.ts +14 -0
  73. package/dist/runtime/auth-flow.js +44 -0
  74. package/dist/runtime/browser.d.ts +25 -0
  75. package/dist/runtime/browser.js +1034 -0
  76. package/dist/runtime/cache.d.ts +10 -0
  77. package/dist/runtime/cache.js +372 -0
  78. package/dist/runtime/choice.d.ts +15 -0
  79. package/dist/runtime/choice.js +435 -0
  80. package/dist/runtime/credential.d.ts +8 -0
  81. package/dist/runtime/credential.js +61 -0
  82. package/dist/runtime/env.d.ts +2 -0
  83. package/dist/runtime/env.js +10 -0
  84. package/dist/runtime/executor.d.ts +16 -0
  85. package/dist/runtime/executor.js +51 -0
  86. package/dist/runtime/http.d.ts +8 -0
  87. package/dist/runtime/http.js +706 -0
  88. package/dist/runtime/insights.d.ts +9 -0
  89. package/dist/runtime/insights.js +324 -0
  90. package/dist/runtime/instrumentation.d.ts +8 -0
  91. package/dist/runtime/instrumentation.js +269 -0
  92. package/dist/runtime/key-derivation.d.ts +24 -0
  93. package/dist/runtime/key-derivation.js +73 -0
  94. package/dist/runtime/keyring.d.ts +25 -0
  95. package/dist/runtime/keyring.js +93 -0
  96. package/dist/runtime/namespace.d.ts +9 -0
  97. package/dist/runtime/namespace.js +19 -0
  98. package/dist/runtime/otlp.d.ts +39 -0
  99. package/dist/runtime/otlp.js +103 -0
  100. package/dist/runtime/perf.d.ts +12 -0
  101. package/dist/runtime/perf.js +52 -0
  102. package/dist/runtime/prevalidate.d.ts +12 -0
  103. package/dist/runtime/prevalidate.js +173 -0
  104. package/dist/runtime/provider.d.ts +2 -0
  105. package/dist/runtime/provider.js +11 -0
  106. package/dist/runtime/proxy-errors.d.ts +21 -0
  107. package/dist/runtime/proxy-errors.js +83 -0
  108. package/dist/runtime/proxy-telemetry.d.ts +8 -0
  109. package/dist/runtime/proxy-telemetry.js +174 -0
  110. package/dist/runtime/redis.d.ts +17 -0
  111. package/dist/runtime/redis.js +82 -0
  112. package/dist/runtime/request-options.d.ts +3 -0
  113. package/dist/runtime/request-options.js +42 -0
  114. package/dist/runtime/state.d.ts +17 -0
  115. package/dist/runtime/state.js +344 -0
  116. package/dist/runtime/stealth.d.ts +18 -0
  117. package/dist/runtime/stealth.js +834 -0
  118. package/dist/runtime/stt.d.ts +22 -0
  119. package/dist/runtime/stt.js +480 -0
  120. package/dist/runtime/trace.d.ts +26 -0
  121. package/dist/runtime/trace.js +142 -0
  122. package/dist/runtime/waterfall.d.ts +12 -0
  123. package/dist/runtime/waterfall.js +147 -0
  124. package/dist/schema.d.ts +74 -0
  125. package/dist/schema.js +243 -0
  126. package/dist/serve.d.ts +1 -0
  127. package/dist/serve.js +1 -0
  128. package/dist/server/index.d.ts +3 -0
  129. package/dist/server/index.js +2 -0
  130. package/dist/server/serve.d.ts +64 -0
  131. package/dist/server/serve.js +1110 -0
  132. package/dist/server/types.d.ts +136 -0
  133. package/dist/server/types.js +86 -0
  134. package/dist/stealth/profiles.d.ts +4 -0
  135. package/dist/stealth/profiles.js +259 -0
  136. package/dist/stream.d.ts +44 -0
  137. package/dist/stream.js +151 -0
  138. package/dist/testing/helpers.d.ts +23 -0
  139. package/dist/testing/helpers.js +95 -0
  140. package/dist/testing/index.d.ts +2 -0
  141. package/dist/testing/index.js +2 -0
  142. package/dist/testing/run.d.ts +34 -0
  143. package/dist/testing/run.js +303 -0
  144. package/dist/types.d.ts +1326 -0
  145. package/dist/types.js +61 -0
  146. package/dist/utils/date.d.ts +6 -0
  147. package/dist/utils/date.js +101 -0
  148. package/dist/utils/parse.d.ts +16 -0
  149. package/dist/utils/parse.js +51 -0
  150. package/dist/utils/text.d.ts +4 -0
  151. package/dist/utils/text.js +14 -0
  152. package/dist/utils/transform.d.ts +8 -0
  153. package/dist/utils/transform.js +48 -0
  154. package/package.json +57 -30
  155. package/src/ceremonies/index.ts +8 -2
  156. package/src/choice-token.ts +165 -0
  157. package/src/cli/commands.ts +34 -11
  158. package/src/cli/create.ts +214 -52
  159. package/src/cli/templates/provider/.dockerignore.tpl +22 -0
  160. package/src/cli/templates/provider/.gitignore.tpl +22 -0
  161. package/src/cli/templates/provider/README.md.tpl +120 -1
  162. package/src/cli/templates/provider/dev.ts.tpl +1 -1
  163. package/src/cli/templates/provider/domain/README.md.tpl +3 -0
  164. package/src/cli/templates/provider/index.ts.tpl +5 -48
  165. package/src/cli/templates/provider/mappers/README.md.tpl +3 -0
  166. package/src/cli/templates/provider/meta.ts.tpl +7 -0
  167. package/src/cli/templates/provider/operations/index.ts.tpl +5 -0
  168. package/src/cli/templates/provider/operations/ping.ts.tpl +24 -0
  169. package/src/cli/templates/provider/schemas/ping.ts.tpl +24 -0
  170. package/src/cli/templates/provider/start.ts.tpl +1 -1
  171. package/src/cli/templates/provider/upstream/README.md.tpl +3 -0
  172. package/src/config/loader.ts +1224 -9
  173. package/src/contract-json.ts +75 -0
  174. package/src/contract-serialization.ts +89 -0
  175. package/src/contract-types.ts +52 -0
  176. package/src/contract.ts +215 -0
  177. package/src/define.ts +1688 -48
  178. package/src/errors.ts +27 -0
  179. package/src/i18n/catalog.ts +277 -0
  180. package/src/i18n/index.ts +2 -0
  181. package/src/i18n/keys.ts +64 -0
  182. package/src/index.ts +174 -9
  183. package/src/lint.ts +547 -73
  184. package/src/observability.ts +41 -0
  185. package/src/provider.ts +104 -4
  186. package/src/public-schema-field-lint.ts +237 -0
  187. package/src/runtime/auth-flow.ts +7 -0
  188. package/src/runtime/browser.ts +762 -51
  189. package/src/runtime/cache.ts +528 -0
  190. package/src/runtime/choice.ts +760 -0
  191. package/src/runtime/executor.ts +32 -3
  192. package/src/runtime/http.ts +939 -195
  193. package/src/runtime/insights.ts +11 -11
  194. package/src/runtime/instrumentation.ts +12 -4
  195. package/src/runtime/key-derivation.ts +1 -1
  196. package/src/runtime/keyring.ts +4 -3
  197. package/src/runtime/proxy-errors.ts +132 -0
  198. package/src/runtime/proxy-telemetry.ts +253 -0
  199. package/src/runtime/redis.ts +116 -0
  200. package/src/runtime/request-options.ts +66 -0
  201. package/src/runtime/state.ts +563 -0
  202. package/src/runtime/stealth.ts +1159 -0
  203. package/src/runtime/stt.ts +629 -0
  204. package/src/runtime/trace.ts +1 -1
  205. package/src/schema.ts +363 -1
  206. package/src/server/serve.ts +1157 -75
  207. package/src/server/types.ts +37 -0
  208. package/src/stream.ts +210 -0
  209. package/src/testing/run.ts +31 -5
  210. package/src/types.ts +1107 -59
  211. package/src/runtime/tls.ts +0 -434
  212. package/src/types/playwright-stealth.d.ts +0 -9
package/dist/types.js ADDED
@@ -0,0 +1,61 @@
1
+ export const OPERATION_TIMEOUT_MS_MIN = 1;
2
+ export const OPERATION_TIMEOUT_MS_MAX = 60_000;
3
+ export const STREAM_HEARTBEAT_MS_MIN = 1_000;
4
+ export const STREAM_HEARTBEAT_MS_MAX = 60_000;
5
+ export const STREAM_IDLE_TIMEOUT_MS_MIN = 1_000;
6
+ export const STREAM_IDLE_TIMEOUT_MS_MAX = 300_000;
7
+ export const STREAM_MAX_DURATION_MS_MIN = 1_000;
8
+ export const STREAM_MAX_DURATION_MS_MAX = 1_800_000;
9
+ export const STREAM_CHUNK_BYTES_MIN = 1;
10
+ export const STREAM_CHUNK_BYTES_MAX = 1_048_576;
11
+ export const DEFAULT_OPERATION_TRANSPORT = {
12
+ kind: "json",
13
+ };
14
+ /**
15
+ * Common probe interval examples retained for discoverability/backwards
16
+ * compatibility. This list is not exhaustive; any positive `ms`-style duration
17
+ * string accepted by `@types/ms` (for example `2m`, `8h`, or `1 day`) is valid.
18
+ */
19
+ export const PROBE_INTERVALS = [
20
+ "30s",
21
+ "1m",
22
+ "3m",
23
+ "5m",
24
+ "15m",
25
+ "30m",
26
+ "1h",
27
+ "2h",
28
+ "8h",
29
+ "24h",
30
+ ];
31
+ export const HEALTH_CHECK_TIMEOUT_MS_MIN = 1;
32
+ export const HEALTH_CHECK_TIMEOUT_MS_MAX = 60_000;
33
+ export const HEALTH_CHECK_DEGRADED_THRESHOLD_MS_MIN = 1;
34
+ export const HEALTH_CHECK_DEGRADED_THRESHOLD_MS_MAX = 60_000;
35
+ export const HttpRetryPreset = {
36
+ Off: "off",
37
+ TransportTransient: "transport_transient",
38
+ SafeRead: "safe_read",
39
+ AggressiveRead: "aggressive_read",
40
+ RateLimitAware: "rate_limit_aware",
41
+ };
42
+ export const HttpRetryJitter = {
43
+ None: "none",
44
+ Full: "full",
45
+ Equal: "equal",
46
+ };
47
+ export const HttpRetryDelayStrategy = {
48
+ Fixed: "fixed",
49
+ Exponential: "exponential",
50
+ };
51
+ export const HttpRetryAfterPolicy = {
52
+ Ignore: "ignore",
53
+ /** Honor Retry-After up to maxDelayMs. */
54
+ Respect: "respect",
55
+ /** Honor Retry-After but also cap it to the SDK-computed backoff delay. */
56
+ Cap: "cap",
57
+ };
58
+ export const HttpRetryUnsafeMethodPolicy = {
59
+ Reject: "reject",
60
+ AllowExplicitUnsafe: "allow_explicit_unsafe",
61
+ };
@@ -0,0 +1,6 @@
1
+ /** Convert various date formats to ISO 8601 string */
2
+ export declare function toISODate(v: unknown, timezone: string): string;
3
+ /** Get today's date as ISO 8601 string in given timezone */
4
+ export declare function today(timezone: string): string;
5
+ /** Get current hour as string in given timezone */
6
+ export declare function currentHour(timezone: string): string;
@@ -0,0 +1,101 @@
1
+ function getDateParts(date, timezone) {
2
+ const formatter = new Intl.DateTimeFormat("en-CA", {
3
+ timeZone: timezone,
4
+ hour12: false,
5
+ year: "numeric",
6
+ month: "2-digit",
7
+ day: "2-digit",
8
+ hour: "2-digit",
9
+ minute: "2-digit",
10
+ second: "2-digit",
11
+ });
12
+ const parts = Object.fromEntries(formatter.formatToParts(date).map((part) => [part.type, part.value]));
13
+ return {
14
+ year: parts.year,
15
+ month: parts.month,
16
+ day: parts.day,
17
+ hour: parts.hour,
18
+ minute: parts.minute,
19
+ second: parts.second,
20
+ };
21
+ }
22
+ function getTimezoneOffsetMinutes(date, timezone) {
23
+ const parts = getDateParts(date, timezone);
24
+ const asUTC = Date.UTC(Number(parts.year), Number(parts.month) - 1, Number(parts.day), Number(parts.hour), Number(parts.minute), Number(parts.second));
25
+ return (asUTC - date.getTime()) / 60000;
26
+ }
27
+ function buildUtcInstantFromLocalParts(year, month, day, hour, minute, second, timezone) {
28
+ const guess = Date.UTC(year, month - 1, day, hour, minute, second);
29
+ let utc = guess;
30
+ for (let i = 0; i < 2; i += 1) {
31
+ const offset = getTimezoneOffsetMinutes(new Date(utc), timezone);
32
+ const nextUtc = guess - offset * 60_000;
33
+ if (nextUtc === utc) {
34
+ break;
35
+ }
36
+ utc = nextUtc;
37
+ }
38
+ return new Date(utc);
39
+ }
40
+ function formatOffset(offsetMinutes) {
41
+ const sign = offsetMinutes >= 0 ? "+" : "-";
42
+ const abs = Math.abs(offsetMinutes);
43
+ const hours = String(Math.floor(abs / 60)).padStart(2, "0");
44
+ const minutes = String(abs % 60).padStart(2, "0");
45
+ return `${sign}${hours}:${minutes}`;
46
+ }
47
+ function formatIso(date, timezone) {
48
+ const parts = getDateParts(date, timezone);
49
+ const offset = getTimezoneOffsetMinutes(date, timezone);
50
+ return `${parts.year}-${parts.month}-${parts.day}T${parts.hour}:${parts.minute}:${parts.second}${formatOffset(offset)}`;
51
+ }
52
+ function parseDateValue(v, timezone) {
53
+ if (v instanceof Date) {
54
+ return Number.isNaN(v.getTime()) ? null : v;
55
+ }
56
+ if (typeof v === "number") {
57
+ const date = new Date(v);
58
+ return Number.isNaN(date.getTime()) ? null : date;
59
+ }
60
+ if (typeof v !== "string") {
61
+ return null;
62
+ }
63
+ const value = v.trim();
64
+ if (value === "") {
65
+ return null;
66
+ }
67
+ const compactDate = /^(\d{4})(\d{2})(\d{2})$/;
68
+ const compactDateTime = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})?$/;
69
+ const dashedDate = /^(\d{4})-(\d{2})-(\d{2})$/;
70
+ let match = value.match(compactDateTime);
71
+ if (match) {
72
+ const [, year, month, day, hour, minute, second = "00"] = match;
73
+ return buildUtcInstantFromLocalParts(Number(year), Number(month), Number(day), Number(hour), Number(minute), Number(second), timezone);
74
+ }
75
+ match = value.match(compactDate);
76
+ if (match) {
77
+ const [, year, month, day] = match;
78
+ return buildUtcInstantFromLocalParts(Number(year), Number(month), Number(day), 0, 0, 0, timezone);
79
+ }
80
+ match = value.match(dashedDate);
81
+ if (match) {
82
+ const [, year, month, day] = match;
83
+ return buildUtcInstantFromLocalParts(Number(year), Number(month), Number(day), 0, 0, 0, timezone);
84
+ }
85
+ const parsed = new Date(value);
86
+ return Number.isNaN(parsed.getTime()) ? null : parsed;
87
+ }
88
+ /** Convert various date formats to ISO 8601 string */
89
+ export function toISODate(v, timezone) {
90
+ const date = parseDateValue(v, timezone);
91
+ return date ? formatIso(date, timezone) : "";
92
+ }
93
+ /** Get today's date as ISO 8601 string in given timezone */
94
+ export function today(timezone) {
95
+ const parts = getDateParts(new Date(), timezone);
96
+ return `${parts.year}-${parts.month}-${parts.day}`;
97
+ }
98
+ /** Get current hour as string in given timezone */
99
+ export function currentHour(timezone) {
100
+ return getDateParts(new Date(), timezone).hour;
101
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Unwrap nested envelope by dot-path
3
+ * e.g., unwrapEnvelope({response: {body: {items: [...]}}}, 'response.body.items')
4
+ */
5
+ export declare function unwrapEnvelope(data: unknown, path?: string): unknown;
6
+ /**
7
+ * Pivot array by field — generalized version of pivotByCategory
8
+ * e.g., pivotByField([{type:'A', val:1},{type:'B', val:2}], 'type', 'val')
9
+ * → { A: 1, B: 2 }
10
+ */
11
+ export declare function pivotByField(items: unknown[], keyField: string, valueField: string): Record<string, unknown>;
12
+ /**
13
+ * Parse XML items from string — returns array
14
+ * Uses simple regex (no DOM), suitable for structured XML
15
+ */
16
+ export declare function parseXmlItems(xml: string, tag: string): string[];
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Unwrap nested envelope by dot-path
3
+ * e.g., unwrapEnvelope({response: {body: {items: [...]}}}, 'response.body.items')
4
+ */
5
+ export function unwrapEnvelope(data, path) {
6
+ if (!path || path.trim() === "") {
7
+ return data;
8
+ }
9
+ const segments = path.split(".").filter(Boolean);
10
+ let current = data;
11
+ for (const segment of segments) {
12
+ if (!current || typeof current !== "object") {
13
+ return undefined;
14
+ }
15
+ current = current[segment];
16
+ }
17
+ return current;
18
+ }
19
+ /**
20
+ * Pivot array by field — generalized version of pivotByCategory
21
+ * e.g., pivotByField([{type:'A', val:1},{type:'B', val:2}], 'type', 'val')
22
+ * → { A: 1, B: 2 }
23
+ */
24
+ export function pivotByField(items, keyField, valueField) {
25
+ const result = {};
26
+ for (const item of items) {
27
+ if (!item || typeof item !== "object") {
28
+ continue;
29
+ }
30
+ const record = item;
31
+ const key = record[keyField];
32
+ const value = record[valueField];
33
+ if (key === undefined || key === null || value === undefined) {
34
+ continue;
35
+ }
36
+ result[String(key)] = value;
37
+ }
38
+ return result;
39
+ }
40
+ /**
41
+ * Parse XML items from string — returns array
42
+ * Uses simple regex (no DOM), suitable for structured XML
43
+ */
44
+ export function parseXmlItems(xml, tag) {
45
+ const matches = [
46
+ ...xml.matchAll(new RegExp(`<${tag}[^>]*>([\\s\\S]*?)</${tag}>`, "gi")),
47
+ ];
48
+ return matches
49
+ .map((match) => match[1].replace(/<[^>]+>/g, "").trim())
50
+ .filter((item) => item.length > 0);
51
+ }
@@ -0,0 +1,4 @@
1
+ /** Remove HTML tags from string */
2
+ export declare function stripHtml(html: string): string;
3
+ /** Truncate string to maxLength with optional suffix */
4
+ export declare function truncate(str: string, maxLength: number, suffix?: string): string;
@@ -0,0 +1,14 @@
1
+ /** Remove HTML tags from string */
2
+ export function stripHtml(html) {
3
+ return html
4
+ .replace(/<[^>]*>/g, "")
5
+ .replace(/\s+/g, " ")
6
+ .trim();
7
+ }
8
+ /** Truncate string to maxLength with optional suffix */
9
+ export function truncate(str, maxLength, suffix = "...") {
10
+ if (str.length <= maxLength) {
11
+ return str;
12
+ }
13
+ return `${str.slice(0, maxLength)}${suffix}`;
14
+ }
@@ -0,0 +1,8 @@
1
+ /** Parse any value to number (returns 0 if not parseable) */
2
+ export declare function toNumber(v: unknown): number;
3
+ /** Parse any value to float */
4
+ export declare function toFloat(v: unknown, decimals?: number): number;
5
+ /** Parse any value to integer */
6
+ export declare function toInt(v: unknown): number;
7
+ /** Parse any value to boolean (handles "true", "1", "yes", true, 1) */
8
+ export declare function toBoolean(v: unknown): boolean;
@@ -0,0 +1,48 @@
1
+ /** Parse any value to number (returns 0 if not parseable) */
2
+ export function toNumber(v) {
3
+ if (typeof v === "number") {
4
+ return Number.isFinite(v) ? v : 0;
5
+ }
6
+ if (typeof v === "boolean") {
7
+ return v ? 1 : 0;
8
+ }
9
+ if (typeof v === "string") {
10
+ const cleaned = v.replaceAll(",", "").trim();
11
+ if (cleaned === "") {
12
+ return 0;
13
+ }
14
+ const parsed = Number(cleaned);
15
+ return Number.isFinite(parsed) ? parsed : 0;
16
+ }
17
+ return 0;
18
+ }
19
+ /** Parse any value to float */
20
+ export function toFloat(v, decimals) {
21
+ const parsed = toNumber(v);
22
+ if (!Number.isFinite(parsed)) {
23
+ return 0;
24
+ }
25
+ if (decimals === undefined) {
26
+ return parsed;
27
+ }
28
+ const factor = 10 ** decimals;
29
+ return Math.round(parsed * factor) / factor;
30
+ }
31
+ /** Parse any value to integer */
32
+ export function toInt(v) {
33
+ return Math.round(toNumber(v));
34
+ }
35
+ /** Parse any value to boolean (handles "true", "1", "yes", true, 1) */
36
+ export function toBoolean(v) {
37
+ if (typeof v === "boolean") {
38
+ return v;
39
+ }
40
+ if (typeof v === "number") {
41
+ return v === 1;
42
+ }
43
+ if (typeof v === "string") {
44
+ const normalized = v.trim().toLowerCase();
45
+ return ["true", "1", "yes", "y"].includes(normalized);
46
+ }
47
+ return false;
48
+ }
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "@apifuse/provider-sdk",
3
- "version": "2.1.0-beta.1",
3
+ "version": "2.1.0-beta.10",
4
4
  "private": false,
5
5
  "type": "module",
6
- "description": "ApiFuse Provider SDK — Build providers with zero architectural constraints",
6
+ "description": "APIFuse Provider SDK — Build providers with zero architectural constraints",
7
7
  "license": "MIT",
8
- "main": "./src/index.ts",
9
- "types": "./src/index.ts",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
13
  "files": [
14
+ "dist",
14
15
  "src",
15
16
  "!src/__tests__",
16
17
  "!src/__tests__/**",
@@ -19,7 +20,8 @@
19
20
  "bin",
20
21
  "README.md",
21
22
  "AUTHORING.md",
22
- "CHANGELOG.md"
23
+ "CHANGELOG.md",
24
+ "SUBMISSION.md"
23
25
  ],
24
26
  "keywords": [
25
27
  "apifuse",
@@ -34,51 +36,76 @@
34
36
  },
35
37
  "exports": {
36
38
  ".": {
37
- "default": "./src/index.ts",
38
- "import": "./src/index.ts",
39
- "types": "./src/index.ts"
39
+ "types": "./dist/index.d.ts",
40
+ "import": "./dist/index.js",
41
+ "default": "./dist/index.js"
40
42
  },
41
43
  "./provider": {
42
- "default": "./src/provider.ts",
43
- "import": "./src/provider.ts",
44
- "types": "./src/provider.ts"
44
+ "types": "./dist/provider.d.ts",
45
+ "import": "./dist/provider.js",
46
+ "default": "./dist/provider.js"
47
+ },
48
+ "./contract": {
49
+ "types": "./dist/contract.d.ts",
50
+ "import": "./dist/contract.js",
51
+ "default": "./dist/contract.js"
45
52
  },
46
53
  "./server": {
47
- "default": "./src/server/index.ts",
48
- "import": "./src/server/index.ts",
49
- "types": "./src/server/index.ts"
54
+ "types": "./dist/server/index.d.ts",
55
+ "import": "./dist/server/index.js",
56
+ "default": "./dist/server/index.js"
50
57
  },
51
58
  "./testing": {
52
- "default": "./src/testing/index.ts",
53
- "import": "./src/testing/index.ts",
54
- "types": "./src/testing/index.ts"
59
+ "types": "./dist/testing/index.d.ts",
60
+ "import": "./dist/testing/index.js",
61
+ "default": "./dist/testing/index.js"
62
+ },
63
+ "./create": {
64
+ "types": "./dist/cli/create.d.ts",
65
+ "import": "./dist/cli/create.js",
66
+ "default": "./dist/cli/create.js"
55
67
  }
56
68
  },
57
69
  "scripts": {
58
- "lint": "biome check",
70
+ "lint": "biome lint .",
59
71
  "lint:fix": "biome lint --write",
60
72
  "format": "biome format --write",
61
73
  "type-check": "tsgo --noEmit",
62
74
  "test": "bun test",
63
- "check": "bun run lint && bun run type-check",
64
- "pack:check": "bun bin/apifuse-pack-check.ts",
65
- "pack:smoke": "bun bin/apifuse-pack-smoke.ts"
75
+ "check": "bun run lint && bun run type-check && bun run build",
76
+ "pack:check": "bun run build && bun bin/apifuse-pack-check.ts",
77
+ "pack:smoke": "bun run build && bun bin/apifuse-pack-smoke.ts",
78
+ "release:guard": "bun scripts/guard-release-pr.ts",
79
+ "format:check": "biome format .",
80
+ "build": "tsgo -p tsconfig.build.json && rm -rf dist/cli/templates && cp -R src/cli/templates dist/cli/templates"
66
81
  },
67
82
  "devDependencies": {
68
- "@biomejs/biome": "^2.4.12",
83
+ "@biomejs/biome": "^2.5.0",
69
84
  "@types/bun": "latest",
70
- "@types/node": "^25.1.0",
71
- "typescript": "^6.0.3"
85
+ "@types/node": "^25.9.3",
86
+ "@typescript/native-preview": "7.0.0-dev.20260419.1"
72
87
  },
73
88
  "dependencies": {
74
- "@clack/prompts": "^1.2.0",
89
+ "@clack/prompts": "^1.5.1",
90
+ "@types/ms": "^2.1.0",
75
91
  "ajv": "^8.17",
76
- "hono": "^4.12.14",
92
+ "hono": "^4.12.25",
93
+ "impit": "0.14.1",
94
+ "ioredis": "^5.11.1",
95
+ "ms": "^2.1.3",
77
96
  "playwright": "^1.55.1",
78
- "playwright-stealth": "^0.0.1",
97
+ "playwright-extra": "^4.3.6",
98
+ "puppeteer-extra-plugin-stealth": "^2.11.2",
79
99
  "re2-wasm": "^1.0",
80
100
  "safe-regex": "^2.1",
81
- "tlsclientwrapper": "^4.2.0",
82
- "zod": "^4.3.6"
83
- }
101
+ "zod": "^4.4.3"
102
+ },
103
+ "repository": {
104
+ "type": "git",
105
+ "url": "git+https://github.com/APIFuseHQ/provider-sdk.git"
106
+ },
107
+ "bugs": {
108
+ "url": "https://github.com/APIFuseHQ/provider-sdk/issues"
109
+ },
110
+ "homepage": "https://github.com/APIFuseHQ/provider-sdk#readme"
84
111
  }
@@ -8,7 +8,12 @@ import {
8
8
  TurnValidationError,
9
9
  ValidationError,
10
10
  } from "../errors";
11
- import type { AuthFlowDefinition, AuthTurn, FlowContext } from "../types";
11
+ import type {
12
+ AuthFlowDefinition,
13
+ AuthFlowInputHandler,
14
+ AuthTurn,
15
+ FlowContext,
16
+ } from "../types";
12
17
 
13
18
  type TurnKind =
14
19
  | "abort"
@@ -21,7 +26,7 @@ type TurnKind =
21
26
  | "redirect"
22
27
  | "retry";
23
28
 
24
- type CeremonyHandler = AuthFlowDefinition["start"];
29
+ type CeremonyHandler = AuthFlowInputHandler;
25
30
 
26
31
  type JsonObject = Record<string, unknown>;
27
32
 
@@ -44,6 +49,7 @@ const authTurnSchema = {
44
49
  additionalProperties: true,
45
50
  },
46
51
  hint: { type: "string" },
52
+ hintKey: { type: "string" },
47
53
  timing: {
48
54
  type: "object",
49
55
  additionalProperties: false,
@@ -0,0 +1,165 @@
1
+ import {
2
+ createCipheriv,
3
+ createDecipheriv,
4
+ createHash,
5
+ createHmac,
6
+ randomBytes,
7
+ timingSafeEqual,
8
+ } from "node:crypto";
9
+
10
+ export type ProviderChoiceTokenPayload = Record<string, unknown>;
11
+
12
+ export type ProviderChoiceTokenErrorReason =
13
+ | "invalid_shape"
14
+ | "invalid_signature"
15
+ | "invalid_payload"
16
+ | "invalid_binding"
17
+ | "stale";
18
+
19
+ export class ProviderChoiceTokenError extends Error {
20
+ readonly reason: ProviderChoiceTokenErrorReason;
21
+
22
+ constructor(reason: ProviderChoiceTokenErrorReason, message: string) {
23
+ super(message);
24
+ this.name = "ProviderChoiceTokenError";
25
+ this.reason = reason;
26
+ }
27
+ }
28
+
29
+ export interface CreateProviderChoiceTokenOptions<
30
+ TPayload extends ProviderChoiceTokenPayload,
31
+ > {
32
+ prefix: string;
33
+ payload: TPayload;
34
+ secret: string;
35
+ }
36
+
37
+ export interface ParseProviderChoiceTokenOptions {
38
+ token: string;
39
+ prefix: string;
40
+ secret: string;
41
+ }
42
+
43
+ export interface FreshProviderChoiceIssuedAtOptions {
44
+ ttlMs: number;
45
+ nowMs?: number;
46
+ futureToleranceMs?: number;
47
+ }
48
+
49
+ export function createProviderChoiceToken<
50
+ TPayload extends ProviderChoiceTokenPayload,
51
+ >(options: CreateProviderChoiceTokenOptions<TPayload>): string {
52
+ const iv = randomBytes(12);
53
+ const cipher = createCipheriv(
54
+ "aes-256-gcm",
55
+ choiceEncryptionKey(options.secret),
56
+ iv,
57
+ );
58
+ const encryptedPayload = Buffer.concat([
59
+ cipher.update(JSON.stringify(options.payload), "utf8"),
60
+ cipher.final(),
61
+ ]).toString("base64url");
62
+ const authTag = cipher.getAuthTag().toString("base64url");
63
+ const encodedIv = iv.toString("base64url");
64
+ const signature = signProviderChoiceTokenBody(
65
+ `${options.prefix}.${encodedIv}.${encryptedPayload}.${authTag}`,
66
+ options.secret,
67
+ );
68
+ return `${options.prefix}.${encodedIv}.${encryptedPayload}.${authTag}.${signature}`;
69
+ }
70
+
71
+ export function parseProviderChoiceToken(
72
+ options: ParseProviderChoiceTokenOptions,
73
+ ): ProviderChoiceTokenPayload {
74
+ const [
75
+ actualPrefix,
76
+ encodedIv,
77
+ encryptedPayload,
78
+ authTag,
79
+ signature,
80
+ ...extra
81
+ ] = options.token.split(".");
82
+ if (
83
+ actualPrefix !== options.prefix ||
84
+ !encodedIv ||
85
+ !encryptedPayload ||
86
+ !authTag ||
87
+ !signature ||
88
+ extra.length > 0
89
+ ) {
90
+ throw new ProviderChoiceTokenError(
91
+ "invalid_shape",
92
+ "Provider choice token shape is invalid.",
93
+ );
94
+ }
95
+
96
+ const signedBody = `${options.prefix}.${encodedIv}.${encryptedPayload}.${authTag}`;
97
+ const expectedSignature = signProviderChoiceTokenBody(
98
+ signedBody,
99
+ options.secret,
100
+ );
101
+ const actualBuffer = Buffer.from(signature);
102
+ const expectedBuffer = Buffer.from(expectedSignature);
103
+ if (
104
+ actualBuffer.length !== expectedBuffer.length ||
105
+ !timingSafeEqual(actualBuffer, expectedBuffer)
106
+ ) {
107
+ throw new ProviderChoiceTokenError(
108
+ "invalid_signature",
109
+ "Provider choice token signature is invalid.",
110
+ );
111
+ }
112
+
113
+ try {
114
+ const decipher = createDecipheriv(
115
+ "aes-256-gcm",
116
+ choiceEncryptionKey(options.secret),
117
+ Buffer.from(encodedIv, "base64url"),
118
+ );
119
+ decipher.setAuthTag(Buffer.from(authTag, "base64url"));
120
+ const decrypted = Buffer.concat([
121
+ decipher.update(Buffer.from(encryptedPayload, "base64url")),
122
+ decipher.final(),
123
+ ]).toString("utf8");
124
+ const parsed = JSON.parse(decrypted);
125
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
126
+ throw new Error("payload is not an object");
127
+ }
128
+ return Object.fromEntries(Object.entries(parsed));
129
+ } catch {
130
+ throw new ProviderChoiceTokenError(
131
+ "invalid_payload",
132
+ "Provider choice token payload is invalid.",
133
+ );
134
+ }
135
+ }
136
+
137
+ export function assertFreshProviderChoiceIssuedAt(
138
+ issuedAtMs: unknown,
139
+ options: FreshProviderChoiceIssuedAtOptions,
140
+ ): number {
141
+ const parsed =
142
+ typeof issuedAtMs === "number" ? issuedAtMs : Number(issuedAtMs);
143
+ const nowMs = options.nowMs ?? Date.now();
144
+ const futureToleranceMs = options.futureToleranceMs ?? 30_000;
145
+ if (
146
+ !Number.isFinite(parsed) ||
147
+ parsed <= 0 ||
148
+ nowMs - parsed > options.ttlMs ||
149
+ parsed - nowMs > futureToleranceMs
150
+ ) {
151
+ throw new ProviderChoiceTokenError(
152
+ "stale",
153
+ "Provider choice token is stale.",
154
+ );
155
+ }
156
+ return parsed;
157
+ }
158
+
159
+ function choiceEncryptionKey(secret: string): Buffer {
160
+ return createHash("sha256").update(secret).digest();
161
+ }
162
+
163
+ function signProviderChoiceTokenBody(body: string, secret: string): string {
164
+ return createHmac("sha256", secret).update(body).digest("base64url");
165
+ }