@defjs/core 0.0.0-test-0008 → 0.0.0-test-0009

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.ts CHANGED
@@ -82,7 +82,7 @@ export interface HttpProgressEvent {
82
82
  */
83
83
  readonly total: number;
84
84
  }
85
- export type HttpProgressFn = (value: HttpProgressEvent) => void;
85
+ export type HttpProgressFn = (event: HttpProgressEvent) => void;
86
86
  export type TransformRequestFn = (request: HttpRequest) => HttpRequest["body"];
87
87
  export type TransformResponseFn<Output = unknown> = (response: HttpResponse<unknown>) => Output;
88
88
  export interface HttpRequest {
@@ -122,11 +122,23 @@ export type RequestOptions<Output = unknown, Input extends Field<any> | Record<P
122
122
  transformRequest?: TransformRequestFn;
123
123
  transformResponse?: TransformResponseFn<Output>;
124
124
  };
125
- declare const REQUEST_REF: unique symbol;
126
- export type RequestRef<_Output = undefined, _Input = undefined> = {
127
- readonly [REQUEST_REF]: RequestOptions;
125
+ export type DoRequestOptions = {
126
+ abort?: AbortSignal;
127
+ handler?: HttpHandler;
128
+ client?: ClientOptions;
129
+ timeout?: number;
130
+ };
131
+ export type DoRequestFn<Input = unknown, Output = unknown> = Input extends undefined ? (options?: DoRequestOptions) => Output : (input: RequestInputValue<Input>, options?: DoRequestOptions) => Output;
132
+ export type UseRequestFn<Output = unknown, Input = unknown> = () => {
133
+ doRequest: DoRequestFn<Input, Output>;
134
+ inputValue: 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;
128
141
  };
129
- export type MakeRequestFn<Output = undefined, Input = undefined> = () => RequestRef<Output, Input>;
130
142
  /** Observe: Body */
131
143
  export declare function defineRequest<Output = unknown, Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
132
144
  method: string;
@@ -140,7 +152,7 @@ export declare function defineRequest<Output = unknown, Input extends Field<any>
140
152
  validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
141
153
  transformRequest?: TransformRequestFn;
142
154
  transformResponse?: TransformResponseFn<Output>;
143
- }): MakeRequestFn<Output, Input>;
155
+ }): UseRequestFn<Output, Input>;
144
156
  export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
145
157
  method: string;
146
158
  endpoint: string;
@@ -153,7 +165,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
153
165
  validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
154
166
  transformRequest?: TransformRequestFn;
155
167
  transformResponse?: TransformResponseFn;
156
- }): MakeRequestFn<string, Input>;
168
+ }): UseRequestFn<string, Input>;
157
169
  export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
158
170
  method: string;
159
171
  endpoint: string;
@@ -166,7 +178,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
166
178
  validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
167
179
  transformRequest?: TransformRequestFn;
168
180
  transformResponse?: TransformResponseFn;
169
- }): MakeRequestFn<Blob, Input>;
181
+ }): UseRequestFn<Blob, Input>;
170
182
  export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
171
183
  method: string;
172
184
  endpoint: string;
@@ -179,7 +191,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
179
191
  validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
180
192
  transformRequest?: TransformRequestFn;
181
193
  transformResponse?: TransformResponseFn;
182
- }): MakeRequestFn<ArrayBuffer, Input>;
194
+ }): UseRequestFn<ArrayBuffer, Input>;
183
195
  /** Observe: Response */
184
196
  export declare function defineRequest<Output = unknown, Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
185
197
  method: string;
@@ -193,7 +205,7 @@ export declare function defineRequest<Output = unknown, Input extends Field<any>
193
205
  validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
194
206
  transformRequest?: TransformRequestFn;
195
207
  transformResponse?: TransformResponseFn<Output>;
196
- }): MakeRequestFn<HttpResponse<Output>, Input>;
208
+ }): UseRequestFn<HttpResponse<Output>, Input>;
197
209
  export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
198
210
  method: string;
199
211
  endpoint: string;
@@ -206,7 +218,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
206
218
  validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
207
219
  transformRequest?: TransformRequestFn;
208
220
  transformResponse?: TransformResponseFn;
209
- }): MakeRequestFn<HttpResponse<string>, Input>;
221
+ }): UseRequestFn<HttpResponse<string>, Input>;
210
222
  export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
211
223
  method: string;
212
224
  endpoint: string;
@@ -219,7 +231,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
219
231
  validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
220
232
  transformRequest?: TransformRequestFn;
221
233
  transformResponse?: TransformResponseFn;
222
- }): MakeRequestFn<HttpResponse<Blob>, Input>;
234
+ }): UseRequestFn<HttpResponse<Blob>, Input>;
223
235
  export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
224
236
  method: string;
225
237
  endpoint: string;
@@ -232,21 +244,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
232
244
  validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
