Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate passwordless.d.ts types #88

Merged
merged 2 commits into from
Nov 7, 2024
Merged

Generate passwordless.d.ts types #88

merged 2 commits into from
Nov 7, 2024

Conversation

abergs
Copy link
Member

@abergs abergs commented Nov 6, 2024

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;
}

@abergs abergs requested a review from a team as a code owner November 6, 2024 13:57
Copy link
Member

@jonashendrickx jonashendrickx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could still look into getting all the types in one type definitions file. I am not sure if it is complete as opposed to the Node.js SDK. We might be missing some files in the index file.

@jonashendrickx jonashendrickx merged commit 787268a into main Nov 7, 2024
4 checks passed
@jonashendrickx jonashendrickx deleted the types-fix branch November 7, 2024 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants