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

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 (79) hide show
  1. package/AUTHORING.md +93 -0
  2. package/CHANGELOG.md +21 -0
  3. package/README.md +133 -28
  4. package/bin/apifuse-check.ts +78 -71
  5. package/bin/apifuse-create.ts +12 -0
  6. package/bin/apifuse-dev.ts +24 -61
  7. package/bin/apifuse-pack-check.ts +87 -0
  8. package/bin/apifuse-pack-smoke.ts +122 -0
  9. package/bin/apifuse-perf.ts +33 -32
  10. package/bin/apifuse-record.ts +17 -7
  11. package/bin/apifuse-test.ts +6 -4
  12. package/bin/apifuse.ts +36 -35
  13. package/package.json +29 -9
  14. package/src/ceremonies/index.ts +768 -0
  15. package/src/cli/commands.ts +87 -0
  16. package/src/cli/create.ts +845 -0
  17. package/src/cli/templates/provider/Dockerfile.tpl +7 -0
  18. package/src/cli/templates/provider/README.md.tpl +41 -0
  19. package/src/cli/templates/provider/dev.ts.tpl +5 -0
  20. package/src/cli/templates/provider/index.test.ts.tpl +13 -0
  21. package/src/cli/templates/provider/index.ts.tpl +58 -0
  22. package/src/cli/templates/provider/start.ts.tpl +5 -0
  23. package/src/config/loader.ts +61 -1
  24. package/src/define.ts +565 -41
  25. package/src/dev.ts +2 -6
  26. package/src/errors.ts +42 -0
  27. package/src/index.ts +44 -38
  28. package/src/lint.ts +574 -0
  29. package/src/provider.ts +13 -0
  30. package/src/runtime/auth-flow.ts +67 -0
  31. package/src/runtime/credential.ts +95 -0
  32. package/src/runtime/env.ts +13 -0
  33. package/src/runtime/executor.ts +13 -14
  34. package/src/runtime/http.ts +36 -12
  35. package/src/runtime/insights.ts +3 -3
  36. package/src/runtime/key-derivation.ts +122 -0
  37. package/src/runtime/keyring.ts +148 -0
  38. package/src/runtime/namespace.ts +33 -0
  39. package/src/runtime/prevalidate.ts +252 -0
  40. package/src/runtime/tls.ts +41 -17
  41. package/src/runtime/waterfall.ts +0 -1
  42. package/src/schema.ts +77 -0
  43. package/src/serve.ts +1 -664
  44. package/src/server/index.ts +22 -0
  45. package/src/server/serve.ts +624 -0
  46. package/src/server/types.ts +78 -0
  47. package/src/stealth/profiles.ts +10 -93
  48. package/src/testing/run.ts +391 -32
  49. package/src/types.ts +390 -41
  50. package/bin/apifuse-init.ts +0 -387
  51. package/src/__tests__/auth.test.ts +0 -396
  52. package/src/__tests__/browser-auth.test.ts +0 -180
  53. package/src/__tests__/browser.test.ts +0 -632
  54. package/src/__tests__/define.test.ts +0 -225
  55. package/src/__tests__/errors.test.ts +0 -69
  56. package/src/__tests__/executor.test.ts +0 -214
  57. package/src/__tests__/http.test.ts +0 -238
  58. package/src/__tests__/insights.test.ts +0 -210
  59. package/src/__tests__/instrumentation.test.ts +0 -290
  60. package/src/__tests__/otlp.test.ts +0 -141
  61. package/src/__tests__/perf.test.ts +0 -60
  62. package/src/__tests__/providers-yaml.test.ts +0 -135
  63. package/src/__tests__/proxy.test.ts +0 -359
  64. package/src/__tests__/recipes.test.ts +0 -36
  65. package/src/__tests__/serve.test.ts +0 -233
  66. package/src/__tests__/session.test.ts +0 -231
  67. package/src/__tests__/state.test.ts +0 -100
  68. package/src/__tests__/stealth.test.ts +0 -57
  69. package/src/__tests__/testing.test.ts +0 -97
  70. package/src/__tests__/tls.test.ts +0 -345
  71. package/src/__tests__/types.test.ts +0 -142
  72. package/src/__tests__/utils.test.ts +0 -62
  73. package/src/__tests__/waterfall.test.ts +0 -270
  74. package/src/config/providers-yaml.ts +0 -370
  75. package/src/index.test.ts +0 -1
  76. package/src/protocol.ts +0 -183
  77. package/src/runtime/auth.ts +0 -245
  78. package/src/runtime/session.ts +0 -573
  79. package/src/runtime/state.ts +0 -124