233
245
  transformRequest?: TransformRequestFn;
234
246
  transformResponse?: TransformResponseFn;
235
- }): MakeRequestFn<HttpResponse<ArrayBuffer>, Input>;
236
- export declare function isRequestRef(value: unknown): value is RequestRef;
237
- export type DoRequestOptions = {
238
- uploadProgress?: HttpProgressFn;
239
- downloadProgress?: HttpProgressFn;
240
- abort?: AbortSignal;
241
- handler?: HttpHandler;
242
- client?: ClientOptions;
243
- timeout?: number;
244
- };
245
- export type RequestRefOutput<T> = T extends RequestRef<infer O> ? O : undefined;
246
- export type RequestRefInput<T> = T extends RequestRef<unknown, infer I> ? RequestInputValue<I> : never;
247
- export type RequestRefWithInput<T> = T extends RequestRef<unknown, infer Input> ? (Input extends undefined ? false : true) : false;
248
- export declare function doRequest<Ref extends RequestRef>(ref: RequestRefWithInput<Ref> extends true ? never : Ref, options?: DoRequestOptions): Promise<RequestRefOutput<Ref>>;
249
- export declare function doRequest<Ref extends RequestRef>(ref: RequestRefWithInput<Ref> extends true ? Ref : never, input: RequestRefInput<Ref>, options?: DoRequestOptions): Promise<RequestRefOutput<Ref>>;
247
+ }): UseRequestFn<HttpResponse<ArrayBuffer>, Input>;
250
248
  export type HttpHandler = (req: HttpRequest) => Promise<HttpResponse<unknown>>;
251
249
  export declare function fetchHandler(httpRequest: HttpRequest): Promise<HttpResponse<unknown>>;
252
250
  export interface ClientOptions {
package/dist/index.js CHANGED
@@ -134,27 +134,103 @@ function detectContentTypeHeader(body) {
134
134
  return null;
135
135
  }
136
136
  }
137
+ function buildFieldValue(input) {
138
+ if (isField(input)) {
139
+ return input();
140
+ }
141
+ if (isFieldGroup(input)) {
142
+ const obj = {};
143
+ for (const [key, field3] of Object.entries(input)) {
144
+ obj[key] = field3();
145
+ }
146
+ return obj;
147
+ }
148
+ return;
149
+ }
137
150
  function defineRequest(options) {
151
+ const requestOptions = {
152
+ ...options
153
+ };
154
+ const requiredInput = !!requestOptions.input;
138
155
  return () => {
139
- const opts = {
140
- ...options
156
+ let uploadProgress;
157
+ let downloadProgress;
158
+ let abortController;
159
+ let abortSignal;
160
+ const setUploadProgress = (fn) => {
161
+ uploadProgress = fn;
162
+ };
163
+ const setDownloadProgress = (fn) => {
164
+ downloadProgress = fn;
165
+ };
166
+ const doRequest = async (input, options2) => {
167
+ if (requiredInput && !input) {
168
+ throw new Error("input is required");
169
+ }
170
+ if (options2?.abort) {
171
+ abortSignal = options2.abort;
172
+ } else {
173
+ abortController = new AbortController;
174
+ abortSignal = abortController.signal;
175
+ }
176
+ const clientOptions = getClientConfig(options2?.client || getGlobalClient());
177
+ const req = {
178
+ host: clientOptions?.host,
179
+ method: requestOptions.method,
180
+ endpoint: requestOptions.endpoint,
181
+ queryParams: new URLSearchParams,
182
+ headers: new Headers,
183
+ body: undefined,
184
+ withCredentials: requestOptions.withCredentials,
185
+ responseType: requestOptions.responseType ?? "json",
186
+ context: requestOptions.context || makeHttpContext(),
187
+ observe: requestOptions.observe ?? "body",
188
+ uploadProgress,
189
+ downloadProgress,
190
+ timeout: options2?.timeout || 0,
191
+ abort: abortSignal
192
+ };
193
+ const handler2 = options2?.handler || clientOptions?.handler || getGlobalHttpHandler();
194
+ if (!handler2) {
195
+ throw new Error("no handler");
196
+ }
197
+ if (requiredInput) {
198
+ await fillRequestFromField(req, requestOptions.input, input);
199
+ }
200
+ if (typeof requestOptions.transformRequest === "function") {
201
+ req.body = requestOptions.transformRequest(req);
202
+ }
203
+ const chain = makeInterceptorChain([...clientOptions?.interceptors || [], ...requestOptions?.interceptors || []]);
204
+ if (typeof req.timeout === "number" && req.timeout > 0 && abortController) {
205
+ setTimeout(() => {
206
+ if (!req.abort?.aborted) {
207
+ abortController?.abort("timeout");
208
+ }
209
+ }, req.timeout);
210
+ }
211
+ const res = await chain(req, handler2);
212
+ if (typeof requestOptions.transformResponse === "function") {
213
+ res.body = requestOptions.transformResponse(res);
214
+ }
215
+ switch (req.observe) {
216
+ case "body":
217
+ return res.body;
218
+ case "stream":
219
+ throw new Error("streaming not implemented");
220
+ case "response":
221
+ return res;
222
+ default:
223
+ throw new Error("invalid observe");
224
+ }
225
+ };
226
+ return {
227
+ doRequest,
228
+ inputValue: buildFieldValue(requestOptions.input),
229
+ setUploadProgress,
230
+ setDownloadProgress
141
231
  };
142
- const ref = {};
143
- Object.defineProperty(ref, REQUEST_REF, {
144
- value: opts
145
- });
146
- return ref;
147
232
  };
148
233
  }
149
- function isRequestRef(value) {
150
- return !!value && typeof value === "object" && REQUEST_REF in value;
151
- }
152
- function getRequestRefOptions(value) {
153
- if (!isRequestRef(value)) {
154
- throw new Error("no request ref");
155
- }
156
- return value[REQUEST_REF];
157
- }
158
234
  function throwAliaNotSet() {
159
235
  throw new Error("alias not set");
160
236
  }
@@ -309,89 +385,6 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
309
385
  }
