reqon-dsl 0.2.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/.claude/settings.local.json +31 -0
- package/.claude/skills/api-integration.md +125 -0
- package/.claude/skills/database-schema.md +51 -0
- package/.claude/skills/dsl-design.md +80 -0
- package/.claude/skills/property-testing.md +143 -0
- package/.claude/skills/reqon/SKILL.md +44 -0
- package/.claude/skills/reqon/references/examples.md +206 -0
- package/.claude/skills/reqon/references/syntax.md +263 -0
- package/.claude/skills/vscode-extension.md +113 -0
- package/.github/dependabot.yml +32 -0
- package/.github/pull_request_template.md +21 -0
- package/.github/workflows/ci.yml +174 -0
- package/.github/workflows/release.yml +73 -0
- package/CLAUDE.md +72 -0
- package/CONTRIBUTING.md +161 -0
- package/README.md +235 -0
- package/TODO.md +51 -0
- package/dist/ast/index.d.ts +1 -0
- package/dist/ast/index.js +1 -0
- package/dist/ast/nodes.d.ts +237 -0
- package/dist/ast/nodes.js +12 -0
- package/dist/auth/auth.test.d.ts +1 -0
- package/dist/auth/auth.test.js +255 -0
- package/dist/auth/circuit-breaker.d.ts +115 -0
- package/dist/auth/circuit-breaker.js +267 -0
- package/dist/auth/credentials.d.ts +91 -0
- package/dist/auth/credentials.js +169 -0
- package/dist/auth/index.d.ts +5 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/oauth2-provider.d.ts +41 -0
- package/dist/auth/oauth2-provider.js +131 -0
- package/dist/auth/rate-limiter.d.ts +61 -0
- package/dist/auth/rate-limiter.js +380 -0
- package/dist/auth/token-store.d.ts +30 -0
- package/dist/auth/token-store.js +148 -0
- package/dist/auth/types.d.ts +142 -0
- package/dist/auth/types.js +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +270 -0
- package/dist/errors/errors.test.d.ts +1 -0
- package/dist/errors/errors.test.js +165 -0
- package/dist/errors/index.d.ts +83 -0
- package/dist/errors/index.js +159 -0
- package/dist/execution/execution.test.d.ts +1 -0
- package/dist/execution/execution.test.js +246 -0
- package/dist/execution/index.d.ts +4 -0
- package/dist/execution/index.js +2 -0
- package/dist/execution/state.d.ts +136 -0
- package/dist/execution/state.js +82 -0
- package/dist/execution/store.d.ts +52 -0
- package/dist/execution/store.js +120 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +57 -0
- package/dist/integration.test.d.ts +1 -0
- package/dist/integration.test.js +168 -0
- package/dist/interpreter/context.d.ts +15 -0
- package/dist/interpreter/context.js +29 -0
- package/dist/interpreter/evaluator.d.ts +5 -0
- package/dist/interpreter/evaluator.js +223 -0
- package/dist/interpreter/evaluator.test.d.ts +1 -0
- package/dist/interpreter/evaluator.test.js +512 -0
- package/dist/interpreter/executor.d.ts +131 -0
- package/dist/interpreter/executor.js +663 -0
- package/dist/interpreter/fetch-handler.d.ts +43 -0
- package/dist/interpreter/fetch-handler.js +203 -0
- package/dist/interpreter/http.d.ts +57 -0
- package/dist/interpreter/http.js +210 -0
- package/dist/interpreter/http.test.d.ts +1 -0
- package/dist/interpreter/http.test.js +299 -0
- package/dist/interpreter/index.d.ts +7 -0
- package/dist/interpreter/index.js +7 -0
- package/dist/interpreter/pagination.d.ts +63 -0
- package/dist/interpreter/pagination.js +155 -0
- package/dist/interpreter/progress.test.d.ts +1 -0
- package/dist/interpreter/progress.test.js +216 -0
- package/dist/interpreter/schema-matcher.d.ts +16 -0
- package/dist/interpreter/schema-matcher.js +136 -0
- package/dist/interpreter/schema-matcher.test.d.ts +1 -0
- package/dist/interpreter/schema-matcher.test.js +122 -0
- package/dist/interpreter/signals.d.ts +57 -0
- package/dist/interpreter/signals.js +73 -0
- package/dist/interpreter/step-handlers/for-handler.d.ts +17 -0
- package/dist/interpreter/step-handlers/for-handler.js +51 -0
- package/dist/interpreter/step-handlers/index.d.ts +8 -0
- package/dist/interpreter/step-handlers/index.js +8 -0
- package/dist/interpreter/step-handlers/map-handler.d.ts +10 -0
- package/dist/interpreter/step-handlers/map-handler.js +20 -0
- package/dist/interpreter/step-handlers/match-handler.d.ts +27 -0
- package/dist/interpreter/step-handlers/match-handler.js +61 -0
- package/dist/interpreter/step-handlers/store-handler.d.ts +13 -0
- package/dist/interpreter/step-handlers/store-handler.js +66 -0
- package/dist/interpreter/step-handlers/types.d.ts +15 -0
- package/dist/interpreter/step-handlers/types.js +1 -0
- package/dist/interpreter/step-handlers/validate-handler.d.ts +10 -0
- package/dist/interpreter/step-handlers/validate-handler.js +26 -0
- package/dist/interpreter/step-handlers/webhook-handler.d.ts +36 -0
- package/dist/interpreter/step-handlers/webhook-handler.js +104 -0
- package/dist/lexer/index.d.ts +10 -0
- package/dist/lexer/index.js +12 -0
- package/dist/lexer/lexer.d.ts +24 -0
- package/dist/lexer/lexer.js +264 -0
- package/dist/lexer/lexer.test.d.ts +1 -0
- package/dist/lexer/lexer.test.js +259 -0
- package/dist/lexer/tokens.d.ts +69 -0
- package/dist/lexer/tokens.js +146 -0
- package/dist/loader/index.d.ts +36 -0
- package/dist/loader/index.js +220 -0
- package/dist/loader/loader.test.d.ts +1 -0
- package/dist/loader/loader.test.js +287 -0
- package/dist/oas/index.d.ts +4 -0
- package/dist/oas/index.js +2 -0
- package/dist/oas/loader.d.ts +21 -0
- package/dist/oas/loader.js +82 -0
- package/dist/oas/oas.test.d.ts +1 -0
- package/dist/oas/oas.test.js +218 -0
- package/dist/oas/validator.d.ts +12 -0
- package/dist/oas/validator.js +227 -0
- package/dist/parser/base.d.ts +33 -0
- package/dist/parser/base.js +97 -0
- package/dist/parser/expressions.d.ts +27 -0
- package/dist/parser/expressions.js +248 -0
- package/dist/parser/expressions.test.d.ts +1 -0
- package/dist/parser/expressions.test.js +378 -0
- package/dist/parser/index.d.ts +3 -0
- package/dist/parser/index.js +3 -0
- package/dist/parser/match.test.d.ts +1 -0
- package/dist/parser/match.test.js +254 -0
- package/dist/parser/parser.d.ts +68 -0
- package/dist/parser/parser.js +1229 -0
- package/dist/parser/parser.test.d.ts +1 -0
- package/dist/parser/parser.test.js +333 -0
- package/dist/parser/schedule.test.d.ts +1 -0
- package/dist/parser/schedule.test.js +241 -0
- package/dist/plugin.d.ts +35 -0
- package/dist/plugin.js +68 -0
- package/dist/scheduler/cron-parser.d.ts +32 -0
- package/dist/scheduler/cron-parser.js +198 -0
- package/dist/scheduler/cron-parser.test.d.ts +1 -0
- package/dist/scheduler/cron-parser.test.js +188 -0
- package/dist/scheduler/index.d.ts +3 -0
- package/dist/scheduler/index.js +2 -0
- package/dist/scheduler/scheduler.d.ts +81 -0
- package/dist/scheduler/scheduler.js +376 -0
- package/dist/scheduler/types.d.ts +65 -0
- package/dist/scheduler/types.js +1 -0
- package/dist/stores/factory.d.ts +36 -0
- package/dist/stores/factory.js +73 -0
- package/dist/stores/file.d.ts +60 -0
- package/dist/stores/file.js +173 -0
- package/dist/stores/file.test.d.ts +1 -0
- package/dist/stores/file.test.js +165 -0
- package/dist/stores/index.d.ts +6 -0
- package/dist/stores/index.js +5 -0
- package/dist/stores/memory.d.ts +19 -0
- package/dist/stores/memory.js +51 -0
- package/dist/stores/memory.test.d.ts +1 -0
- package/dist/stores/memory.test.js +157 -0
- package/dist/stores/postgrest.d.ts +55 -0
- package/dist/stores/postgrest.js +217 -0
- package/dist/stores/stores.test.d.ts +1 -0
- package/dist/stores/stores.test.js +158 -0
- package/dist/stores/types.d.ts +31 -0
- package/dist/stores/types.js +26 -0
- package/dist/sync/index.d.ts +4 -0
- package/dist/sync/index.js +2 -0
- package/dist/sync/state.d.ts +69 -0
- package/dist/sync/state.js +66 -0
- package/dist/sync/store.d.ts +49 -0
- package/dist/sync/store.js +93 -0
- package/dist/sync/sync.test.d.ts +1 -0
- package/dist/sync/sync.test.js +221 -0
- package/dist/utils/async.d.ts +7 -0
- package/dist/utils/async.js +9 -0
- package/dist/utils/file.d.ts +38 -0
- package/dist/utils/file.js +92 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/logger.d.ts +34 -0
- package/dist/utils/logger.js +39 -0
- package/dist/utils/path.d.ts +12 -0
- package/dist/utils/path.js +41 -0
- package/dist/webhook/index.d.ts +8 -0
- package/dist/webhook/index.js +7 -0
- package/dist/webhook/server.d.ts +84 -0
- package/dist/webhook/server.js +319 -0
- package/dist/webhook/store.d.ts +67 -0
- package/dist/webhook/store.js +193 -0
- package/dist/webhook/types.d.ts +88 -0
- package/dist/webhook/types.js +6 -0
- package/docusaurus/README.md +41 -0
- package/docusaurus/docs/advanced/execution-state.md +283 -0
- package/docusaurus/docs/advanced/extending-reqon.md +388 -0
- package/docusaurus/docs/advanced/multi-file-missions.md +250 -0
- package/docusaurus/docs/advanced/parallel-execution.md +353 -0
- package/docusaurus/docs/api-reference.md +443 -0
- package/docusaurus/docs/authentication/api-key.md +339 -0
- package/docusaurus/docs/authentication/basic.md +276 -0
- package/docusaurus/docs/authentication/bearer.md +282 -0
- package/docusaurus/docs/authentication/oauth2.md +317 -0
- package/docusaurus/docs/authentication/overview.md +251 -0
- package/docusaurus/docs/cli.md +229 -0
- package/docusaurus/docs/core-concepts/actions.md +286 -0
- package/docusaurus/docs/core-concepts/missions.md +264 -0
- package/docusaurus/docs/core-concepts/schemas.md +353 -0
- package/docusaurus/docs/core-concepts/sources.md +339 -0
- package/docusaurus/docs/core-concepts/stores.md +332 -0
- package/docusaurus/docs/dsl-syntax/expressions.md +361 -0
- package/docusaurus/docs/dsl-syntax/fetch.md +293 -0
- package/docusaurus/docs/dsl-syntax/for-loops.md +324 -0
- package/docusaurus/docs/dsl-syntax/map.md +345 -0
- package/docusaurus/docs/dsl-syntax/match.md +387 -0
- package/docusaurus/docs/dsl-syntax/pipelines.md +397 -0
- package/docusaurus/docs/dsl-syntax/validate.md +401 -0
- package/docusaurus/docs/error-handling/dead-letter-queues.md +399 -0
- package/docusaurus/docs/error-handling/flow-control.md +337 -0
- package/docusaurus/docs/error-handling/retry-strategies.md +368 -0
- package/docusaurus/docs/examples.md +488 -0
- package/docusaurus/docs/getting-started.md +256 -0
- package/docusaurus/docs/http/circuit-breaker.md +401 -0
- package/docusaurus/docs/http/incremental-sync.md +394 -0
- package/docusaurus/docs/http/pagination.md +361 -0
- package/docusaurus/docs/http/rate-limiting.md +383 -0
- package/docusaurus/docs/http/requests.md +328 -0
- package/docusaurus/docs/http/retry.md +402 -0
- package/docusaurus/docs/intro.md +90 -0
- package/docusaurus/docs/openapi/loading-specs.md +305 -0
- package/docusaurus/docs/openapi/operation-calls.md +314 -0
- package/docusaurus/docs/openapi/overview.md +212 -0
- package/docusaurus/docs/openapi/response-validation.md +344 -0
- package/docusaurus/docs/scheduling/cron.md +305 -0
- package/docusaurus/docs/scheduling/daemon-mode.md +317 -0
- package/docusaurus/docs/scheduling/intervals.md +289 -0
- package/docusaurus/docs/scheduling/overview.md +231 -0
- package/docusaurus/docs/stores/custom-adapters.md +376 -0
- package/docusaurus/docs/stores/file.md +236 -0
- package/docusaurus/docs/stores/memory.md +193 -0
- package/docusaurus/docs/stores/overview.md +274 -0
- package/docusaurus/docs/stores/postgrest.md +316 -0
- package/docusaurus/docusaurus.config.ts +148 -0
- package/docusaurus/package-lock.json +18029 -0
- package/docusaurus/package.json +47 -0
- package/docusaurus/sidebars.ts +155 -0
- package/docusaurus/src/components/HomepageFeatures/index.tsx +105 -0
- package/docusaurus/src/components/HomepageFeatures/styles.module.css +12 -0
- package/docusaurus/src/css/custom.css +169 -0
- package/docusaurus/src/pages/index.module.css +48 -0
- package/docusaurus/src/pages/index.tsx +110 -0
- package/docusaurus/src/pages/markdown-page.md +7 -0
- package/docusaurus/static/.nojekyll +0 -0
- package/docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docusaurus/static/img/docusaurus.png +0 -0
- package/docusaurus/static/img/favicon.ico +0 -0
- package/docusaurus/static/img/logo.svg +10 -0
- package/docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docusaurus/tsconfig.json +8 -0
- package/examples/README.md +112 -0
- package/examples/error-handling/README.md +150 -0
- package/examples/error-handling/payment-processor.vague +287 -0
- package/examples/github-sync/README.md +74 -0
- package/examples/github-sync/fetch-issues.vague +47 -0
- package/examples/github-sync/fetch-prs.vague +40 -0
- package/examples/github-sync/mission.vague +101 -0
- package/examples/github-sync/normalize.vague +70 -0
- package/examples/jsonplaceholder/README.md +28 -0
- package/examples/jsonplaceholder/posts.vague +48 -0
- package/examples/petstore/README.md +35 -0
- package/examples/petstore/openapi.yaml +97 -0
- package/examples/petstore/sync.vague +52 -0
- package/examples/temporal-comparison/README.md +297 -0
- package/examples/temporal-comparison/reconciliation.vague +355 -0
- package/examples/temporal-comparison/temporal/activities/index.ts +8 -0
- package/examples/temporal-comparison/temporal/activities/shipstation.ts +225 -0
- package/examples/temporal-comparison/temporal/activities/shopify.ts +257 -0
- package/examples/temporal-comparison/temporal/activities/storage.ts +198 -0
- package/examples/temporal-comparison/temporal/activities/stripe.ts +169 -0
- package/examples/temporal-comparison/temporal/activities/validation.ts +205 -0
- package/examples/temporal-comparison/temporal/client/schedule.ts +218 -0
- package/examples/temporal-comparison/temporal/config/retry.ts +63 -0
- package/examples/temporal-comparison/temporal/types/index.ts +129 -0
- package/examples/temporal-comparison/temporal/workers/main.ts +130 -0
- package/examples/temporal-comparison/temporal/workflows/orderReconciliation.ts +262 -0
- package/examples/xero/README.md +88 -0
- package/examples/xero/invoices.vague +189 -0
- package/package.json +40 -0
- package/src/api-integration.test.ts +954 -0
- package/src/ast/index.ts +1 -0
- package/src/ast/nodes.ts +310 -0
- package/src/auth/auth.test.ts +326 -0
- package/src/auth/circuit-breaker.test.ts +390 -0
- package/src/auth/circuit-breaker.ts +379 -0
- package/src/auth/credentials.test.ts +273 -0
- package/src/auth/credentials.ts +246 -0
- package/src/auth/index.ts +40 -0
- package/src/auth/oauth2-provider.ts +177 -0
- package/src/auth/rate-limiter.ts +459 -0
- package/src/auth/token-store.ts +177 -0
- package/src/auth/types.ts +159 -0
- package/src/benchmark/e2e.bench.ts +288 -0
- package/src/benchmark/evaluator.bench.ts +331 -0
- package/src/benchmark/fixtures.ts +295 -0
- package/src/benchmark/index.ts +108 -0
- package/src/benchmark/lexer.bench.ts +69 -0
- package/src/benchmark/parser.bench.ts +103 -0
- package/src/benchmark/resilience.bench.ts +193 -0
- package/src/benchmark/store.bench.ts +147 -0
- package/src/benchmark/utils.ts +230 -0
- package/src/cli.ts +313 -0
- package/src/errors/errors.test.ts +234 -0
- package/src/errors/index.ts +223 -0
- package/src/execution/execution.test.ts +307 -0
- package/src/execution/index.ts +21 -0
- package/src/execution/state.ts +207 -0
- package/src/execution/store.ts +188 -0
- package/src/index.ts +169 -0
- package/src/integration.test.ts +192 -0
- package/src/interpreter/context.ts +57 -0
- package/src/interpreter/evaluator.test.ts +796 -0
- package/src/interpreter/evaluator.ts +245 -0
- package/src/interpreter/executor.ts +946 -0
- package/src/interpreter/fetch-handler.ts +302 -0
- package/src/interpreter/http.test.ts +423 -0
- package/src/interpreter/http.ts +308 -0
- package/src/interpreter/index.ts +32 -0
- package/src/interpreter/pagination.ts +207 -0
- package/src/interpreter/progress.test.ts +276 -0
- package/src/interpreter/schema-matcher.test.ts +160 -0
- package/src/interpreter/schema-matcher.ts +168 -0
- package/src/interpreter/signals.ts +73 -0
- package/src/interpreter/step-handlers/for-handler.ts +65 -0
- package/src/interpreter/step-handlers/index.ts +17 -0
- package/src/interpreter/step-handlers/map-handler.ts +24 -0
- package/src/interpreter/step-handlers/match-handler.ts +101 -0
- package/src/interpreter/step-handlers/store-handler.ts +78 -0
- package/src/interpreter/step-handlers/types.ts +17 -0
- package/src/interpreter/step-handlers/validate-handler.ts +30 -0
- package/src/interpreter/step-handlers/webhook-handler.ts +142 -0
- package/src/lexer/index.ts +18 -0
- package/src/lexer/lexer.test.ts +316 -0
- package/src/lexer/tokens.ts +179 -0
- package/src/loader/index.ts +288 -0
- package/src/loader/loader.test.ts +360 -0
- package/src/oas/index.ts +4 -0
- package/src/oas/loader.ts +126 -0
- package/src/oas/oas.test.ts +254 -0
- package/src/oas/validator.ts +299 -0
- package/src/parser/base.ts +124 -0
- package/src/parser/expressions.test.ts +525 -0
- package/src/parser/expressions.ts +314 -0
- package/src/parser/index.ts +3 -0
- package/src/parser/match.test.ts +296 -0
- package/src/parser/parser.test.ts +739 -0
- package/src/parser/parser.ts +1469 -0
- package/src/parser/schedule.test.ts +287 -0
- package/src/parser/webhook.test.ts +248 -0
- package/src/plugin.ts +83 -0
- package/src/scheduler/cron-parser.test.ts +236 -0
- package/src/scheduler/cron-parser.ts +236 -0
- package/src/scheduler/index.ts +10 -0
- package/src/scheduler/scheduler.ts +443 -0
- package/src/scheduler/types.ts +71 -0
- package/src/stores/factory.ts +104 -0
- package/src/stores/file.test.ts +276 -0
- package/src/stores/file.ts +211 -0
- package/src/stores/index.ts +6 -0
- package/src/stores/memory.test.ts +238 -0
- package/src/stores/memory.ts +63 -0
- package/src/stores/postgrest.test.ts +488 -0
- package/src/stores/postgrest.ts +263 -0
- package/src/stores/stores.test.ts +197 -0
- package/src/stores/types.ts +58 -0
- package/src/sync/index.ts +16 -0
- package/src/sync/state.ts +126 -0
- package/src/sync/store.ts +139 -0
- package/src/sync/sync.test.ts +271 -0
- package/src/utils/async.ts +10 -0
- package/src/utils/file.ts +106 -0
- package/src/utils/index.ts +14 -0
- package/src/utils/logger.ts +53 -0
- package/src/utils/path.ts +47 -0
- package/src/webhook/index.ts +15 -0
- package/src/webhook/server.test.ts +253 -0
- package/src/webhook/server.ts +389 -0
- package/src/webhook/store.ts +239 -0
- package/src/webhook/types.ts +93 -0
- package/tsconfig.json +17 -0
- package/vitest.config.ts +39 -0
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { HttpClient, BearerAuthProvider, OAuth2AuthProvider } from './http.js';
|
|
3
|
+
|
|
4
|
+
describe('HttpClient', () => {
|
|
5
|
+
let originalFetch: typeof globalThis.fetch;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
originalFetch = globalThis.fetch;
|
|
9
|
+
vi.useFakeTimers();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
globalThis.fetch = originalFetch;
|
|
14
|
+
vi.useRealTimers();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('buildUrl', () => {
|
|
18
|
+
it('constructs URL with base and path', async () => {
|
|
19
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
20
|
+
let capturedUrl = '';
|
|
21
|
+
|
|
22
|
+
globalThis.fetch = vi.fn(async (url: RequestInfo | URL) => {
|
|
23
|
+
capturedUrl = url.toString();
|
|
24
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
await client.request({ method: 'GET', path: '/users' });
|
|
28
|
+
|
|
29
|
+
expect(capturedUrl).toBe('https://api.example.com/users');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('handles trailing slash on base URL', async () => {
|
|
33
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com/' });
|
|
34
|
+
let capturedUrl = '';
|
|
35
|
+
|
|
36
|
+
globalThis.fetch = vi.fn(async (url: RequestInfo | URL) => {
|
|
37
|
+
capturedUrl = url.toString();
|
|
38
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
await client.request({ method: 'GET', path: '/users' });
|
|
42
|
+
|
|
43
|
+
expect(capturedUrl).toBe('https://api.example.com/users');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('handles path without leading slash', async () => {
|
|
47
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
48
|
+
let capturedUrl = '';
|
|
49
|
+
|
|
50
|
+
globalThis.fetch = vi.fn(async (url: RequestInfo | URL) => {
|
|
51
|
+
capturedUrl = url.toString();
|
|
52
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
await client.request({ method: 'GET', path: 'users' });
|
|
56
|
+
|
|
57
|
+
expect(capturedUrl).toBe('https://api.example.com/users');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('adds query parameters to URL', async () => {
|
|
61
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
62
|
+
let capturedUrl = '';
|
|
63
|
+
|
|
64
|
+
globalThis.fetch = vi.fn(async (url: RequestInfo | URL) => {
|
|
65
|
+
capturedUrl = url.toString();
|
|
66
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
await client.request({
|
|
70
|
+
method: 'GET',
|
|
71
|
+
path: '/users',
|
|
72
|
+
query: { page: '1', limit: '10' },
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
expect(capturedUrl).toBe('https://api.example.com/users?page=1&limit=10');
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('headers', () => {
|
|
80
|
+
it('sets default Content-Type and Accept headers', async () => {
|
|
81
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
82
|
+
let capturedHeaders: Record<string, string> = {};
|
|
83
|
+
|
|
84
|
+
globalThis.fetch = vi.fn(async (_url: RequestInfo | URL, init?: RequestInit) => {
|
|
85
|
+
capturedHeaders = Object.fromEntries(
|
|
86
|
+
Object.entries(init?.headers || {})
|
|
87
|
+
);
|
|
88
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
await client.request({ method: 'GET', path: '/users' });
|
|
92
|
+
|
|
93
|
+
expect(capturedHeaders['Content-Type']).toBe('application/json');
|
|
94
|
+
expect(capturedHeaders['Accept']).toBe('application/json');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('merges config headers', async () => {
|
|
98
|
+
const client = new HttpClient({
|
|
99
|
+
baseUrl: 'https://api.example.com',
|
|
100
|
+
headers: { 'X-Custom': 'value' },
|
|
101
|
+
});
|
|
102
|
+
let capturedHeaders: Record<string, string> = {};
|
|
103
|
+
|
|
104
|
+
globalThis.fetch = vi.fn(async (_url: RequestInfo | URL, init?: RequestInit) => {
|
|
105
|
+
capturedHeaders = Object.fromEntries(
|
|
106
|
+
Object.entries(init?.headers || {})
|
|
107
|
+
);
|
|
108
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
await client.request({ method: 'GET', path: '/users' });
|
|
112
|
+
|
|
113
|
+
expect(capturedHeaders['X-Custom']).toBe('value');
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('request headers override config headers', async () => {
|
|
117
|
+
const client = new HttpClient({
|
|
118
|
+
baseUrl: 'https://api.example.com',
|
|
119
|
+
headers: { 'X-Custom': 'config' },
|
|
120
|
+
});
|
|
121
|
+
let capturedHeaders: Record<string, string> = {};
|
|
122
|
+
|
|
123
|
+
globalThis.fetch = vi.fn(async (_url: RequestInfo | URL, init?: RequestInit) => {
|
|
124
|
+
capturedHeaders = Object.fromEntries(
|
|
125
|
+
Object.entries(init?.headers || {})
|
|
126
|
+
);
|
|
127
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
await client.request({
|
|
131
|
+
method: 'GET',
|
|
132
|
+
path: '/users',
|
|
133
|
+
headers: { 'X-Custom': 'request' },
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
expect(capturedHeaders['X-Custom']).toBe('request');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('adds Authorization header when auth provider is set', async () => {
|
|
140
|
+
const auth = new BearerAuthProvider('test-token');
|
|
141
|
+
const client = new HttpClient({
|
|
142
|
+
baseUrl: 'https://api.example.com',
|
|
143
|
+
auth,
|
|
144
|
+
});
|
|
145
|
+
let capturedHeaders: Record<string, string> = {};
|
|
146
|
+
|
|
147
|
+
globalThis.fetch = vi.fn(async (_url: RequestInfo | URL, init?: RequestInit) => {
|
|
148
|
+
capturedHeaders = Object.fromEntries(
|
|
149
|
+
Object.entries(init?.headers || {})
|
|
150
|
+
);
|
|
151
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
await client.request({ method: 'GET', path: '/users' });
|
|
155
|
+
|
|
156
|
+
expect(capturedHeaders['Authorization']).toBe('Bearer test-token');
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe('calculateDelay', () => {
|
|
161
|
+
it('calculates exponential backoff delay', async () => {
|
|
162
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
163
|
+
|
|
164
|
+
let callCount = 0;
|
|
165
|
+
globalThis.fetch = vi.fn(async () => {
|
|
166
|
+
callCount++;
|
|
167
|
+
if (callCount < 3) {
|
|
168
|
+
throw new Error('Network error');
|
|
169
|
+
}
|
|
170
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const requestPromise = client.request(
|
|
174
|
+
{ method: 'GET', path: '/users' },
|
|
175
|
+
{ maxAttempts: 3, backoff: 'exponential', initialDelay: 1000 }
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
// First retry delay: ~1000ms (1000 * 2^0) + up to 10% jitter
|
|
179
|
+
// Second retry delay: ~2000ms (1000 * 2^1) + up to 10% jitter
|
|
180
|
+
// Advance enough time for both retries with jitter buffer
|
|
181
|
+
await vi.advanceTimersByTimeAsync(4000);
|
|
182
|
+
|
|
183
|
+
await requestPromise;
|
|
184
|
+
|
|
185
|
+
expect(callCount).toBe(3);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('calculates linear backoff delay', async () => {
|
|
189
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
190
|
+
|
|
191
|
+
let callCount = 0;
|
|
192
|
+
globalThis.fetch = vi.fn(async () => {
|
|
193
|
+
callCount++;
|
|
194
|
+
if (callCount < 3) {
|
|
195
|
+
throw new Error('Network error');
|
|
196
|
+
}
|
|
197
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
const requestPromise = client.request(
|
|
201
|
+
{ method: 'GET', path: '/users' },
|
|
202
|
+
{ maxAttempts: 3, backoff: 'linear', initialDelay: 1000 }
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
// Advance through all retries with buffer for jitter
|
|
206
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
207
|
+
|
|
208
|
+
await requestPromise;
|
|
209
|
+
|
|
210
|
+
expect(callCount).toBe(3);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('calculates constant backoff delay', async () => {
|
|
214
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
215
|
+
|
|
216
|
+
let callCount = 0;
|
|
217
|
+
globalThis.fetch = vi.fn(async () => {
|
|
218
|
+
callCount++;
|
|
219
|
+
if (callCount < 3) {
|
|
220
|
+
throw new Error('Network error');
|
|
221
|
+
}
|
|
222
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const requestPromise = client.request(
|
|
226
|
+
{ method: 'GET', path: '/users' },
|
|
227
|
+
{ maxAttempts: 3, backoff: 'constant', initialDelay: 1000 }
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
// Both retries should use ~1000ms
|
|
231
|
+
await vi.advanceTimersByTimeAsync(1100);
|
|
232
|
+
await vi.advanceTimersByTimeAsync(1100);
|
|
233
|
+
|
|
234
|
+
await requestPromise;
|
|
235
|
+
|
|
236
|
+
expect(callCount).toBe(3);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
describe('retry behavior', () => {
|
|
241
|
+
it('retries on 5xx server errors', async () => {
|
|
242
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
243
|
+
|
|
244
|
+
let callCount = 0;
|
|
245
|
+
globalThis.fetch = vi.fn(async () => {
|
|
246
|
+
callCount++;
|
|
247
|
+
if (callCount < 2) {
|
|
248
|
+
return new Response(JSON.stringify({ error: 'Server error' }), { status: 500 });
|
|
249
|
+
}
|
|
250
|
+
return new Response(JSON.stringify({ data: 'success' }), { status: 200 });
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
const requestPromise = client.request(
|
|
254
|
+
{ method: 'GET', path: '/users' },
|
|
255
|
+
{ maxAttempts: 3, backoff: 'constant', initialDelay: 100 }
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
await vi.advanceTimersByTimeAsync(150);
|
|
259
|
+
const result = await requestPromise;
|
|
260
|
+
|
|
261
|
+
expect(callCount).toBe(2);
|
|
262
|
+
expect(result.status).toBe(200);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('respects Retry-After header on 429', async () => {
|
|
266
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
267
|
+
|
|
268
|
+
let callCount = 0;
|
|
269
|
+
globalThis.fetch = vi.fn(async () => {
|
|
270
|
+
callCount++;
|
|
271
|
+
if (callCount < 2) {
|
|
272
|
+
const headers = new Headers();
|
|
273
|
+
headers.set('Retry-After', '5'); // 5 seconds
|
|
274
|
+
return new Response(JSON.stringify({}), { status: 429, headers });
|
|
275
|
+
}
|
|
276
|
+
return new Response(JSON.stringify({ data: 'success' }), { status: 200 });
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const requestPromise = client.request(
|
|
280
|
+
{ method: 'GET', path: '/users' },
|
|
281
|
+
{ maxAttempts: 3, backoff: 'constant', initialDelay: 100 }
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
// Should wait 5 seconds as specified in Retry-After
|
|
285
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
286
|
+
const result = await requestPromise;
|
|
287
|
+
|
|
288
|
+
expect(callCount).toBe(2);
|
|
289
|
+
expect(result.status).toBe(200);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('throws after max retry attempts', async () => {
|
|
293
|
+
// Use real timers for this test to avoid unhandled rejection timing issues
|
|
294
|
+
vi.useRealTimers();
|
|
295
|
+
|
|
296
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
297
|
+
|
|
298
|
+
globalThis.fetch = vi.fn(async () => {
|
|
299
|
+
throw new Error('Network error');
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
await expect(
|
|
303
|
+
client.request(
|
|
304
|
+
{ method: 'GET', path: '/users' },
|
|
305
|
+
{ maxAttempts: 3, backoff: 'constant', initialDelay: 10 } // Short delay for fast test
|
|
306
|
+
)
|
|
307
|
+
).rejects.toThrow('Network error');
|
|
308
|
+
|
|
309
|
+
expect(globalThis.fetch).toHaveBeenCalledTimes(3);
|
|
310
|
+
|
|
311
|
+
// Restore fake timers for remaining tests
|
|
312
|
+
vi.useFakeTimers();
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
describe('response handling', () => {
|
|
317
|
+
it('parses JSON response body', async () => {
|
|
318
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
319
|
+
const responseData = { id: 1, name: 'Test' };
|
|
320
|
+
|
|
321
|
+
globalThis.fetch = vi.fn(async () => {
|
|
322
|
+
return new Response(JSON.stringify(responseData), { status: 200 });
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
const result = await client.request<typeof responseData>({
|
|
326
|
+
method: 'GET',
|
|
327
|
+
path: '/users/1',
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
expect(result.data).toEqual(responseData);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it('returns response headers', async () => {
|
|
334
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
335
|
+
|
|
336
|
+
globalThis.fetch = vi.fn(async () => {
|
|
337
|
+
const headers = new Headers();
|
|
338
|
+
headers.set('X-Total-Count', '100');
|
|
339
|
+
return new Response(JSON.stringify({}), { status: 200, headers });
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
const result = await client.request({ method: 'GET', path: '/users' });
|
|
343
|
+
|
|
344
|
+
expect(result.headers['x-total-count']).toBe('100');
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it('sends request body as JSON', async () => {
|
|
348
|
+
const client = new HttpClient({ baseUrl: 'https://api.example.com' });
|
|
349
|
+
let capturedBody: string | undefined;
|
|
350
|
+
|
|
351
|
+
globalThis.fetch = vi.fn(async (_url: RequestInfo | URL, init?: RequestInit) => {
|
|
352
|
+
capturedBody = init?.body as string;
|
|
353
|
+
return new Response(JSON.stringify({}), { status: 201 });
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
await client.request({
|
|
357
|
+
method: 'POST',
|
|
358
|
+
path: '/users',
|
|
359
|
+
body: { name: 'Test', email: 'test@example.com' },
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
expect(capturedBody).toBe(JSON.stringify({ name: 'Test', email: 'test@example.com' }));
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
describe('BearerAuthProvider', () => {
|
|
368
|
+
it('returns the configured token', async () => {
|
|
369
|
+
const provider = new BearerAuthProvider('my-token');
|
|
370
|
+
const token = await provider.getToken();
|
|
371
|
+
expect(token).toBe('my-token');
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
describe('OAuth2AuthProvider', () => {
|
|
376
|
+
it('returns the access token', async () => {
|
|
377
|
+
const provider = new OAuth2AuthProvider({
|
|
378
|
+
accessToken: 'access-token',
|
|
379
|
+
});
|
|
380
|
+
const token = await provider.getToken();
|
|
381
|
+
expect(token).toBe('access-token');
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it('throws when refreshToken called without required config', async () => {
|
|
385
|
+
const provider = new OAuth2AuthProvider({
|
|
386
|
+
accessToken: 'access-token',
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
await expect(provider.refreshToken()).rejects.toThrow(
|
|
390
|
+
'Cannot refresh token: missing refresh token or endpoint'
|
|
391
|
+
);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it('refreshes token using token endpoint', async () => {
|
|
395
|
+
const originalFetch = globalThis.fetch;
|
|
396
|
+
const provider = new OAuth2AuthProvider({
|
|
397
|
+
accessToken: 'old-token',
|
|
398
|
+
refreshToken: 'refresh-token',
|
|
399
|
+
tokenEndpoint: 'https://auth.example.com/token',
|
|
400
|
+
clientId: 'client-id',
|
|
401
|
+
clientSecret: 'client-secret',
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
globalThis.fetch = vi.fn(async () => {
|
|
405
|
+
return new Response(
|
|
406
|
+
JSON.stringify({
|
|
407
|
+
access_token: 'new-token',
|
|
408
|
+
refresh_token: 'new-refresh-token',
|
|
409
|
+
}),
|
|
410
|
+
{ status: 200 }
|
|
411
|
+
);
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
const newToken = await provider.refreshToken();
|
|
415
|
+
expect(newToken).toBe('new-token');
|
|
416
|
+
|
|
417
|
+
// Token should be updated internally
|
|
418
|
+
const currentToken = await provider.getToken();
|
|
419
|
+
expect(currentToken).toBe('new-token');
|
|
420
|
+
|
|
421
|
+
globalThis.fetch = originalFetch;
|
|
422
|
+
});
|
|
423
|
+
});
|