Skip to content

Commit

Permalink
yes dear type overlords
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Sep 30, 2024
1 parent 04793dc commit c5f9043
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { getToken } from "./get-token";

import { Options, Result } from "./types";

export async function githubAppJwt({
export async function githubAppJwt<T extends number | string = number>({
id,
privateKey,
now = Math.floor(Date.now() / 1000),
}: Options): Promise<Result> {
}: Options<T>): Promise<Result<T>> {
// When creating a JSON Web Token, it sets the "issued at time" (iat) to 30s
// in the past as we have seen people running situations where the GitHub API
// claimed the iat would be in future. It turned out the clocks on the
Expand Down
11 changes: 5 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
export type PrivateKey = string;
export type AppId = number;
export type Expiration = number;
export type Token = string;

export type Options = {
id: AppId;
export type Options<IdType extends number | string = number> = {
id: IdType;
privateKey: PrivateKey;
now?: number;
};

export type Result = {
appId: AppId;
export type Result<IdType extends number | string = number> = {
appId: IdType;
expiration: Expiration;
token: Token;
};

export type Payload = {
iat: number;
exp: number;
iss: number;
iss: number | string;
};

export type GetTokenOptions = {
Expand Down
8 changes: 3 additions & 5 deletions test/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,12 @@ test("Include the time difference in the expiration and issued_at field", async
});

// New test for id set to Client ID
test("id set to Client ID", async (t) => {
MockDate.set(0);

test("id set to Client ID", async () => {
const result = await githubAppJwt({
id: "client_id_string",
privateKey: PRIVATE_KEY_PKCS8,
});

t.is(typeof result.token, "string");
t.is(result.appId, "client_id_string");
expect(typeof result.token).toEqual("string");
expect(result.appId).toEqual("client_id_string");
});

0 comments on commit c5f9043

Please sign in to comment.