@tahanabavi/typefetch 1.5.6 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -37,6 +37,56 @@ type EncryptionConfig<TReq, TRes> = {
37
37
  */
38
38
  type RequestSchema = z.ZodTypeAny;
39
39
  type ResponseSchema = z.ZodTypeAny;
40
+ /**
41
+ * EndpointTestContext
42
+ * ===================
43
+ * Shared runtime state used by the API test runner.
44
+ * It allows one endpoint test to store values that later tests can reuse.
45
+ */
46
+ type EndpointTestContext = {
47
+ data: Record<string, unknown>;
48
+ get<T = unknown>(key: string): T | undefined;
49
+ set<T = unknown>(key: string, value: T): void;
50
+ has(key: string): boolean;
51
+ };
52
+ type EndpointTestInputFactory<TReq> = (ctx: EndpointTestContext) => TReq | Promise<TReq>;
53
+ type EndpointTestAssertion<TReq, TRes> = (result: {
54
+ input: TReq;
55
+ response: TRes;
56
+ ctx: EndpointTestContext;
57
+ }) => void | Promise<void>;
58
+ type EndpointTestCase<TReq, TRes> = {
59
+ /** Human-readable name shown in the generated report. */
60
+ name?: string;
61
+ /** Static input or a factory that can read/write shared test context. */
62
+ input?: TReq | EndpointTestInputFactory<TReq>;
63
+ /** Skip this specific case. A string is used as the skip reason. */
64
+ skip?: boolean | string;
65
+ /** Expected HTTP status. Defaults to any successful client response. */
66
+ expectStatus?: number | number[];
67
+ /** Optional user-defined assertion after a successful response. */
68
+ expect?: EndpointTestAssertion<TReq, TRes>;
69
+ /** Per-case timeout in milliseconds. */
70
+ timeout?: number;
71
+ /** Tags used by the runner for include/exclude filtering. */
72
+ tags?: string[];
73
+ };
74
+ type EndpointTestConfig<TReq, TRes> = {
75
+ /** Disable all generated/manual tests for this endpoint. */
76
+ enabled?: boolean;
77
+ /** Tags used by the runner for include/exclude filtering. */
78
+ tags?: string[];
79
+ /** Mark endpoints such as DELETE/reset/payment as unsafe for default runs. */
80
+ destructive?: boolean;
81
+ /** Default input used when cases are not provided. */
82
+ input?: TReq | EndpointTestInputFactory<TReq>;
83
+ /** One or more test cases for this endpoint. */
84
+ cases?: Array<EndpointTestCase<TReq, TRes>>;
85
+ /** Runs before the endpoint cases. */
86
+ setup?: (ctx: EndpointTestContext) => void | Promise<void>;
87
+ /** Runs after the endpoint cases. */
88
+ teardown?: (ctx: EndpointTestContext) => void | Promise<void>;
89
+ };
40
90
  /**
41
91
  * EndpointDef
42
92
  * ============
@@ -81,6 +131,10 @@ type EndpointDef<TReq extends RequestSchema, TRes extends ResponseSchema> = {
81
131
  * - `"form-data"`: multipart form
82
132
  */
83
133
  bodyType?: "json" | "form-data";
134
+ /**
135
+ * Optional contract-driven tests used by the TypeFetch test runner.
136
+ */
137
+ test?: EndpointTestConfig<z.infer<TReq>, z.infer<TRes>>;
84
138
  };
