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

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 +51 -0
  3. package/README.md +118 -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 +21 -0
  117. package/dist/runtime/stealth.js +980 -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 +1359 -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 +179 -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 +1336 -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 +1145 -59
  211. package/src/runtime/tls.ts +0 -434
  212. package/src/types/playwright-stealth.d.ts +0 -9
package/src/errors.ts CHANGED
@@ -1,7 +1,12 @@
1
+ import type { ProviderErrorCategory } from "./observability";
2
+
1
3
  export type ProviderErrorOptions = {
2
4
  fix?: string;
3
5
  code?: string;
6
+ details?: unknown;
4
7
  cause?: Error;
8
+ category?: ProviderErrorCategory;
9
+ retryable?: boolean;
5
10
  };
6
11
 
7
12
  export class ProviderError extends Error {
@@ -23,6 +28,10 @@ export class ProviderError extends Error {
23
28
  get code(): string | undefined {
24
29
  return this.options?.code;
25
30
  }
31
+
32
+ get details(): unknown {
33
+ return this.options?.details;
34
+ }
26
35
  }
27
36
 
28
37
  export class SDKError extends ProviderError {
@@ -39,6 +48,21 @@ export class AuthError extends ProviderError {
39
48
  }
40
49
  }
41
50
 
51
+ export class SessionExpiredError extends AuthError {
52
+ constructor(
53
+ message = "Provider session expired",
54
+ options?: ProviderErrorOptions,
55
+ ) {
56
+ super(message, {
57
+ code: "reauth_required",
58
+ category: "credential_expired",
59
+ retryable: false,
60
+ ...options,
61
+ });
62
+ this.name = "SessionExpiredError";
63
+ }
64
+ }
65
+
42
66
  export type ValidationErrorOptions = ProviderErrorOptions & {
43
67
  zodError?: unknown;
44
68
  };
@@ -55,15 +79,18 @@ export class ValidationError extends ProviderError {
55
79
 
56
80
  export type TransportErrorOptions = ProviderErrorOptions & {
57
81
  status?: number;
82
+ upstreamStatus?: number;
58
83
  };
59
84
 
60
85
  export class TransportError extends ProviderError {
61
86
  readonly status?: number;
87
+ readonly upstreamStatus?: number;
62
88
 
63
89
  constructor(message: string, options?: TransportErrorOptions) {
64
90
  super(message, options);
65
91
  this.name = "TransportError";
66
92
  this.status = options?.status;
93
+ this.upstreamStatus = options?.upstreamStatus ?? options?.status;
67
94
  }
68
95
  }
69
96
 
@@ -0,0 +1,277 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+
4
+ import type { AuthTurn } from "../types";
5
+ import {
6
+ getProviderLocalePath,
7
+ isProviderLocaleValue,
8
+ type ProviderLocale,
9
+ type ProviderLocaleCatalog,
10
+ type ProviderLocaleKey,
11
+ type ProviderLocaleValue,
12
+ providerLocaleKey,
13
+ } from "./keys";
14
+
15
+ export type ProviderLocaleCatalogMap = Record<
16
+ ProviderLocale,
17
+ ProviderLocaleCatalog
18
+ >;
19
+
20
+ export interface LoadProviderLocaleCatalogsOptions {
21
+ providerDir: string;
22
+ locales: readonly ProviderLocale[];
23
+ }
24
+
25
+ export interface ProviderLocaleValidationIssue {
26
+ locale: ProviderLocale;
27
+ key: string;
28
+ message: string;
29
+ severity: "error" | "warning";
30
+ }
31
+
32
+ export interface ProviderLocaleValidationResult {
33
+ ok: boolean;
34
+ issues: ProviderLocaleValidationIssue[];
35
+ }
36
+
37
+ export function loadProviderLocaleCatalogs(
38
+ options: LoadProviderLocaleCatalogsOptions,
39
+ ): ProviderLocaleCatalogMap {
40
+ const catalogs: ProviderLocaleCatalogMap = {};
41
+ for (const locale of options.locales) {
42
+ const filePath = join(options.providerDir, "locales", `${locale}.json`);
43
+ catalogs[locale] = JSON.parse(readFileSync(filePath, "utf8"));
44
+ }
45
+ return catalogs;
46
+ }
47
+
48
+ export function resolveProviderLocaleValue(
49
+ catalogs: ProviderLocaleCatalogMap,
50
+ key: ProviderLocaleKey | string,
51
+ locale: ProviderLocale,
52
+ fallbackLocale: ProviderLocale = "en",
53
+ ): ProviderLocaleValue | undefined {
54
+ providerLocaleKey(key);
55
+ return (
56
+ getProviderLocalePath(catalogs[locale] ?? {}, key) ??
57
+ getProviderLocalePath(catalogs[fallbackLocale] ?? {}, key)
58
+ );
59
+ }
60
+
61
+ function asStringValue(
62
+ value: ProviderLocaleValue | undefined,
63
+ ): string | undefined {
64
+ return typeof value === "string" ? value : undefined;
65
+ }
66
+
67
+ function isStringRecord(value: unknown): value is Record<string, string> {
68
+ return (
69
+ !!value &&
70
+ typeof value === "object" &&
71
+ !Array.isArray(value) &&
72
+ Object.values(value).every((entry) => typeof entry === "string")
73
+ );
74
+ }
75
+
76
+ function localizeAuthInputSchema(
77
+ expectedInput: Record<string, unknown> | undefined,
78
+ options: {
79
+ catalogs: ProviderLocaleCatalogMap;
80
+ locale: ProviderLocale;
81
+ fallbackLocale: ProviderLocale;
82
+ },
83
+ ): Record<string, unknown> | undefined {
84
+ if (!expectedInput) return undefined;
85
+ const schema = isRecord(expectedInput.schema)
86
+ ? expectedInput.schema
87
+ : expectedInput;
88
+ const localizedSchema = localizeAuthSchemaObject(schema, options);
89
+ if (localizedSchema === schema) return undefined;
90
+ return isRecord(expectedInput.schema)
91
+ ? { ...expectedInput, schema: localizedSchema }
92
+ : localizedSchema;
93
+ }
94
+
95
+ function isRecord(value: unknown): value is Record<string, unknown> {
96
+ return !!value && typeof value === "object" && !Array.isArray(value);
97
+ }
98
+
99
+ function localizeAuthSchemaObject(
100
+ schema: Record<string, unknown>,
101
+ options: {
102
+ catalogs: ProviderLocaleCatalogMap;
103
+ locale: ProviderLocale;
104
+ fallbackLocale: ProviderLocale;
105
+ },
106
+ ): Record<string, unknown> {
107
+ const properties = isRecord(schema.properties)
108
+ ? schema.properties
109
+ : undefined;
110
+ if (!properties) return schema;
111
+
112
+ let changed = false;
113
+ const localizedProperties = Object.fromEntries(
114
+ Object.entries(properties).map(([fieldName, property]) => {
115
+ if (!isRecord(property)) return [fieldName, property];
116
+ const nameKey =
117
+ typeof property.nameKey === "string" ? property.nameKey : undefined;
118
+ const descriptionKey =
119
+ typeof property.descriptionKey === "string"
120
+ ? property.descriptionKey
121
+ : undefined;
122
+ const title = nameKey
123
+ ? asStringValue(
124
+ resolveProviderLocaleValue(
125
+ options.catalogs,
126
+ nameKey,
127
+ options.locale,
128
+ options.fallbackLocale,
129
+ ),
130
+ )
131
+ : undefined;
132
+ const description = descriptionKey
133
+ ? asStringValue(
134
+ resolveProviderLocaleValue(
135
+ options.catalogs,
136
+ descriptionKey,
137
+ options.locale,
138
+ options.fallbackLocale,
139
+ ),
140
+ )
141
+ : undefined;
142
+ if (!title && !description) return [fieldName, property];
143
+ changed = true;
144
+ return [
145
+ fieldName,
146
+ {
147
+ ...property,
148
+ ...(title ? { title } : {}),
149
+ ...(description ? { description } : {}),
150
+ },
151
+ ];
152
+ }),
153
+ );
154
+
155
+ return changed ? { ...schema, properties: localizedProperties } : schema;
156
+ }
157
+
158
+ export function localizeAuthTurn(
159
+ turn: AuthTurn,
160
+ options: {
161
+ catalogs: ProviderLocaleCatalogMap;
162
+ locale: ProviderLocale;
163
+ fallbackLocale?: ProviderLocale;
164
+ },
165
+ ): AuthTurn {
166
+ const fallbackLocale = options.fallbackLocale ?? "en";
167
+ const hint = turn.hintKey
168
+ ? asStringValue(
169
+ resolveProviderLocaleValue(
170
+ options.catalogs,
171
+ turn.hintKey,
172
+ options.locale,
173
+ fallbackLocale,
174
+ ),
175
+ )
176
+ : undefined;
177
+ const expectedInput = localizeAuthInputSchema(turn.expectedInput, {
178
+ catalogs: options.catalogs,
179
+ locale: options.locale,
180
+ fallbackLocale,
181
+ });
182
+ const fieldErrorKeys = turn.data?.fieldErrorKeys;
183
+ const fieldErrors = isStringRecord(fieldErrorKeys)
184
+ ? Object.fromEntries(
185
+ Object.entries(fieldErrorKeys).flatMap(([fieldName, key]) => {
186
+ const message = asStringValue(
187
+ resolveProviderLocaleValue(
188
+ options.catalogs,
189
+ key,
190
+ options.locale,
191
+ fallbackLocale,
192
+ ),
193
+ );
194
+ return message ? [[fieldName, message]] : [];
195
+ }),
196
+ )
197
+ : undefined;
198
+
199
+ if (!hint && !fieldErrors && !expectedInput) return turn;
200
+
201
+ return {
202
+ ...turn,
203
+ ...(hint ? { hint } : {}),
204
+ ...(expectedInput ? { expectedInput } : {}),
205
+ ...(fieldErrors
206
+ ? {
207
+ data: {
208
+ ...(turn.data ?? {}),
209
+ fieldErrors,
210
+ },
211
+ }
212
+ : {}),
213
+ };
214
+ }
215
+
216
+ export function validateProviderLocaleCatalogs(options: {
217
+ catalogs: ProviderLocaleCatalogMap;
218
+ requiredLocales: readonly ProviderLocale[];
219
+ requiredKeys: readonly (ProviderLocaleKey | string)[];
220
+ }): ProviderLocaleValidationResult {
221
+ const issues: ProviderLocaleValidationIssue[] = [];
222
+ const uniqueKeys = Array.from(
223
+ new Set(
224
+ options.requiredKeys.map((key) => {
225
+ providerLocaleKey(key);
226
+ return key;
227
+ }),
228
+ ),
229
+ );
230
+
231
+ for (const locale of options.requiredLocales) {
232
+ const catalog = options.catalogs[locale];
233
+ if (!catalog) {
234
+ issues.push({
235
+ locale,
236
+ key: "*",
237
+ message: `Missing provider locale catalog for ${locale}`,
238
+ severity: "error",
239
+ });
240
+ continue;
241
+ }
242
+
243
+ for (const key of uniqueKeys) {
244
+ const value = getProviderLocalePath(catalog, key);
245
+ if (value === undefined) {
246
+ issues.push({
247
+ locale,
248
+ key,
249
+ message: `Missing provider locale key ${key} in ${locale}`,
250
+ severity: "error",
251
+ });
252
+ continue;
253
+ }
254
+ if (!isProviderLocaleValue(value)) {
255
+ issues.push({
256
+ locale,
257
+ key,
258
+ message: `Provider locale key ${key} must resolve to a string or string array`,
259
+ severity: "error",
260
+ });
261
+ continue;
262
+ }
263
+ for (const text of Array.isArray(value) ? value : [value]) {
264
+ if (text.trim().length === 0 || /\bTODO\b/i.test(text)) {
265
+ issues.push({
266
+ locale,
267
+ key,
268
+ message: `Provider locale key ${key} in ${locale} is empty or placeholder text`,
269
+ severity: "error",
270
+ });
271
+ }
272
+ }
273
+ }
274
+ }
275
+
276
+ return { ok: issues.every((issue) => issue.severity !== "error"), issues };
277
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./catalog";
2
+ export * from "./keys";
@@ -0,0 +1,64 @@
1
+ import type { ProviderLocaleKey } from "../types";
2
+
3
+ export type { ProviderLocale, ProviderLocaleKey } from "../types";
4
+
5
+ const PROVIDER_LOCALE_KEY_RE =
6
+ /^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]+)*(?:\.[a-z][a-z0-9]*(?:[A-Z][a-z0-9]+)*|\.[0-9]+)*$/;
7
+
8
+ export function providerLocaleKey(key: string): ProviderLocaleKey {
9
+ assertProviderLocaleKey(key);
10
+ return key;
11
+ }
12
+
13
+ export function isProviderLocaleKey(
14
+ value: unknown,
15
+ ): value is ProviderLocaleKey {
16
+ return typeof value === "string" && PROVIDER_LOCALE_KEY_RE.test(value);
17
+ }
18
+
19
+ export function assertProviderLocaleKey(
20
+ value: unknown,
21
+ ): asserts value is ProviderLocaleKey {
22
+ if (!isProviderLocaleKey(value)) {
23
+ throw new Error(
24
+ `Provider locale key must be a dot path such as "meta.description" or "operations.search.description"; received ${JSON.stringify(value)}`,
25
+ );
26
+ }
27
+ }
28
+
29
+ export function qualifyProviderLocaleKey(
30
+ providerId: string,
31
+ key: ProviderLocaleKey | string,
32
+ ): string {
33
+ assertProviderLocaleKey(key);
34
+ return `providers.${providerId}.${key}`;
35
+ }
36
+
37
+ export function getProviderLocalePath(
38
+ catalog: ProviderLocaleCatalog,
39
+ key: ProviderLocaleKey | string,
40
+ ): ProviderLocaleValue | undefined {
41
+ assertProviderLocaleKey(key);
42
+ let cursor: unknown = catalog;
43
+ for (const segment of key.split(".")) {
44
+ if (!isRecord(cursor)) return undefined;
45
+ cursor = cursor[segment];
46
+ }
47
+ return isProviderLocaleValue(cursor) ? cursor : undefined;
48
+ }
49
+
50
+ export type ProviderLocaleValue = string | readonly string[];
51
+ export type ProviderLocaleCatalog = Record<string, unknown>;
52
+
53
+ export function isProviderLocaleValue(
54
+ value: unknown,
55
+ ): value is ProviderLocaleValue {
56
+ return (
57
+ typeof value === "string" ||
58
+ (Array.isArray(value) && value.every((entry) => typeof entry === "string"))
59
+ );
60
+ }
61
+
62
+ function isRecord(value: unknown): value is Record<string, unknown> {
63
+ return typeof value === "object" && value !== null && !Array.isArray(value);
64
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  // @apifuse/provider-sdk
2
2
 
3
- export { z } from "zod";
4
3
  export * from "./ceremonies";
4
+ export * from "./choice-token";
5
5
  export type {
6
6
  ApiFuseConfig,
7
7
  BrowserConfig,
@@ -9,10 +9,29 @@ export type {
9
9
  SessionConfig,
10
10
  } from "./config/loader";
11
11
  export { defineConfig, loadApiFuseConfig } from "./config/loader";
12
- export { defineOperation, defineProvider, type ProviderConfig } from "./define";
12
+ export {
13
+ canonicalJson,
14
+ digestProviderContract,
15
+ extractProviderContract,
16
+ type JsonPrimitive,
17
+ type JsonValue,
18
+ PROVIDER_CONTRACT_SCHEMA_VERSION,
19
+ type ProviderContractOperation,
20
+ type ProviderContractSnapshot,
21
+ } from "./contract";
22
+ export {
23
+ defineHealthJourney,
24
+ defineOperation,
25
+ defineProvider,
26
+ defineSmsOtpMatcher,
27
+ defineStreamOperation,
28
+ every,
29
+ type ProviderConfig,
30
+ } from "./define";
13
31
  export type { DevServerOptions } from "./dev";
14
32
  export { createDevServer, startDevServer } from "./dev";
15
33
  export * from "./errors";
34
+ export * from "./i18n";
16
35
  export {
17
36
  type LintDiagnostic,
18
37
  lintOperation,
@@ -23,6 +42,18 @@ export * from "./recipes/rest-api";
23
42
  export { createFlowContext, createScratchpad } from "./runtime/auth-flow";
24
43
  export type { BrowserClientOptions } from "./runtime/browser";
25
44
  export { BrowserClient, createBrowserClient } from "./runtime/browser";
45
+ export {
46
+ createBypassProviderCache,
47
+ createProviderCache,
48
+ type ProviderCacheOptions,
49
+ resetProviderCacheForTests,
50
+ } from "./runtime/cache";
51
+ export {
52
+ type CreateProviderChoiceContextOptions,
53
+ createProviderChoiceContext,
54
+ createTestProviderChoiceContext,
55
+ PROVIDER_RUNTIME_CHOICE_TOKEN_MASTER_SECRET_ENV,
56
+ } from "./runtime/choice";
26
57
  export {
27
58
  type CreateCredentialContextOptions,
28
59
  createCredentialContext,
@@ -39,30 +70,65 @@ export {
39
70
  } from "./runtime/instrumentation";
40
71
  export { type PrevalidateResult, prevalidate } from "./runtime/prevalidate";
41
72
  export { getProviderBaseUrl } from "./runtime/provider";
42
- export { createTlsClient } from "./runtime/tls";
73
+ export {
74
+ createUnsupportedProviderRuntimeState,
75
+ UnsupportedProviderStateError,
76
+ } from "./runtime/state";
77
+ export { createStealthClient } from "./runtime/stealth";
78
+ export {
79
+ APIFUSE__STT__BACKEND_ENV,
80
+ APIFUSE__STT__CLOUDFLARE_API_TOKEN_ENV,
81
+ APIFUSE__STT__MODEL_ENV,
82
+ createSttClientFromEnv,
83
+ createUnsupportedSttClient,
84
+ extractVerificationCode,
85
+ resolveSttPrompt,
86
+ } from "./runtime/stt";
43
87
  export {
44
88
  type CreateTraceContextOptions,
45
89
  createTraceContext,
46
90
  type Span,
47
91
  type TraceContext,
48
92
  } from "./runtime/trace";
93
+ export {
94
+ APIFUSE_DESCRIPTION_KEY_META_KEY,
95
+ APIFUSE_REDACTION_MARKER,
96
+ APIFUSE_SENSITIVE_KIND_META_KEY,
97
+ APIFUSE_SENSITIVE_META_KEY,
98
+ collectSensitivePaths,
99
+ describeKey,
100
+ field,
101
+ fields,
102
+ isSensitiveSchema,
103
+ redactPayload,
104
+ type SensitiveFieldKind,
105
+ type SensitiveFieldOptions,
106
+ type SensitivePath,
107
+ sensitive,
108
+ z,
109
+ } from "./schema";
49
110
  export { createServerApp, type ServeOptions, serve } from "./server";
50
111
  export { getStealthProfile, listStealthProfiles } from "./stealth/profiles";
112
+ export * from "./stream";
51
113
  export type {
52
114
  ApiFuseResponse,
53
115
  AuthConfig,
54
116
  AuthContext,
55
117
  AuthFlowDefinition,
56
- AuthFlowHandler,
118
+ AuthFlowInputHandler,
119
+ AuthFlowStartHandler,
57
120
  AuthMode,
58
121
  AuthTurn,
122
+ Bcp47Locale,
59
123
  BrowserEngine,
60
124
  BrowserOptions,
61
125
  ConnectionMode,
62
126
  ContextDeclaration,
127
+ CookieJar,
63
128
  ContextScratchpad,
64
129
  CredentialContext,
65
130
  CredentialDeclaration,
131
+ E164PhoneNumber,
66
132
  EnvContext,
67
133
  FlowContext,
68
134
  FlowContextStore,
@@ -71,35 +137,139 @@ export type {
71
137
  HealthCheckCaseResult,
72
138
  HealthCheckSuite,
73
139
  HealthCheckUnsupported,
140
+ HealthJourneyDefinition,
141
+ HealthJourneyEventContext,
142
+ HealthJourneyGatewayContext,
143
+ HealthJourneyJournalContext,
144
+ HealthJourneyManualTriggerPolicy,
145
+ HealthJourneyRunContext,
146
+ HealthJourneyRunResult,
147
+ HealthJourneySchedule,
148
+ HealthJourneySmsContext,
149
+ HealthJourneyStep,
74
150
  HttpClient,
151
+ HttpMethod,
75
152
  HttpResponse,
153
+ HttpRetryOptions,
154
+ HttpRetrySummary,
155
+ HttpStreamResponse,
156
+ IanaTimeZone,
76
157
  InferSchemaOutput,
158
+ Iso3166Alpha2CountryCode,
159
+ Iso4217CurrencyCode,
160
+ Iso8601Duration,
77
161
  OperationAnnotations,
162
+ OperationApprovalPolicy,
163
+ OperationContractMetadata,
78
164
  OperationDefinition,
165
+ OperationDeprecationMetadata,
79
166
  OperationDocMeta,
80
167
  OperationErrorCode,
168
+ OperationHandlerResult,
81
169
  OperationInputExample,
170
+ OperationLifecycle,
171
+ OperationObservabilityConfig,
172
+ OperationObservabilitySensitiveConfig,
82
173
  OperationRelationships,
174
+ OperationRiskClass,
175
+ OperationSensitivePath,
176
+ OperationToolRouterMetadata,
177
+ OperationTransport,
178
+ OperationTransportKind,
83
179
  ProbeInterval,
180
+ ProviderAccessConfig,
181
+ ProviderAccessVisibility,
182
+ ProviderCache,
183
+ ProviderCacheGetOrSetOptions,
184
+ ProviderCacheKeyOptions,
185
+ ProviderCacheLookupMeta,
186
+ ProviderCacheResponseMeta,
187
+ ProviderCacheResult,
188
+ ProviderChoiceBindingOptions,
189
+ ProviderChoiceContext,
190
+ ProviderChoiceIssueOptions,
191
+ ProviderChoiceParseOptions,
84
192
  ProviderContext,
85
193
  ProviderDefinition,
86
194
  ProviderHealthMonitorConfig,
195
+ ProviderLocale,
196
+ ProviderLocaleKey,
197
+ ProviderLocaleKeyInput,
198
+ ProviderLogoProfile,
199
+ ProviderLogoSource,
87
200
  ProviderMeta,
201
+ ProviderProxyConfig,
202
+ ProviderProxyMode,
203
+ ProviderProxyPolicy,
204
+ ProviderProxyProvider,
205
+ ProviderProxySessionAffinity,
206
+ ProviderPublicConnectionMode,
207
+ ProviderPublicProfile,
88
208
  ProviderReviewed,
209
+ ProviderRuntimeState,
89
210
  ProviderSecretDeclaration,
211
+ ProviderStateDurationString,
212
+ ProviderStateNamespace,
213
+ ProviderStreamEvent,
214
+ ProviderSttConfig,
215
+ ProviderSttMode,
216
+ ProviderSupportLevel,
90
217
  RequestOptions,
218
+ Rfc3339Instant,
91
219
  SchemaLike,
220
+ SmsOrigin,
221
+ SmsOtpExtractionPattern,
222
+ SmsOtpMatcherDefinition,
223
+ SseMessage,
92
224
  StandardSchemaV1,
225
+ StateCasResult,
226
+ StateNamespaceOptions,
227
+ StateValue,
228
+ StateWriteOptions,
229
+ StealthClient,
230
+ StealthFetchOptions,
93
231
  StealthPlatform,
94
232
  StealthProfile,
95
- TlsClient,
96
- TlsFetchOptions,
97
- TlsResponse,
98
- TlsSession,
233
+ StealthRedirectHop,
234
+ StealthRedirectRunOptions,
235
+ StealthRedirectRunResult,
236
+ StealthResponse,
237
+ StealthSession,
238
+ StealthSessionCookies,
239
+ SttAudioInput,
240
+ SttContext,
241
+ SttPromptPolicy,
242
+ SttSegment,
243
+ SttTranscribeMode,
244
+ SttTranscribeRequest,
245
+ SttTranscript,
246
+ SttUnsupportedOptionPolicy,
247
+ SttUsage,
248
+ SttVerificationCodeOptions,
249
+ SttWarning,
99
250
  TraceConfig,
100
251
  TraceSpan,
252
+ VerificationCodeCandidate,
253
+ VerificationCodeCandidateSource,
254
+ VerificationCodeExtractionResult,
255
+ } from "./types";
256
+ export {
257
+ DEFAULT_OPERATION_TRANSPORT,
258
+ HttpRetryAfterPolicy,
259
+ HttpRetryDelayStrategy,
260
+ HttpRetryJitter,
261
+ HttpRetryPreset,
262
+ HttpRetryUnsafeMethodPolicy,
263
+ PROBE_INTERVALS,
264
+ STREAM_CHUNK_BYTES_MAX,
265
+ STREAM_CHUNK_BYTES_MIN,
266
+ STREAM_HEARTBEAT_MS_MAX,
267
+ STREAM_HEARTBEAT_MS_MIN,
268
+ STREAM_IDLE_TIMEOUT_MS_MAX,
269
+ STREAM_IDLE_TIMEOUT_MS_MIN,
270
+ STREAM_MAX_DURATION_MS_MAX,
271
+ STREAM_MAX_DURATION_MS_MIN,
101
272
  } from "./types";
102
- export { PROBE_INTERVALS } from "./types";
103
273
  export * from "./utils/date";
104
274
  export * from "./utils/parse";
105
275
  export * from "./utils/text";