twilio / twilio-verify-for-react-native

React native package for Twilio Verify
https://www.twilio.com/docs/verify/push
Apache License 2.0
7 stars 10 forks source link

Typescript support #29

Open kg-currenxie opened 1 year ago

kg-currenxie commented 1 year ago

Would be nice to get a type definition for all the methods :)

image

Not sure why it can't load tho.. The node module includes these files: image in ./node_modules/@twilio/twilio-verify-for-react-native/lib/typescript

kg-currenxie commented 1 year ago

For anyone who needs it. Here's my best attempt so far for the workaround:

Add to any .d.ts file that your project loads:

declare module '@twilio/twilio-verify-for-react-native' {
  export interface Metadata {
    nextPageToken?: string
    page: number
    pageSize: number
    previousPageToken?: string
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  export type JSONObject = { [key: string]: any }

  export interface Challenge {
    challengeDetails: ChallengeDetails
    createdAt: Date
    expirationDate: Date
    factorSid: string
    hiddenDetails: JSONObject
    sid: string
    status: ChallengeStatus
    updatedAt: Date
  }

  export interface ChallengeList {
    challenges: Challenge[]
    metadata: Metadata
  }

  export enum ChallengeListOrder {
    Asc = 'asc',
    Desc = 'desc',
  }

  export enum ChallengeStatus {
    Pending = 'pending',
    Approved = 'approved',
    Denied = 'denied',
    Expired = 'expired',
  }

  export interface ChallengeDetails {
    date?: Date
    fields: Detail[]
    message: string
  }

  export interface Detail {
    label: string
    value: string
  }

  export interface Factor {
    accountSid: string
    createdAt: Date
    friendlyName: string
    identity: string
    serviceSid: string
    sid: string
    status: FactorStatus
    type: FactorType
  }

  enum FactorType {
    Push = 'push',
  }

  export enum FactorStatus {
    Verified = 'verified',
    Unverified = 'unverified',
  }
  /*
  Create factor method
  */
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
  declare class PushFactorPayload {
    /**
     * Used as a parameter for the `createFactor()` method.
     *
     * @param factorName - Any friendly name
     * @param verifyServiceSid - The service SID (e.g. VA00f7e870e0ebc27c85ca8ab75e2c770d, from Twilio Verify's dashboard)
     * @param identity - A unique identifier (usually a user's UUID)
     * @param accessToken - Access token by authentication with Twilio
     * @param pushToken - Your device's FCM or APNS token
     * @returns A payload object
     *
     */
    constructor(
      factorName: string,
      verifyServiceSid: string,
      identity: string,
      accessToken: string,
      pushToken: string,
    )
  }

  /**
   * Used to create a factor for your device.
   *
   * @param payload - Created with the `PushFactorPayload` class
   * @returns A Factor
   *
   */
  export function createFactor(payload: PushFactorPayload): Promise<Factor>

  /*
  Verify factor method
  */
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
  declare class VerifyPushFactorPayload {
    /**
     * Used as a parameter for the `createFactor()` method.
     *
     * @param factorSid - The factor SID
     * @returns A payload object
     *
     */
    constructor(factorSid: string)
  }

  /**
   * Used to verify a factor for your device, that you previously created with the `createFactor()` method, or a factor received by the backend.
   *
   * @param payload - Created with the `VerifyPushFactorPayload` class
   * @returns A Factor
   *
   */
  export function verifyFactor(
    payload: VerifyPushFactorPayload,
  ): Promise<Factor>

  /*
  Update challenge method
  */
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
  declare class UpdatePushChallengePayload {
    /**
     * Used as a parameter for the `updateChallenge()` method.
     *
     * @param factorSid - The factor SID
     * @param challengeSid - The challenge SID
     * @param newStatus - The new status of the challenge to be updated
     * @returns A payload object
     *
     */
    constructor(
      factorSid: string,
      challengeSid: string,
      newStatus: ChallengeStatus,
    )
  }

  /**
   * Used to update a challenge
   *
   * @param payload - Created with the `UpdatePushChallengePayload` class
   * @returns A Challenge
   *
   */
  export function updateChallenge(
    payload: UpdatePushChallengePayload,
  ): Promise<Challenge>

  /*
  Get all challenges method
  */
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
  declare class ChallengeListPayload {
    /**
     * Used as a parameter for the `updateChallenge()` method.
     *
     * @param factorSid - The factor SID
     * @param limit - The max number of challenges to return
     * @param status - The status of the challenges to return
     * @param order - The order of the challenges to return
     * @returns A payload object
     *
     */
    constructor(
      public factorSid: string,
      public pageSize: number,
      public status?: ChallengeStatus,
      public order: ChallengeListOrder = ChallengeListOrder.Asc,
      public pageToken?: string,
    ) {}
  }

