bitwarden / passwordless-client-js

Bitwarden Passwordless.dev JavaScript SDK.
https://bitwarden.com/
Apache License 2.0
54 stars 19 forks source link

Generate passwordless.d.ts types #88

Closed abergs closed 2 weeks ago

abergs commented 2 weeks ago

Ticket

Description

This outputs a single passwordless.d.ts file, including typings from both types.ts and passwordless.ts. I do think this approach has a problem in that the types from types.ts are not marked with export in the generated asset Fixed in later commit. (see comparison with 1.2.0-beta1 build output below)

// dist/passwordless.d.ts
type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;
/**
 * Represents a sign-in method.
 */
type SigninMethod = {
    userId: string;
} | {
    alias: string;
} | {
    autofill: boolean;
} | {
    discoverable: boolean;
};
/**
 * Represents a step-up request to initiate a specific action or operation.
 *
 * @interface StepupRequest
 */
interface StepupRequest {
    signinMethod: SigninMethod;
    purpose: string;
}
type RegisterBeginResponse = {
    session: string;
    data: PublicKeyCredentialCreationOptions;
};
type Success<T> = {
    [P in keyof T]: T[P];
} & {
    error: undefined;
};
type Error<T> = {
    [P in keyof T]?: undefined;
} & {
    error: ProblemDetails;
};
type Result<T> = Success<T> | Error<T>;
type PromiseResult<T> = Promise<Result<T>>;
interface TokenResponse {
    token: string;
}
type SigninBeginResponse = {
    data: PublicKeyCredentialRequestOptions;
    session: string;
};
interface ProblemDetails {
    from: string;
    errorCode: string;
    title: string;
    status?: number;
    detail?: string;
}

interface Config {
    apiUrl: string;
    apiKey: string;
    origin: string;
    rpid: string;
}
declare class Client {
    private config;
    private _clientVersion;
    private abortController;
    constructor(config: AtLeast<Config, 'apiKey'>);
    /**
     * Register a new credential to a user
     *
     * @param {string} token Token generated by your backend and the Passwordless API
     * @param {string} credentialNickname A nickname for the passkey credential being created
     */
    register(token: string, credentialNickname?: string): PromiseResult<TokenResponse>;
    /**
     * Sign in a user using the userid
     * @param {string} userId
     * @returns
     */
    signinWithId(userId: string): PromiseResult<TokenResponse>;
    /**
     * Sign in a user using an alias
     * @param {string} alias
     * @returns a verify_token
     */
    signinWithAlias(alias: string): PromiseResult<TokenResponse>;
    /**
     * Sign in a user using autofill UI (a.k.a conditional) sign in
     * @returns a verify_token
     */
    signinWithAutofill(): PromiseResult<TokenResponse>;
    /**
     * Sign in a user using discoverable credentials
     * @returns a verify_token
     */
    signinWithDiscoverable(): PromiseResult<TokenResponse>;
    abort(): void;
    isPlatformSupported(): Promise<boolean>;
    isBrowserSupported(): boolean;
    isAutofillSupported(): Promise<boolean>;
    private registerBegin;
    private registerComplete;
    /**
     * Sign in a user
     *
     * @param {SigninMethod} Object containing either UserID or Alias
     * @returns
     */
    private signin;
    /**
     * Performs a step-up authentication process. This is essentially an overload for the sign-in workflow. It allows for
     * a user authentication to be given a purpose or context for the sign-in, enabling a "step-up" authentication flow.
     *
     * @param {StepupRequest} stepup - The step-up request object. This includes the sign-in method and the purpose of the authentication
     *
     * @returns {token} - The result of the step-up sign-in process.
     */
    stepup(stepup: StepupRequest): PromiseResult<TokenResponse>;
    private signinBegin;
    private signinComplete;
    private handleAbort;
    private assertBrowserSupported;
    private createHeaders;
    get clientVersionRegex(): RegExp;
    /**
     * (Internal use only) Sets the `Client-Version` header for client SDK implementations based off the Javascript Client SDK. By setting the
     * property, the parameter will be prepended.
     * @param {string} value The new `Client-Version` header value.
     * @throws {Error} Throws an error if the `Client-Version` has already been set.
     * @throws {Error} Throws an error if the `Client-Version` format is invalid. Expected format is 'prefix-x.x.x' where prefix is a lowercase string.
     * @remarks Do not set this property when integrating the client SDK.
     */
    set clientVersion(value: string);
    /**
     * Gets the `Client-Version` header for client SDK implementations based off the Javascript Client SDK.
     */
    get clientVersion(): string;
}
declare function isPlatformSupported(): Promise<boolean>;
declare function isBrowserSupported(): boolean;
declare function isAutofillSupported(): Promise<boolean>;

