From 6606ed8d140acc02a2bd3d8147594fe29e7f7c74 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Thu, 2 May 2024 13:59:47 -0700 Subject: [PATCH 1/5] Update 'id' argument type to accept number or string --- README.md | 6 +++--- index.d.ts | 4 ++-- index.test-d.ts | 12 ++++++++++++ internals.d.ts | 2 +- test/node.test.js | 13 +++++++++++++ 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 78649d3..ff562cf 100644 --- a/README.md +++ b/README.md @@ -87,10 +87,10 @@ For a complete implementation of GitHub App authentication strategies, see [`@oc options.id - number + number | string - Required. Find App ID on the app’s about page in settings. + Required. The GitHub App's ID or Client ID. For github.com and GHES 3.14+, it is recommended to use the Client ID. @@ -154,7 +154,7 @@ For a complete implementation of GitHub App authentication strategies, see [`@oc number - The GitHub App database ID passed in options.id. + The GitHub App database ID or Client ID passed in options.id. diff --git a/index.d.ts b/index.d.ts index dba0a82..1067f6c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,11 +1,11 @@ export type Options = { - id: number; + id: number | string; privateKey: string; now?: number; }; export type Result = { - appId: number; + appId: number | string; expiration: number; token: string; }; diff --git a/index.test-d.ts b/index.test-d.ts index 4107655..34aedb9 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -11,3 +11,15 @@ export async function test() { expectType(result.expiration); expectType(result.token); } + +// Test case to verify `id` can be set to a string +export async function testWithStringId() { + const resultWithStringId = await githubAppJwt({ + id: "client_id_string", + privateKey: "", + }); + + expectType(resultWithStringId.appId); + expectType(resultWithStringId.expiration); + expectType(resultWithStringId.token); +} diff --git a/internals.d.ts b/internals.d.ts index 586ab39..a7c9301 100644 --- a/internals.d.ts +++ b/internals.d.ts @@ -1,7 +1,7 @@ export type Payload = { iat: number; exp: number; - iss: number; + iss: number | string; }; export type Header = { alg: "RS256"; typ: "JWT" }; diff --git a/test/node.test.js b/test/node.test.js index 18a5d34..f47a01f 100644 --- a/test/node.test.js +++ b/test/node.test.js @@ -177,3 +177,16 @@ test("Replace escaped line breaks with actual linebreaks", async (t) => { token: BEARER, }); }); + +// New test for id set to Client ID +test("id set to Client ID", async (t) => { + MockDate.set(0); + + 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"); +}); From e289934443e8701bbb282f383812e957db7116a6 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Thu, 2 May 2024 14:01:43 -0700 Subject: [PATCH 2/5] Update index.test-d.ts --- index.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.test-d.ts b/index.test-d.ts index 34aedb9..13c9e7a 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -19,7 +19,7 @@ export async function testWithStringId() { privateKey: "", }); - expectType(resultWithStringId.appId); + expectType(resultWithStringId.appId); expectType(resultWithStringId.expiration); expectType(resultWithStringId.token); } From 8bd281afdbdf99ab807abd20e3dee0f17516d547 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Thu, 2 May 2024 14:06:52 -0700 Subject: [PATCH 3/5] Update index.test-d.ts --- index.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.test-d.ts b/index.test-d.ts index 13c9e7a..5593682 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -7,7 +7,7 @@ export async function test() { privateKey: "", }); - expectType(result.appId); + expectType(result.appId); expectType(result.expiration); expectType(result.token); } From ebe561cf49699c6517eb9ae069f2aff5f36e38f8 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Thu, 2 May 2024 14:13:36 -0700 Subject: [PATCH 4/5] test: infer `appId` from type of passed `id` argument --- index.test-d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.test-d.ts b/index.test-d.ts index 5593682..f500ad5 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -7,7 +7,7 @@ export async function test() { privateKey: "", }); - expectType(result.appId); + expectType(result.appId); expectType(result.expiration); expectType(result.token); } @@ -19,7 +19,7 @@ export async function testWithStringId() { privateKey: "", }); - expectType(resultWithStringId.appId); + expectType(resultWithStringId.appId); expectType(resultWithStringId.expiration); expectType(resultWithStringId.token); } From 7867f8eea70669eed7e538df6badb72849aef309 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Thu, 2 May 2024 14:18:05 -0700 Subject: [PATCH 5/5] infer `result.appId` type from `options.id` --- index.d.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 1067f6c..6f67a05 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,13 +1,13 @@ -export type Options = { - id: number | string; +export type Options = { + id: IdType; privateKey: string; now?: number; }; -export type Result = { - appId: number | string; +export type Result = { + appId: IdType extends string ? string : number; expiration: number; token: string; }; -export default function githubAppJwt(options: Options): Promise; +export default function githubAppJwt(options: Options): Promise>;