verygoodsecurity / vgs-collect-js

VGS Collect.js script loading module
https://www.verygoodsecurity.com/
7 stars 14 forks source link

Typescript definitions #33

Open nimo-juice opened 2 years ago

nimo-juice commented 2 years ago

Expected Behavior

Your type library does not cover most of the type definitions for the library itself. It would be great to add more.

Current Behavior

Typescript definitions are failing in my repository.

Possible Solution

import { IConfig } from '@vgs/collect-js/dist/utils/IConfig';

type stateParam = {
  errorMessages: string[];
  isDirty: boolean;
  isEmpty: boolean;
  isFocused: boolean;
  isTouched: boolean;
  isValid: boolean;
  name: string;
};

type responseData = {
  errors: {
    detail: string;
    source: {
      pointer: string;
    };
    status: string;
    title: string;
  }[];
};

declare module '@vgs/collect-js' {
  export interface VGSForm {
    field: (
      id: string,
      options: {
        type: string;
        name: string;
        placeholder: string;
        validations: string[];
        successColor?: string;
        errorColor?: string;
        maxLength?: number;
        yearLength?: number;
        serializers?: string[];
        css?: Record<string, string>;
      },
    ) => void;
    SERIALIZERS: string;
    submit: (
      path: string,
      options: {
        mapDotToObject: string;
        headers: Record<string, string>;
      },
      onResponse?: (status: number, data: responseData) => void,
    ) => void;
    state: Record<string, stateParam>;
    onUpdateCallback: unknown;
  }

  export interface CollectFN {
    init: (fn?: (state: Record<string, stateParam>) => void) => VGSFormDef;
  }

  export const loadVGSCollect: (config?: IConfig) => Promise<CollectFN>;
}

Context

Your Environment

AnnaKudriasheva commented 2 years ago

@nimo-juice thank you for pointing this out! We'll work on adding more types.

nimo-juice commented 2 years ago

@AnnaKudriasheva happy to open a PR if there are recommendations on how : )