hanzio 1.0.9 → 1.1.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/api-wrapper/actions-impl.d.ts +4 -0
- package/dist/api-wrapper/builders.d.ts +12 -0
- package/dist/api-wrapper/create-client.d.ts +6 -0
- package/dist/api-wrapper/error-reporting.d.ts +7 -0
- package/dist/api-wrapper/errors.d.ts +63 -0
- package/dist/api-wrapper/headers.d.ts +9 -0
- package/dist/api-wrapper/index.d.ts +5 -84
- package/dist/api-wrapper/redaction.d.ts +2 -0
- package/dist/api-wrapper/shared.d.ts +16 -0
- package/dist/api-wrapper/size.d.ts +1 -0
- package/dist/api-wrapper/test-helpers.d.ts +6 -0
- package/dist/api-wrapper/transport.d.ts +52 -0
- package/dist/api-wrapper/types.d.ts +153 -0
- package/dist/api-wrapper/zod-issues.d.ts +17 -0
- package/dist/secrets/index.d.ts +10 -8
- package/dist/secrets/infisical.d.ts +19 -2
- package/dist/secrets/loaders.d.ts +16 -0
- package/dist/src/api-wrapper/index.js +1018 -166
- package/dist/src/index.js +1155 -232
- package/dist/src/secrets/index.js +1138 -230
- package/dist/src/secrets/infisical.js +1028 -173
- package/dist/src/secrets/loaders.js +1236 -0
- package/package.json +2 -2
- package/src/api-wrapper/actions-impl.ts +100 -0
- package/src/api-wrapper/builders.ts +175 -0
- package/src/api-wrapper/create-client.ts +373 -0
- package/src/api-wrapper/error-reporting.ts +31 -0
- package/src/api-wrapper/errors.ts +221 -0
- package/src/api-wrapper/headers.ts +66 -0
- package/src/api-wrapper/index.ts +5 -492
- package/src/api-wrapper/redaction.ts +61 -0
- package/src/api-wrapper/shared.ts +25 -0
- package/src/api-wrapper/size.ts +19 -0
- package/src/api-wrapper/test-helpers.ts +25 -0
- package/src/api-wrapper/transport.ts +354 -0
- package/src/api-wrapper/types.ts +258 -0
- package/src/api-wrapper/zod-issues.ts +157 -0
- package/src/secrets/index.ts +125 -81
- package/src/secrets/infisical.ts +50 -14
- package/src/secrets/loaders.ts +67 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ConfigError } from './errors';
|
|
2
|
+
import type { ApiErrorContext, BaseApiUrl, PathParams, QueryParams } from './shared';
|
|
3
|
+
import type { ApiEndpoint } from './types';
|
|
4
|
+
export declare function buildUrl(endpoint: ApiEndpoint, params: PathParams | undefined, reqQuery: QueryParams | undefined, baseApiUrls: Record<string, BaseApiUrl>, defaultBaseApiUrl: string | undefined, url: string | undefined, configError: (msg: string) => ConfigError): {
|
|
5
|
+
finalUrl: string;
|
|
6
|
+
fullUrl: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function buildQueryParamsRaw(endpoint: ApiEndpoint, query: QueryParams | undefined): Record<string, string>;
|
|
9
|
+
export declare function buildQueryParams(endpoint: ApiEndpoint, query: QueryParams | undefined, ctx: (over?: Partial<ApiErrorContext>) => ApiErrorContext): Record<string, string>;
|
|
10
|
+
export declare function buildRequestBody(endpoint: ApiEndpoint, body: unknown, ctx: (over?: Partial<ApiErrorContext>) => ApiErrorContext): unknown;
|
|
11
|
+
export declare function buildHeaders(endpoint: ApiEndpoint, headers: Record<string, string> | undefined, defaultHeaders: Record<string, string>, ctx: (over?: Partial<ApiErrorContext>) => ApiErrorContext): Record<string, string>;
|
|
12
|
+
export declare function replacePathParams(path: string, params: PathParams | undefined, configError: (msg: string) => ConfigError): string;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ApiAction, ApiClient, ApiClientConfigNoActions, ApiClientConfigWithActions, ApiEndpoint } from './types';
|
|
2
|
+
export interface CreateApiClientFn {
|
|
3
|
+
<T extends Record<string, ApiEndpoint>>(apiConfig: ApiClientConfigNoActions<T>): ApiClient<T, Record<string, never>>;
|
|
4
|
+
<T extends Record<string, ApiEndpoint>, A extends Record<string, ApiAction>>(apiConfig: ApiClientConfigWithActions<T, A>): ApiClient<T, A>;
|
|
5
|
+
}
|
|
6
|
+
export declare const createApiClient: CreateApiClientFn;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ApiError } from './errors';
|
|
2
|
+
import type { ApiClientConfig } from './types';
|
|
3
|
+
import type { ApiErrorContext } from './shared';
|
|
4
|
+
type ErrorHookConfig = Pick<ApiClientConfig<never, never>, 'logger' | 'onError' | 'redact'>;
|
|
5
|
+
export declare function applyErrorContextRedaction(ctx: ApiErrorContext, config: Pick<ApiClientConfig<never, never>, 'redact'>): ApiErrorContext;
|
|
6
|
+
export declare function reportApiError(err: ApiError, config: ErrorHookConfig): Promise<ApiError>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { ZodError } from 'zod';
|
|
2
|
+
import type { ApiErrorContext } from './shared';
|
|
3
|
+
import { type FormattedZodIssue } from './zod-issues';
|
|
4
|
+
export declare class ApiError extends Error {
|
|
5
|
+
context: ApiErrorContext;
|
|
6
|
+
cause?: unknown;
|
|
7
|
+
constructor(message: string, context: ApiErrorContext, options?: {
|
|
8
|
+
cause?: unknown;
|
|
9
|
+
});
|
|
10
|
+
toJSON(): Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
export declare class HttpResponseError extends ApiError {
|
|
13
|
+
readonly status: number;
|
|
14
|
+
readonly body: string;
|
|
15
|
+
readonly bodyJson?: unknown;
|
|
16
|
+
readonly responseHeaders: Record<string, string>;
|
|
17
|
+
readonly requestId?: string;
|
|
18
|
+
constructor(status: number, body: string, responseHeaders: Record<string, string>, context: ApiErrorContext);
|
|
19
|
+
toJSON(): Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
export declare class ResponseValidationError extends ApiError {
|
|
22
|
+
readonly zodError: ZodError;
|
|
23
|
+
readonly rawResponse: unknown;
|
|
24
|
+
readonly issues: FormattedZodIssue[];
|
|
25
|
+
readonly validationIssues: string;
|
|
26
|
+
constructor(zodError: ZodError, rawResponse: unknown, context: ApiErrorContext);
|
|
27
|
+
toJSON(): Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
export type RequestValidationTarget = 'body' | 'query' | 'params' | 'headers';
|
|
30
|
+
export declare class RequestValidationError extends ApiError {
|
|
31
|
+
readonly zodError: ZodError;
|
|
32
|
+
readonly target: RequestValidationTarget;
|
|
33
|
+
readonly rawInput: unknown;
|
|
34
|
+
readonly issues: FormattedZodIssue[];
|
|
35
|
+
readonly validationIssues: string;
|
|
36
|
+
constructor(zodError: ZodError, target: RequestValidationTarget, rawInput: unknown, context: ApiErrorContext);
|
|
37
|
+
toJSON(): Record<string, unknown>;
|
|
38
|
+
}
|
|
39
|
+
export declare class RequestTimeoutError extends ApiError {
|
|
40
|
+
readonly timeoutMs: number;
|
|
41
|
+
constructor(timeoutMs: number, context: ApiErrorContext);
|
|
42
|
+
}
|
|
43
|
+
export declare class NetworkError extends ApiError {
|
|
44
|
+
constructor(message: string, context: ApiErrorContext, cause?: unknown);
|
|
45
|
+
}
|
|
46
|
+
export declare class RequestAbortedError extends ApiError {
|
|
47
|
+
constructor(context: ApiErrorContext, cause?: unknown);
|
|
48
|
+
}
|
|
49
|
+
export declare class ConfigError extends ApiError {
|
|
50
|
+
constructor(message: string, context: ApiErrorContext);
|
|
51
|
+
}
|
|
52
|
+
export declare class ActionError extends ApiError {
|
|
53
|
+
constructor(message: string, context: ApiErrorContext, cause?: unknown);
|
|
54
|
+
}
|
|
55
|
+
export declare const isApiError: (e: unknown) => e is ApiError;
|
|
56
|
+
export declare const isHttpResponseError: (e: unknown) => e is HttpResponseError;
|
|
57
|
+
export declare const isResponseValidationError: (e: unknown) => e is ResponseValidationError;
|
|
58
|
+
export declare const isRequestValidationError: (e: unknown) => e is RequestValidationError;
|
|
59
|
+
export declare const isRequestTimeoutError: (e: unknown) => e is RequestTimeoutError;
|
|
60
|
+
export declare const isNetworkError: (e: unknown) => e is NetworkError;
|
|
61
|
+
export declare const isRequestAbortedError: (e: unknown) => e is RequestAbortedError;
|
|
62
|
+
export declare const isConfigError: (e: unknown) => e is ConfigError;
|
|
63
|
+
export declare const isActionError: (e: unknown) => e is ActionError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize headers to canonical case so lookups are deterministic. Later
|
|
3
|
+
* occurrences of the same header (case-insensitive) win.
|
|
4
|
+
*/
|
|
5
|
+
export declare function normalizeHeaders(headers: Record<string, string>): Record<string, string>;
|
|
6
|
+
export declare function getHeader(headers: Record<string, string>, name: string): string | undefined;
|
|
7
|
+
export declare function hasJsonContentType(headers: Record<string, string>): boolean;
|
|
8
|
+
export declare function buildFetchBody(body: unknown, headers: Record<string, string>): RequestInit['body'];
|
|
9
|
+
export declare function getHeadersAsObject(headers: Headers): Record<string, string>;
|
|
@@ -1,84 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export type QueryParams = Record<string, string | number | boolean | undefined>;
|
|
7
|
-
export declare class ResponseValidationError extends Error {
|
|
8
|
-
readonly zodError: ZodError;
|
|
9
|
-
readonly rawResponse: unknown;
|
|
10
|
-
readonly validationIssues: string;
|
|
11
|
-
constructor(zodError: ZodError, rawResponse: unknown);
|
|
12
|
-
}
|
|
13
|
-
export declare class HttpResponseError extends Error {
|
|
14
|
-
readonly status: number;
|
|
15
|
-
readonly body: string;
|
|
16
|
-
constructor(status: number, body: string);
|
|
17
|
-
}
|
|
18
|
-
export interface ApiEndpoint<TReqBody extends z.ZodType | undefined = z.ZodType | undefined, TReqParams extends z.ZodType | undefined = z.ZodType | undefined, TReqQuery extends z.ZodType | undefined = z.ZodType | undefined, TReqHeaders extends z.ZodType | undefined = z.ZodType | undefined, TResponse extends z.ZodType = z.ZodType> {
|
|
19
|
-
method: HttpMethod;
|
|
20
|
-
path: string | ((baseUrl: string) => string);
|
|
21
|
-
reqBodySchema?: TReqBody;
|
|
22
|
-
reqParamsSchema?: TReqParams;
|
|
23
|
-
reqQuerySchema?: TReqQuery;
|
|
24
|
-
reqHeadersSchema?: TReqHeaders;
|
|
25
|
-
resSchema: TResponse;
|
|
26
|
-
baseApiUrl?: string;
|
|
27
|
-
reqBodyFormat?: RequestBodyFormat;
|
|
28
|
-
defaultHeaders?: Record<string, string>;
|
|
29
|
-
resFormatter?: (data: unknown, headers: Record<string, string>) => z.infer<TResponse>;
|
|
30
|
-
reqDefaultQueryParams?: QueryParams;
|
|
31
|
-
doNotEncodeQueryParams?: boolean;
|
|
32
|
-
}
|
|
33
|
-
export interface ApiWrapperResponse<T> {
|
|
34
|
-
data: T;
|
|
35
|
-
requestSizeMb: number;
|
|
36
|
-
responseSizeMb: number;
|
|
37
|
-
requestSizeTotalMb: number;
|
|
38
|
-
responseTimeMs: number;
|
|
39
|
-
httpStatus: number;
|
|
40
|
-
retryCount: number;
|
|
41
|
-
}
|
|
42
|
-
export type RetryContext = {
|
|
43
|
-
error?: unknown;
|
|
44
|
-
response?: Response;
|
|
45
|
-
retryCount: number;
|
|
46
|
-
maxRetries: number;
|
|
47
|
-
};
|
|
48
|
-
export interface ApiClientConfig<T extends Record<string, ApiEndpoint>> {
|
|
49
|
-
name?: string;
|
|
50
|
-
baseApiUrls: Record<string, BaseApiUrl>;
|
|
51
|
-
defaultBaseApiUrl?: string;
|
|
52
|
-
endpoints: T;
|
|
53
|
-
defaultHeaders?: Record<string, string>;
|
|
54
|
-
timeoutMs?: number;
|
|
55
|
-
retries?: number;
|
|
56
|
-
retryDelayMs?: number;
|
|
57
|
-
shouldRetry?: (context: RetryContext) => boolean;
|
|
58
|
-
logger?: Pick<Console, 'debug' | 'error'>;
|
|
59
|
-
fetch?: typeof fetch;
|
|
60
|
-
onRequestStart?: (context: {
|
|
61
|
-
endpoint: string;
|
|
62
|
-
url: string;
|
|
63
|
-
}) => void;
|
|
64
|
-
onRequestEnd?: (context: {
|
|
65
|
-
endpoint: string;
|
|
66
|
-
url: string;
|
|
67
|
-
status: number;
|
|
68
|
-
retryCount: number;
|
|
69
|
-
responseTimeMs: number;
|
|
70
|
-
}) => void;
|
|
71
|
-
}
|
|
72
|
-
type RequestInput<TEndpoint extends ApiEndpoint> = {
|
|
73
|
-
reqBody?: InferOptionalSchema<TEndpoint['reqBodySchema']>;
|
|
74
|
-
reqParams?: InferOptionalSchema<TEndpoint['reqParamsSchema']>;
|
|
75
|
-
reqQuery?: InferOptionalSchema<TEndpoint['reqQuerySchema']>;
|
|
76
|
-
reqHeaders?: InferOptionalSchema<TEndpoint['reqHeadersSchema']>;
|
|
77
|
-
url?: string;
|
|
78
|
-
};
|
|
79
|
-
type InferOptionalSchema<TSchema> = TSchema extends z.ZodType ? z.infer<TSchema> : undefined;
|
|
80
|
-
export declare function createApiClient<T extends Record<string, ApiEndpoint>>(apiConfig: ApiClientConfig<T>): {
|
|
81
|
-
request: <K extends keyof T>(endpointKey: K, input?: RequestInput<T[K]>) => Promise<ApiWrapperResponse<z.TypeOf<T[K]["resSchema"]>>>;
|
|
82
|
-
};
|
|
83
|
-
export declare const createApiWrapper: typeof createApiClient;
|
|
84
|
-
export {};
|
|
1
|
+
export type { FormattedZodIssue } from './zod-issues';
|
|
2
|
+
export * from './types';
|
|
3
|
+
export * from './errors';
|
|
4
|
+
export { createApiClient } from './create-client';
|
|
5
|
+
export { defineAction } from './actions-impl';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
|
2
|
+
export type RequestBodyFormat = 'json' | 'form-data';
|
|
3
|
+
export type BaseApiUrl = string | (() => string);
|
|
4
|
+
export type PathParams = Record<string, string | number>;
|
|
5
|
+
export type QueryParams = Record<string, string | number | boolean | undefined>;
|
|
6
|
+
export type ApiErrorContext = {
|
|
7
|
+
endpoint: string;
|
|
8
|
+
method: HttpMethod;
|
|
9
|
+
url: string;
|
|
10
|
+
attempt: number;
|
|
11
|
+
maxRetries: number;
|
|
12
|
+
elapsedMs: number;
|
|
13
|
+
requestHeaders?: Record<string, string>;
|
|
14
|
+
requestBody?: unknown;
|
|
15
|
+
meta?: Record<string, unknown>;
|
|
16
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function calculateSizeInMb(data: unknown): number;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const originalFetch: typeof fetch;
|
|
2
|
+
export declare const createMockResponse: (body: unknown, options?: {
|
|
3
|
+
status?: number | undefined;
|
|
4
|
+
contentType?: string | undefined;
|
|
5
|
+
}) => Response;
|
|
6
|
+
export declare const mockFetch: (fn: (url: string, options?: RequestInit | undefined) => Promise<Response>) => void;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type z } from 'zod';
|
|
2
|
+
import type { ApiEndpoint, OnResponseContext, OnRetryContext, RetryContext } from './types';
|
|
3
|
+
import type { ApiErrorContext, HttpMethod } from './shared';
|
|
4
|
+
export type RequestConfig = {
|
|
5
|
+
method: HttpMethod;
|
|
6
|
+
url: string;
|
|
7
|
+
queryParams: Record<string, string>;
|
|
8
|
+
body: unknown;
|
|
9
|
+
headers: Record<string, string>;
|
|
10
|
+
doNotEncodeQueryParams?: boolean;
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
fetchFn: typeof fetch;
|
|
13
|
+
userSignal?: AbortSignal;
|
|
14
|
+
};
|
|
15
|
+
export declare function validateResponse<T extends z.ZodType>(schema: T, data: unknown, ctx: (over?: Partial<ApiErrorContext>) => ApiErrorContext): Promise<z.infer<T>>;
|
|
16
|
+
export declare function httpRequest(config: RequestConfig, ctx: (over?: Partial<ApiErrorContext>) => ApiErrorContext): Promise<Response>;
|
|
17
|
+
export declare function mergeSignals(a: AbortSignal, b: AbortSignal | undefined): AbortSignal;
|
|
18
|
+
export declare function readErrorResponse(response: Response): Promise<{
|
|
19
|
+
body: string;
|
|
20
|
+
headers: Record<string, string>;
|
|
21
|
+
status: number;
|
|
22
|
+
}>;
|
|
23
|
+
export declare function getRetryDelay(params: {
|
|
24
|
+
retryDelay: number | ((attempt: number) => number);
|
|
25
|
+
attempt: number;
|
|
26
|
+
response?: Response;
|
|
27
|
+
}): number;
|
|
28
|
+
export declare function shouldAttemptRetryOnError(error: unknown): boolean;
|
|
29
|
+
export declare function shouldAttemptRetryOnHttp(retryCtx: RetryContext): boolean;
|
|
30
|
+
type MakeRequestArgs<TValidated> = {
|
|
31
|
+
config: RequestConfig;
|
|
32
|
+
maxRetries: number;
|
|
33
|
+
retryDelay: number | ((attempt: number) => number);
|
|
34
|
+
validateFn: (response: Response) => Promise<TValidated>;
|
|
35
|
+
shouldRetry: (context: RetryContext) => boolean;
|
|
36
|
+
logger?: Pick<Console, 'debug'>;
|
|
37
|
+
ctx: (over?: Partial<ApiErrorContext>) => ApiErrorContext;
|
|
38
|
+
endpointName: string;
|
|
39
|
+
method: HttpMethod;
|
|
40
|
+
fullUrl: string;
|
|
41
|
+
meta?: Record<string, unknown>;
|
|
42
|
+
onResponse?: (context: OnResponseContext) => void | Promise<void>;
|
|
43
|
+
onRetry?: (context: OnRetryContext) => void | Promise<void>;
|
|
44
|
+
};
|
|
45
|
+
export declare function makeRequestWithRetry<TValidated>(args: MakeRequestArgs<TValidated>): Promise<{
|
|
46
|
+
validatedData: TValidated;
|
|
47
|
+
retryCount: number;
|
|
48
|
+
httpStatus: number;
|
|
49
|
+
}>;
|
|
50
|
+
export declare function validateAndTransformResponse<T extends z.ZodType>(endpoint: ApiEndpoint<z.ZodType | undefined, z.ZodType | undefined, z.ZodType | undefined, z.ZodType | undefined, T>, response: Response, ctx: (over?: Partial<ApiErrorContext>) => ApiErrorContext): Promise<z.infer<T>>;
|
|
51
|
+
export declare function defaultShouldRetry(ctx: RetryContext): boolean;
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { ApiError } from './errors';
|
|
3
|
+
import type { ApiErrorContext, BaseApiUrl, HttpMethod, PathParams, QueryParams, RequestBodyFormat } from './shared';
|
|
4
|
+
export interface ApiEndpoint<TReqBody extends z.ZodType | undefined = z.ZodType | undefined, TReqParams extends z.ZodType | undefined = z.ZodType | undefined, TReqQuery extends z.ZodType | undefined = z.ZodType | undefined, TReqHeaders extends z.ZodType | undefined = z.ZodType | undefined, TResponse extends z.ZodType = z.ZodType> {
|
|
5
|
+
method: HttpMethod;
|
|
6
|
+
path: string | ((baseUrl: string) => string);
|
|
7
|
+
reqBodySchema?: TReqBody;
|
|
8
|
+
reqParamsSchema?: TReqParams;
|
|
9
|
+
reqQuerySchema?: TReqQuery;
|
|
10
|
+
reqHeadersSchema?: TReqHeaders;
|
|
11
|
+
resSchema: TResponse;
|
|
12
|
+
baseApiUrl?: string;
|
|
13
|
+
reqBodyFormat?: RequestBodyFormat;
|
|
14
|
+
defaultHeaders?: Record<string, string>;
|
|
15
|
+
resFormatter?: (data: unknown, headers: Record<string, string>) => z.infer<TResponse>;
|
|
16
|
+
reqDefaultQueryParams?: QueryParams;
|
|
17
|
+
doNotEncodeQueryParams?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface ApiWrapperResponse<T> {
|
|
20
|
+
data: T;
|
|
21
|
+
requestBodySizeMb: number;
|
|
22
|
+
responseSizeMb: number;
|
|
23
|
+
responseTimeMs: number;
|
|
24
|
+
httpStatus: number;
|
|
25
|
+
retryCount: number;
|
|
26
|
+
}
|
|
27
|
+
export type RetryContext = {
|
|
28
|
+
error?: unknown;
|
|
29
|
+
response?: Response;
|
|
30
|
+
retryCount: number;
|
|
31
|
+
maxRetries: number;
|
|
32
|
+
endpoint?: string;
|
|
33
|
+
method?: HttpMethod;
|
|
34
|
+
url?: string;
|
|
35
|
+
};
|
|
36
|
+
export type OnRetryContext = RetryContext & {
|
|
37
|
+
delayMs: number;
|
|
38
|
+
nextAttempt: number;
|
|
39
|
+
};
|
|
40
|
+
export type RequestInterceptorResult = {
|
|
41
|
+
headers?: Record<string, string>;
|
|
42
|
+
body?: unknown;
|
|
43
|
+
} | void | undefined;
|
|
44
|
+
export type OnRequestContext = {
|
|
45
|
+
endpoint: string;
|
|
46
|
+
method: HttpMethod;
|
|
47
|
+
url: string;
|
|
48
|
+
headers: Record<string, string>;
|
|
49
|
+
body: unknown;
|
|
50
|
+
meta?: Record<string, unknown>;
|
|
51
|
+
};
|
|
52
|
+
export type OnResponseContext = {
|
|
53
|
+
endpoint: string;
|
|
54
|
+
method: HttpMethod;
|
|
55
|
+
url: string;
|
|
56
|
+
response: Response;
|
|
57
|
+
attempt: number;
|
|
58
|
+
meta?: Record<string, unknown>;
|
|
59
|
+
};
|
|
60
|
+
export type ActionCache = {
|
|
61
|
+
<T>(key: string, fn: () => Promise<T> | T, options?: {
|
|
62
|
+
ttlMs?: number;
|
|
63
|
+
}): Promise<T>;
|
|
64
|
+
get<T>(key: string): T | undefined;
|
|
65
|
+
set<T>(key: string, value: T, options?: {
|
|
66
|
+
ttlMs?: number;
|
|
67
|
+
}): void;
|
|
68
|
+
invalidate(key: string): void;
|
|
69
|
+
clear(): void;
|
|
70
|
+
};
|
|
71
|
+
export type ActionInvokeOptions = {
|
|
72
|
+
signal?: AbortSignal;
|
|
73
|
+
meta?: Record<string, unknown>;
|
|
74
|
+
};
|
|
75
|
+
export type ActionContext<TInput, TApi = any> = {
|
|
76
|
+
input: TInput;
|
|
77
|
+
api: TApi;
|
|
78
|
+
signal?: AbortSignal;
|
|
79
|
+
meta?: Record<string, unknown>;
|
|
80
|
+
logger?: Pick<Console, 'debug' | 'error'>;
|
|
81
|
+
cache: ActionCache;
|
|
82
|
+
};
|
|
83
|
+
export interface ApiAction<TInput = any, TOutput = any> {
|
|
84
|
+
handler: (ctx: ActionContext<TInput>) => Promise<TOutput> | TOutput;
|
|
85
|
+
/** When true, the action accepts only optional `{ signal, meta }`; input is always `undefined`. */
|
|
86
|
+
readonly noRuntimeInput?: boolean;
|
|
87
|
+
}
|
|
88
|
+
type ActionDefinition<TInput, TOutput, TApi> = {
|
|
89
|
+
handler: (ctx: ActionContext<TInput, TApi>) => Promise<TOutput> | TOutput;
|
|
90
|
+
};
|
|
91
|
+
export type DefineAction<TApi> = {
|
|
92
|
+
<TOutput>(def: ActionDefinition<undefined, TOutput, TApi>): ApiAction<undefined, TOutput>;
|
|
93
|
+
<TInput>(): <TOutput>(def: ActionDefinition<TInput, TOutput, TApi>) => ApiAction<TInput, TOutput>;
|
|
94
|
+
};
|
|
95
|
+
type ActionInput<A> = A extends ApiAction<infer I, any> ? I : never;
|
|
96
|
+
export type ActionOutput<A> = A extends ApiAction<any, infer O> ? O : never;
|
|
97
|
+
export type ActionArgs<A> = [ActionInput<A>] extends [undefined] ? [options?: ActionInvokeOptions] : [input: ActionInput<A>, options?: ActionInvokeOptions];
|
|
98
|
+
type SchemaInputKeys<TEndpoint extends ApiEndpoint> = (TEndpoint['reqBodySchema'] extends z.ZodType ? 'reqBody' : never) | (TEndpoint['reqParamsSchema'] extends z.ZodType ? 'reqParams' : never) | (TEndpoint['reqQuerySchema'] extends z.ZodType ? 'reqQuery' : never) | (TEndpoint['reqHeadersSchema'] extends z.ZodType ? 'reqHeaders' : never);
|
|
99
|
+
type SchemaInputs<TEndpoint extends ApiEndpoint> = {
|
|
100
|
+
reqBody: TEndpoint['reqBodySchema'] extends z.ZodType ? z.infer<TEndpoint['reqBodySchema']> : unknown;
|
|
101
|
+
reqParams: TEndpoint['reqParamsSchema'] extends z.ZodType ? z.infer<TEndpoint['reqParamsSchema']> : PathParams;
|
|
102
|
+
reqQuery: TEndpoint['reqQuerySchema'] extends z.ZodType ? z.infer<TEndpoint['reqQuerySchema']> : QueryParams;
|
|
103
|
+
reqHeaders: TEndpoint['reqHeadersSchema'] extends z.ZodType ? z.infer<TEndpoint['reqHeadersSchema']> : Record<string, string>;
|
|
104
|
+
};
|
|
105
|
+
type CommonRequestOptions = {
|
|
106
|
+
url?: string;
|
|
107
|
+
signal?: AbortSignal;
|
|
108
|
+
timeoutMs?: number;
|
|
109
|
+
retries?: number;
|
|
110
|
+
fetch?: typeof fetch;
|
|
111
|
+
meta?: Record<string, unknown>;
|
|
112
|
+
};
|
|
113
|
+
export type RequestInput<TEndpoint extends ApiEndpoint> = Pick<SchemaInputs<TEndpoint>, SchemaInputKeys<TEndpoint>> & Partial<Omit<SchemaInputs<TEndpoint>, SchemaInputKeys<TEndpoint>>> & CommonRequestOptions;
|
|
114
|
+
type RequestArgs<TEndpoint extends ApiEndpoint> = SchemaInputKeys<TEndpoint> extends never ? [input?: RequestInput<TEndpoint>] : [input: RequestInput<TEndpoint>];
|
|
115
|
+
export type ApiClient<T extends Record<string, ApiEndpoint>, A extends Record<string, ApiAction> = Record<string, never>> = {
|
|
116
|
+
request: {
|
|
117
|
+
<K extends keyof T>(endpointKey: K, ...args: RequestArgs<T[K]>): Promise<ApiWrapperResponse<z.infer<T[K]['resSchema']>>>;
|
|
118
|
+
<K extends keyof A>(actionKey: K, ...args: ActionArgs<A[K]>): Promise<ActionOutput<A[K]>>;
|
|
119
|
+
};
|
|
120
|
+
cache: ActionCache;
|
|
121
|
+
} & {
|
|
122
|
+
[K in keyof T]: (...args: RequestArgs<T[K]>) => Promise<ApiWrapperResponse<z.infer<T[K]['resSchema']>>>;
|
|
123
|
+
} & {
|
|
124
|
+
[K in keyof A]: (...args: ActionArgs<A[K]>) => Promise<ActionOutput<A[K]>>;
|
|
125
|
+
};
|
|
126
|
+
export type ActionsFactoryHelpers<T extends Record<string, ApiEndpoint>, A extends Record<string, ApiAction> = Record<string, never>> = {
|
|
127
|
+
api: ApiClient<T, A>;
|
|
128
|
+
defineAction: DefineAction<ApiClient<T, A>>;
|
|
129
|
+
};
|
|
130
|
+
export interface ApiClientConfig<T extends Record<string, ApiEndpoint>, A extends Record<string, ApiAction> = Record<string, never>> {
|
|
131
|
+
name?: string;
|
|
132
|
+
baseApiUrls: Record<string, BaseApiUrl>;
|
|
133
|
+
defaultBaseApiUrl?: string;
|
|
134
|
+
endpoints: T;
|
|
135
|
+
defaultHeaders?: Record<string, string>;
|
|
136
|
+
timeoutMs?: number;
|
|
137
|
+
retries?: number;
|
|
138
|
+
retryDelayMs?: number | ((attempt: number) => number);
|
|
139
|
+
shouldRetry?: (context: RetryContext) => boolean;
|
|
140
|
+
logger?: Pick<Console, 'debug' | 'error'>;
|
|
141
|
+
fetch?: typeof fetch;
|
|
142
|
+
onRequest?: (context: OnRequestContext) => RequestInterceptorResult | Promise<RequestInterceptorResult>;
|
|
143
|
+
onResponse?: (context: OnResponseContext) => void | Promise<void>;
|
|
144
|
+
onError?: (error: ApiError) => void | Promise<void>;
|
|
145
|
+
onRetry?: (context: OnRetryContext) => void | Promise<void>;
|
|
146
|
+
redact?: (context: ApiErrorContext) => ApiErrorContext;
|
|
147
|
+
actions?: A | ((helpers: ActionsFactoryHelpers<T, A>) => A);
|
|
148
|
+
}
|
|
149
|
+
export type ApiClientConfigNoActions<T extends Record<string, ApiEndpoint>> = Omit<ApiClientConfig<T, Record<string, never>>, 'actions'>;
|
|
150
|
+
export type ApiClientConfigWithActions<T extends Record<string, ApiEndpoint>, A extends Record<string, ApiAction>> = Omit<ApiClientConfig<T, Record<string, never>>, 'actions'> & {
|
|
151
|
+
actions: A | ((helpers: ActionsFactoryHelpers<T, A>) => A);
|
|
152
|
+
};
|
|
153
|
+
export type { BaseApiUrl, HttpMethod, PathParams, QueryParams, RequestBodyFormat, ApiErrorContext } from './shared';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ZodError } from 'zod';
|
|
2
|
+
export type FormattedZodIssue = {
|
|
3
|
+
path: string;
|
|
4
|
+
pathArray: (string | number)[];
|
|
5
|
+
code: string;
|
|
6
|
+
message: string;
|
|
7
|
+
expected?: string;
|
|
8
|
+
received?: string;
|
|
9
|
+
value?: unknown;
|
|
10
|
+
valuePreview?: string;
|
|
11
|
+
extra?: Record<string, unknown>;
|
|
12
|
+
};
|
|
13
|
+
export declare function formatZodIssues(error: ZodError, rawData: unknown): FormattedZodIssue[];
|
|
14
|
+
export declare function renderIssueSummary(issues: FormattedZodIssue[]): string;
|
|
15
|
+
export declare function previewValue(value: unknown, maxLen?: number): string;
|
|
16
|
+
export declare function makeJsonParseZodError(error: unknown, text: string): ZodError;
|
|
17
|
+
export declare function tryParseJson(body: string): unknown;
|
package/dist/secrets/index.d.ts
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
export type SecretEnvironment = 'dev' | 'staging' | 'prod';
|
|
2
|
-
export type
|
|
2
|
+
export type SecretEnvironmentOption = SecretEnvironment | (() => SecretEnvironment);
|
|
3
|
+
export type InfisicalDefineSecretSetOptions = {
|
|
3
4
|
readonly projectId: string;
|
|
4
5
|
readonly siteUrl?: string;
|
|
5
|
-
readonly environment?:
|
|
6
|
+
readonly environment?: SecretEnvironmentOption;
|
|
6
7
|
readonly clientIdEnvKey?: string;
|
|
7
8
|
readonly clientSecretEnvKey?: string;
|
|
8
|
-
readonly loader?:
|
|
9
|
-
readonly getLocalIp?: () => string;
|
|
9
|
+
readonly loader?: undefined;
|
|
10
10
|
};
|
|
11
|
+
export type CustomLoaderDefineSecretSetOptions<SecretKey extends string> = {
|
|
12
|
+
readonly loader: SecretSetLoader<SecretKey>;
|
|
13
|
+
readonly environment?: SecretEnvironmentOption;
|
|
14
|
+
};
|
|
15
|
+
export type DefineSecretSetOptions<SecretKey extends string> = InfisicalDefineSecretSetOptions | CustomLoaderDefineSecretSetOptions<SecretKey>;
|
|
11
16
|
export type SecretSetLoaderContext<SecretKey extends string> = {
|
|
12
17
|
readonly keys: readonly SecretKey[];
|
|
13
|
-
readonly projectId: string;
|
|
14
18
|
readonly environment: SecretEnvironment;
|
|
15
|
-
readonly siteUrl: string;
|
|
16
|
-
readonly clientIdEnvKey: string;
|
|
17
|
-
readonly clientSecretEnvKey: string;
|
|
18
19
|
};
|
|
19
20
|
export type SecretSetLoader<SecretKey extends string> = (context: SecretSetLoaderContext<SecretKey>) => Promise<Partial<Record<SecretKey, string>>>;
|
|
20
21
|
export type SecretSet<SecretKey extends string> = {
|
|
@@ -23,6 +24,7 @@ export type SecretSet<SecretKey extends string> = {
|
|
|
23
24
|
reload: () => Promise<SecretSet<SecretKey>>;
|
|
24
25
|
};
|
|
25
26
|
export * from './infisical';
|
|
27
|
+
export * from './loaders';
|
|
26
28
|
export * from './vite';
|
|
27
29
|
export declare function getSecretEnvironment(): SecretEnvironment;
|
|
28
30
|
export declare function defineSecretSet<const SecretKeys extends readonly string[]>(keys: SecretKeys, options: DefineSecretSetOptions<SecretKeys[number]>): Promise<SecretSet<SecretKeys[number]>>;
|
|
@@ -1,2 +1,19 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare
|
|
1
|
+
import type { SecretEnvironment } from '.';
|
|
2
|
+
export declare const DEFAULT_INFISICAL_SITE_URL = "https://eu.infisical.com";
|
|
3
|
+
export declare function fetchInfisicalSecrets<SecretKey extends string>({ keys, projectId, environment, siteUrl, clientId, clientSecret }: {
|
|
4
|
+
readonly keys: readonly SecretKey[];
|
|
5
|
+
readonly projectId: string;
|
|
6
|
+
readonly environment: SecretEnvironment;
|
|
7
|
+
readonly siteUrl: string;
|
|
8
|
+
readonly clientId: string;
|
|
9
|
+
readonly clientSecret: string;
|
|
10
|
+
}): Promise<Partial<Record<SecretKey, string>>>;
|
|
11
|
+
export type LoadInfisicalSecretsFromEnvOptions<SecretKey extends string> = {
|
|
12
|
+
readonly keys: readonly SecretKey[];
|
|
13
|
+
readonly projectId: string;
|
|
14
|
+
readonly environment: SecretEnvironment;
|
|
15
|
+
readonly siteUrl: string;
|
|
16
|
+
readonly clientIdEnvKey: string;
|
|
17
|
+
readonly clientSecretEnvKey: string;
|
|
18
|
+
};
|
|
19
|
+
export declare function loadInfisicalSecrets<SecretKey extends string>({ keys, projectId, environment, siteUrl, clientIdEnvKey, clientSecretEnvKey }: LoadInfisicalSecretsFromEnvOptions<SecretKey>): Promise<Partial<Record<SecretKey, string>>>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SecretSetLoader, SecretSetLoaderContext } from './index';
|
|
2
|
+
/**
|
|
3
|
+
* Reads secrets from a Cloudflare Worker `env` bindings object (or any similar
|
|
4
|
+
* `Record`), for Workers where `process.env` is not populated. Pass the same
|
|
5
|
+
* object you get as `env` in your fetch handler, or values you’d read from
|
|
6
|
+
* `import { env } from "cloudflare:workers"` in your Worker bundle — this
|
|
7
|
+
* package does not import that module.
|
|
8
|
+
*/
|
|
9
|
+
export declare function cloudflareWorkerEnvLoader<SecretKey extends string>(workerEnv: Record<string, unknown>): SecretSetLoader<SecretKey>;
|
|
10
|
+
export declare function infisicalLoader<SecretKey extends string>(config: {
|
|
11
|
+
readonly projectId: string;
|
|
12
|
+
readonly clientId: string;
|
|
13
|
+
readonly clientSecret: string;
|
|
14
|
+
readonly siteUrl?: string;
|
|
15
|
+
}): SecretSetLoader<SecretKey>;
|
|
16
|
+
export declare function processEnvLoader<SecretKey extends string>(_ctx: SecretSetLoaderContext<SecretKey>): Promise<Partial<Record<SecretKey, string>>>;
|