85
139
  /**
86
140
  * Contracts
@@ -406,4 +460,167 @@ declare const makeRequestSchema: <TPath extends z.ZodRawShape = {}, TQuery exten
406
460
  headers: THeaders;
407
461
  }, z.core.$strip>;
408
462
 
409
- export { ApiClient, type AuthOptions, type CacheOptions, type Contracts, type DeepEncryptionMap, type EncryptionConfig, type EncryptionMethod, type EncryptionOptions, type EndpointDef, type EndpointDefZ, type EndpointMethods, type ErrorLike, type LoggingOptions, type Method, type Middleware, type MiddlewareContext, type MiddlewareNext, type RequestOptions, type RequestParts, type RequestSchema, type ResponseSchema, type RetryOptions, RichError, type TokenProvider, authMiddleware, cacheMiddleware, encryptionMiddleware, loggingMiddleware, makeRequestSchema, processDeep, retryMiddleware };
463
+ declare class TypeFetchTestContext implements EndpointTestContext {
464
+ data: Record<string, unknown>;
465
+ constructor(initialData?: Record<string, unknown>);
466
+ get<T = unknown>(key: string): T | undefined;
467
+ set<T = unknown>(key: string, value: T): void;
468
+ has(key: string): boolean;
469
+ }
470
+
471
+ type ApiTestMode = "schema" | "mock" | "live" | "full";
472
+ type ApiTestStatus = "passed" | "failed" | "skipped";
473
+ type ApiTestReportFormat = "json" | "markdown" | "html";
474
+ type AutoInputValue = unknown | ((path: string) => unknown);
475
+ type AutoInputOptions = {
476
+ /** Maximum recursive generation depth for nested schemas. */
477
+ maxDepth?: number;
478
+ /** Prefer optional object fields when generating inputs. Default: true. */
479
+ includeOptional?: boolean;
480
+ /** Generate one item for arrays by default. Default: true. */
481
+ includeArrayItems?: boolean;
482
+ /** Overrides by full dot path, field name, or request part path. */
483
+ values?: Record<string, AutoInputValue>;
484
+ /** Custom fallback for files. Defaults to Blob when available, otherwise string. */
485
+ fileFactory?: () => unknown;
486
+ };
487
+ type ApiTestRunnerOptions = {
488
+ mode?: ApiTestMode;
489
+ timeout?: number;
490
+ concurrency?: number;
491
+ stopOnFail?: boolean;
492
+ includeDestructive?: boolean;
493
+ includeTags?: string[];
494
+ excludeTags?: string[];
495
+ autoInput?: AutoInputOptions;
496
+ requestOptions?: RequestOptions;
497
+ };
498
+ type ApiTestRunnerConfig<C extends Contracts = Contracts> = {
499
+ contracts: C;
500
+ /** Initialized ApiClient instance or compatible object with modules. */
501
+ client: {
502
+ modules: Record<string, Record<string, (input: any, options?: RequestOptions) => Promise<any>>>;
503
+ };
504
+ options?: ApiTestRunnerOptions;
505
+ context?: Record<string, unknown>;
506
+ };
507
+ type ApiTestCaseMeta = {
508
+ module: string;
509
+ endpoint: string;
510
+ caseName: string;
511
+ phase: ApiTestMode | "schema" | "mock" | "live";
512
+ method: Method;
513
+ path: string;
514
+ tags: string[];
515
+ destructive: boolean;
516
+ };
517
+ type ApiTestError = {
518
+ name?: string;
519
+ message: string;
520
+ status?: number;
521
+ code?: string;
522
+ issues?: unknown;
523
+ stack?: string;
524
+ };
525
+ type ApiTestResult = ApiTestCaseMeta & {
526
+ status: ApiTestStatus;
527
+ durationMs: number;
528
+ input?: unknown;
529
+ response?: unknown;
530
+ error?: ApiTestError;
531
+ skipReason?: string;
532
+ };
533
+ type ApiTestReportSummary = {
534
+ total: number;
535
+ passed: number;
536
+ failed: number;
537
+ skipped: number;
538
+ durationMs: number;
539
+ };
540
+ type ApiTestReport = {
541
+ generatedAt: string;
542
+ mode: ApiTestMode;
543
+ summary: ApiTestReportSummary;
544
+ results: ApiTestResult[];
545
+ };
546
+
547
+ declare function generateInput(schema: z.ZodTypeAny, options?: AutoInputOptions): unknown;
548
+
549
+ declare function createMarkdownReport(report: ApiTestReport): string;
550
+ declare function createHtmlReport(report: ApiTestReport): string;
551
+
552
+ declare function createApiTestRunner<C extends Contracts>(config: ApiTestRunnerConfig<C>): ApiTestRunner<C>;
553
+ declare class ApiTestRunner<C extends Contracts> {
554
+ private readonly config;
555
+ private readonly ctx;
556
+ private readonly options;
557
+ constructor(config: ApiTestRunnerConfig<C>);
558
+ run(): Promise<ApiTestReport>;
559
+ private discoverEndpoints;
560
+ private runEndpoint;
561
+ private getEndpointSkipReason;
562
+ private getCases;
563
+ private getPhases;
564
+ private runCasePhase;
565
+ private callClient;
566
+ }
567
+
568
+ type TypeFetchCliCommand = "test" | "list" | "init" | "release-doc" | "help" | "version";
569
+ type ParsedCliArgs = {
570
+ command: TypeFetchCliCommand;
571
+ flags: Record<string, string | boolean | string[]>;
572
+ positionals: string[];
573
+ raw: string[];
574
+ };
575
+ type TypeFetchClientLike = {
576
+ init?: () => void;
577
+ modules: Record<string, Record<string, (input: any, options?: RequestOptions) => Promise<any>>>;
578
+ };
579
+ type TypeFetchCreateClientOptions = {
580
+ baseUrl?: string;
581
+ token?: string;
582
+ };
583
+ type TypeFetchReportConfig = {
584
+ /** Path base or full file path. Examples: ./typefetch-report/report or ./typefetch-report/report.md */
585
+ output?: string;
586
+ formats?: ApiTestReportFormat[];
587
+ };
588
+ type TypeFetchCliTestConfig<C extends Contracts = Contracts> = {
589
+ contracts: C;
590
+ /** Use client for simple projects. */
591
+ client?: TypeFetchClientLike;
592
+ /** Prefer createClient when you want CLI flags like --base-url and --token to work. */
593
+ createClient?: (options: TypeFetchCreateClientOptions) => TypeFetchClientLike | Promise<TypeFetchClientLike>;
594
+ options?: ApiTestRunnerOptions;
595
+ context?: Record<string, unknown>;
596
+ report?: TypeFetchReportConfig;
597
+ };
598
+ type CliResolvedOptions = {
599
+ mode?: ApiTestMode;
600
+ baseUrl?: string;
601
+ token?: string;
602
+ timeout?: number;
603
+ includeTags?: string[];
604
+ excludeTags?: string[];
605
+ includeDestructive?: boolean;
606
+ stopOnFail?: boolean;
607
+ output?: string;
608
+ formats?: ApiTestReportFormat[];
609
+ config?: string;
610
+ };
611
+ type InitCommandOptions = {
612
+ force?: boolean;
613
+ packageName?: string;
614
+ contractsPath?: string;
615
+ output?: string;
616
+ };
617
+ type ReleaseDocCommandOptions = {
618
+ force?: boolean;
619
+ version?: string;
620
+ outputDir?: string;
621
+ title?: string;
622
+ };
623
+
624
+ declare function defineTypeFetchTestConfig<C extends Contracts>(config: TypeFetchCliTestConfig<C>): TypeFetchCliTestConfig<C>;
625
+
626
+ export { ApiClient, type ApiTestCaseMeta, type ApiTestError, type ApiTestMode, type ApiTestReport, type ApiTestReportFormat, type ApiTestReportSummary, type ApiTestResult, ApiTestRunner, type ApiTestRunnerConfig, type ApiTestRunnerOptions, type ApiTestStatus, type AuthOptions, type AutoInputOptions, type CacheOptions, type CliResolvedOptions, type Contracts, type DeepEncryptionMap, type EncryptionConfig, type EncryptionMethod, type EncryptionOptions, type EndpointDef, type EndpointDefZ, type EndpointMethods, type EndpointTestAssertion, type EndpointTestCase, type EndpointTestConfig, type EndpointTestContext, type EndpointTestInputFactory, type ErrorLike, type InitCommandOptions, type LoggingOptions, type Method, type Middleware, type MiddlewareContext, type MiddlewareNext, type ParsedCliArgs, type ReleaseDocCommandOptions, type RequestOptions, type RequestParts, type RequestSchema, type ResponseSchema, type RetryOptions, RichError, type TokenProvider, type TypeFetchCliCommand, type TypeFetchCliTestConfig, type TypeFetchClientLike, type TypeFetchCreateClientOptions, type TypeFetchReportConfig, TypeFetchTestContext, authMiddleware, cacheMiddleware, createApiTestRunner, createHtmlReport, createMarkdownReport, defineTypeFetchTestConfig, encryptionMiddleware, generateInput, loggingMiddleware, makeRequestSchema, processDeep, retryMiddleware };
package/dist/index.d.ts CHANGED
@@ -37,6 +37,56 @@ type EncryptionConfig<TReq, TRes> = {
37
37
  */
38
38
  type RequestSchema = z.ZodTypeAny;
39
39
  type ResponseSchema = z.ZodTypeAny;
40
+ /**
41
+ * EndpointTestContext
42
+ * ===================
43
+ * Shared runtime state used by the API test runner.
44
+ * It allows one endpoint test to store values that later tests can reuse.
45
+ */
46
+ type EndpointTestContext = {
47
+ data: Record<string, unknown>;
48
+ get<T = unknown>(key: string): T | undefined;
49
+ set<T = unknown>(key: string, value: T): void;
50
+ has(key: string): boolean;
51
+ };
52
+ type EndpointTestInputFactory<TReq> = (ctx: EndpointTestContext) => TReq | Promise<TReq>;
53
+ type EndpointTestAssertion<TReq, TRes> = (result: {
54
+ input: TReq;
55
+ response: TRes;
56
+ ctx: EndpointTestContext;
57
+ }) => void | Promise<void>;
58
+ type EndpointTestCase<TReq, TRes> = {
59
+ /** Human-readable name shown in the generated report. */
60
+ name?: string;
61
+ /** Static input or a factory that can read/write shared test context. */
62
+ input?: TReq | EndpointTestInputFactory<TReq>;
63
+ /** Skip this specific case. A string is used as the skip reason. */
64
+ skip?: boolean | string;
65
+ /** Expected HTTP status. Defaults to any successful client response. */
66
+ expectStatus?: number | number[];
67
+ /** Optional user-defined assertion after a successful response. */
68
+ expect?: EndpointTestAssertion<TReq, TRes>;
69
+ /** Per-case timeout in milliseconds. */
70
+ timeout?: number;
71
+ /** Tags used by the runner for include/exclude filtering. */
72
+ tags?: string[];
73
+ };
74
+ type EndpointTestConfig<TReq, TRes> = {
75
+ /** Disable all generated/manual tests for this endpoint. */
76
+ enabled?: boolean;
77
+ /** Tags used by the runner for include/exclude filtering. */
78
+ tags?: string[];
79
+ /** Mark endpoints such as DELETE/reset/payment as unsafe for default runs. */
80
+ destructive?: boolean;
81
+ /** Default input used when cases are not provided. */
82
+ input?: TReq | EndpointTestInputFactory<TReq>;
83
+ /** One or more test cases for this endpoint. */
84
+ cases?: Array<EndpointTestCase<TReq, TRes>>;
85
+ /** Runs before the endpoint cases. */
86
+ setup?: (ctx: EndpointTestContext) => void | Promise<void>;
87
+ /** Runs after the endpoint cases. */
88
+ teardown?: (ctx: EndpointTestContext) => void | Promise<void>;
89
+ };
40
90
  /**
41
91
  * EndpointDef
42
92
  * ============
@@ -81,6 +131,10 @@ type EndpointDef<TReq extends RequestSchema, TRes extends ResponseSchema> = {
81
131
  * - `"form-data"`: multipart form
82
132
  */
83
133
  bodyType?: "json" | "form-data";
134
+ /**
135
+ * Optional contract-driven tests used by the TypeFetch test runner.
136
+ */
137
+ test?: EndpointTestConfig<z.infer<TReq>, z.infer<TRes>>;
84
138
  };
