pokusew / node-pcsclite

Bindings over pcsclite to access Smart Cards
ISC License
59 stars 55 forks source link

Export interfaces and types #34

Open jeppech opened 4 years ago

jeppech commented 4 years ago

I've exported the types and interfaces. So they can be imported, and used within a project.

My reason for this is, that I need to put the readers in an array, for later use.

import pcsclite, { CardReader } from '@pokusew/node-pcsclite'

const pcsc = pcsclite()
const readers: CardReader[] = []

pcsc.on('reader', (reader) => {
  readers.push(reader)
})
jeppech commented 4 years ago

@pokusew Could you please take a look at this?

BallsOfGod commented 3 years ago

@pokusew could you maybe look into it?

jwigankow commented 3 years ago

I needed some typings as well. The typings as in this PR did not work for me because this lib is not using es6 modules but the typings would require that.

My solution for typings is as follows:

import { EventEmitter } from 'events'

export = pcsc;

declare function pcsc(): pcsc.PCSCLite;

declare namespace pcsc {

    export type ConnectOptions = {
        share_mode?: number;
        protocol?: number;
    };

    export type Status = {
        atr?: Buffer;
        state: number;
    };

    export type AnyOrNothing = any | undefined | null;

    export interface PCSCLite extends EventEmitter {
        on(type: 'error', listener: (error: any) => void): this;

        once(type: 'error', listener: (error: any) => void): this;

        on(type: 'reader', listener: (reader: CardReader) => void): this;

        once(type: 'reader', listener: (reader: CardReader) => void): this;

        close(): void;
    }

    export class CardReaderOptions {
        share_mode?: number
        protocol?: number
    }

    export class CardReader extends EventEmitter {
        // Share Mode
        SCARD_SHARE_SHARED: number
        SCARD_SHARE_EXCLUSIVE: number
        SCARD_SHARE_DIRECT: number
        // Protocol
        SCARD_PROTOCOL_T0: number
        SCARD_PROTOCOL_T1: number
        SCARD_PROTOCOL_RAW: number
        //  State
        SCARD_STATE_UNAWARE: number
        SCARD_STATE_IGNORE: number
        SCARD_STATE_CHANGED: number
        SCARD_STATE_UNKNOWN: number
        SCARD_STATE_UNAVAILABLE: number
        SCARD_STATE_EMPTY: number
        SCARD_STATE_PRESENT: number
        SCARD_STATE_ATRMATCH: number
        SCARD_STATE_EXCLUSIVE: number
        SCARD_STATE_INUSE: number
        SCARD_STATE_MUTE: number
        // Disconnect disposition
        SCARD_LEAVE_CARD: number
        SCARD_RESET_CARD: number
        SCARD_UNPOWER_CARD: number
        SCARD_EJECT_CARD: number
        name: string
        state: number
        connected: boolean

        on(type: 'error', listener: (this: CardReader, error: any) => void): this;

        once(type: 'error', listener: (this: CardReader, error: any) => void): this;

        on(type: 'end', listener: (this: CardReader) => void): this;

        once(type: 'end', listener: (this: CardReader) => void): this;

        on(
            type: 'status',
            listener: (this: CardReader, status: Status) => void
        ): this;

        once(
            type: 'status',
            listener: (this: CardReader, status: Status) => void
        ): this;

        SCARD_CTL_CODE(code: number): number;

        get_status(
            cb: (err: AnyOrNothing, state: number, atr?: Buffer) => void
        ): void;

        connect(callback: (err: AnyOrNothing, protocol: number) => void): void;

        connect(
            options: ConnectOptions,
            callback: (err: AnyOrNothing, protocol: number) => void
        ): void;

        disconnect(callback: (err: AnyOrNothing) => void): void;

        disconnect(disposition: number, callback: (err: AnyOrNothing) => void): void;

        transmit(
            data: Buffer,
            res_len: number,
            protocol: number,
            cb: (err: AnyOrNothing, response: Buffer) => void
        ): void;

        control(
            data: Buffer,
            control_code: number,
            res_len: number,
            cb: (err: AnyOrNothing, response: Buffer) => void
        ): void;

        close(): void;
    }
}

Usage would be like this:

import { CardReader, ConnectOptions, PCSCLite, Status } from '@pokusew/pcsclite'
import pcsc = require('@pokusew/pcsclite')

I'm not sure if this project/pull request is still active. But I would appreciate if these or similar typings would make it into the project. If there is intrest I can provide a PR with these changes.

Joepin8 commented 2 years ago

This would be a very welcome change. I just copied the interface as of now..