  /**
   * Used to update a challenge
   *
   * @param payload - Created with the `ChallengeListPayload` class
   * @returns A ChallengeList
   *
   */
  export function getAllChallenges(
    payload: ChallengeListPayload,
  ): Promise<ChallengeList>
}
redbluenat commented 1 year ago

I created Twillio.d.ts and updated @kg-currenxie types:


declare module "@twilio/twilio-verify-for-react-native" {
  export interface Metadata {
    nextPageToken?: string;
    page: number;
    pageSize: number;
    previousPageToken?: string;
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  export type JSONObject = { [key: string]: any };

  export interface Challenge {
    challengeDetails: ChallengeDetails;
    createdAt: Date;
    expirationDate: Date;
    factorSid: string;
    hiddenDetails: JSONObject;
    sid: string;
    status: ChallengeStatus;
    updatedAt: Date;
  }

  export interface ChallengeList {
    challenges: Challenge[];
    metadata: Metadata;
  }

  export enum ChallengeListOrder {
    Asc = "asc",
    Desc = "desc",
  }

  export enum ChallengeStatus {
    Pending = "pending",
    Approved = "approved",
    Denied = "denied",
    Expired = "expired",
  }

  export interface ChallengeDetails {
    date?: Date;
    fields: Detail[];
    message: string;
  }

  export interface Detail {
    label: string;
    value: string;
  }

  export interface Factor {
    accountSid: string;
    createdAt: Date;
    friendlyName: string;
    identity: string;
    serviceSid: string;
    sid: string;
    status: FactorStatus;
    type: FactorType;
  }

  enum FactorType {
    Push = "push",
  }

  export enum FactorStatus {
    Verified = "verified",
    Unverified = "unverified",
  }
  /*
  Create factor method
  */
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
  declare class PushFactorPayload {
    /**
     * Used as a parameter for the `createFactor()` method.
     *
     * @param factorName - Any friendly name
     * @param verifyServiceSid - The service SID (e.g. VA00f7e870e0ebc27c85ca8ab75e2c770d, from Twilio Verify's dashboard)
     * @param identity - A unique identifier (usually a user's UUID)
     * @param accessToken - Access token by authentication with Twilio
     * @param pushToken - Your device's FCM or APNS token
     * @returns A payload object
     *
     */
    constructor(
      factorName: string,
      verifyServiceSid: string,
      identity: string,
      accessToken: string,
      pushToken: string,
    );
  }

  /**
   * Used to create a factor for your device.
   *
   * @param payload - Created with the `PushFactorPayload` class
   * @returns A Factor
   *
   */
  export function createFactor(payload: PushFactorPayload): Promise<Factor>;

  /*
  Verify factor method
  */
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
  declare class VerifyPushFactorPayload {
    /**
     * Used as a parameter for the `createFactor()` method.
     *
     * @param factorSid - The factor SID
     * @returns A payload object
     *
     */
    constructor(factorSid: string);
  }

  /**
   * Used to verify a factor for your device, that you previously created with the `createFactor()` method, or a factor received by the backend.
   *
   * @param payload - Created with the `VerifyPushFactorPayload` class
   * @returns A Factor
   *
   */
  export function verifyFactor(payload: VerifyPushFactorPayload): Promise<Factor>;

  /*
  Update challenge method
  */
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
  declare class UpdatePushChallengePayload {
    /**
     * Used as a parameter for the `updateChallenge()` method.
     *
     * @param factorSid - The factor SID
     * @param challengeSid - The challenge SID
     * @param newStatus - The new status of the challenge to be updated
     * @returns A payload object
     *
     */
    constructor(factorSid: string, challengeSid: string, newStatus: ChallengeStatus);
  }

  /**
   * Used to update a challenge
   *
   * @param payload - Created with the `UpdatePushChallengePayload` class
   * @returns A Challenge
   *
   */
  export function updateChallenge(payload: UpdatePushChallengePayload): Promise<Challenge>;

  /*
  Get all challenges method
  */
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
  declare class ChallengeListPayload {
    /**
     * Used as a parameter for the `updateChallenge()` method.
     *
     * @param factorSid - The factor SID
     * @param limit - The max number of challenges to return
     * @param status - The status of the challenges to return
     * @param order - The order of the challenges to return
     * @returns A payload object
     *
     */
    constructor(
      public factorSid: string,
      public pageSize: number,
      public status?: ChallengeStatus,
      public order: ChallengeListOrder = ChallengeListOrder.Asc,
      public pageToken?: string,
    ) {}
  }

  /**
   * Used to update a challenge
   *
   * @param payload - Created with the `ChallengeListPayload` class
   * @returns A ChallengeList
   *
   */
  export function getAllChallenges(payload: ChallengeListPayload): Promise<ChallengeList>;

  /**
   * Used to get all factors
   * @returns An array of Factors
   */

  export function getAllFactors(): Promise<Array<Factor>>;

  /**
   *
   * @param challengeSid - The challenge SID
   * @param factorSid - The factor SID
   * @returns A Challenge
   */
  export function getChallenge(challengeSid: string, factorSid: string): Promise<Challenge>;
}