85
139
  /**
86
140
  * Contracts
@@ -406,4 +460,167 @@ declare const makeRequestSchema: <TPath extends z.ZodRawShape = {}, TQuery exten
406
460
  headers: THeaders;
407
461
  }, z.core.$strip>;
408
462
 
409
- export { ApiClient, type AuthOptions, type CacheOptions, type Contracts, type DeepEncryptionMap, type EncryptionConfig, type EncryptionMethod, type EncryptionOptions, type EndpointDef, type EndpointDefZ, type EndpointMethods, type ErrorLike, type LoggingOptions, type Method, type Middleware, type MiddlewareContext, type MiddlewareNext, type RequestOptions, type RequestParts, type RequestSchema, type ResponseSchema, type RetryOptions, RichError, type TokenProvider, authMiddleware, cacheMiddleware, encryptionMiddleware, loggingMiddleware, makeRequestSchema, processDeep, retryMiddleware };
463
+ declare class TypeFetchTestContext implements EndpointTestContext {
464
+ data: Record<string, unknown>;
465
+ constructor(initialData?: Record<string, unknown>);
466
+ get<T = unknown>(key: string): T | undefined;
467
+ set<T = unknown>(key: string, value: T): void;
468
+ has(key: string): boolean;
469
+ }
470
+
471
+ type ApiTestMode = "schema" | "mock" | "live" | "full";
472
+ type ApiTestStatus = "passed" | "failed" | "skipped";
473
+ type ApiTestReportFormat = "json" | "markdown" | "html";
474
+ type AutoInputValue = unknown | ((path: string) => unknown);
475
+ type AutoInputOptions = {
476
+ /** Maximum recursive generation depth for nested schemas. */
477
+ maxDepth?: number;
478
+ /** Prefer optional object fields when generating inputs. Default: true. */
479
+ includeOptional?: boolean;
480
+ /** Generate one item for arrays by default. Default: true. */
481
+ includeArrayItems?: boolean;
482
+ /** Overrides by full dot path, field name, or request part path. */
483
+ values?: Record<string, AutoInputValue>;
484
+ /** Custom fallback for files. Defaults to Blob when available, otherwise string. */
485
+ fileFactory?: () => unknown;
486
+ };
487
+ type ApiTestRunnerOptions = {
488
+ mode?: ApiTestMode;
489
+ timeout?: number;
490
+ concurrency?: number;
491
+ stopOnFail?: boolean;
492
+ includeDestructive?: boolean;
493
+ includeTags?: string[];
494
+ excludeTags?: string[];
495
+ autoInput?: AutoInputOptions;
496
+ requestOptions?: RequestOptions;
497
+ };
498
+ type ApiTestRunnerConfig<C extends Contracts = Contracts> = {
499
+ contracts: C;
500
+ /** Initialized ApiClient instance or compatible object with modules. */
501
+ client: {
502
+ modules: Record<string, Record<string, (input: any, options?: RequestOptions) => Promise<any>>>;
503
+ };
504
+ options?: ApiTestRunnerOptions;
505
+ context?: Record<string, unknown>;
506
+ };
507
+ type ApiTestCaseMeta = {
508
+ module: string;
509
+ endpoint: string;
510
+ caseName: string;
511
+ phase: ApiTestMode | "schema" | "mock" | "live";
512
+ method: Method;
513
+ path: string;
514
+ tags: string[];
515
+ destructive: boolean;
516
+ };
517
+ type ApiTestError = {
518
+ name?: string;
519
+ message: string;
520
+ status?: number;
521
+ code?: string;
522
+ issues?: unknown;
523
+ stack?: string;
524
+ };
525
+ type ApiTestResult = ApiTestCaseMeta & {
526
+ status: ApiTestStatus;
527
+ durationMs: number;
528
+ input?: unknown;
529
+ response?: unknown;
530
+ error?: ApiTestError;
531
+ skipReason?: string;
532
+ };
533
+ type ApiTestReportSummary = {
534
+ total: number;
535
+ passed: number;
536
+ failed: number;
537
+ skipped: number;
538
+ durationMs: number;
539
+ };
540
+ type ApiTestReport = {
541
+ generatedAt: string;
542
+ mode: ApiTestMode;
543
+ summary: ApiTestReportSummary;
544
+ results: ApiTestResult[];
545
+ };
546
+
547
+ declare function generateInput(schema: z.ZodTypeAny, options?: AutoInputOptions): unknown;
548
+
549
+ declare function createMarkdownReport(report: ApiTestReport): string;
550
+ declare function createHtmlReport(report: ApiTestReport): string;
551
+
552
+ declare function createApiTestRunner<C extends Contracts>(config: ApiTestRunnerConfig<C>): ApiTestRunner<C>;
553
+ declare class ApiTestRunner<C extends Contracts> {
554
+ private readonly config;
555
+ private readonly ctx;
556
+ private readonly options;
557
+ constructor(config: ApiTestRunnerConfig<C>);
558
+ run(): Promise<ApiTestReport>;
559
+ private discoverEndpoints;
560
+ private runEndpoint;
561
+ private getEndpointSkipReason;
562
+ private getCases;
563
+ private getPhases;
564
+ private runCasePhase;
565
+ private callClient;
566
+ }
567
+
568
+ type TypeFetchCliCommand = "test" | "list" | "init" | "release-doc" | "help" | "version";
569
+ type ParsedCliArgs = {
570
+ command: TypeFetchCliCommand;
571
+ flags: Record<string, string | boolean | string[]>;
572
+ positionals: string[];
573
+ raw: string[];
574
+ };
575
+ type TypeFetchClientLike = {
576
+ init?: () => void;
577
+ modules: Record<string, Record<string, (input: any, options?: RequestOptions) => Promise<any>>>;
578
+ };
579
+ type TypeFetchCreateClientOptions = {
580
+ baseUrl?: string;
581
+ token?: string;
582
+ };
583
+ type TypeFetchReportConfig = {
584
+ /** Path base or full file path. Examples: ./typefetch-report/report or ./typefetch-report/report.md */
585
+ output?: string;
586
+ formats?: ApiTestReportFormat[];
587
+ };
588
+ type TypeFetchCliTestConfig<C extends Contracts = Contracts> = {
589
+ contracts: C;
590
+ /** Use client for simple projects. */
591
+ client?: TypeFetchClientLike;
592
+ /** Prefer createClient when you want CLI flags like --base-url and --token to work. */
593
+ createClient?: (options: TypeFetchCreateClientOptions) => TypeFetchClientLike | Promise<TypeFetchClientLike>;
594
+ options?: ApiTestRunnerOptions;
595
+ context?: Record<string, unknown>;
596
+ report?: TypeFetchReportConfig;
597
+ };
598
+ type CliResolvedOptions = {
599
+ mode?: ApiTestMode;
600
+ baseUrl?: string;
601
+ token?: string;
602
+ timeout?: number;
603
+ includeTags?: string[];
604
+ excludeTags?: string[];
605
+ includeDestructive?: boolean;
606
+ stopOnFail?: boolean;
607
+ output?: string;
608
+ formats?: ApiTestReportFormat[];
609
+ config?: string;
610
+ };
611
+ type InitCommandOptions = {
612
+ force?: boolean;
613
+ packageName?: string;
614
+ contractsPath?: string;
615
+ output?: string;
616
+ };
617
+ type ReleaseDocCommandOptions = {
618
+ force?: boolean;
619
+ version?: string;
620
+ outputDir?: string;
621
+ title?: string;
622
+ };
623
+
624
+ declare function defineTypeFetchTestConfig<C extends Contracts>(config: TypeFetchCliTestConfig<C>): TypeFetchCliTestConfig<C>;
625
+
626
+ export { ApiClient, type ApiTestCaseMeta, type ApiTestError, type ApiTestMode, type ApiTestReport, type ApiTestReportFormat, type ApiTestReportSummary, type ApiTestResult, ApiTestRunner, type ApiTestRunnerConfig, type ApiTestRunnerOptions, type ApiTestStatus, type AuthOptions, type AutoInputOptions, type CacheOptions, type CliResolvedOptions, type Contracts, type DeepEncryptionMap, type EncryptionConfig, type EncryptionMethod, type EncryptionOptions, type EndpointDef, type EndpointDefZ, type EndpointMethods, type EndpointTestAssertion, type EndpointTestCase, type EndpointTestConfig, type EndpointTestContext, type EndpointTestInputFactory, type ErrorLike, type InitCommandOptions, type LoggingOptions, type Method, type Middleware, type MiddlewareContext, type MiddlewareNext, type ParsedCliArgs, type ReleaseDocCommandOptions, type RequestOptions, type RequestParts, type RequestSchema, type ResponseSchema, type RetryOptions, RichError, type TokenProvider, type TypeFetchCliCommand, type TypeFetchCliTestConfig, type TypeFetchClientLike, type TypeFetchCreateClientOptions, type TypeFetchReportConfig, TypeFetchTestContext, authMiddleware, cacheMiddleware, createApiTestRunner, createHtmlReport, createMarkdownReport, defineTypeFetchTestConfig, encryptionMiddleware, generateInput, loggingMiddleware, makeRequestSchema, processDeep, retryMiddleware };