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

Export interfaces and types #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Export interfaces and types #34

wants to merge 1 commit into from

Conversation

jeppech
Copy link

@jeppech jeppech commented Jun 3, 2020

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
Copy link
Author

jeppech commented Sep 29, 2020

@pokusew Could you please take a look at this?

@BallsOfGod
Copy link

@pokusew could you maybe look into it?

@jwigankow
Copy link

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
Copy link

Joepin8 commented Feb 16, 2022

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

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.

4 participants