@azure-net/kit 1.1.2 → 1.1.4

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.
@@ -55,7 +55,7 @@ export class HttpService {
55
55
  return new HttpServiceResponse({
56
56
  headers: request.headers instanceof Headers ? Object.fromEntries(request.headers.entries()) : {},
57
57
  status: request.status,
58
- success: request.status <= 400,
58
+ success: request.status < 400,
59
59
  data: await request?.[format](),
60
60
  message: 'Request completed'
61
61
  });
@@ -1,8 +1,8 @@
1
1
  import type { RequestErrors } from '../../delivery/schema/index.js';
2
2
  import type { AsyncActionResponse } from '../../index.js';
3
- export interface FormConfig {
4
- initialData?: unknown;
5
- onSuccess?: () => Promise<void> | void;
3
+ export interface FormConfig<FormData, Response> {
4
+ initialData?: FormData;
5
+ onSuccess?: (response: Response) => Promise<void> | void;
6
6
  onError?: () => Promise<void> | void;
7
7
  }
8
8
  export interface ActiveForm<FormData, Response, Custom> {
@@ -13,4 +13,14 @@ export interface ActiveForm<FormData, Response, Custom> {
13
13
  pending: boolean;
14
14
  dirty: boolean;
15
15
  }
16
- export declare function createActiveForm<Response, FormData, Custom>(onSubmit: (formData: Partial<FormData>) => Promise<AsyncActionResponse<Response, FormData, Custom>>, config?: FormConfig): ActiveForm<FormData, Response, Custom>;
16
+ type ExtractResponse<T> = T extends AsyncActionResponse<infer R, unknown, unknown> ? R : never;
17
+ type ExtractFormData<T> = T extends AsyncActionResponse<unknown, infer D, unknown> ? D : never;
18
+ type ExtractCustom<T> = T extends AsyncActionResponse<unknown, unknown, infer C> ? C : never;
19
+ type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
20
+ type ExtractFromSubmit<T> = {
21
+ response: ExtractResponse<UnwrapPromise<T>>;
22
+ formData: ExtractFormData<UnwrapPromise<T>>;
23
+ custom: ExtractCustom<UnwrapPromise<T>>;
24
+ };
25
+ export declare const createActiveForm: <SubmitReturn extends Promise<AsyncActionResponse<unknown, unknown, unknown>>>(onSubmit: (formData: Partial<ExtractFromSubmit<SubmitReturn>["formData"]>) => SubmitReturn, config?: FormConfig<ExtractFromSubmit<SubmitReturn>["formData"], ExtractFromSubmit<SubmitReturn>["response"]>) => ActiveForm<ExtractFromSubmit<SubmitReturn>["formData"], ExtractFromSubmit<SubmitReturn>["response"], ExtractFromSubmit<SubmitReturn>["custom"]>;
26
+ export {};
@@ -1,5 +1,5 @@
1
1
  import { ObjectUtil } from 'azure-net-tools';
2
- export function createActiveForm(onSubmit, config) {
2
+ export const createActiveForm = (onSubmit, config) => {
3
3
  const initial = config?.initialData ?? {};
4
4
  let formData = $state(ObjectUtil.deepClone(initial));
5
5
  let formErrors = $state({});
@@ -13,7 +13,7 @@ export function createActiveForm(onSubmit, config) {
13
13
  formErrors = result.error.fields;
14
14
  }
15
15
  if (result.success) {
16
- await config?.onSuccess?.();
16
+ await config?.onSuccess?.(result.response);
17
17
  }
18
18
  else {
19
19
  await config?.onError?.();
@@ -47,4 +47,4 @@ export function createActiveForm(onSubmit, config) {
47
47
  submit,
48
48
  reset
49
49
  };
50
- }
50
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-net/kit",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "files": [
5
5
  "dist",
6
6
  "!dist/**/*.test.*",