310
386
  throw new Error("unsupported field type");
311
387
  }
312
- async function doRequest(...args) {
313
- const argsLength = args.length;
314
- let ref;
315
- let input;
316
- let options;
317
- switch (argsLength) {
318
- case 1: {
319
- ref = args[0];
320
- options = args[1];
321
- break;
322
- }
323
- case 2: {
324
- ref = args[0];
325
- input = args[1];
326
- options = args[2];
327
- break;
328
- }
329
- default: {
330
- throw new Error("invalid arguments");
331
- }
332
- }
333
- const requestOptions = getRequestRefOptions(ref);
334
- const requiredInput = !!requestOptions.input;
335
- if (requiredInput && !input) {
336
- throw new Error("input is required");
337
- }
338
- let abortController;
339
- let abortSignal;
340
- if (options?.abort) {
341
- abortSignal = options.abort;
342
- } else {
343
- abortController = new AbortController;
344
- abortSignal = abortController.signal;
345
- }
346
- const clientOptions = getClientConfig(options?.client || getGlobalClient());
347
- const req = {
348
- host: clientOptions?.host,
349
- method: requestOptions.method,
350
- endpoint: requestOptions.endpoint,
351
- queryParams: new URLSearchParams,
352
- headers: new Headers,
353
- body: undefined,
354
- withCredentials: requestOptions.withCredentials,
355
- responseType: requestOptions.responseType ?? "json",
356
- context: requestOptions.context || makeHttpContext(),
357
- observe: requestOptions.observe ?? "body",
358
- uploadProgress: options?.uploadProgress,
359
- downloadProgress: options?.downloadProgress,
360
- timeout: options?.timeout || 0,
361
- abort: abortSignal
362
- };
363
- const handler2 = options?.handler || clientOptions?.handler || getGlobalHttpHandler();
364
- if (!handler2) {
365
- throw new Error("no handler");
366
- }
367
- await fillRequestFromField(req, requestOptions.input, input);
368
- if (typeof requestOptions.transformRequest === "function") {
369
- req.body = requestOptions.transformRequest(req);
370
- }
371
- const chain = makeInterceptorChain([...clientOptions?.interceptors || [], ...requestOptions?.interceptors || []]);
372
- if (typeof req.timeout === "number" && req.timeout > 0 && abortController) {
373
- setTimeout(() => {
374
- if (!req.abort?.aborted) {
375
- abortController.abort("timeout");
376
- }
377
- }, req.timeout);
378
- }
379
- const res = await chain(req, handler2);
380
- if (typeof requestOptions.transformResponse === "function") {
381
- res.body = requestOptions.transformResponse(res);
382
- }
383
- switch (req.observe) {
384
- case "body":
385
- return res.body;
386
- case "stream":
387
- throw new Error("streaming not implemented");
388
- case "response":
389
- return res;
390
- default:
391
- throw new Error("invalid observe");
392
- }
393
- }
394
- var REQUEST_REF = Symbol("RequestRef");
395
388
 
396
389
  // src/response.ts
397
390
  function makeResponse(options) {
@@ -593,14 +586,12 @@ export {
593
586
  makeResponse,
594
587
  makeHttpContextToken,
595
588
  makeHttpContext,
596
- isRequestRef,
597
589
  isFieldGroup,
598
590
  isField,
599
591
  isClient,
600
592
  getGlobalClient,
601
593
  field,
602
594
  fetchHandler,
603
- doRequest,
604
595
  defineRequest,
605
596
  createGlobalClient,
606
597
  createClient
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defjs/core",
3
- "version": "0.0.0-test-0008",
3
+ "version": "0.0.0-test-0009",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "bun run ./scripts/build.ts",