@@ -0,0 +1,7 @@
1
+ FROM oven/bun:1.2-alpine
2
+ WORKDIR /provider
3
+ COPY package.json bun.lockb* ./
4
+ RUN bun install --frozen-lockfile
5
+ COPY . .
6
+ EXPOSE 3000
7
+ CMD ["bun", "run", "start"]
@@ -0,0 +1,41 @@
1
+ # {{DISPLAY_NAME}}
2
+
3
+ Generated with `apifuse create`.
4
+
5
+ ## Commands
6
+
7
+ ```bash
8
+ bun run dev
9
+ bun run check
10
+ bun run test
11
+ ```
12
+
13
+ ## Provider server contract
14
+
15
+ - Dev default: `3900`
16
+ - Start/Docker/container contract: `3000`
17
+ - `GET /health`
18
+ - `POST /v1/{operation}`
19
+ - `POST /auth/start`
20
+ - `POST /auth/continue`
21
+ - `POST /auth/poll`
22
+ - `POST /auth/disconnect`
23
+
24
+ ## Next steps
25
+
26
+ 1. Replace the sample `ping` operation with real upstream logic.
27
+ 2. Record a real fixture with `bun run record:sample` or a provider-specific equivalent.
28
+ 3. Replace the starter `healthCheckUnsupported` with a real `healthCheck` for read-only upstream operations when safe.
29
+ 4. Extend tests and operation metadata until the provider is bounty-ready.
30
+
31
+ ## Health-check authorship
32
+
33
+ Every operation must declare exactly one of:
34
+
35
+ - `healthCheck` — preferred for safe read-only upstream probes.
36
+ - `healthCheckUnsupported` — allowed only when a probe is destructive, paid,
37
+ credential-sensitive, flaky by design, or otherwise unsafe. Use a specific
38
+ reason; reviewers reject placeholder reasons such as "TODO" or "later".
39
+
40
+ The generated `ping` operation uses `healthCheckUnsupported` only because it is
41
+ a local scaffold check, not a real upstream API probe.
@@ -0,0 +1,5 @@
1
+ import { startDevServer } from "@apifuse/provider-sdk";
2
+
3
+ import provider from "./index";
4
+
5
+ startDevServer(provider, { port: Number(process.env.PORT) || 3900 });
@@ -0,0 +1,13 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import { runStandardTests } from "@apifuse/provider-sdk/testing";
3
+
4
+ import provider from "../index";
5
+
6
+ runStandardTests(provider);
7
+
8
+ describe("{{PROVIDER_ID}}", () => {
9
+ it("exposes provider metadata from defineProvider", () => {
10
+ expect(provider.id).toBe("{{PROVIDER_ID}}");
11
+ expect(provider.reviewed).toBe("community");
12
+ });
13
+ });
@@ -0,0 +1,58 @@
1
+ import { defineProvider, z } from "@apifuse/provider-sdk";
2
+
3
+ const InputSchema = z
4
+ .object({
5
+ value: z
6
+ .string()
7
+ .default("hello")
8
+ .describe("Sample input value used to verify the generated provider scaffold is wired correctly."),
9
+ })
10
+ .describe("Input payload for the generated ping operation.");
11
+
12
+ const OutputSchema = z
13
+ .object({
14
+ ok: z
15
+ .boolean()
16
+ .describe("Whether the generated provider handled the sample request successfully."),
17
+ message: z
18
+ .string()
19
+ .describe("Human-readable confirmation that the generated provider round-tripped the sample payload."),
20
+ })
21
+ .describe("Output payload returned by the generated ping operation.");
22
+
23
+ export default defineProvider({
24
+ id: "{{PROVIDER_ID}}",
25
+ version: "1.0.0",
26
+ runtime: "{{RUNTIME}}"{{BROWSER_BLOCK}},
27
+ allowedHosts: ["api.example.com"],
28
+ reviewed: "community",
29
+ {{SECRETS_BLOCK}}{{CREDENTIAL_BLOCK}}auth: {{AUTH_BLOCK}},
30
+ meta: {
31
+ displayName: "{{DISPLAY_NAME}}",
32
+ description: "{{DISPLAY_NAME}} provider starter for ApiFuse community contributions.",
33
+ category: "{{CATEGORY}}",
34
+ tags: ["{{PROVIDER_ID}}", "starter", "community"],
35
+ },
36
+ operations: {
37
+ ping: {
38
+ description:
39
+ "Confirms the generated provider wiring is operational by echoing a small sample payload through the ApiFuse runtime contract. Use when validating local development, baseline checks, or first-pass bounty scaffolds. Do NOT use for production data retrieval or upstream-specific workflows because this starter operation exists only to prove the generated project compiles, serves, and round-trips input/output correctly. Returns a success flag plus a message containing the supplied value.",
40
+ input: InputSchema,
41
+ output: OutputSchema,
42
+ handler: async (_ctx, input) => {
43
+ return {
44
+ ok: true,
45
+ message: "{{DISPLAY_NAME}} received: " + input.value,
46
+ };
47
+ },
48
+ fixtures: {
49
+ request: { value: "hello" },
50
+ response: { ok: true, message: "{{DISPLAY_NAME}} received: hello" },
51
+ },
52
+ healthCheckUnsupported: {
53
+ reason:
54
+ "Generated local-only scaffold operation. Replace this with a real healthCheck for upstream-backed bounty operations when safe; keep healthCheckUnsupported only for destructive, paid, credential-sensitive, or otherwise unprobeable operations with a specific rationale.",
55
+ },
56
+ },
57
+ },
58
+ });
@@ -0,0 +1,5 @@
1
+ import { serve } from "@apifuse/provider-sdk";
2
+
3
+ import provider from "./index";
4
+
5
+ await serve(provider, { port: Number(process.env.PORT) || 3000 });
@@ -43,7 +43,67 @@ export type ResolvedProxyConfig = {
43
43
 
44
44
  function normalizeProxyUrl(url?: string): string | undefined {
45
45
  const normalized = url?.trim();
46
- return normalized ? normalized : undefined;
46
+ return normalized ? applyStickyProxySession(normalized) : undefined;
47
+ }
48
+
49
+ function applyStickyProxySession(proxyUrl: string): string {
50
+ let parsed: URL;
51
+ try {
52
+ parsed = new URL(proxyUrl);
53
+ } catch {
54
+ return proxyUrl;
55
+ }
56
+
57
+ if (!parsed.hostname || !parsed.username || !parsed.password) {
58
+ return proxyUrl;
59
+ }
60
+
61
+ const host = parsed.hostname.toLowerCase();
62
+ if (!host.includes("smartproxy") && !host.includes("decodo")) {
63
+ return proxyUrl;
64
+ }
65
+
66
+ const username = decodeURIComponent(parsed.username);
67
+ const sessionId =
68
+ process.env.APIFUSE_PROXY_SESSION_ID?.trim() || "apifuse-shared";
69
+ const sessionDuration =
70
+ process.env.APIFUSE_PROXY_SESSION_DURATION?.trim() || undefined;
71
+ const stickyUsername = host.includes("smartproxy")
72
+ ? buildSmartproxyUsername(username, sessionId, sessionDuration)
73
+ : buildDecodoUsername(username, sessionId, sessionDuration ?? "60");
74
+
75
+ parsed.username = stickyUsername;
76
+ return parsed.toString();
77
+ }
78
+
79
+ function buildSmartproxyUsername(
80
+ username: string,
81
+ sessionId: string,
82
+ sessionDuration?: string,
83
+ ): string {
84
+ const parts = username.split("_");
85
+ const configuredLife = parts
86
+ .find((part) => part.startsWith("life-"))
87
+ ?.slice("life-".length);
88
+ const baseUsername = parts
89
+ .filter((part) => !part.startsWith("session-") && !part.startsWith("life-"))
90
+ .join("_");
91
+ return `${baseUsername}_session-${sessionId}_life-${sessionDuration ?? configuredLife ?? "60"}`;
92
+ }
93
+
94
+ function buildDecodoUsername(
95
+ username: string,
96
+ sessionId: string,
97
+ sessionDuration: string,
98
+ ): string {
99
+ const withoutSticky = username.replace(
100
+ /-session-.+-sessionduration-\d+$/,
101
+ "",
102
+ );
103
+ const baseUsername = withoutSticky.startsWith("user-")
104
+ ? withoutSticky
105
+ : `user-${withoutSticky}`;
106
+ return `${baseUsername}-session-${sessionId}-sessionduration-${sessionDuration}`;
47
107
  }
48
108
 
49
109
  function syncProxyEnv(config: ApiFuseConfig): void {