tsndr / cloudflare-worker-jwt

A lightweight JWT implementation with ZERO dependencies for Cloudflare Workers.
MIT License
680 stars 55 forks source link

Generate typescript declarations #2

Closed Yrlish closed 3 years ago

Yrlish commented 3 years ago

Before publishing, the tsc command needs to be executed, or the declarations script in package.json. This will generate a index.d.ts file in the root folder of the repository.

I've tested with manually copying the generated d.ts file into the correct node_modules folder in my project, at it seems to work nicely in my environment.

tsndr commented 3 years ago

And where are the type declarations?

Yrlish commented 3 years ago

They are automatically generated with the command mentioned above.

tsndr commented 3 years ago

I've tried it, but that didn't work right, that's all it generated:

export namespace algorithms {
    namespace HS256 {
        const name: string;
        namespace hash {
            const name_1: string;
            export { name_1 as name };
        }
    }
    namespace HS512 {
        const name_2: string;
        export { name_2 as name };
        export namespace hash_1 {
            const name_3: string;
            export { name_3 as name };
        }
        export { hash_1 as hash };
    }
}

I'm not that experienced with TS, but I think I might have to go in and create the declarations by hand.

Yrlish commented 3 years ago

Hmm, it shouldn't be like that. I'm going to verify it again.

Yrlish commented 3 years ago

I did some changes to the configuration, in hope it will work better. For me it didn't change anything. This is what my index.d.ts contains after running tsc.

declare const _exports: JWT;
export = _exports;
declare class JWT {
    algorithms: {
        HS256: {
            name: string;
            hash: {
                name: string;
            };
        };
        HS512: {
            name: string;
            hash: {
                name: string;
            };
        };
    };
    utf8ToUint8Array(str: any): Uint8Array;
    sign(payload: any, secret: any, alg?: string): Promise<string>;
    verify(token: any, secret: any, alg?: string): Promise<boolean>;
    decode(token: any): any;
}
tsndr commented 3 years ago

Ok, I took your output, updated it by hand to the right types, and got rid of the private methods. However now we got TS support. Thanks for your help :)

Yrlish commented 3 years ago

Great! Although, you should avoid using object as a type. It is bad. Use any instead, for simplicity. With any one can throw in classes. :)

ESLint: Don't use object as a type. The object type is currently hard to use (see this issue). Consider using Record<string, unknown> instead, as it allows you to more easily inspect and use the keys. (@typescript-eslint/ban-types)

tsndr commented 3 years ago

But it's literally expecting it to be an object here, see: https://github.com/tsndr/cloudflare-worker-jwt/blob/eebc0e988a63dd1cfc7c5729e327b2cd219167f7/index.js#L33

Correct me if I'm wrong, but in this circumstance changing it to any might be a bit confusing.

Yrlish commented 3 years ago

Oh yeah. That is true. My bad, I though I threw in a class, but it was an object. await JWT.sign({...data, expires: expires}, SECRET) In the case here, my variable data is an instance of a interface, converted into an object with an extra entry. 🙂

Although, recommendation to avoid object stands, as per ESLint recommendation. 🙂

tsndr commented 3 years ago

You don't need to convert your interface to an object like this, the object type accepts interfaces as well. I just tried it and no errors for me.

Yrlish commented 3 years ago

Yes, I know. It's just for adding more entries that is not part of the interface.

tsndr commented 3 years ago

But you should import it like this:

import * as JWT from "@tsndr/cloudflare-worker-jwt"

Since I got an error otherwise.