@defjs/core 0.0.0-test-003 → 0.0.0-test-005

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/index.d.ts CHANGED
@@ -1,227 +1,293 @@
1
- declare const HTTP_CONTEXT_TOKEN: unique symbol;
2
- export type HttpContextToken<T> = (() => T) & {
3
- readonly [HTTP_CONTEXT_TOKEN]: () => T;
4
- };
5
- export declare function makeHttpContextToken<T>(defaultValue: () => T): HttpContextToken<T>;
6
- export declare function isHttpContextToken(value: unknown): value is HttpContextToken<unknown>;
7
- export type HttpContext = {
8
- set<T>(token: HttpContextToken<T>, value: T): HttpContext;
9
- get<T>(token: HttpContextToken<T>): T;
10
- del(token: HttpContextToken<unknown>): HttpContext;
11
- has(token: HttpContextToken<unknown>): boolean;
12
- keys(): IterableIterator<HttpContextToken<unknown>>;
13
- };
14
- export declare function makeHttpContext(): HttpContext;
15
- export type ValidatorFn<T = unknown> = (value: T) => Error | null | undefined;
16
- export type AsyncValidatorFn<T = unknown> = (value: T) => Promise<Error | null | undefined>;
17
- export declare const required: ValidatorFn;
18
- export declare const requiredTrue: ValidatorFn;
19
- export declare const minLength: (min: number) => ValidatorFn;
20
- export declare const maxLength: (max: number) => ValidatorFn;
21
- export declare const min: (min: number) => ValidatorFn;
22
- export declare const max: (max: number) => ValidatorFn;
23
- export declare const pattern: (pattern: RegExp) => ValidatorFn;
24
- declare const FIELD: unique symbol;
25
- declare enum FieldType {
26
- Json = 1,
27
- Query = 2,
28
- Param = 3,
29
- Header = 4,
30
- Form = 5,
31
- Body = 6,
32
- UrlForm = 7
33
- }
34
- export interface FieldMetadata<T = undefined> {
35
- alias: Map<FieldType, string | undefined>;
36
- validators: ValidatorFn<T>[];
37
- asyncValidator: AsyncValidatorFn<T>[];
38
- }
39
- export type Field<T = undefined> = (() => T) & {
40
- withJson(alias?: string): Field<T>;
41
- withForm(alias?: string): Field<T>;
42
- withQuery(alias?: string): Field<T>;
43
- withParam(alias?: string): Field<T>;
44
- withHeader(alias?: string): Field<T>;
45
- withUrlForm(alias?: string): Field<T>;
46
- withBody(): Field<T>;
47
- withValidators(...fn: ValidatorFn<T>[]): Field<T>;
48
- withAsyncValidators(...fn: AsyncValidatorFn<T>[]): Field<T>;
49
- readonly [FIELD]: FieldMetadata<T>;
50
- };
51
- export declare function field(): Field;
52
- export declare function field<T>(): Field<T | undefined>;
53
- export declare function field<T>(defaultValue: T): Field<T>;
54
- export declare function validatorField(validators: ValidatorFn<any>[], value: unknown): void;
55
- export declare function asyncValidatorField(asyncValidator: AsyncValidatorFn<any>[], value: unknown): Promise<void>;
56
- export declare function isField(value: unknown): value is Field;
57
- export declare function isFieldGroup(value: unknown): value is Record<PropertyKey, Field>;
58
- export type HttpResponseBody = string | ArrayBuffer | Blob | object | null;
59
- export type HttpResponse<R> = {
60
- readonly url: string;
61
- readonly status: number;
62
- readonly statusText: string;
63
- readonly headers: Headers;
64
- readonly body: R | null;
65
- };
66
- export type MakeResponseOptions<R> = {
67
- status?: number;
68
- statusText?: string;
69
- url?: string;
70
- headers?: Headers;
71
- body?: R | null;
72
- };
73
- export type InterceptorFn = (req: HttpRequest, next: HttpHandler) => Promise<HttpResponse<unknown>>;
74
- export type BasicCredential = {
75
- username: string;
76
- password: string;
77
- };
78
- export type BasicAuthInterceptorOptions = {
79
- encode?: (credential: BasicCredential) => string;
80
- };
81
- export declare function basicAuthInterceptor(fn: () => BasicCredential, options?: BasicAuthInterceptorOptions): InterceptorFn;
82
- export type HttpResponseType = "arraybuffer" | "blob" | "json" | "text";
83
- export interface HttpProgressEvent {
84
- /**
85
- * Indicates whether the resources associated with Progress have a computable length.
86
- * Otherwise, the Progress.total attribute will be a meaningless value.
87
- */
88
- readonly lengthComputable: boolean;
89
- /** Indicates the workload of the underlying process. */
90
- readonly loaded: number;
91
- /**
92
- * The total size of the data being processed or transmitted.
93
- * If the Progress.lengthComputable attribute is false, this value is meaningless and should be ignored.
94
- */
95
- readonly total: number;
96
- }
97
- export type HttpProgressFn = (event: HttpProgressEvent) => void;
98
- export type TransformResponseFn<Output = unknown> = (response: HttpResponse<unknown>) => Output;
99
- export interface HttpRequest {
100
- host?: string;
101
- method: string;
102
- endpoint: string;
103
- body?: Blob | ArrayBuffer | FormData | URLSearchParams | object | string | number | boolean | null;
104
- headers?: Headers;
105
- queryParams?: URLSearchParams;
106
- /** default:json */
107
- responseType?: HttpResponseType;
108
- context?: HttpContext;
109
- timeout?: number;
110
- withCredentials?: boolean;
111
- /** when use fetch handler, no upload progress */
112
- uploadProgress?: HttpProgressFn;
113
- downloadProgress?: HttpProgressFn;
114
- /**
115
- * default:body
116
- */
117
- observe?: "body" | "response";
118
- abort?: AbortSignal;
119
- }
120
- export type RequestInputValue<T> = T extends Field<infer V> ? V : T extends {
121
- [K in keyof T]: Field<any>;
122
- } ? {
123
- [K in keyof T]: RequestInputValue<T[K]>;
124
- } : never;
125
- export type DoRequestOptions = {
126
- abort?: AbortSignal;
127
- handler?: HttpHandler;
128
- client?: Client;
129
- timeout?: number;
130
- };
131
- export type ObserveType<Observe, Output> = Observe extends "body" ? Output : Observe extends "response" ? HttpResponse<Output> : never;
132
- export type UseRequestFn<Input, Output, Observe> = {
133
- doRequest: Input extends undefined ? (options?: DoRequestOptions) => Promise<ObserveType<Observe, Output>> : (input: RequestInputValue<Input>, options?: DoRequestOptions) => Promise<ObserveType<Observe, Output>>;
134
- getInitValue: () => Input extends Field<infer V> ? V : Input extends {
135
- [K in keyof Input]: Field<any>;
136
- } ? {
137
- [K in keyof Input]: RequestInputValue<Input[K]>;
138
- } : undefined;
139
- setUploadProgress: (fn: HttpProgressFn) => void;
140
- setDownloadProgress: (fn: HttpProgressFn) => void;
141
- };
142
- export interface DefineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined, Output = unknown, Observe extends "body" | "response" = "body"> {
143
- (): UseRequestFn<Input, Output, Observe>;
144
- withField<I extends Field<any> | Record<PropertyKey, Field<any>>>(value: I): DefineRequest<I, Output, Observe>;
145
- withInterceptors(value: InterceptorFn[]): DefineRequest<Input, Output, Observe>;
146
- withContext(value: HttpContext): DefineRequest<Input, Output, Observe>;
147
- withCredentials(value: boolean): DefineRequest<Input, Output, Observe>;
148
- withValidators(value: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[]): DefineRequest<Input, Output, Observe>;
149
- withTransformResponse<O>(fn: TransformResponseFn<O>): DefineRequest<Input, O, Observe>;
150
- withObserve(value: "body"): DefineRequest<Input, Output, "body">;
151
- withObserve(value: "response"): DefineRequest<Input, Output, "response">;
152
- withResponseType(value: "json"): DefineRequest<Input, Output, Observe>;
153
- withResponseType(value: "text"): DefineRequest<Input, string, Observe>;
154
- withResponseType(value: "blob"): DefineRequest<Input, Blob, Observe>;
155
- withResponseType(value: "arraybuffer"): DefineRequest<Input, ArrayBuffer, Observe>;
156
- }
157
- export declare function defineRequest<Output>(method: string, endpoint: string): DefineRequest<undefined, Output>;
158
- export type HttpHandler = (req: HttpRequest) => Promise<HttpResponse<unknown>>;
159
- export interface FetchConfig {
160
- /** A string indicating how the request will interact with the browser's cache to set request's cache. */
161
- cache?: RequestCache;
162
- /** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
163
- credentials?: RequestCredentials;
164
- /** A boolean to set request's keepalive. */
165
- keepalive?: boolean;
166
- /** A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. */
167
- mode?: RequestMode;
168
- priority?: RequestPriority;
169
- /** A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. */
170
- redirect?: RequestRedirect;
171
- /** A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. */
172
- referrer?: string;
173
- /** A referrer policy to set request's referrerPolicy. */
174
- referrerPolicy?: ReferrerPolicy;
175
- }
176
- export declare const FETCH_CONFIG_KEY: HttpContextToken<FetchConfig>;
177
- export declare function fetchHandler(httpRequest: HttpRequest): Promise<HttpResponse<unknown>>;
178
- export interface ClientOptions {
179
- host: string;
180
- interceptors?: InterceptorFn[];
181
- handler?: HttpHandler;
182
- }
183
- export interface ClientConfig {
184
- host: string;
185
- handler: HttpHandler;
186
- interceptors?: InterceptorFn[];
187
- }
188
- declare const CLIENT: unique symbol;
189
- export type Client = {} & {
190
- readonly [CLIENT]: ClientConfig;
191
- };
192
- export declare function isClient(value: unknown): value is Client;
193
- export declare function createClient(options: ClientOptions): Client;
194
- export declare function cloneClient(client: Client, options: Partial<ClientOptions>): Client;
195
- export declare function getGlobalClient(): Client;
196
- export declare function setGlobalClient(client: Client): void;
197
- export declare function restGlobalClient(): void;
198
- export declare function createGlobalClient(options: ClientOptions): void;
199
- export declare const ERR_ABORTED: Error;
200
- export declare const ERR_TIMEOUT: Error;
201
- export declare const ERR_NETWORK: Error;
202
- export declare const ERR_NOT_FOUND_HANDLER: Error;
203
- export declare const ERR_OBSERVE: Error;
204
- export declare const ERR_NOT_SET_ALIAS: Error;
205
- export declare const ERR_UNSUPPORTED_FIELD_TYPE: Error;
206
- export declare const ERR_INVALID_CLIENT: Error;
207
- export declare const ERR_NOT_FOUND_GLOBAL_CLIENT: Error;
208
- export declare const ERR_INVALID_HTTP_CONTEXT_TOKEN: Error;
209
- export declare const ERR_UNKNOWN: Error;
210
- export declare class HttpErrorResponse extends Error {
211
- readonly status: number;
212
- readonly statusText: string;
213
- readonly url: string | null;
214
- readonly headers: Headers;
215
- readonly ok: boolean;
216
- readonly body: unknown;
217
- constructor(init: {
218
- error?: Error | string | unknown;
219
- status?: number;
220
- statusText?: string;
221
- url?: string;
222
- headers?: Headers;
223
- body?: unknown;
224
- });
225
- }
1
+ export declare type AsyncValidatorFn<T = unknown> = (value: T) => Promise<void>;
2
+
3
+ export declare function basicAuthInterceptor(fn: () => BasicCredential, options?: BasicAuthInterceptorOptions): InterceptorFn;
4
+
5
+ export declare type BasicAuthInterceptorOptions = {
6
+ encode?: (credential: BasicCredential) => string;
7
+ };
8
+
9
+ declare type BasicCredential = {
10
+ username: string;
11
+ password: string;
12
+ };
13
+
14
+ declare const CLIENT: unique symbol;
15
+
16
+ export declare type Client = {
17
+ readonly [CLIENT]: ClientConfig;
18
+ };
19
+
20
+ export declare interface ClientConfig {
21
+ host: string;
22
+ strict: boolean;
23
+ handler: HttpHandler;
24
+ interceptors?: InterceptorFn[];
25
+ }
26
+
27
+ export declare interface ClientOptions {
28
+ host: string;
29
+ /** Default: true */
30
+ strict?: boolean;
31
+ interceptors?: InterceptorFn[];
32
+ handler?: HttpHandler;
33
+ }
34
+
35
+ export declare function cloneClient(client: Client, options: Partial<ClientOptions>): Client;
36
+
37
+ export declare function createClient(options: ClientOptions): Client;
38
+
39
+ export declare function createGlobalClient(options: ClientOptions): void;
40
+
41
+ declare interface DefineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined, Output = unknown, Observe extends 'body' | 'response' = 'body'> {
42
+ (): UseRequestFn<Input, Output, Observe>;
43
+ withField<I extends Field<any> | Record<PropertyKey, Field<any>>>(value: I): DefineRequest<I, Output, Observe>;
44
+ withInterceptors(value: InterceptorFn[]): DefineRequest<Input, Output, Observe>;
45
+ withContext(value: HttpContext): DefineRequest<Input, Output, Observe>;
46
+ withCredentials(value: boolean): DefineRequest<Input, Output, Observe>;
47
+ withValidators(value: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[]): DefineRequest<Input, Output, Observe>;
48
+ withTransformResponse<O>(fn: TransformResponseFn<O>): DefineRequest<Input, O, Observe>;
49
+ withObserve(value: 'body'): DefineRequest<Input, Output, 'body'>;
50
+ withObserve(value: 'response'): DefineRequest<Input, Output, 'response'>;
51
+ withResponseType(value: 'json'): DefineRequest<Input, Output, Observe>;
52
+ withResponseType(value: 'text'): DefineRequest<Input, string, Observe>;
53
+ withResponseType(value: 'blob'): DefineRequest<Input, Blob, Observe>;
54
+ withResponseType(value: 'arraybuffer'): DefineRequest<Input, ArrayBuffer, Observe>;
55
+ }
56
+
57
+ export declare function defineRequest<Output>(method: string, endpoint: string): DefineRequest<undefined, Output>;
58
+
59
+ export declare type DoRequestOptions = {
60
+ abort?: AbortSignal;
61
+ handler?: HttpHandler;
62
+ client?: Client;
63
+ timeout?: number;
64
+ };
65
+
66
+ export declare function doValid<T>(validators: (ValidatorFn<T> | AsyncValidatorFn<T>)[], value: T): Promise<void>;
67
+
68
+ export declare const ERR_ABORTED: Error;
69
+
70
+ export declare const ERR_INVALID_CLIENT: Error;
71
+
72
+ export declare const ERR_INVALID_HTTP_CONTEXT_TOKEN: Error;
73
+
74
+ export declare const ERR_NETWORK: Error;
75
+
76
+ export declare const ERR_NOT_FOUND_GLOBAL_CLIENT: Error;
77
+
78
+ export declare const ERR_NOT_FOUND_HANDLER: Error;
79
+
80
+ export declare const ERR_NOT_SET_ALIAS: Error;
81
+
82
+ export declare const ERR_OBSERVE: Error;
83
+
84
+ export declare const ERR_TIMEOUT: Error;
85
+
86
+ export declare const ERR_UNKNOWN: Error;
87
+
88
+ export declare const ERR_UNSUPPORTED_FIELD_TYPE: Error;
89
+
90
+ export declare function fetchHandler(httpRequest: HttpRequest): Promise<HttpResponse<unknown>>;
91
+
92
+ declare const FIELD: unique symbol;
93
+
94
+ export declare type Field<T = undefined> = (() => T) & {
95
+ withJson(alias?: string): Field<T>;
96
+ withForm(alias?: string): Field<T>;
97
+ withQuery(alias?: string): Field<T>;
98
+ withParam(alias?: string): Field<T>;
99
+ withHeader(alias?: string): Field<T>;
100
+ withUrlForm(alias?: string): Field<T>;
101
+ withBody(): Field<T>;
102
+ withValidators(...fn: ValidatorFn<T>[]): Field<T>;
103
+ withAsyncValidators(...fn: AsyncValidatorFn<T>[]): Field<T>;
104
+ readonly [FIELD]: FieldMetadata<T>;
105
+ };
106
+
107
+ export declare function field(): Field;
108
+
109
+ export declare function field<T>(): Field<T | undefined>;
110
+
111
+ export declare function field<T>(defaultValue: T): Field<T>;
112
+
113
+ declare interface FieldMetadata<T = undefined> {
114
+ required: boolean;
115
+ alias: Map<FieldType, string | undefined>;
116
+ validators: ValidatorFn<T>[];
117
+ asyncValidator: AsyncValidatorFn<T>[];
118
+ }
119
+
120
+ declare enum FieldType {
121
+ Json = 0,
122
+ Query = 1,
123
+ Param = 2,
124
+ Header = 3,
125
+ Form = 4,
126
+ Body = 5,
127
+ UrlForm = 6
128
+ }
129
+
130
+ export declare function getGlobalClient(): Client;
131
+
132
+ declare const HTTP_CONTEXT: unique symbol;
133
+
134
+ declare const HTTP_CONTEXT_TOKEN: unique symbol;
135
+
136
+ export declare type HttpContext = {
137
+ set<T>(token: HttpContextToken<T>, value: T): HttpContext;
138
+ get<T>(token: HttpContextToken<T>): T;
139
+ del(token: HttpContextToken<unknown>): HttpContext;
140
+ has(token: HttpContextToken<unknown>): boolean;
141
+ keys(): IterableIterator<HttpContextToken<unknown>>;
142
+ get length(): number;
143
+ readonly [HTTP_CONTEXT]: Map<HttpContextToken<unknown>, unknown>;
144
+ };
145
+
146
+ export declare type HttpContextToken<T> = (() => T) & {
147
+ readonly [HTTP_CONTEXT_TOKEN]: () => T;
148
+ };
149
+
150
+ export declare class HttpErrorResponse extends Error {
151
+ readonly status: number;
152
+ readonly statusText: string;
153
+ readonly url: string;
154
+ readonly headers: Headers;
155
+ readonly body: unknown;
156
+ constructor(init?: {
157
+ error?: Error | string | unknown;
158
+ status?: number;
159
+ statusText?: string;
160
+ url?: string;
161
+ headers?: Headers;
162
+ body?: unknown;
163
+ });
164
+ }
165
+
166
+ export declare type HttpHandler = (req: HttpRequest) => Promise<HttpResponse<unknown>>;
167
+
168
+ export declare interface HttpProgressEvent {
169
+ /**
170
+ * Indicates whether the resources associated with Progress have a computable length.
171
+ * Otherwise, the Progress.total attribute will be a meaningless value.
172
+ */
173
+ readonly lengthComputable: boolean;
174
+ /** Indicates the workload of the underlying process. */
175
+ readonly loaded: number;
176
+ /**
177
+ * The total size of the data being processed or transmitted.
178
+ * If the Progress.lengthComputable attribute is false, this value is meaningless and should be ignored.
179
+ */
180
+ readonly total: number;
181
+ }
182
+
183
+ export declare type HttpProgressFn = (event: HttpProgressEvent) => void;
184
+
185
+ export declare interface HttpRequest {
186
+ host?: string;
187
+ method: string;
188
+ endpoint: string;
189
+ body?: Blob | ArrayBuffer | FormData | URLSearchParams | object | string | number | boolean | null;
190
+ headers?: Headers;
191
+ queryParams?: URLSearchParams;
192
+ /** default:json */
193
+ responseType?: HttpResponseType;
194
+ context?: HttpContext;
195
+ timeout?: number;
196
+ withCredentials?: boolean;
197
+ /** when use fetch handler, no upload progress */
198
+ uploadProgress?: HttpProgressFn;
199
+ downloadProgress?: HttpProgressFn;
200
+ /**
201
+ * default:body
202
+ */
203
+ observe?: 'body' | 'response';
204
+ abort?: AbortSignal;
205
+ }
206
+
207
+ export declare type HttpResponse<R> = {
208
+ readonly url: string;
209
+ readonly status: number;
210
+ readonly statusText: string;
211
+ readonly headers: Headers;
212
+ readonly body: R | null;
213
+ };
214
+
215
+ export declare type HttpResponseBody = string | ArrayBuffer | Blob | object | null;
216
+
217
+ export declare type HttpResponseType = 'arraybuffer' | 'blob' | 'json' | 'text';
218
+
219
+ export declare type InterceptorFn = (req: HttpRequest, next: HttpHandler) => Promise<HttpResponse<unknown>>;
220
+
221
+ export declare function isClient(value: unknown): value is Client;
222
+
223
+ export declare function isField(value: unknown): value is Field;
224
+
225
+ export declare function isFieldGroup(value: unknown): value is Record<PropertyKey, Field>;
226
+
227
+ export declare function isHttpContextToken(value: unknown): value is HttpContextToken<unknown>;
228
+
229
+ export declare function makeHttpContext(): HttpContext;
230
+
231
+ export declare function makeHttpContext(entries?: readonly (readonly [HttpContextToken<unknown>, unknown])[]): HttpContext;
232
+
233
+ export declare function makeHttpContext(context?: HttpContext): HttpContext;
234
+
235
+ export declare function makeHttpContextToken<T>(defaultValue: () => T): HttpContextToken<T>;
236
+
237
+ export declare type MakeResponseOptions<R> = {
238
+ status?: number;
239
+ statusText?: string;
240
+ url?: string;
241
+ headers?: Headers;
242
+ body?: R | null;
243
+ };
244
+
245
+ export declare const max: (max: number) => ValidatorFn;
246
+
247
+ export declare const maxLength: (max: number) => ValidatorFn;
248
+
249
+ export declare const min: (min: number) => ValidatorFn;
250
+
251
+ export declare const minLength: (min: number) => ValidatorFn;
252
+
253
+ declare type ObserveType<Observe, Output> = Observe extends 'body' ? Output : Observe extends 'response' ? HttpResponse<Output> : never;
254
+
255
+ export declare const pattern: (pattern: RegExp) => ValidatorFn;
256
+
257
+ export declare type RequestInputValue<T> = T extends Field<infer V> ? V : T extends {
258
+ [K in keyof T]: Field<any>;
259
+ } ? {
260
+ [K in keyof T]: RequestInputValue<T[K]>;
261
+ } : never;
262
+
263
+ export declare const required: ValidatorFn;
264
+
265
+ export declare const requiredTrue: ValidatorFn;
266
+
267
+ export declare function restGlobalClient(): void;
268
+
269
+ export declare function setGlobalClient(client: Client): void;
270
+
271
+ declare type TransformResponseFn<Output = unknown> = (response: HttpResponse<unknown>) => Output;
272
+
273
+ export declare type UseRequestFn<Input, Output, Observe> = {
274
+ doRequest: Input extends undefined ? (options?: DoRequestOptions) => Promise<ObserveType<Observe, Output>> : RequestInputValue<Input> extends RequestInputValue<Input> | undefined ? (input?: RequestInputValue<Input>, options?: DoRequestOptions) => Promise<ObserveType<Observe, Output>> : (input: RequestInputValue<Input>, options?: DoRequestOptions) => Promise<ObserveType<Observe, Output>>;
275
+ getInitValue: () => Input extends Field<infer V> ? V : Input extends {
276
+ [K in keyof Input]: Field<any>;
277
+ } ? {
278
+ [K in keyof Input]: RequestInputValue<Input[K]>;
279
+ } : undefined;
280
+ setUploadProgress: (fn: HttpProgressFn) => void;
281
+ setDownloadProgress: (fn: HttpProgressFn) => void;
282
+ };
283
+
284
+ export declare type ValidatorFn<T = unknown> = (value: T) => void;
285
+
286
+ export { }
226
287
 
227
- export {};
288
+
289
+ declare module 'vitest' {
290
+ interface ProvidedContext {
291
+ testServerHost: string;
292
+ }
293
+ }