export { type AtLeast, Client, type Config, type Error, type ProblemDetails, type PromiseResult, type RegisterBeginResponse, type Result, type SigninBeginResponse, type SigninMethod, type StepupRequest, type Success, type TokenResponse, isAutofillSupported, isBrowserSupported, isPlatformSupported };

To compare with the output from 1.2.0-beta (before rollup was upgraded), the output was:

// dist/passwordlessd.ts
import { AtLeast, PromiseResult, StepupRequest, TokenResponse } from './types';
export interface Config {
    apiUrl: string;
    apiKey: string;
    origin: string;
    rpid: string;
}
export declare class Client {
    private config;
    private abortController;
    constructor(config: AtLeast<Config, 'apiKey'>);
    /**
     * Register a new credential to a user
     *
     * @param {string} token Token generated by your backend and the Passwordless API
     * @param {string} credentialNickname A nickname for the passkey credential being created
     */
    register(token: string, credentialNickname?: string): PromiseResult<TokenResponse>;
    /**
     * Sign in a user using the userid
     * @param {string} userId
     * @returns
     */
    signinWithId(userId: string): PromiseResult<TokenResponse>;
    /**
     * Sign in a user using an alias
     * @param {string} alias
     * @returns a verify_token
     */
    signinWithAlias(alias: string): PromiseResult<TokenResponse>;
    /**
     * Sign in a user using autofill UI (a.k.a conditional) sign in
     * @returns a verify_token
     */
    signinWithAutofill(): PromiseResult<TokenResponse>;
    /**
     * Sign in a user using discoverable credentials
     * @returns a verify_token
     */
    signinWithDiscoverable(): PromiseResult<TokenResponse>;
    abort(): void;
    isPlatformSupported(): Promise<boolean>;
    isBrowserSupported(): boolean;
    isAutofillSupported(): Promise<boolean>;
    private registerBegin;
    private registerComplete;
    /**
     * Sign in a user
     *
     * @param {SigninMethod} Object containing either UserID or Alias
     * @returns
     */
    private signin;
    /**
     * Performs a step-up authentication process. This is essentially an overload for the sign-in workflow. It allows for
     * a user authentication to be given a purpose or context for the sign-in, enabling a "step-up" authentication flow.
     *
     * @param {StepupRequest} stepup - The step-up request object. This includes the sign-in method and the purpose of the authentication
     *
     * @returns {token} - The result of the step-up sign-in process.
     */
    stepup(stepup: StepupRequest): PromiseResult<TokenResponse>;
    private signinBegin;
    private signinComplete;
    private handleAbort;
    private assertBrowserSupported;
    private createHeaders;
}
export declare function isPlatformSupported(): Promise<boolean>;
export declare function isBrowserSupported(): boolean;
export declare function isAutofillSupported(): Promise<boolean>;
// types.d.ts
export type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;
/**
 * Represents a sign-in method.
 */
export type SigninMethod = {
    userId: string;
} | {
    alias: string;
} | {
    autofill: boolean;
} | {
    discoverable: boolean;
};
/**
 * Represents a step-up request to initiate a specific action or operation.
 *
 * @interface StepupRequest
 */
export interface StepupRequest {
    signinMethod: SigninMethod;
    purpose: string;
}
export type RegisterBeginResponse = {
    session: string;
    data: PublicKeyCredentialCreationOptions;
};
export type Success<T> = {
    [P in keyof T]: T[P];
} & {
    error: undefined;
};
export type Error<T> = {
    [P in keyof T]?: undefined;
} & {
    error: ProblemDetails;
};
export type Result<T> = Success<T> | Error<T>;
export type PromiseResult<T> = Promise<Result<T>>;
export interface TokenResponse {
    token: string;
}
export type SigninBeginResponse = {
    data: PublicKeyCredentialRequestOptions;
    session: string;
};
export interface ProblemDetails {
    from: string;
    errorCode: string;
    title: string;
    status?: number;
    detail?: string;
}